summaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/ControllerTester.java
blob: b49d55aeb3b4881af66616c400ea0b0aec8f1f51 (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
// 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;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationName;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Zone;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.hosted.controller.api.Tenant;
import com.yahoo.vespa.hosted.controller.api.application.v4.model.DeployOptions;
import com.yahoo.vespa.hosted.controller.api.application.v4.model.GitRevision;
import com.yahoo.vespa.hosted.controller.api.application.v4.model.ScrewdriverBuildJob;
import com.yahoo.vespa.hosted.controller.api.identifiers.AthenzDomain;
import com.yahoo.vespa.hosted.controller.api.identifiers.GitBranch;
import com.yahoo.vespa.hosted.controller.api.identifiers.GitCommit;
import com.yahoo.vespa.hosted.controller.api.identifiers.GitRepository;
import com.yahoo.vespa.hosted.controller.api.identifiers.Property;
import com.yahoo.vespa.hosted.controller.api.identifiers.PropertyId;
import com.yahoo.vespa.hosted.controller.api.identifiers.ScrewdriverId;
import com.yahoo.vespa.hosted.controller.api.identifiers.TenantId;
import com.yahoo.vespa.hosted.controller.api.integration.chef.ChefMock;
import com.yahoo.vespa.hosted.controller.api.integration.dns.MemoryNameService;
import com.yahoo.vespa.hosted.controller.api.integration.entity.MemoryEntityService;
import com.yahoo.vespa.hosted.controller.api.integration.github.GitHubMock;
import com.yahoo.vespa.hosted.controller.api.integration.jira.JiraMock;
import com.yahoo.vespa.hosted.controller.api.integration.routing.MemoryGlobalRoutingService;
import com.yahoo.vespa.hosted.controller.application.ApplicationPackage;
import com.yahoo.vespa.hosted.controller.athenz.mock.AthenzDbMock;
import com.yahoo.vespa.hosted.controller.athenz.mock.AthenzClientFactoryMock;
import com.yahoo.vespa.hosted.controller.integration.MockMetricsService;
import com.yahoo.vespa.hosted.controller.persistence.ControllerDb;
import com.yahoo.vespa.hosted.controller.persistence.CuratorDb;
import com.yahoo.vespa.hosted.controller.persistence.MemoryControllerDb;
import com.yahoo.vespa.hosted.controller.persistence.MockCuratorDb;
import com.yahoo.vespa.hosted.controller.routing.MockRoutingGenerator;
import com.yahoo.vespa.hosted.controller.versions.VersionStatus;
import com.yahoo.vespa.hosted.rotation.MemoryRotationRepository;

import java.util.Optional;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * Convenience methods for controller tests.
 *
 * @author bratseth
 * @author mpolden
 */
public final class ControllerTester {

    private final ControllerDb db;
    private final AthenzDbMock athenzDb;
    private final ManualClock clock;
    private final ConfigServerClientMock configServer;
    private final ZoneRegistryMock zoneRegistry;
    private final GitHubMock gitHub;
    private final CuratorDb curator;
    private final MemoryNameService nameService;

    private Controller controller;

    public ControllerTester() {
        this(new MemoryControllerDb(), new AthenzDbMock(), new ManualClock(), new ConfigServerClientMock(),
             new ZoneRegistryMock(), new GitHubMock(), new MockCuratorDb(), new MemoryNameService());
    }

    public ControllerTester(ManualClock clock) {
        this(new MemoryControllerDb(), new AthenzDbMock(), clock, new ConfigServerClientMock(),
             new ZoneRegistryMock(), new GitHubMock(), new MockCuratorDb(), new MemoryNameService());
    }

    private ControllerTester(ControllerDb db, AthenzDbMock athenzDb, ManualClock clock,
                             ConfigServerClientMock configServer, ZoneRegistryMock zoneRegistry,
                             GitHubMock gitHub, CuratorDb curator, MemoryNameService nameService) {
        this.db = db;
        this.athenzDb = athenzDb;
        this.clock = clock;
        this.configServer = configServer;
        this.zoneRegistry = zoneRegistry;
        this.gitHub = gitHub;
        this.curator = curator;
        this.nameService = nameService;
        this.controller = createController(db, curator, configServer, clock, gitHub, zoneRegistry,
                                           athenzDb, nameService);
    }

    public Controller controller() { return controller; }

    public CuratorDb curator() { return curator; }

    public ManualClock clock() { return clock; }

    public AthenzDbMock athenzDb() { return athenzDb; }

    public MemoryNameService nameService() { return nameService; }

    public ZoneRegistryMock zoneRegistry() { return zoneRegistry; }

    public ConfigServerClientMock configServer() { return configServer; }

    public GitHubMock gitHub() { return gitHub; }

    /** Create a new controller instance. Useful to verify that controller state is rebuilt from persistence */
    public final void createNewController() {
        controller = createController(db, curator, configServer, clock, gitHub, zoneRegistry, athenzDb, nameService);
    }

    /** Creates the given tenant and application and deploys it */
    public Application createAndDeploy(String tenantName, String domainName, String applicationName, Environment environment, long projectId, Long propertyId) {
        return createAndDeploy(tenantName, domainName, applicationName, toZone(environment), projectId, propertyId);
    }

    /** Creates the given tenant and application and deploys it */
    public Application createAndDeploy(String tenantName, String domainName, String applicationName,
                                       String instanceName, Zone zone, long projectId, Long propertyId) {
        TenantId tenant = createTenant(tenantName, domainName, propertyId);
        Application application = createApplication(tenant, applicationName, instanceName, projectId);
        deploy(application, zone);
        return application;
    }

    /** Creates the given tenant and application and deploys it */
    public Application createAndDeploy(String tenantName, String domainName, String applicationName,
                                       String instanceName, Environment environment, long projectId, Long propertyId) {
        return createAndDeploy(tenantName, domainName, applicationName, instanceName, toZone(environment), projectId, propertyId);
    }

    /** Creates the given tenant and application and deploys it */
    public Application createAndDeploy(String tenantName, String domainName, String applicationName, Zone zone, long projectId, Long propertyId) {
        return createAndDeploy(tenantName, domainName, applicationName, "default", zone, projectId, propertyId);
    }

    /** Creates the given tenant and application and deploys it */
    public Application createAndDeploy(String tenantName, String domainName, String applicationName, Environment environment, long projectId) {
        return createAndDeploy(tenantName, domainName, applicationName, environment, projectId, null);
    }

    public Zone toZone(Environment environment) {
        switch (environment) {
            case dev: case test: return new Zone(environment, RegionName.from("us-east-1"));
            case staging: return new Zone(environment, RegionName.from("us-east-3"));
            default: return new Zone(environment, RegionName.from("us-west-1"));
        }
    }

    public AthenzDomain createDomain(String domainName) {
        AthenzDomain domain = new AthenzDomain(domainName);
        athenzDb.addDomain(new AthenzDbMock.Domain(domain));
        return domain;
    }

    public TenantId createTenant(String tenantName, String domainName, Long propertyId) {
        TenantId id = new TenantId(tenantName);
        Optional<Tenant> existing = controller().tenants().tenant(id);
        if (existing.isPresent()) return id;

        Tenant tenant = Tenant.createAthensTenant(id, createDomain(domainName), new Property("app1Property"),
                propertyId == null ? Optional.empty() : Optional.of(new PropertyId(propertyId.toString())));
        controller().tenants().addTenant(tenant, Optional.of(TestIdentities.userNToken));
        assertNotNull(controller().tenants().tenant(id));
        return id;
    }

    public Application createApplication(TenantId tenant, String applicationName, String instanceName, long projectId) {
        ApplicationId applicationId = applicationId(tenant.id(), applicationName, instanceName);
        Application application = controller().applications().createApplication(applicationId, Optional.of(TestIdentities.userNToken))
                                                             .withProjectId(projectId);
        assertTrue(controller().applications().get(applicationId).isPresent());
        return application;
    }

    public void deploy(Application application, Zone zone) {
        deploy(application, zone, new ApplicationPackage(new byte[0]));
    }

    public void deploy(Application application, Zone zone, ApplicationPackage applicationPackage) {
        deploy(application, zone, applicationPackage, false);
    }

    public void deploy(Application application, Zone zone, ApplicationPackage applicationPackage, boolean deployCurrentVersion) {
        ScrewdriverId app1ScrewdriverId = new ScrewdriverId(String.valueOf(application.deploymentJobs().projectId().get()));
        GitRevision app1RevisionId = new GitRevision(new GitRepository("repo"), new GitBranch("master"), new GitCommit("commit1"));
        controller().applications().deployApplication(application.id(),
                                                      zone,
                                                      applicationPackage,
                                                      new DeployOptions(Optional.of(new ScrewdriverBuildJob(app1ScrewdriverId, app1RevisionId)), Optional.empty(), false, deployCurrentVersion));
    }

    public ApplicationId applicationId(String tenant, String application, String instance) {
        return ApplicationId.from(TenantName.from(tenant),
                                  ApplicationName.from(application),
                                  InstanceName.from(instance));
    }

    private static Controller createController(ControllerDb db, CuratorDb curator,
                                               ConfigServerClientMock configServerClientMock, ManualClock clock,
                                               GitHubMock gitHubClientMock, ZoneRegistryMock zoneRegistryMock,
                                               AthenzDbMock athensDb, MemoryNameService nameService) {
        Controller controller = new Controller(db,
                                               curator,
                                               new MemoryRotationRepository(),
                                               gitHubClientMock,
                                               new JiraMock(),
                                               new MemoryEntityService(),
                                               new MemoryGlobalRoutingService(),
                                               zoneRegistryMock,
                                               configServerClientMock,
                                               new MockMetricsService(),
                                               nameService,
                                               new MockRoutingGenerator(),
                                               new ChefMock(),
                                               clock,
                                               new AthenzClientFactoryMock(athensDb));
        controller.updateVersionStatus(VersionStatus.compute(controller));
        return controller;
    }

}