summaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/ControllerTester.java
blob: 27b77cfc9016e4a7b8a989f79fbae3f983236859 (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
// 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.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.slime.Slime;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.athenz.api.AthenzDomain;
import com.yahoo.vespa.curator.Lock;
import com.yahoo.vespa.curator.mock.MockCurator;
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.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.integration.BuildService;
import com.yahoo.vespa.hosted.controller.api.integration.chef.ChefMock;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.ArtifactRepository;
import com.yahoo.vespa.hosted.controller.api.integration.dns.MemoryNameService;
import com.yahoo.vespa.hosted.controller.api.integration.entity.EntityService;
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.organization.MockOrganization;
import com.yahoo.vespa.hosted.controller.api.integration.routing.MemoryGlobalRoutingService;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.application.ApplicationPackage;
import com.yahoo.vespa.hosted.controller.tenant.AthenzTenant;
import com.yahoo.vespa.hosted.controller.tenant.Tenant;
import com.yahoo.vespa.hosted.controller.athenz.mock.AthenzClientFactoryMock;
import com.yahoo.vespa.hosted.controller.athenz.mock.AthenzDbMock;
import com.yahoo.vespa.hosted.controller.deployment.MockBuildService;
import com.yahoo.vespa.hosted.controller.integration.MockMetricsService;
import com.yahoo.vespa.hosted.controller.persistence.ApplicationSerializer;
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.config.RotationsConfig;

import java.util.Optional;
import java.util.logging.Logger;

import static org.junit.Assert.assertNotNull;

/**
 * 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 final RotationsConfig rotationsConfig;
    private final ArtifactRepositoryMock artifactRepository;
    private final EntityService entityService;
    private final MockBuildService buildService;

    private Controller controller;

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

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

    public ControllerTester(RotationsConfig rotationsConfig) {
        this(new MemoryControllerDb(), new AthenzDbMock(), new ManualClock(), new ConfigServerClientMock(),
             new ZoneRegistryMock(), new GitHubMock(), new MockCuratorDb(), rotationsConfig, new MemoryNameService(),
             new ArtifactRepositoryMock(), new MemoryEntityService(), new MockBuildService());
    }

    private ControllerTester(ControllerDb db, AthenzDbMock athenzDb, ManualClock clock,
                             ConfigServerClientMock configServer, ZoneRegistryMock zoneRegistry,
                             GitHubMock gitHub, CuratorDb curator, RotationsConfig rotationsConfig,
                             MemoryNameService nameService, ArtifactRepositoryMock artifactRepository,
                             EntityService entityService, MockBuildService buildService) {
        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.rotationsConfig = rotationsConfig;
        this.artifactRepository = artifactRepository;
        this.entityService = entityService;
        this.buildService = buildService;
        this.controller = createController(db, curator, rotationsConfig, configServer, clock, gitHub, zoneRegistry,
                                           athenzDb, nameService, artifactRepository, entityService, buildService);

        // Set the log output from the root logger to use timestamps from the manual clock ;)
        Logger.getLogger("").getHandlers()[0].setFilter(
                record -> {
                    record.setMillis(clock.millis());
                    return true;
                });
    }

    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; }

    public ArtifactRepositoryMock artifactRepository() { return artifactRepository; }

    public EntityService entityService() { return entityService; }

    public MockBuildService buildService() { return buildService; }

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

    /** 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, ZoneId zone, long projectId, Long propertyId) {
        TenantName 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, ZoneId 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);
    }

    /** Create application from slime */
    public Application createApplication(Slime slime) {
        ApplicationSerializer serializer = new ApplicationSerializer();
        Application application = serializer.fromSlime(slime);
        try (Lock lock = controller().applications().lock(application.id())) {
            controller().applications().store(new LockedApplication(application, lock));
        }
        return application;
    }

    public ZoneId toZone(Environment environment) {
        switch (environment) {
            case dev: case test:
                return ZoneId.from(environment, RegionName.from("us-east-1"));
            case staging:
                return ZoneId.from(environment, RegionName.from("us-east-3"));
            default:
                return ZoneId.from(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 TenantName createTenant(String tenantName, String domainName, Long propertyId) {
        TenantName name = TenantName.from(tenantName);
        Optional<Tenant> existing = controller().tenants().tenant(name);
        if (existing.isPresent()) return name;
        AthenzTenant tenant = AthenzTenant.create(name, createDomain(domainName), new Property("app1Property"),
                                                  Optional.ofNullable(propertyId)
                                                          .map(Object::toString)
                                                          .map(PropertyId::new));
        controller().tenants().create(tenant, TestIdentities.userNToken);
        assertNotNull(controller().tenants().tenant(name));
        return name;
    }

    public Application createApplication(TenantName tenant, String applicationName, String instanceName, long projectId) {
        ApplicationId applicationId = ApplicationId.from(tenant.value(), applicationName, instanceName);
        controller().applications().createApplication(applicationId, Optional.of(TestIdentities.userNToken));
        controller().applications().lockOrThrow(applicationId, lockedApplication ->
                controller().applications().store(lockedApplication.withProjectId(Optional.of(projectId))));
        return controller().applications().require(applicationId);
    }

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

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

    public void deploy(Application application, ZoneId zone, ApplicationPackage applicationPackage, boolean deployCurrentVersion) {
        deploy(application, zone, Optional.of(applicationPackage), deployCurrentVersion);
    }

    public void deploy(Application application, ZoneId zone, Optional<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));
    }

    // Used by ApplicationSerializerTest to avoid breaking encapsulation. Should not be used by anything else
    public static LockedApplication writable(Application application) {
        return new LockedApplication(application, new Lock("/test", new MockCurator()));
    }

    private static Controller createController(ControllerDb db, CuratorDb curator, RotationsConfig rotationsConfig,
                                               ConfigServerClientMock configServerClientMock, ManualClock clock,
                                               GitHubMock gitHubClientMock, ZoneRegistryMock zoneRegistryMock,
                                               AthenzDbMock athensDb, MemoryNameService nameService,
                                               ArtifactRepository artifactRepository, EntityService entityService,
                                               BuildService buildService) {
        Controller controller = new Controller(db,
                                               curator,
                                               rotationsConfig,
                                               gitHubClientMock,
                                               entityService,
                                               new MockOrganization(clock),
                                               new MemoryGlobalRoutingService(),
                                               zoneRegistryMock,
                                               configServerClientMock,
                                               new NodeRepositoryClientMock(),
                                               new MockMetricsService(),
                                               nameService,
                                               new MockRoutingGenerator(),
                                               new ChefMock(),
                                               clock,
                                               new AthenzClientFactoryMock(athensDb),
                                               artifactRepository,
                                               buildService);
        controller.updateVersionStatus(VersionStatus.compute(controller));
        return controller;
    }

    private static RotationsConfig defaultRotationsConfig() {
        RotationsConfig.Builder builder = new RotationsConfig.Builder();
        for (int i = 1; i <= 10; i++) {
            String id = String.format("%02d", i);
            builder = builder.rotations("rotation-id-" + id, "rotation-fqdn-" + id);
        }
        return new RotationsConfig(builder);
    }

}