summaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/screwdriver/ScrewdriverApiTest.java
blob: 1269bb23105a4a8a906c628efaa07657cd5efde3 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.screwdriver;

import com.yahoo.application.container.handler.Request;
import com.yahoo.application.container.handler.Response;
import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.ZoneId;
import com.yahoo.vespa.config.SlimeUtils;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.application.ApplicationPackage;
import com.yahoo.vespa.hosted.controller.application.DeploymentJobs.JobError;
import com.yahoo.vespa.hosted.controller.application.DeploymentJobs.JobType;
import com.yahoo.vespa.hosted.controller.application.JobStatus;
import com.yahoo.vespa.hosted.controller.deployment.ApplicationPackageBuilder;
import com.yahoo.vespa.hosted.controller.deployment.BuildSystem;
import com.yahoo.vespa.hosted.controller.restapi.ContainerControllerTester;
import com.yahoo.vespa.hosted.controller.restapi.ControllerContainerTest;
import com.yahoo.vespa.hosted.controller.versions.VersionStatus;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * @author bratseth
 * @author jvenstad
 */
public class ScrewdriverApiTest extends ControllerContainerTest {

    private static final String responseFiles = "src/test/java/com/yahoo/vespa/hosted/controller/restapi/screwdriver/responses/";
    private static final ZoneId testZone = ZoneId.from(Environment.test, RegionName.from("us-east-1"));
    private static final ZoneId stagingZone = ZoneId.from(Environment.staging, RegionName.from("us-east-3"));

    @Test
    public void testGetReleaseStatus() throws Exception {
        ContainerControllerTester tester = new ContainerControllerTester(container, responseFiles);
        tester.containerTester().assertResponse(new Request("http://localhost:8080/screwdriver/v1/release/vespa"),
                                                "{\"error-code\":\"NOT_FOUND\",\"message\":\"Information about the current system version is not available at this time\"}",
                                                404);

        tester.controller().updateVersionStatus(VersionStatus.compute(tester.controller()));
        tester.containerTester().assertResponse(new Request("http://localhost:8080/screwdriver/v1/release/vespa"),
                                                new File("release-response.json"), 200);
    }

    @Test
    public void testJobStatusReporting() throws Exception {
        ContainerControllerTester tester = new ContainerControllerTester(container, responseFiles);
        tester.containerTester().updateSystemVersion();
        long projectId = 1;
        Application app = tester.createApplication();
        ApplicationPackage applicationPackage = new ApplicationPackageBuilder()
                .environment(Environment.prod)
                .region("corp-us-east-1")
                .build();

        Version vespaVersion = new Version("6.1"); // system version from mock config server client

        // Make web service calls.
        notifyCompletion(app.id(), projectId, JobType.component, Optional.empty());
        tester.deploy(app, applicationPackage, testZone, projectId);
        notifyCompletion(app.id(), projectId, JobType.systemTest, Optional.empty());

        // Notifying about unknown job fails
        tester.containerTester().assertResponse(new Request("http://localhost:8080/screwdriver/v1/jobreport",
                                                            jsonReport(app.id(), JobType.productionUsEast3, projectId, 1L,
                                                                       Optional.empty())
                                                                    .getBytes(StandardCharsets.UTF_8),
                                                            Request.Method.POST),
                                                new File("unexpected-completion.json"), 400);
        
        // ... and assert it was recorded
        JobStatus recordedStatus =
                tester.controller().applications().get(app.id()).get().deploymentJobs().jobStatus().get(JobType.component);
        
        assertNotNull("Status was recorded", recordedStatus);
        assertTrue(recordedStatus.isSuccess());
        assertEquals(vespaVersion, recordedStatus.lastCompleted().get().version());

        recordedStatus =
                tester.controller().applications().get(app.id()).get().deploymentJobs().jobStatus().get(JobType.productionApNortheast2);
        assertNull("Status of never-triggered jobs is empty", recordedStatus);

        Response response;

        response = container.handleRequest(new Request("http://localhost:8080/screwdriver/v1/jobsToRun", "", Request.Method.GET));
        assertTrue("Response contains system-test", response.getBodyAsString().contains(JobType.systemTest.jobName()));
        assertTrue("Response contains staging-test", response.getBodyAsString().contains(JobType.stagingTest.jobName()));
        assertEquals("Response contains only two items", 2, SlimeUtils.jsonToSlime(response.getBody()).get().entries());

        // Check that GET didn't affect the enqueued jobs.
        response = container.handleRequest(new Request("http://localhost:8080/screwdriver/v1/jobsToRun", "", Request.Method.DELETE));
        assertTrue("Response contains system-test", response.getBodyAsString().contains(JobType.systemTest.jobName()));
        assertTrue("Response contains staging-test", response.getBodyAsString().contains(JobType.stagingTest.jobName()));
        assertEquals("Response contains only two items", 2, SlimeUtils.jsonToSlime(response.getBody()).get().entries());

        Thread.sleep(50);
        // Check that the *first* DELETE has removed the enqueued jobs.
        assertResponse(new Request("http://localhost:8080/screwdriver/v1/jobsToRun", "", Request.Method.DELETE),
                200, "[]");
    }

    @Test
    public void testJobStatusReportingOutOfCapacity() throws Exception {
        ContainerControllerTester tester = new ContainerControllerTester(container, responseFiles);
        tester.containerTester().updateSystemVersion();

        long projectId = 1;
        Application app = tester.createApplication();
        ApplicationPackage applicationPackage = new ApplicationPackageBuilder()
                .environment(Environment.prod)
                .region("corp-us-east-1")
                .build();

        // Report job failing with out of capacity
        notifyCompletion(app.id(), projectId, JobType.component, Optional.empty());
        tester.deploy(app, applicationPackage, testZone, projectId);
        notifyCompletion(app.id(), projectId, JobType.systemTest, Optional.empty());
        tester.deploy(app, applicationPackage, stagingZone, projectId);
        notifyCompletion(app.id(), projectId, JobType.stagingTest, Optional.of(JobError.outOfCapacity));

        // Appropriate error is recorded
        JobStatus jobStatus = tester.controller().applications().get(app.id())
                .get()
                .deploymentJobs()
                .jobStatus()
                .get(JobType.stagingTest);
        assertFalse(jobStatus.isSuccess());
        assertEquals(JobError.outOfCapacity, jobStatus.jobError().get());
    }

    @Test
    public void testTriggerJobForApplication() throws Exception {
        ContainerControllerTester tester = new ContainerControllerTester(container, responseFiles);
        BuildSystem buildSystem = tester.controller().applications().deploymentTrigger().buildSystem();
        tester.containerTester().updateSystemVersion();

        Application app = tester.createApplication();
        tester.controller().applications().lockOrThrow(app.id(), application ->
                tester.controller().applications().store(application.withProjectId(1)));

        // Unknown application
        assertResponse(new Request("http://localhost:8080/screwdriver/v1/trigger/tenant/foo/application/bar",
                                   new byte[0], Request.Method.POST),
                       400, "{\"error-code\":\"BAD_REQUEST\",\"message\":\"foo.bar not found\"}");

        // Invalid job
        assertResponse(new Request("http://localhost:8080/screwdriver/v1/trigger/tenant/" +
                                   app.id().tenant().value() + "/application/" + app.id().application().value(),
                                   "invalid".getBytes(StandardCharsets.UTF_8), Request.Method.POST),
                       400, "{\"error-code\":\"BAD_REQUEST\",\"message\":\"Unknown job name 'invalid'\"}");

        // component is triggered if no job is specified in request body
        assertResponse(new Request("http://localhost:8080/screwdriver/v1/trigger/tenant/" +
                                   app.id().tenant().value() + "/application/" + app.id().application().value(),
                                   new byte[0], Request.Method.POST),
                       200, "{\"message\":\"Triggered component for tenant1.application1\"}");

        assertFalse(buildSystem.jobs().isEmpty());
        assertEquals(JobType.component.jobName(), buildSystem.jobs().get(0).jobName());
        assertEquals(1L, buildSystem.jobs().get(0).projectId());
        buildSystem.takeJobsToRun();

        // Triggers specific job when given
        assertResponse(new Request("http://localhost:8080/screwdriver/v1/trigger/tenant/" +
                                   app.id().tenant().value() + "/application/" + app.id().application().value(),
                                   "staging-test".getBytes(StandardCharsets.UTF_8), Request.Method.POST),
                       200, "{\"message\":\"Triggered staging-test for tenant1.application1\"}");
        assertFalse(buildSystem.jobs().isEmpty());
        assertEquals(JobType.stagingTest.jobName(), buildSystem.jobs().get(0).jobName());
        assertEquals(1L, buildSystem.jobs().get(0).projectId());
    }
    
    private void notifyCompletion(ApplicationId app, long projectId, JobType jobType, Optional<JobError> error) throws IOException {
        assertResponse(new Request("http://localhost:8080/screwdriver/v1/jobreport",
                                   jsonReport(app, jobType, projectId, 1L, error).getBytes(StandardCharsets.UTF_8),
                                   Request.Method.POST),
                       200, "ok");
    }

    private static String jsonReport(ApplicationId applicationId, JobType jobType, long projectId, long buildNumber,
                                     Optional<JobError> jobError) {
        return
                "{\n" +
                "    \"projectId\"     : " + projectId + ",\n" +
                "    \"jobName\"       :\"" + jobType.jobName() + "\",\n" +
                "    \"buildNumber\"   : " + buildNumber + ",\n" +
                jobError.map(message -> "    \"jobError\"      : \""  + message + "\",\n").orElse("") +
                "    \"tenant\"        :\"" + applicationId.tenant().value() + "\",\n" +
                "    \"application\"   :\"" + applicationId.application().value() + "\",\n" +
                "    \"instance\"      :\"" + applicationId.instance().value() + "\"\n" +
                "}";
    }

}