aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/model/LbServicesProducerTest.java
blob: e1ad6bf51f0bfc7a90d0522ecf49a73349137831 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.model;

import com.yahoo.cloud.config.LbServicesConfig;
import com.yahoo.cloud.config.LbServicesConfig.Tenants.Applications.Endpoints;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.model.NullConfigModelRegistry;
import com.yahoo.config.model.api.ApplicationClusterEndpoint;
import com.yahoo.config.model.api.ApplicationInfo;
import com.yahoo.config.model.api.ContainerEndpoint;
import com.yahoo.config.model.api.Model;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.config.model.provision.InMemoryProvisioner;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.config.ConfigPayload;
import com.yahoo.vespa.flags.InMemoryFlagSource;
import com.yahoo.vespa.model.VespaModel;
import org.junit.Test;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import static com.yahoo.cloud.config.LbServicesConfig.Tenants.Applications.Endpoints.RoutingMethod.Enum.sharedLayer4;
import static com.yahoo.cloud.config.LbServicesConfig.Tenants.Applications.Endpoints.Scope.Enum.application;
import static com.yahoo.cloud.config.LbServicesConfig.Tenants.Applications.Endpoints.Scope.Enum.global;
import static com.yahoo.cloud.config.LbServicesConfig.Tenants.Applications.Endpoints.Scope.Enum.zone;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

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

    private static final Set<ContainerEndpoint> endpoints = Set.of(
            new ContainerEndpoint("mydisc", ApplicationClusterEndpoint.Scope.zone, List.of("mydisc.foo.foo.endpoint1.suffix")),
            new ContainerEndpoint("mydisc", ApplicationClusterEndpoint.Scope.zone, List.of("mydisc.foo.foo.endpoint2.suffix")),
            new ContainerEndpoint("mydisc", ApplicationClusterEndpoint.Scope.global, List.of("rotation-1", "rotation-2")),
            new ContainerEndpoint("mydisc", ApplicationClusterEndpoint.Scope.application, List.of("app-endpoint"))
    );

    private final InMemoryFlagSource flagSource = new InMemoryFlagSource();

    @Test
    public void testDeterministicGetConfig() {
        Map<TenantName, Set<ApplicationInfo>> testModel = createTestModel(new DeployState.Builder().endpoints(endpoints));
        LbServicesConfig last = null;
        for (int i = 0; i < 100; i++) {
            testModel = randomizeApplications(testModel, i);
            LbServicesConfig config = getLbServicesConfig(Zone.defaultZone(), testModel);
            if (last != null) {
                assertConfig(last, config);
            }
            last = config;
        }
    }

    @Test
    public void testConfigActiveRotation() {
        RegionName regionName = RegionName.from("us-east-1");
        LbServicesConfig conf = createModelAndGetLbServicesConfig(regionName);
        assertTrue(conf.tenants("foo").applications("foo:prod:" + regionName.value() + ":default").activeRotation());
    }

    private LbServicesConfig createModelAndGetLbServicesConfig(RegionName regionName) {
        Zone zone = new Zone(Environment.prod, regionName);
        Map<TenantName, Set<ApplicationInfo>> testModel = createTestModel(new DeployState.Builder().endpoints(Set.of(new ContainerEndpoint("mydisc", ApplicationClusterEndpoint.Scope.zone, List.of("md.example.com")))).zone(zone));
        return getLbServicesConfig(new Zone(Environment.prod, regionName), testModel);
    }

    private LbServicesConfig getLbServicesConfig(Zone zone, Map<TenantName, Set<ApplicationInfo>> testModel) {
        LbServicesProducer producer = new LbServicesProducer(testModel, zone, flagSource);
        LbServicesConfig.Builder builder = new LbServicesConfig.Builder();
        producer.getConfig(builder);
        return new LbServicesConfig(builder);
    }

    @Test
    public void testConfigAliasesWithEndpoints() {
        Map<TenantName, Set<ApplicationInfo>> testModel = createTestModel(new DeployState.Builder()
                .endpoints(endpoints)
                .properties(new TestProperties().setHostedVespa(true)));
        RegionName regionName = RegionName.from("us-east-1");
        LbServicesConfig config = getLbServicesConfig(new Zone(Environment.prod, regionName), testModel);

        List<Endpoints> endpointList = config.tenants("foo").applications("foo:prod:" + regionName.value() + ":default").endpoints();
        // Expect 2 zone endpoints (2 suffixes), 2 global endpoints and 1 application endpoint
        assertEquals(5, endpointList.size());
        List<Endpoints> zoneEndpoints = endpointList.stream().filter(e -> e.scope() == zone).toList();
        assertEquals(2, zoneEndpoints.size());
        assertTrue(zoneEndpoints.stream()
                           .filter(e -> e.routingMethod() == sharedLayer4)
                           .map(Endpoints::dnsName).toList()
                .containsAll(List.of("mydisc.foo.foo.endpoint1.suffix", "mydisc.foo.foo.endpoint2.suffix")));
        assertContainsEndpoint(zoneEndpoints, "mydisc.foo.foo.endpoint1.suffix", "mydisc", zone, sharedLayer4, 1, List.of("foo.foo.yahoo.com"));
        assertContainsEndpoint(zoneEndpoints, "mydisc.foo.foo.endpoint2.suffix", "mydisc", zone, sharedLayer4, 1, List.of("foo.foo.yahoo.com"));

        List<Endpoints> globalEndpoints = endpointList.stream().filter(e -> e.scope() == global).toList();
        assertEquals(2, globalEndpoints.size());
        assertTrue(globalEndpoints.stream().map(Endpoints::dnsName).toList().containsAll(List.of("rotation-1", "rotation-2")));
        assertContainsEndpoint(globalEndpoints, "rotation-1", "mydisc", global, sharedLayer4, 1, List.of("foo.foo.yahoo.com"));
        assertContainsEndpoint(globalEndpoints, "rotation-2", "mydisc", global, sharedLayer4, 1, List.of("foo.foo.yahoo.com"));

        List<Endpoints> applicationEndpoints = endpointList.stream().filter(e -> e.scope() == application).toList();
        assertEquals(1, applicationEndpoints.size());
        assertTrue(applicationEndpoints.stream().map(Endpoints::dnsName).toList().contains("app-endpoint"));
        assertContainsEndpoint(applicationEndpoints, "app-endpoint", "mydisc", application, sharedLayer4, 1, List.of("foo.foo.yahoo.com"));
    }

    @Test
    public void testRoutingConfigForTesterApplication() {
        Map<TenantName, Set<ApplicationInfo>> testModel = createTestModel(new DeployState.Builder().endpoints(Set.of(new ContainerEndpoint("mydisc", ApplicationClusterEndpoint.Scope.zone, List.of("md.example.com")))));

        // No config for tester application
        assertNull(getLbServicesConfig(Zone.defaultZone(), testModel)
                           .tenants("foo")
                           .applications("baz:prod:default:custom-t"));
    }

    private void assertContainsEndpoint(List<Endpoints> endpoints, String dnsName, String clusterId, Endpoints.Scope.Enum scope, Endpoints.RoutingMethod.Enum routingMethod, int weight, List<String> hosts) {
        assertTrue(endpoints.contains(new Endpoints.Builder()
                                              .dnsName(dnsName)
                                              .clusterId(clusterId)
                                              .scope(scope)
                                              .routingMethod(routingMethod)
                                              .weight(weight)
                                              .hosts(hosts)
                                              .build()));
    }

    private Map<TenantName, Set<ApplicationInfo>> randomizeApplications(Map<TenantName, Set<ApplicationInfo>> testModel, int seed) {
        Map<TenantName, Set<ApplicationInfo>> randomizedApplications = new LinkedHashMap<>();
        List<TenantName> keys = new ArrayList<>(testModel.keySet());
        Collections.shuffle(keys, new Random(seed));
        for (TenantName key : keys) {
            randomizedApplications.put(key, testModel.get(key));
        }
        return randomizedApplications;
    }

    private Map<TenantName, Set<ApplicationInfo>> createTestModel(DeployState.Builder deployStateBuilder) {
        Map<TenantName, Set<ApplicationInfo>> tMap = new LinkedHashMap<>();
        TenantName foo = TenantName.from("foo");
        TenantName bar = TenantName.from("bar");
        TenantName baz = TenantName.from("baz");
        tMap.put(foo, createTestApplications(foo, deployStateBuilder));
        tMap.put(bar, createTestApplications(bar, deployStateBuilder));
        tMap.put(baz, createTestApplications(baz, deployStateBuilder));
        return tMap;
    }

    private Set<ApplicationInfo> createTestApplications(TenantName tenant, DeployState.Builder deployStateBuilder) {
        ApplicationId fooApp = new ApplicationId.Builder().tenant(tenant).applicationName("foo").build();
        ApplicationId barApp = new ApplicationId.Builder().tenant(tenant).applicationName("bar").build();
        ApplicationId bazApp = new ApplicationId.Builder().tenant(tenant).applicationName("baz").instanceName("custom-t").build(); // tester app
        return new LinkedHashSet<>(createApplication(List.of(fooApp, barApp, bazApp), deployStateBuilder));
    }

    private Set<ApplicationInfo> createApplication(List<ApplicationId> appIds, DeployState.Builder deployStateBuilder) {
        Set<ApplicationInfo> applicationInfoSet = new HashSet<>();
        List<String> hostnames = new ArrayList<>();
        appIds.forEach(appId -> {
            deployStateBuilder.properties(getTestproperties(appId));
            hostnames.add(appId.tenant() + "." + appId.application() + ".yahoo.com");
            hostnames.add(appId.tenant().value() + "." + appId.application().value() + "2.yahoo.com");
            try {
                InMemoryProvisioner provisioner = new InMemoryProvisioner(true, false, hostnames);
                deployStateBuilder.modelHostProvisioner(provisioner);
                applicationInfoSet.add(new ApplicationInfo(appId, 3, createVespaModel(createApplicationPackage(), deployStateBuilder)));
            } catch (IOException | SAXException e) {
                throw new RuntimeException(e);
            }
        });

        return applicationInfoSet;
    }

    private ApplicationPackage createApplicationPackage() {
        String services = "<services>" +
                          "<admin version='4.0'><logservers> <nodes count='1' /> </logservers></admin>" +
                          "  <container id='mydisc' version='1.0'>" +
                          "    <nodes count='1' />" +
                          "    <search/>" +
                          "  </container>" +
                          "</services>";

        String deploymentInfo = "<?xml version='1.0' encoding='UTF-8'?>" +
                                "<deployment version='1.0'>" +
                                "  <test />" +
                                "  <prod>" +
                                "    <region>us-east-1</region>" +
                                "    <region>us-east-2</region>" +
                                "  </prod>" +
                                "  <endpoints>" +
                                "    <endpoint container-id='mydisc' />" +
                                "  </endpoints>" +
                                "</deployment>";
        return new MockApplicationPackage.Builder().withServices(services).withDeploymentSpec(deploymentInfo).build();
    }

    private Model createVespaModel(ApplicationPackage applicationPackage, DeployState.Builder deployStateBuilder) throws IOException, SAXException {
        return new VespaModel(new NullConfigModelRegistry(), deployStateBuilder.applicationPackage(applicationPackage).build());
    }

    private void assertConfig(LbServicesConfig expected, LbServicesConfig actual) {
        assertFalse(expected.toString().isEmpty());
        assertFalse(actual.toString().isEmpty());
        assertEquals(actual.toString(), expected.toString());
        assertEquals(ConfigPayload.fromInstance(expected).toString(true), ConfigPayload.fromInstance(actual).toString(true));
    }

    private TestProperties getTestproperties(ApplicationId applicationId) {
        return new TestProperties()
                .setHostedVespa(true)
                .setApplicationId(applicationId);
    }

}