aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/routing/RoutingApiTest.java
blob: 309501f5679a045016f3e14281936f512f495227 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.routing;

import com.yahoo.application.container.handler.Request;
import com.yahoo.component.Version;
import com.yahoo.config.application.api.ValidationId;
import com.yahoo.config.provision.AthenzDomain;
import com.yahoo.config.provision.AthenzService;
import com.yahoo.config.provision.zone.RoutingMethod;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.ControllerTester;
import com.yahoo.vespa.hosted.controller.deployment.ApplicationPackageBuilder;
import com.yahoo.vespa.hosted.controller.deployment.DeploymentTester;
import com.yahoo.vespa.hosted.controller.integration.ZoneApiMock;
import com.yahoo.vespa.hosted.controller.restapi.ContainerTester;
import com.yahoo.vespa.hosted.controller.restapi.ControllerContainerTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.List;

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

/**
 * @author mpolden
 */
public class RoutingApiTest extends ControllerContainerTest {

    private static final String responseFiles = "src/test/java/com/yahoo/vespa/hosted/controller/restapi/routing/responses/";

    private ContainerTester tester;
    private DeploymentTester deploymentTester;

    @BeforeEach
    public void before() {
        tester = new ContainerTester(container, responseFiles);
        deploymentTester = new DeploymentTester(new ControllerTester(tester));
        deploymentTester.controllerTester().upgradeSystem(Version.fromString("6.1"));
    }

    @Test
    void discovery() {
        // Deploy
        var context = deploymentTester.newDeploymentContext("t1", "a1", "default");
        var westZone = ZoneId.from("prod", "us-west-1");
        var eastZone = ZoneId.from("prod", "us-east-3");
        var applicationPackage = new ApplicationPackageBuilder()
                .region(westZone.region())
                .region(eastZone.region())
                .endpoint("default", "default", eastZone.region().value(), westZone.region().value())
                .build();
        context.submit(applicationPackage).deploy();

        // GET root
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/", "",
                Request.Method.GET),
                new File("discovery/root.json"));

        // GET tenant
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/t1", "",
                Request.Method.GET),
                new File("discovery/tenant.json"));

        // GET application
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/t1/application/a1/",
                "",
                Request.Method.GET),
                new File("discovery/application.json"));

        // GET instance
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/t1/application/a1/instance/default/",
                "",
                Request.Method.GET),
                new File("discovery/instance.json"));

        // GET environment
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/environment/", "",
                Request.Method.GET),
                new File("discovery/environment.json"));
    }

    @Test
    void recursion() {
        var context1 = deploymentTester.newDeploymentContext("t1", "a1", "default");
        var westZone = ZoneId.from("prod", "us-west-1");
        var eastZone = ZoneId.from("prod", "us-east-3");
        var package1 = new ApplicationPackageBuilder()
                .region(westZone.region())
                .region(eastZone.region())
                .endpoint("default", "default", eastZone.region().value(), westZone.region().value())
                .build();
        context1.submit(package1).deploy();

        var context2 = deploymentTester.newDeploymentContext("t1", "a2", "default");
        var package2 = new ApplicationPackageBuilder()
                .region(westZone.region())
                .region(eastZone.region())
                .endpoint("default", "default", eastZone.region().value(), westZone.region().value())
                .build();
        context2.submit(package2).deploy();

        // GET tenant recursively
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/t1?recursive=true", "",
                Request.Method.GET),
                new File("recursion/tenant.json"));

        // GET application recursively
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/t1/application/a1?recursive=true", "",
                Request.Method.GET),
                new File("recursion/application.json"));

        // GET instance recursively
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/t1/application/a1/instance/default?recursive=true", "",
                Request.Method.GET),
                new File("recursion/application.json"));

        // GET environment recursively
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/environment?recursive=true", "",
                Request.Method.GET),
                new File("recursion/environment.json"));
    }

    @Test
    void exclusive_routing() {
        var context = deploymentTester.newDeploymentContext();
        // Zones support direct routing
        var westZone = ZoneId.from("prod", "us-west-1");
        var eastZone = ZoneId.from("prod", "us-east-3");
        deploymentTester.controllerTester().zoneRegistry().exclusiveRoutingIn(ZoneApiMock.from(westZone),
                ZoneApiMock.from(eastZone));
        // Deploy application
        var applicationPackage = new ApplicationPackageBuilder()
                .athenzIdentity(AthenzDomain.from("domain"), AthenzService.from("service"))
                .region(westZone.region())
                .region(eastZone.region())
                .endpoint("default", "default", eastZone.region().value(), westZone.region().value())
                .build();
        context.submit(applicationPackage).deploy();

        // GET initial deployment status
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("policy/deployment-status-initial.json"));

        // POST sets deployment out
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/inactive/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.POST),
                "{\"message\":\"Set global routing status for tenant.application in prod.us-west-1 to OUT\"}");
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("policy/deployment-status-out.json"));

        // DELETE sets deployment in
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/inactive/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.DELETE),
                "{\"message\":\"Set global routing status for tenant.application in prod.us-west-1 to IN\"}");
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("policy/deployment-status-in.json"));

        // GET initial zone status
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("policy/zone-status-initial.json"));

        // POST sets zone out
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/inactive/environment/prod/region/us-west-1",
                "", Request.Method.POST),
                "{\"message\":\"Set global routing status for deployments in prod.us-west-1 to OUT\"}");
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("policy/zone-status-out.json"));

        // DELETE sets zone in
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/inactive/environment/prod/region/us-west-1",
                "", Request.Method.DELETE),
                "{\"message\":\"Set global routing status for deployments in prod.us-west-1 to IN\"}");
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("policy/zone-status-in.json"));

        // Endpoint is removed
        applicationPackage = new ApplicationPackageBuilder()
                .athenzIdentity(AthenzDomain.from("domain"), AthenzService.from("service"))
                .region(westZone.region())
                .region(eastZone.region())
                .allow(ValidationId.globalEndpointChange)
                .build();
        context.submit(applicationPackage).deploy();

        // GET deployment status. Now empty as no routing policies have global endpoints
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                "{\"deployments\":[]}");
    }

    @Test
    void shared_routing() {
        // Deploy application
        var context = deploymentTester.newDeploymentContext();
        var westZone = ZoneId.from("prod", "us-west-1");
        var eastZone = ZoneId.from("prod", "us-east-3");
        var applicationPackage = new ApplicationPackageBuilder()
                .region(westZone.region())
                .region(eastZone.region())
                .endpoint("default", "default", eastZone.region().value(), westZone.region().value())
                .build();
        context.submit(applicationPackage).deploy();

        assertNotEquals(List.of(), context.instance().rotations(), "Rotation is assigned");

        // GET initial deployment status
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("rotation/deployment-status-initial.json"));

        // GET initial deployment status: unknown instance
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/foo/environment/prod/region/us-west-1",
                                              "", Request.Method.GET),
                              "{\"error-code\":\"BAD_REQUEST\",\"message\":\"Unknown instance 'foo' in 'tenant.application'\"}",
                              400);

        // POST sets deployment out
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/inactive/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.POST),
                "{\"message\":\"Set global routing status for tenant.application in prod.us-west-1 to OUT\"}");
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("rotation/deployment-status-out.json"));

        // DELETE sets deployment in
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/inactive/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.DELETE),
                "{\"message\":\"Set global routing status for tenant.application in prod.us-west-1 to IN\"}");
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("rotation/deployment-status-in.json"));

        // GET initial zone status
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("rotation/zone-status-initial.json"));

        // POST sets zone out
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/inactive/environment/prod/region/us-west-1",
                "", Request.Method.POST),
                "{\"message\":\"Set global routing status for deployments in prod.us-west-1 to OUT\"}");
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("rotation/zone-status-out.json"));

        // DELETE sets zone in
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/inactive/environment/prod/region/us-west-1",
                "", Request.Method.DELETE),
                "{\"message\":\"Set global routing status for deployments in prod.us-west-1 to IN\"}");
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("rotation/zone-status-in.json"));
    }

    @Test
    void mixed_routing_multiple_zones() {
        var westZone = ZoneId.from("prod", "us-west-1");
        var eastZone = ZoneId.from("prod", "us-east-3");

        // One shared and one exclusive zone
        deploymentTester.controllerTester().zoneRegistry().setRoutingMethod(ZoneApiMock.from(westZone),
                RoutingMethod.sharedLayer4);
        deploymentTester.controllerTester().zoneRegistry().setRoutingMethod(ZoneApiMock.from(eastZone),
                RoutingMethod.exclusive);

        // Deploy application
        var context = deploymentTester.newDeploymentContext();
        var applicationPackage = new ApplicationPackageBuilder()
                .region(westZone.region())
                .region(eastZone.region())
                .athenzIdentity(AthenzDomain.from("domain"), AthenzService.from("service"))
                .endpoint("endpoint1", "default", westZone.region().value())
                .endpoint("endpoint2", "default", eastZone.region().value())
                .build();
        context.submit(applicationPackage).deploy();

        // GET status for zone using sharedLayer4 routing
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                new File("rotation/deployment-status-initial.json"));

        // GET status for zone using exclusive routing
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/default/environment/prod/region/us-east-3",
                "", Request.Method.GET),
                "{\"deployments\":[{\"routingMethod\":\"exclusive\",\"instance\":\"tenant:application:default\"," +
                        "\"environment\":\"prod\",\"region\":\"us-east-3\",\"status\":\"in\",\"agent\":\"system\",\"changedAt\":0}]}");
    }

    @Test
    void invalid_requests() {
        // GET non-existent application
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/t1/application/a1/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                "{\"error-code\":\"BAD_REQUEST\",\"message\":\"t1.a1 not found\"}",
                400);

        // GET, DELETE non-existent deployment
        var context = deploymentTester.newDeploymentContext();
        var applicationPackage = new ApplicationPackageBuilder()
                .region("us-east-3")
                .endpoint("default", "default")
                .build();
        context.submit(applicationPackage).deploy();
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.GET),
                "{\"error-code\":\"BAD_REQUEST\",\"message\":\"No such deployment: tenant.application in prod.us-west-1\"}",
                400);
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/inactive/tenant/tenant/application/application/instance/default/environment/prod/region/us-west-1",
                "", Request.Method.DELETE),
                "{\"error-code\":\"BAD_REQUEST\",\"message\":\"No such deployment: tenant.application in prod.us-west-1\"}",
                400);

        // GET non-existent zone
        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/environment/prod/region/us-north-1",
                "", Request.Method.GET),
                "{\"error-code\":\"BAD_REQUEST\",\"message\":\"No such zone: prod.us-north-1\"}",
                400);
    }

    @Test
    void endpoints_list() {
        var context = deploymentTester.newDeploymentContext("t1", "a1", "default");
        var westZone = ZoneId.from("prod", "us-west-1");
        var eastZone = ZoneId.from("prod", "us-east-3");
        var applicationPackage = new ApplicationPackageBuilder()
                .region(westZone.region())
                .region(eastZone.region())
                .endpoint("default", "default", eastZone.region().value(), westZone.region().value())
                .build();
        context.submit(applicationPackage).deploy();

        tester.assertResponse(operatorRequest("http://localhost:8080/routing/v1/status/tenant/t1/application/a1/instance/default/endpoint", "", Request.Method.GET),
                new File("endpoint/endpoints.json"));
    }

}