aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/maintenance/DeploymentExpirerTest.java
blob: 4df1336ac1f2c8649f005acb6473a9b20063ee04 (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
// 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.maintenance;

import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.Instance;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.JobType;
import com.yahoo.vespa.hosted.controller.application.ApplicationPackage;
import com.yahoo.vespa.hosted.controller.application.Deployment;
import com.yahoo.vespa.hosted.controller.deployment.ApplicationPackageBuilder;
import com.yahoo.vespa.hosted.controller.deployment.DeploymentTester;
import com.yahoo.vespa.hosted.controller.persistence.MockCuratorDb;
import org.junit.Test;

import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;

/**
 * @author bratseth
 */
public class DeploymentExpirerTest {

    private final DeploymentTester tester = new DeploymentTester();

    @Test
    public void testDeploymentExpiry() {
        tester.controllerTester().zoneRegistry().setDeploymentTimeToLive(
                ZoneId.from(Environment.dev, RegionName.from("us-east-1")),
                Duration.ofDays(14)
        );
        DeploymentExpirer expirer = new DeploymentExpirer(tester.controller(), Duration.ofDays(10),
                                                          new JobControl(new MockCuratorDb()));
        Application devApp = tester.createApplication("tenant1", "app1", "default");
        Application prodApp = tester.createApplication("tenant2", "app2", "default");

        ApplicationPackage appPackage = new ApplicationPackageBuilder()
                .region("us-west-1")
                .build();

        Instance devInstance = tester.instance(devApp.id().defaultInstance());
        Instance prodInstance = tester.instance(prodApp.id().defaultInstance());

        // Deploy dev
        tester.runJob(devInstance.id(), JobType.devUsEast1, appPackage);

        // Deploy prod
        tester.deployNewSubmission(prodApp.id(), tester.newSubmission(prodApp.id(), appPackage));

        assertEquals(1, permanentDeployments(devInstance).size());
        assertEquals(1, permanentDeployments(prodInstance).size());

        // Not expired at first
        expirer.maintain();
        assertEquals(1, permanentDeployments(devInstance).size());
        assertEquals(1, permanentDeployments(prodInstance).size());

        // The dev application is removed
        tester.clock().advance(Duration.ofDays(15));
        expirer.maintain();
        assertEquals(0, permanentDeployments(devInstance).size());
        assertEquals(1, permanentDeployments(prodInstance).size());
    }

    private List<Deployment> permanentDeployments(Instance instance) {
        return tester.controller().applications().getInstance(instance.id()).get().deployments().values().stream()
                     .filter(deployment -> deployment.zone().environment() != Environment.test &&
                                      deployment.zone().environment() != Environment.staging)
                     .collect(Collectors.toList());
    }

}