aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/SuperModelControllerTest.java
blob: 0595be3230fdbc6c36e32be28915b36ade794f97 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server;

import com.yahoo.cloud.config.LbServicesConfig;
import com.yahoo.cloud.config.LbServicesConfig.Tenants.Applications;
import com.yahoo.config.model.api.ApplicationInfo;
import com.yahoo.config.model.api.SuperModel;
import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.config.model.deploy.DeployProperties;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationName;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Zone;
import com.yahoo.jrt.Request;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.protocol.CompressionType;
import com.yahoo.vespa.config.protocol.DefContent;
import com.yahoo.vespa.config.protocol.JRTClientConfigRequestV3;
import com.yahoo.vespa.config.protocol.JRTServerConfigRequestV3;
import com.yahoo.vespa.config.protocol.Trace;
import com.yahoo.vespa.config.server.model.SuperModelConfigProvider;
import com.yahoo.vespa.config.server.rpc.UncompressedConfigResponseFactory;
import com.yahoo.vespa.model.VespaModel;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * @author Ulf Lilleengen
 */
public class SuperModelControllerTest {

    private SuperModelController handler;

    @Before
    public void setupHandler() throws IOException, SAXException {
        Map<ApplicationId, ApplicationInfo> models = new LinkedHashMap<>();

        File testApp = new File("src/test/resources/deploy/app");
        ApplicationId app = ApplicationId.from(TenantName.from("a"),
                                               ApplicationName.from("foo"), InstanceName.defaultName());
        models.put(app, new ApplicationInfo(app, 4l, new VespaModel(FilesApplicationPackage.fromFile(testApp))));
        SuperModel superModel = new SuperModel(models);
        handler = new SuperModelController(new SuperModelConfigProvider(superModel, Zone.defaultZone()), new TestConfigDefinitionRepo(), 2, new UncompressedConfigResponseFactory());
    }
    
    @Test
    public void test_lb_config_simple() {
        LbServicesConfig.Builder lb = new LbServicesConfig.Builder();
        handler.getSuperModel().getConfig(lb);
        LbServicesConfig lbc = new LbServicesConfig(lb);
        assertThat(lbc.tenants().size(), is(1));
        assertThat(lbc.tenants("a").applications().size(), is(1));
        Applications app = lbc.tenants("a").applications("foo:prod:default:default");
        assertTrue(app.hosts().size() > 0);
    }


    @Test(expected = UnknownConfigDefinitionException.class)
    public void test_unknown_config_definition() {
        String md5 = "asdfasf";
        Request request = JRTClientConfigRequestV3.createWithParams(new ConfigKey<>("foo", "id", "bar", md5, null), DefContent.fromList(Collections.emptyList()),
                                                                    "fromHost", md5, 1, 1, Trace.createDummy(), CompressionType.UNCOMPRESSED,
                                                                    Optional.empty())
                                                  .getRequest();
        JRTServerConfigRequestV3 v3Request = JRTServerConfigRequestV3.createFromRequest(request);
        handler.resolveConfig(v3Request);
    }

    @Test
    public void test_lb_config_multiple_apps_legacy_super_model() throws IOException, SAXException {
        Map<ApplicationId, ApplicationInfo> models = new LinkedHashMap<>();
        TenantName t1 = TenantName.from("t1");
        TenantName t2 = TenantName.from("t2");
        File testApp1 = new File("src/test/resources/deploy/app");
        File testApp2 = new File("src/test/resources/deploy/advancedapp");
        File testApp3 = new File("src/test/resources/deploy/advancedapp");

        ApplicationId simple = applicationId("mysimpleapp", t1);
        ApplicationId advanced = applicationId("myadvancedapp", t1);
        ApplicationId tooAdvanced = applicationId("minetooadvancedapp", t2);
        models.put(simple, createApplicationInfo(testApp1, simple, 4l));
        models.put(advanced, createApplicationInfo(testApp2, advanced, 4l));
        models.put(tooAdvanced, createApplicationInfo(testApp3, tooAdvanced, 4l));

        SuperModel superModel = new SuperModel(models);
        SuperModelController han = new SuperModelController(new SuperModelConfigProvider(superModel, Zone.defaultZone()), new TestConfigDefinitionRepo(), 2, new UncompressedConfigResponseFactory());
        LbServicesConfig.Builder lb = new LbServicesConfig.Builder();
        han.getSuperModel().getConfig(lb);
        LbServicesConfig lbc = new LbServicesConfig(lb);
        assertThat(lbc.tenants().size(), is(2));
        assertThat(lbc.tenants("t1").applications().size(), is(2));
        assertThat(lbc.tenants("t2").applications().size(), is(1));
        assertThat(lbc.tenants("t2").applications("minetooadvancedapp:prod:default:default").hosts().size(), is(1));
        assertQrServer(lbc.tenants("t2").applications("minetooadvancedapp:prod:default:default"));
    }

    @Test
    public void test_lb_config_multiple_apps() throws IOException, SAXException {
        Map<ApplicationId, ApplicationInfo> models = new LinkedHashMap<>();
        TenantName t1 = TenantName.from("t1");
        TenantName t2 = TenantName.from("t2");
        File testApp1 = new File("src/test/resources/deploy/app");
        File testApp2 = new File("src/test/resources/deploy/advancedapp");
        File testApp3 = new File("src/test/resources/deploy/advancedapp");

        ApplicationId simple = applicationId("mysimpleapp", t1);
        ApplicationId advanced = applicationId("myadvancedapp", t1);
        ApplicationId tooAdvanced = applicationId("minetooadvancedapp", t2);
        models.put(simple, createApplicationInfo(testApp1, simple, 4l));
        models.put(advanced, createApplicationInfo(testApp2, advanced, 4l));
        models.put(tooAdvanced, createApplicationInfo(testApp3, tooAdvanced, 4l));

        SuperModel superModel = new SuperModel(models);
        SuperModelController han = new SuperModelController(new SuperModelConfigProvider(superModel, Zone.defaultZone()), new TestConfigDefinitionRepo(), 2, new UncompressedConfigResponseFactory());
        LbServicesConfig.Builder lb = new LbServicesConfig.Builder();
        han.getSuperModel().getConfig(lb);
        LbServicesConfig lbc = new LbServicesConfig(lb);
        assertThat(lbc.tenants().size(), is(2));
        assertThat(lbc.tenants("t1").applications().size(), is(2));
        assertThat(lbc.tenants("t2").applications().size(), is(1));
        assertThat(lbc.tenants("t2").applications("minetooadvancedapp:prod:default:default").hosts().size(), is(1));
        assertQrServer(lbc.tenants("t2").applications("minetooadvancedapp:prod:default:default"));
    }

    private ApplicationId applicationId(String applicationName, TenantName tenantName) {
        return ApplicationId.from(tenantName, ApplicationName.from(applicationName), InstanceName.defaultName());
    }

    private void assertQrServer(Applications app) {
        String host = app.hosts().keySet().iterator().next();
        Applications.Hosts hosts = app.hosts(host);
        assertThat(hosts.hostname(), is(host));
        for (Map.Entry<String, Applications.Hosts.Services> e : app.hosts(host).services().entrySet()) {
            System.out.println(e);
            if ("qrserver".equals(e.getKey())) {
                Applications.Hosts.Services s = e.getValue();
                assertThat(s.type(), is("qrserver"));
                assertThat(s.ports().size(), is(4));
                assertThat(s.index(), is(0));
                return;
            }
        }
        org.junit.Assert.fail("No qrserver service in config");
    }

    private DeployState createDeployState(File applicationPackage, ApplicationId applicationId) {
        return new DeployState.Builder()
                .applicationPackage(FilesApplicationPackage.fromFile(applicationPackage))
                .properties(new DeployProperties.Builder().applicationId(applicationId).build())
                .build();
    }

    private ApplicationInfo createApplicationInfo(File applicationPackage, ApplicationId applicationId, long generation) throws IOException, SAXException {
        DeployState deployState = createDeployState(applicationPackage, applicationId);
        return new ApplicationInfo(applicationId, generation, new VespaModel(deployState));
    }

}