aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/config/model/deploy/SystemModelTestCase.java
blob: a9f97d95b742a4450335e1150e733a15c6878543 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.deploy;

import com.yahoo.cloud.config.SentinelConfig;
import com.yahoo.net.HostName;
import com.yahoo.test.StandardConfig;
import com.yahoo.config.model.ConfigModel;
import com.yahoo.config.model.ConfigModelRegistry;
import com.yahoo.config.model.MapConfigModelRegistry;
import com.yahoo.config.model.ApplicationConfigProducerRoot;
import com.yahoo.vespa.model.ConfigProducer;
import com.yahoo.vespa.model.HostResource;
import com.yahoo.vespa.model.HostSystem;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.test.ApiConfigModel;
import com.yahoo.vespa.model.test.SimpleConfigModel;
import com.yahoo.vespa.model.test.SimpleService;
import com.yahoo.vespa.model.test.utils.VespaModelCreatorWithFilePkg;
import org.junit.jupiter.api.Test;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author bratseth
 */
public class SystemModelTestCase {

    private static final String TESTDIR = "src/test/cfg/application/";

    private static VespaModel getVespaModelDoNotValidateXml(String configPath) {
        ConfigModelRegistry registry = MapConfigModelRegistry.createFromList(new SimpleConfigModel.Builder(), new ApiConfigModel.Builder());
        VespaModelCreatorWithFilePkg creator = new VespaModelCreatorWithFilePkg(configPath,registry);
        return creator.create(false);  // do not validate against schema  -- the xml files used here are not valid
    }

    // Debugging
    @SuppressWarnings({"UnusedDeclaration"})
    private void dumpTree(ConfigProducer producer) {
        Map<String,? extends ConfigProducer> id2cp = producer.getChildren();
        for (ConfigProducer c : id2cp.values()) {
            System.out.println("id: " + c.getConfigId());
            if (c.getChildren().size() > 0) {
                dumpTree(c);
            }
        }
    }

    @Test
    void testMetrics() {
        VespaModel vespaModel = getVespaModelDoNotValidateXml(TESTDIR + "metricsconfig");
        SimpleService service0 = (SimpleService) vespaModel.getConfigProducer("simple/simpleservice.0").get();
        vespaModel.getConfigProducer("simple/simpleservice.1");
        assertEquals("testClusterName", service0.getDefaultMetricDimensions().get("clustername"));
    }

    @Test
    void testVespaModel() {
        VespaModel vespaModel = getVespaModelDoNotValidateXml(TESTDIR + "simpleconfig/");
        assertNotNull(vespaModel);

        assertEquals(4, vespaModel.configModelRepo().asMap().size(), "There are two instances of the simple model + Routing and AdminModel (set up implicitly)");
        assertNotNull(vespaModel.configModelRepo().asMap().get("simple"), "One gets the default name as there is no explicit id");
        assertNotNull(vespaModel.configModelRepo().asMap().get("second"), "The other gets the explicit id as name");

        ApplicationConfigProducerRoot root = vespaModel.getVespa();
        assertNotNull(root);

        // Verify configIds from vespa
        assertTrue(6 <= root.getConfigIds().size());
        assertTrue(root.getConfigIds().contains("client"));
        assertTrue(root.getConfigIds().contains("simple"));
        assertTrue(root.getConfigIds().contains("second"));
        assertTrue(root.getConfigIds().contains("simple/simpleservice.0"));
        assertTrue(root.getConfigIds().contains("simple/simpleservice.1"));
        assertTrue(root.getConfigIds().contains("second/simpleservice.0"));

        // Verify configIds from vespaModel
        assertTrue(12 <= vespaModel.getConfigIds().size());
        String localhost = HostName.getLocalhost();
        String localhostConfigId = "hosts/" + localhost;
        Set<String> configIds = vespaModel.getConfigIds();
        assertTrue(configIds.contains("client"));
        assertTrue(configIds.contains(localhostConfigId));
        assertTrue(configIds.contains("simple/simpleservice.0"));
        assertTrue(configIds.contains("second/simpleservice.0"));
        assertTrue(configIds.contains("hosts/" + localhost + "/logd"));

        // Verify sentinel config
        SentinelConfig sentinelConfig = new SentinelConfig((SentinelConfig.Builder) vespaModel.getConfig(new SentinelConfig.Builder(), localhostConfigId));
        boolean found = false;
        for (SentinelConfig.Service service : sentinelConfig.service()) {
            if ("logd".equals(service.name())) {
                found = true;
            }
        }
        assertTrue(found);

        // Get the simple model config from VespaModel
        assertEquals(vespaModel.getConfig(StandardConfig.class, "simple/simpleservice.0").astring(), "simpleservice");
        assertEquals(vespaModel.getConfig(StandardConfig.class, "second/simpleservice.0").astring(), "simpleservice");
    }

    @Test
    void testHostSystem() {
        VespaModel vespaModel = getVespaModelDoNotValidateXml(TESTDIR + "simpleconfig/");
        HostSystem hostSystem = vespaModel.hostSystem();

        HostResource host1 = hostSystem.getHost("host1");
        HostResource host2 = hostSystem.getHost("host2");
        HostResource host3 = hostSystem.getHost("host3");

        assertEquals(host1, host2);
        assertEquals(host2, host3);

        // all three host aliases are for the same host, so the number of services should be 3 + 8
        // (3 simpleservices and logd, configproxy, config sentinel, admin server config server, slobrok, logserver and metricsproxy)
        assertEquals(10, host1.getServices().size());

        assertNotNull(host1.getService("simpleservice"));
        assertNotNull(host1.getService("simpleservice2"));
        assertNotNull(host3.getService("simpleservice3"));
    }

    @Test
    void testBasePorts() {
        VespaModel vespaModel = getVespaModelDoNotValidateXml(TESTDIR + "simpleconfig");
        assertNotNull(vespaModel);

        assertEquals(vespaModel.getConfig(StandardConfig.class, "simple/simpleservice.0").baseport(), 10000);
        assertTrue(vespaModel.getConfig(StandardConfig.class, "simple/simpleservice.1").baseport() != 10000);
    }

    /**
     * This test is the same as the system test cloudconfig/plugins.
     * Be sure to update it as well if you change this.
     */
    @Test
    void testPlugins() {
        VespaModel vespaModel = getVespaModelDoNotValidateXml(TESTDIR + "plugins");

        assertNotNull(vespaModel);
        ApplicationConfigProducerRoot root = vespaModel.getVespa();

        assertEquals(5, vespaModel.configModelRepo().asMap().size());
        assertTrue(vespaModel.configModelRepo().asMap().containsKey("simple"));
        assertTrue(vespaModel.configModelRepo().asMap().containsKey("api"));
        assertTrue(root.getConfigIds().contains("simple/simpleservice.0"));
        assertTrue(root.getConfigIds().contains("simple/simpleservice.1"));
        assertTrue(root.getConfigIds().contains("api/apiservice.0"));

        // Verify that configModelRegistry iterates in dependency order
        Iterator<ConfigModel> i = vespaModel.configModelRepo().iterator();
        ConfigModel plugin = i.next();
        assertEquals("admin", plugin.getId());
        plugin = i.next();
        assertEquals("simple", plugin.getId());
        plugin = i.next();
        assertEquals("simple2", plugin.getId());
        plugin = i.next();
        assertEquals("api", plugin.getId());
        plugin = i.next();
        assertEquals("routing", plugin.getId());

        assertEquals(vespaModel.getConfig(StandardConfig.class, "api/apiservice.0").astring(), "apiservice");

        assertEquals(vespaModel.getConfig(StandardConfig.class, "simple/simpleservice.0").astring(), "simpleservice");
        assertEquals(vespaModel.getConfig(StandardConfig.class, "simple/simpleservice.1").astring(), "simpleservice");
        assertEquals(vespaModel.getConfig(StandardConfig.class, "simple2/simpleservice.0").astring(), "simpleservice");
    }

    @Test
    void testEqualPlugins() {
        try {
            getVespaModelDoNotValidateXml(TESTDIR + "doubleconfig");
            fail("No exception upon two plugins with the same name");
        } catch (RuntimeException expected) {
            assertEquals("Could not resolve tag <simpleplugin version=\"1.0\"> to a config model component", expected.getMessage());
        }
    }

}