aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionPrepareHandlerTest.java
blob: 88aed6b058c22f7b8fd6f0eaf60e749cbec62993 (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http.v2;

import com.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.concurrent.UncheckedTimeoutException;
import com.yahoo.config.model.application.provider.BaseDeployLogger;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationLockException;
import com.yahoo.config.provision.ApplicationName;
import com.yahoo.config.provision.DockerImage;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.NodeAllocationException;
import com.yahoo.config.provision.TenantName;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.jdisc.http.HttpRequest;
import com.yahoo.slime.Slime;
import com.yahoo.slime.SlimeUtils;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.TimeoutBudget;
import com.yahoo.vespa.config.server.application.OrchestratorMock;
import com.yahoo.vespa.config.server.http.HttpErrorResponse;
import com.yahoo.vespa.config.server.http.SessionHandler;
import com.yahoo.vespa.config.server.http.SessionHandlerTest;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import com.yahoo.vespa.config.server.tenant.TestTenantRepository;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.time.Clock;
import java.time.Duration;
import java.util.Map;

import static com.yahoo.jdisc.Response.Status.BAD_REQUEST;
import static com.yahoo.jdisc.Response.Status.METHOD_NOT_ALLOWED;
import static com.yahoo.jdisc.Response.Status.NOT_FOUND;
import static com.yahoo.jdisc.Response.Status.OK;
import static com.yahoo.vespa.config.server.http.HandlerTest.assertHttpStatusCodeErrorCodeAndMessage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * @author hmusum
 */
public class SessionPrepareHandlerTest extends SessionHandlerTest {
    private static final TenantName tenant = TenantName.from("test");
    private static final File app = new File("src/test/resources/deploy/validapp");

    private final Curator curator = new MockCurator();
    private TimeoutBudget timeoutBudget;
    private ApplicationRepository applicationRepository;

    private ConfigserverConfig configserverConfig;
    private String preparedMessage = " prepared.\"}";
    private TenantRepository tenantRepository;

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Before
    public void setupRepo() throws IOException {
        configserverConfig = new ConfigserverConfig.Builder()
                .configServerDBDir(temporaryFolder.newFolder().getAbsolutePath())
                .configDefinitionsDir(temporaryFolder.newFolder().getAbsolutePath())
                .fileReferencesDir(temporaryFolder.newFolder().getAbsolutePath())
                .build();
        Clock clock = Clock.systemUTC();
        timeoutBudget = new TimeoutBudget(clock, Duration.ofSeconds(10));
        tenantRepository = new TestTenantRepository.Builder()
                .withConfigserverConfig(configserverConfig)
                .withCurator(curator)
                .build();
        tenantRepository.addTenant(tenant);
        applicationRepository = new ApplicationRepository.Builder()
                .withTenantRepository(tenantRepository)
                .withOrchestrator(new OrchestratorMock())
                .withClock(clock)
                .withConfigserverConfig(configserverConfig)
                .build();
        pathPrefix = "/application/v2/tenant/" + tenant + "/session/";
        preparedMessage = " for tenant '" + tenant + "' prepared.\"";
    }

    @Test
    public void require_error_when_session_id_does_not_exist() throws Exception {
        // No session with this id exists
        HttpResponse response = request(HttpRequest.Method.PUT, 9999L);
        assertHttpStatusCodeErrorCodeAndMessage(response, NOT_FOUND, HttpErrorResponse.ErrorCode.NOT_FOUND, "Local session 9999 for 'test' was not found");
    }

    @Test
    public void require_error_when_session_id_not_a_number() throws Exception {
        final String session = "notanumber/prepared";
        HttpResponse response = createHandler().handle(SessionHandlerTest.createTestRequest(pathPrefix + session));
        assertHttpStatusCodeErrorCodeAndMessage(response, BAD_REQUEST,
                                                HttpErrorResponse.ErrorCode.BAD_REQUEST,
                                                "Session id in request is not a number, request was 'http://" + hostname + ":" + port + pathPrefix + session + "'");
    }

    @Test
    public void require_that_handler_gives_error_for_unsupported_methods() throws Exception {
        testUnsupportedMethod(createTestRequest(pathPrefix, HttpRequest.Method.POST, Cmd.PREPARED, 1L));
        testUnsupportedMethod(createTestRequest(pathPrefix, HttpRequest.Method.DELETE, Cmd.PREPARED, 1L));
    }

    private void testUnsupportedMethod(com.yahoo.container.jdisc.HttpRequest request) throws Exception {
        HttpResponse response = createHandler().handle(request);
        assertHttpStatusCodeErrorCodeAndMessage(response, METHOD_NOT_ALLOWED,
                                                HttpErrorResponse.ErrorCode.METHOD_NOT_ALLOWED,
                                                "Method '" + request.getMethod().name() + "' is not supported");
    }

    @Test
    public void require_that_response_has_all_fields() throws Exception {
        long sessionId = createSession(applicationId());
        HttpResponse response = request(HttpRequest.Method.PUT, sessionId);
        assertNotNull(response);
        assertEquals(OK, response.getStatus());
        assertResponseContains(response, "\"activate\":\"http://foo:1337" + pathPrefix + sessionId + "/active\"");
        assertResponseContains(response, "\"message\":\"Session " + sessionId + preparedMessage);
        assertResponseContains(response, "\"tenant\":\"" + tenant + "\"");
        assertResponseContains(response, "\"session-id\":\"" + sessionId + "\"");
        assertResponseContains(response, "\"log\":[]");
    }

    @Test
    public void require_debug() throws Exception {
        HttpResponse response = createHandler().handle(
                createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 9999L, "?debug=true"));
        assertEquals(NOT_FOUND, response.getStatus());
        assertTrue(SessionHandlerTest.getRenderedString(response).contains("NotFoundException"));
    }

    @Test
    public void require_verbose() throws Exception {
        long sessionId = createSession(applicationId());
        HttpResponse response = createHandler().handle(
                createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, sessionId, "?verbose=true"));
        System.out.println(getRenderedString(response));
        assertEquals(OK, response.getStatus());
        assertTrue(getRenderedString(response).contains("Created application "));
    }

    @Test
    public void require_get_response_activate_url_on_ok() throws Exception {
        long sessionId = createSession(applicationId());
        request(HttpRequest.Method.PUT, sessionId);
        HttpResponse getResponse = request(HttpRequest.Method.GET, sessionId);
        assertResponseContains(getResponse, "\"activate\":\"http://foo:1337" + pathPrefix +
                sessionId + "/active\",\"message\":\"Session " + sessionId + preparedMessage);
    }

    @Test
    public void require_get_response_error_on_not_prepared() throws Exception {
        long sessionId = createSession(applicationId());

        HttpResponse getResponse = request(HttpRequest.Method.GET, sessionId);
        assertHttpStatusCodeErrorCodeAndMessage(getResponse, BAD_REQUEST,
                                                HttpErrorResponse.ErrorCode.BAD_REQUEST,
                                                "Session not prepared: " + sessionId);

        request(HttpRequest.Method.PUT, sessionId);
        applicationRepository.activate(tenantRepository.getTenant(tenant), sessionId, timeoutBudget, false);

        getResponse = request(HttpRequest.Method.GET, sessionId);
        assertHttpStatusCodeErrorCodeAndMessage(getResponse, BAD_REQUEST,
                                                HttpErrorResponse.ErrorCode.BAD_REQUEST,
                                                "Session is active: " + sessionId);
    }

    @Test
    public void require_get_response_error_when_session_id_does_not_exist() throws Exception {
        HttpResponse getResponse = request(HttpRequest.Method.GET, 9999L);
        assertHttpStatusCodeErrorCodeAndMessage(getResponse, NOT_FOUND,
                                                HttpErrorResponse.ErrorCode.NOT_FOUND,
                                                "Remote session 9999 for 'test' was not found");
    }

    @Test
    public void prepare_with_multiple_tenants() throws Exception {
        SessionHandler handler = createHandler();

        TenantName defaultTenant = TenantName.from("test2");
        tenantRepository.addTenant(defaultTenant);
        ApplicationId applicationId1 = ApplicationId.from(defaultTenant, ApplicationName.from("app"), InstanceName.defaultName());
        long sessionId = createSession(applicationId1);

        pathPrefix = "/application/v2/tenant/" + defaultTenant + "/session/";
        HttpResponse response = request(HttpRequest.Method.PUT, sessionId);
        assertNotNull(response);
        assertEquals(SessionHandlerTest.getRenderedString(response), OK, response.getStatus());

        String applicationName = "myapp";
        ApplicationId applicationId2 = ApplicationId.from(tenant.value(), applicationName, "default");
        long sessionId2 = createSession(applicationId2);
        assertEquals(sessionId, sessionId2);  // Want to test when they are equal (but for different tenants)

        pathPrefix = "/application/v2/tenant/" + tenant + "/session/" + sessionId2 +
                     "/prepared?applicationName=" + applicationName;
        response = handler.handle(SessionHandlerTest.createTestRequest(pathPrefix));
        assertNotNull(response);
        assertEquals(SessionHandlerTest.getRenderedString(response), OK, response.getStatus());

        ApplicationId applicationId3 = ApplicationId.from(tenant.value(), applicationName, "quux");
        long sessionId3 = createSession(applicationId3);
        pathPrefix = "/application/v2/tenant/" + tenant + "/session/" + sessionId3 +
                     "/prepared?applicationName=" + applicationName + "&instance=quux";
        response = handler.handle(SessionHandlerTest.createTestRequest(pathPrefix));
        assertNotNull(response);
        assertEquals(SessionHandlerTest.getRenderedString(response), OK, response.getStatus());
    }

    @Test
    public void require_that_config_change_actions_are_in_response() throws Exception {
        long sessionId = createSession(applicationId());
        HttpResponse response = request(HttpRequest.Method.PUT, sessionId);
        assertResponseContains(response, "\"configChangeActions\":{\"restart\":[],\"refeed\":[],\"reindex\":[]}");
    }

    @Test
    public void require_that_config_change_actions_are_not_logged_if_not_existing() throws Exception {
        long sessionId = createSession(applicationId());
        HttpResponse response = request(HttpRequest.Method.PUT, sessionId);
        assertResponseNotContains(response, "Change(s) between active and new application that may require restart");
        assertResponseNotContains(response, "Change(s) between active and new application that may require re-feed");
        assertResponseNotContains(response, "Change(s) between active and new application that may require re-index");
    }

    @Test
    public void test_out_of_capacity_response() throws IOException {
        long sessionId = createSession(applicationId());
        String exceptionMessage = "Node allocation failure";
        FailingSessionPrepareHandler handler = new FailingSessionPrepareHandler(SessionPrepareHandler.testContext(),
                                                                                applicationRepository,
                                                                                configserverConfig,
                                                                                new NodeAllocationException(exceptionMessage, true));
        HttpResponse response = handler.handle(createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, sessionId));
        assertEquals(400, response.getStatus());
        Slime data = getData(response);
        assertEquals(HttpErrorResponse.ErrorCode.NODE_ALLOCATION_FAILURE.name(), data.get().field("error-code").asString());
        assertEquals(exceptionMessage, data.get().field("message").asString());
    }

    @Test
    public void test_that_nullpointerexception_gives_internal_server_error() throws IOException {
        long sessionId = createSession(applicationId());
        String exceptionMessage = "nullpointer thrown in test handler";
        FailingSessionPrepareHandler handler = new FailingSessionPrepareHandler(SessionPrepareHandler.testContext(),
                                                                                applicationRepository,
                                                                                configserverConfig,
                                                                                new NullPointerException(exceptionMessage));
        HttpResponse response = handler.handle(createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, sessionId));
        assertEquals(500, response.getStatus());
        Slime data = getData(response);
        assertEquals(HttpErrorResponse.ErrorCode.INTERNAL_SERVER_ERROR.name(), data.get().field("error-code").asString());
        assertEquals(exceptionMessage, data.get().field("message").asString());
    }

    @Test
    public void test_application_lock_failure() throws IOException {
        String exceptionMessage = "Timed out after waiting PT1M to acquire lock '/provision/v1/locks/foo/bar/default'";
        long sessionId = createSession(applicationId());
        FailingSessionPrepareHandler handler = new FailingSessionPrepareHandler(SessionPrepareHandler.testContext(),
                                                                                applicationRepository,
                                                                                configserverConfig,
                                                                                new ApplicationLockException(new UncheckedTimeoutException(exceptionMessage)));
        HttpResponse response = handler.handle(createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, sessionId));
        assertEquals(500, response.getStatus());
        Slime data = getData(response);
        assertEquals(HttpErrorResponse.ErrorCode.APPLICATION_LOCK_FAILURE.name(), data.get().field("error-code").asString());
        assertEquals(exceptionMessage, data.get().field("message").asString());
    }

    @Test
    public void test_docker_image_repository() {
        long sessionId = createSession(applicationId());
        String dockerImageRepository = "foo.bar.com:4443/baz/qux";
        request(HttpRequest.Method.PUT, sessionId, Map.of("dockerImageRepository", dockerImageRepository,
                                                          "applicationName", applicationId().application().value()));
        applicationRepository.activate(tenantRepository.getTenant(tenant), sessionId, timeoutBudget, false);
        assertEquals(DockerImage.fromString(dockerImageRepository),
                     applicationRepository.getActiveSession(applicationId()).get().getDockerImageRepository().get());
    }

    private Slime getData(HttpResponse response) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        response.render(baos);
        return SlimeUtils.jsonToSlime(baos.toByteArray());
    }

    private static void assertResponseContains(HttpResponse response, String string) throws IOException {
        String s = SessionHandlerTest.getRenderedString(response);
        assertTrue(s, s.contains(string));
    }

    private static void assertResponseNotContains(HttpResponse response, String string) throws IOException {
        assertFalse(SessionHandlerTest.getRenderedString(response).contains(string));
    }

    private SessionHandler createHandler() {
        return new SessionPrepareHandler(SessionPrepareHandler.testContext(), applicationRepository, configserverConfig);
    }

    private HttpResponse request(HttpRequest.Method put, long l) {
        return request(put, l, Map.of());
    }

    private HttpResponse request(HttpRequest.Method put, long l, Map<String, String> requestParameters) {
        return createHandler().handle(SessionHandlerTest.createTestRequest(pathPrefix, put, Cmd.PREPARED, l, "", null, requestParameters));
    }

    private ApplicationId applicationId() {
        return ApplicationId.from(tenant.value(), "app", "default");
    }

    private long createSession(ApplicationId applicationId) {
        return applicationRepository.createSession(applicationId, timeoutBudget, app, new BaseDeployLogger());
    }

    private static class FailingSessionPrepareHandler extends SessionPrepareHandler {

        private final RuntimeException exceptionToBeThrown;

        public FailingSessionPrepareHandler(Context ctx, ApplicationRepository applicationRepository,
                                            ConfigserverConfig configserverConfig, RuntimeException exceptionToBeThrown) {
            super(ctx, applicationRepository, configserverConfig);
            this.exceptionToBeThrown = exceptionToBeThrown;
        }

        @Override
        protected HttpResponse handlePUT(com.yahoo.container.jdisc.HttpRequest request) {
            throw exceptionToBeThrown;
        }
    }

}