aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/deployment/DeploymentTester.java
blob: 582333154a2c43837d0c0480a47bb12ef2c025cd (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.deployment;

import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.ApplicationController;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.ControllerTester;
import com.yahoo.vespa.hosted.controller.Instance;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.JobType;
import com.yahoo.vespa.hosted.controller.api.integration.stubs.MockBuildService;
import com.yahoo.vespa.hosted.controller.application.ApplicationPackage;
import com.yahoo.vespa.hosted.controller.application.Change;
import com.yahoo.vespa.hosted.controller.application.DeploymentJobs;
import com.yahoo.vespa.hosted.controller.application.JobStatus;
import com.yahoo.vespa.hosted.controller.application.SystemApplication;
import com.yahoo.vespa.hosted.controller.application.TenantAndApplicationId;
import com.yahoo.vespa.hosted.controller.integration.ConfigServerMock;
import com.yahoo.vespa.hosted.controller.maintenance.JobControl;
import com.yahoo.vespa.hosted.controller.maintenance.NameServiceDispatcher;
import com.yahoo.vespa.hosted.controller.maintenance.OutstandingChangeDeployer;
import com.yahoo.vespa.hosted.controller.maintenance.ReadyJobsTrigger;
import com.yahoo.vespa.hosted.controller.maintenance.Upgrader;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * This class provides convenience methods for testing deployments
 *
 * @author bratseth
 * @author mpolden
 */
public class DeploymentTester {

    // Set a long interval so that maintainers never do scheduled runs during tests
    private static final Duration maintenanceInterval = Duration.ofDays(1);

    private final ControllerTester tester;
    private final Upgrader upgrader;
    private final OutstandingChangeDeployer outstandingChangeDeployer;
    private final ReadyJobsTrigger readyJobTrigger;
    private final NameServiceDispatcher nameServiceDispatcher;
    private final boolean updateDnsAutomatically;

    public DeploymentTester() {
        this(new ControllerTester());
    }

    public DeploymentTester(ControllerTester tester) {
        this(tester, true);
    }

    public DeploymentTester(ControllerTester tester, boolean updateDnsAutomatically) {
        this.tester = tester;
        this.updateDnsAutomatically = updateDnsAutomatically;
        tester.curator().writeUpgradesPerMinute(100);

        JobControl jobControl = new JobControl(tester.curator());
        this.upgrader = new Upgrader(tester.controller(), maintenanceInterval, jobControl, tester.curator());
        this.upgrader.setUpgradesPerMinute(1); // Anything that makes it at least one for any maintenance period is fine.
        this.outstandingChangeDeployer = new OutstandingChangeDeployer(tester.controller(), maintenanceInterval, jobControl);
        this.readyJobTrigger = new ReadyJobsTrigger(tester.controller(), maintenanceInterval, jobControl);
        this.nameServiceDispatcher = new NameServiceDispatcher(tester.controller(), Duration.ofHours(12),
                                                               new JobControl(tester.controller().curator()),
                                                               Integer.MAX_VALUE);
        atHourOfDay(5); // Set hour of day which always allows confidence to change
    }

    public DeploymentTester atHourOfDay(int hour) {
        var dateTime = tester.clock().instant().atZone(ZoneOffset.UTC);
        return at(LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDayOfMonth(), hour,
                                   dateTime.getMinute(), dateTime.getSecond())
                               .toInstant(ZoneOffset.UTC));
    }

    public DeploymentTester at(Instant instant) {
        tester.clock().setInstant(instant);
        return this;
    }

    public Upgrader upgrader() { return upgrader; }

    public OutstandingChangeDeployer outstandingChangeDeployer() { return outstandingChangeDeployer; }

    public ReadyJobsTrigger readyJobTrigger() { return readyJobTrigger; }

    public Controller controller() { return tester.controller(); }

    public ApplicationController applications() { return tester.controller().applications(); }

    public MockBuildService buildService() { return tester.serviceRegistry().buildServiceMock(); }

    public DeploymentTrigger deploymentTrigger() { return tester.controller().applications().deploymentTrigger(); }

    public ManualClock clock() { return tester.clock(); }

    public ControllerTester controllerTester() { return tester; }

    public ConfigServerMock configServer() { return tester.serviceRegistry().configServerMock(); }

    public Application application(TenantAndApplicationId id) {
        return controller().applications().requireApplication(id);
    }

    public Instance defaultInstance(String name) {
        return instance(ApplicationId.from("tenant1", name, "default"));
    }

    public Instance defaultInstance(TenantAndApplicationId application) {
        return controller().applications().requireApplication(application).require(InstanceName.defaultName());
    }

    public Instance instance(ApplicationId application) {
        return controller().applications().requireInstance(application);
    }

    // TODO(mpolden): Change callers to use ControllerTester#computeVersionStatus and remove this
    public void computeVersionStatus() {
        tester.computeVersionStatus();
    }

    // TODO(mpolden): Change callers to use ControllerTester#upgradeController and remove this
    public void upgradeController(Version version) {
        tester.upgradeController(version);
    }

    // TODO(mpolden): Change callers to use ControllerTester#upgradeController and remove this
    public void upgradeController(Version version, String commitSha, Instant commitDate) {
        tester.upgradeController(version, commitSha, commitDate);
    }

    // TODO(mpolden): Change callers to use ControllerTester#upgradeSystemApplications and remove this
    public void upgradeSystemApplications(Version version) {
        tester.upgradeSystemApplications(version);
    }

    // TODO(mpolden): Change callers to use ControllerTester#upgradeSystemApplications and remove this
    public void upgradeSystemApplications(Version version, List<SystemApplication> systemApplications) {
        tester.upgradeSystemApplications(version, systemApplications);
    }

    // TODO(mpolden): Change callers to use ControllerTester#upgradeSystem and remove this
    public void upgradeSystem(Version version) {
        tester.upgradeSystem(version);
        upgrader().maintain();
        readyJobTrigger().maintain();
    }

    // TODO(mpolden): Change callers to use InternalDeploymentTester#flushDnsRequests and remove this
    public void flushDnsRequests() {
        nameServiceDispatcher.run();
        assertTrue("All name service requests dispatched",
                   controller().curator().readNameServiceQueue().requests().isEmpty());
    }

    /** Triggers jobs until nothing more triggers, and returns the number of triggered jobs. */
    public int triggerUntilQuiescence() {
        int triggered = 0;
        while (triggered != (triggered += deploymentTrigger().triggerReadyJobs()));
        return triggered;
    }

    public Application createApplication(String applicationName, String tenantName, long projectId, long propertyId) {
        return createApplication("default", applicationName, tenantName, projectId, propertyId);
    }

    public Application createApplication(String instanceName, String applicationName, String tenantName, long projectId, long propertyId) {
        TenantName tenant = tester.createTenant(tenantName, UUID.randomUUID().toString(), propertyId);
        return tester.createApplication(tenant, applicationName, instanceName, projectId);
    }

    public void restartController() { tester.createNewController(); }

    /** Notify the controller about a job completing */
    public BuildJob jobCompletion(JobType job) {
        return new BuildJob(this::notifyJobCompletion, tester.serviceRegistry().artifactRepositoryMock()).type(job);
    }

    /** Simulate the full lifecycle of an application deployment as declared in given application package */
    public Application createAndDeploy(String applicationName, int projectId, ApplicationPackage applicationPackage) {
        TenantName tenant = tester.createTenant("tenant1", "domain1", 1L);
        return createAndDeploy(tenant, applicationName, projectId, applicationPackage);
    }

    /** Simulate the full lifecycle of an application deployment as declared in given application package */
    public Application createAndDeploy(TenantName tenant, String applicationName, int projectId, ApplicationPackage applicationPackage) {
        Application application = tester.createApplication(tenant, applicationName, "default", projectId);
        deployCompletely(application, applicationPackage);
        return applications().requireApplication(application.id());
    }

    /** Simulate the full lifecycle of an application deployment to prod.us-west-1 with the given upgrade policy */
    public Application createAndDeploy(String applicationName, int projectId, String upgradePolicy) {
        return createAndDeploy(applicationName, projectId, applicationPackage(upgradePolicy));
    }

    /** Simulate the full lifecycle of an application deployment to prod.us-west-1 with the given upgrade policy */
    public void createAndDeploy(TenantName tenant, String applicationName, int projectId, String upgradePolicy) {
        createAndDeploy(tenant, applicationName, projectId, applicationPackage(upgradePolicy));
    }

    /** Deploy application completely using the given application package */
    public void deployCompletely(Application application, ApplicationPackage applicationPackage) {
        deployCompletely(application, applicationPackage, BuildJob.defaultBuildNumber);
    }

    public void completeDeploymentWithError(Application application, ApplicationPackage applicationPackage, long buildNumber, JobType failOnJob) {
        jobCompletion(JobType.component).application(application)
                                        .buildNumber(buildNumber)
                                        .uploadArtifact(applicationPackage)
                                        .submit();
        completeDeployment(application, applicationPackage, Optional.ofNullable(failOnJob));
    }

    public void deployCompletely(Application application, ApplicationPackage applicationPackage, long buildNumber) {
        completeDeploymentWithError(application, applicationPackage, buildNumber, null);
    }

    private void completeDeployment(Application application, ApplicationPackage applicationPackage, Optional<JobType> failOnJob) {
        assertTrue(application.id() + " has pending changes to deploy", applications().requireApplication(application.id()).change().hasTargets());
        DeploymentSteps steps = controller().applications().deploymentTrigger().steps(applicationPackage.deploymentSpec());
        List<JobType> jobs = steps.jobs();
        // TODO jonmv: Change to list instances here.
        for (JobType job : jobs) {
            boolean failJob = failOnJob.map(j -> j.equals(job)).orElse(false);
            deployAndNotify(application.id().defaultInstance(), applicationPackage, ! failJob, job);
            if (failJob) {
                break;
            }
        }
        if (failOnJob.isPresent()) {
            assertTrue(applications().requireApplication(application.id()).change().hasTargets());
            assertTrue(defaultInstance(application.id()).deploymentJobs().hasFailures());
        } else {
            assertFalse(applications().requireApplication(application.id()).change().hasTargets());
        }
        if (updateDnsAutomatically) {
            flushDnsRequests();
        }
    }

    public void completeUpgrade(Application application, Version version, String upgradePolicy) {
        completeUpgrade(application, version, applicationPackage(upgradePolicy));
    }

    public void completeUpgrade(Application application, Version version, ApplicationPackage applicationPackage) {
        assertTrue(application + " has a change", applications().requireApplication(application.id()).change().hasTargets());
        assertEquals(Change.of(version), applications().requireApplication(application.id()).change());
        completeDeployment(application, applicationPackage, Optional.empty());
    }

    public void completeUpgradeWithError(Application application, Version version, String upgradePolicy, JobType failOnJob) {
        completeUpgradeWithError(application, version, applicationPackage(upgradePolicy), Optional.of(failOnJob));
    }

    public void completeUpgradeWithError(Application application, Version version, ApplicationPackage applicationPackage, JobType failOnJob) {
        completeUpgradeWithError(application, version, applicationPackage, Optional.of(failOnJob));
    }

    private void completeUpgradeWithError(Application application, Version version, ApplicationPackage applicationPackage, Optional<JobType> failOnJob) {
        assertTrue(applications().requireApplication(application.id()).change().hasTargets());
        assertEquals(Change.of(version), applications().requireApplication(application.id()).change());
        completeDeployment(application, applicationPackage, failOnJob);
    }

    public void deploy(JobType job, ApplicationId id, ApplicationPackage applicationPackage) {
        deploy(job, id, Optional.of(applicationPackage), false);
    }

    public void deploy(JobType job, ApplicationId id, ApplicationPackage applicationPackage,
                       boolean deployCurrentVersion) {
        deploy(job, id, Optional.of(applicationPackage), deployCurrentVersion);
    }

    public void deploy(JobType job, ApplicationId id, Optional<ApplicationPackage> applicationPackage,
                       boolean deployCurrentVersion) {
        tester.deploy(id, job.zone(controller().system()), applicationPackage, deployCurrentVersion);
    }

    public void deployAndNotify(Instance i, String upgradePolicy, boolean success, JobType job) {
        deployAndNotify(i.id(), applicationPackage(upgradePolicy), success, job);
    }

    public void deployAndNotify(ApplicationId id, ApplicationPackage applicationPackage, boolean success, JobType job) {
        deployAndNotify(id, Optional.of(applicationPackage), success, job);
    }

    public void deployAndNotify(Instance i, boolean success, JobType job) {
        deployAndNotify(i.id(), Optional.empty(), success, job);
    }
    public void deployAndNotify(ApplicationId id, boolean success, JobType job) {
        deployAndNotify(id, Optional.empty(), success, job);
    }

    public void deployAndNotify(ApplicationId id, Optional<ApplicationPackage> applicationPackage, boolean success, JobType job) {
        if (success) {
            // Staging deploys twice, once with current version and once with new version
            if (job == JobType.stagingTest) {
                deploy(job, id, applicationPackage, true);
            }
            deploy(job, id, applicationPackage, false);
        }
        // Deactivate test deployments after deploy. This replicates the behaviour of the tenant pipeline
        if (job.isTest()) {
            controller().applications().deactivate(id, job.zone(controller().system()));
        }
        jobCompletion(job).application(id).success(success).submit();
    }

    public Optional<JobStatus.JobRun> firstFailing(Instance instance, JobType job) {
        return tester.controller().applications().requireInstance(instance.id())
                     .deploymentJobs().jobStatus().get(job).firstFailing();
    }

    private void notifyJobCompletion(DeploymentJobs.JobReport report) {
        if (report.jobType() != JobType.component && ! buildService().remove(report.buildJob()))
            throw new IllegalArgumentException(report.jobType() + " is not running for " + report.applicationId());
        assertFalse("Unexpected entry '" + report.jobType() + "@" + report.projectId() + " in: " + buildService().jobs(),
                    buildService().remove(report.buildJob()));

        applications().deploymentTrigger().notifyOfCompletion(report);
        applications().deploymentTrigger().triggerReadyJobs();
    }

    public static ApplicationPackage applicationPackage(String upgradePolicy) {
        return new ApplicationPackageBuilder()
                .upgradePolicy(upgradePolicy)
                .environment(Environment.prod)
                .region("us-west-1")
                .region("us-east-3")
                .build();
    }

    public void assertRunning(JobType job, ApplicationId application) {
        assertTrue(String.format("Job %s for %s is running", job, application), isRunning(job, application));
    }

    public void assertNotRunning(JobType job, ApplicationId application) {
        assertFalse(String.format("Job %s for %s is not running", job, application), isRunning(job, application));
    }

    private boolean isRunning(JobType job, ApplicationId application) {
        return buildService().jobs().contains(ControllerTester.buildJob(instance(application).id(), job));
    }

}