summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/DeployTester.java
blob: 1121ac3a8cc5f96eec120f7b590ff0e591d764b7 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.deploy;

import com.google.common.io.Files;
import com.yahoo.cloud.config.ConfigserverConfig;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.model.NullConfigModelRegistry;
import com.yahoo.config.model.api.HostProvisioner;
import com.yahoo.config.model.api.Model;
import com.yahoo.config.model.api.ModelContext;
import com.yahoo.config.model.api.ModelCreateResult;
import com.yahoo.config.model.api.ModelFactory;
import com.yahoo.config.model.api.ValidationParameters;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.provision.InMemoryProvisioner;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Capacity;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.HostFilter;
import com.yahoo.config.provision.HostSpec;
import com.yahoo.config.provision.AllocatedHosts;
import com.yahoo.config.provision.ProvisionLogger;
import com.yahoo.config.provision.Provisioner;
import com.yahoo.config.provision.TenantName;
import com.yahoo.component.Version;
import com.yahoo.config.provision.Zone;
import com.yahoo.transaction.NestedTransaction;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.TestComponentRegistry;
import com.yahoo.vespa.config.server.TimeoutBudget;
import com.yahoo.vespa.config.server.application.OrchestratorMock;
import com.yahoo.vespa.config.server.http.LogRetriever;
import com.yahoo.vespa.config.server.http.v2.PrepareResult;
import com.yahoo.vespa.config.server.modelfactory.ModelFactoryRegistry;
import com.yahoo.vespa.config.server.monitoring.Metrics;
import com.yahoo.vespa.config.server.session.LocalSession;
import com.yahoo.vespa.config.server.session.PrepareParams;
import com.yahoo.vespa.config.server.tenant.Tenant;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.mock.MockCurator;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.VespaModelFactory;

import java.io.File;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

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

    private static final TenantName tenantName = TenantName.from("deploytester");
    private static final ApplicationId applicationId = ApplicationId.from(tenantName.value(), "myApp", "default");

    private final Clock clock;
    private final TenantRepository tenantRepository;
    private final ApplicationRepository applicationRepository;

    public DeployTester() {
        this(Collections.singletonList(createModelFactory(Clock.systemUTC())));
    }

    public DeployTester(List<ModelFactory> modelFactories) {
        this(modelFactories,
             new ConfigserverConfig(new ConfigserverConfig.Builder()
                     .configServerDBDir(Files.createTempDir().getAbsolutePath())
                     .configDefinitionsDir(Files.createTempDir().getAbsolutePath())),
             Clock.systemUTC());
    }

    public DeployTester(ConfigserverConfig configserverConfig) {
        this(Collections.singletonList(createModelFactory(Clock.systemUTC())), configserverConfig, Clock.systemUTC());
    }

    public DeployTester(ConfigserverConfig configserverConfig, HostProvisioner provisioner) {
        this(Collections.singletonList(createModelFactory(Clock.systemUTC())), configserverConfig, Clock.systemUTC(), provisioner);
    }

    public DeployTester(ConfigserverConfig configserverConfig, Clock clock) {
        this(Collections.singletonList(createModelFactory(clock)), configserverConfig, clock);
    }

    public DeployTester(List<ModelFactory> modelFactories, ConfigserverConfig configserverConfig) {
        this(modelFactories, configserverConfig, Clock.systemUTC());
    }

    public DeployTester(List<ModelFactory> modelFactories, ConfigserverConfig configserverConfig, Clock clock) {
        this(modelFactories, configserverConfig, clock, Zone.defaultZone());
    }

    public DeployTester(List<ModelFactory> modelFactories, ConfigserverConfig configserverConfig, Clock clock, HostProvisioner provisioner) {
        this(modelFactories, configserverConfig, clock, Zone.defaultZone(), provisioner);
    }

    public DeployTester(List<ModelFactory> modelFactories, ConfigserverConfig configserverConfig, Clock clock, Zone zone) {
        this(modelFactories, configserverConfig, clock, zone, createProvisioner());
    }

    public DeployTester(List<ModelFactory> modelFactories, ConfigserverConfig configserverConfig, Clock clock, Zone zone, HostProvisioner provisioner) {
        this(modelFactories, configserverConfig, clock, zone, provisioner, new MockCurator());
    }

    public DeployTester(List<ModelFactory> modelFactories, ConfigserverConfig configserverConfig, Clock clock, Zone zone,
                        HostProvisioner provisioner, Curator curator) {
        this.clock = clock;
        TestComponentRegistry componentRegistry = createComponentRegistry(curator, Metrics.createTestMetrics(),
                                                                          modelFactories, configserverConfig, clock, zone,
                                                                          provisioner);
        try {
            this.tenantRepository = new TenantRepository(componentRegistry);
            tenantRepository.addTenant(tenantName);
        }
        catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
        applicationRepository = new ApplicationRepository(tenantRepository,
                                                          new ProvisionerAdapter(provisioner),
                                                          new OrchestratorMock(),
                                                          configserverConfig,
                                                          new LogRetriever(),
                                                          clock);
    }

    public Tenant tenant() {
        return tenantRepository.getTenant(tenantName);
    }
    
    /** Create a model factory for the version of this source*/
    public static CountingModelFactory createModelFactory(Clock clock) {
        return new CountingModelFactory(clock);
    }

    /** Create a model factory for a particular version */
    public static CountingModelFactory createModelFactory(Version version) {
        return createModelFactory(version, Clock.systemUTC());
    }

    /** Create a model factory for a particular version and clock */
    public static CountingModelFactory createModelFactory(Version version, Clock clock) {
        return createModelFactory(version, clock, Zone.defaultZone());
    }

    /** Create a model factory for a particular version and zone */
    public static CountingModelFactory createModelFactory(Version version, Zone zone) {
        return new CountingModelFactory(version, Clock.systemUTC(), zone);
    }

    /** Create a model factory for a particular version, clock and zone */
    public static CountingModelFactory createModelFactory(Version version, Clock clock, Zone zone) {
        return new CountingModelFactory(version, clock, zone);
    }

    /** Create a model factory which always fails validation */
    public static ModelFactory createFailingModelFactory(Version version) { return new FailingModelFactory(version); }


    /**
     * Do the initial "deploy" with the existing API-less code as the deploy API doesn't support first deploys yet.
     */
    public PrepareResult deployApp(String applicationPath) {
        return deployApp(applicationPath, null, Instant.now());
    }

    /**
     * Do the initial "deploy" with the existing API-less code as the deploy API doesn't support first deploys yet.
     */
    public PrepareResult deployApp(String applicationPath, Instant now) {
        return deployApp(applicationPath, null, now);
    }

    /**
     * Do the initial "deploy" with the existing API-less code as the deploy API doesn't support first deploys yet.
     */
    public PrepareResult deployApp(String applicationPath, String vespaVersion) {
        return deployApp(applicationPath, vespaVersion, Instant.now());
    }

    /**
     * Do the initial "deploy" with the existing API-less code as the deploy API doesn't support first deploys yet.
     */
    public PrepareResult deployApp(String applicationPath, String vespaVersion, Instant now)  {
        PrepareParams.Builder paramsBuilder = new PrepareParams.Builder()
                .applicationId(applicationId)
                .timeoutBudget(new TimeoutBudget(clock, Duration.ofSeconds(60)));
        if (vespaVersion != null)
            paramsBuilder.vespaVersion(vespaVersion);

        return applicationRepository.deploy(new File(applicationPath), paramsBuilder.build(), false, now);
    }

    public AllocatedHosts getAllocatedHostsOf(ApplicationId applicationId) {
        Tenant tenant = tenant();
        LocalSession session = tenant.getLocalSessionRepo().getSession(tenant.getApplicationRepo()
                                                                             .requireActiveSessionOf(applicationId));
        return session.getAllocatedHosts();
    }

    public ApplicationId applicationId() { return applicationId; }

    public Optional<com.yahoo.config.provision.Deployment> redeployFromLocalActive() {
        return applicationRepository.deployFromLocalActive(applicationId);
    }

    public Optional<com.yahoo.config.provision.Deployment> redeployFromLocalActive(ApplicationId id) {
        return applicationRepository.deployFromLocalActive(id, Duration.ofSeconds(60));
    }

    public ApplicationRepository applicationRepository() {
        return applicationRepository;
    }

    private static HostProvisioner createProvisioner() {
        return new InMemoryProvisioner(true, "host0", "host1", "host2", "host3", "host4", "host5");
    }

    private TestComponentRegistry createComponentRegistry(Curator curator, Metrics metrics,
                                                          List<ModelFactory> modelFactories,
                                                          ConfigserverConfig configserverConfig,
                                                          Clock clock,
                                                          Zone zone,
                                                          HostProvisioner provisioner) {
        TestComponentRegistry.Builder builder = new TestComponentRegistry.Builder();

        if (configserverConfig.hostedVespa())
            builder.provisioner(new ProvisionerAdapter(provisioner));

        builder.configServerConfig(configserverConfig)
               .curator(curator)
               .modelFactoryRegistry(new ModelFactoryRegistry(modelFactories))
               .metrics(metrics)
               .zone(zone)
               .clock(clock);
        return builder.build();
    }

    private static class ProvisionerAdapter implements Provisioner {

        private final HostProvisioner hostProvisioner;

        public ProvisionerAdapter(HostProvisioner hostProvisioner) {
            this.hostProvisioner = hostProvisioner;
        }

        @Override
        public List<HostSpec> prepare(ApplicationId applicationId, ClusterSpec cluster, Capacity capacity, int groups, ProvisionLogger logger) {
            return hostProvisioner.prepare(cluster, capacity, groups, logger);
        }

        @Override
        public void activate(NestedTransaction transaction, ApplicationId application, Collection<HostSpec> hosts) {
            // noop
        }

        @Override
        public void remove(NestedTransaction transaction, ApplicationId application) {
            // noop
        }

        @Override
        public void restart(ApplicationId application, HostFilter filter) {
            // noop
        }

    }

    private static class FailingModelFactory implements ModelFactory {

        private final Version version;
        
        public FailingModelFactory(Version version) {
            this.version = version;
        }
        
        @Override
        public Version version() { return version; }

        @Override
        public Model createModel(ModelContext modelContext) {
            try {
                Instant now = LocalDate.parse("2000-01-01", DateTimeFormatter.ISO_DATE).atStartOfDay().atZone(ZoneOffset.UTC).toInstant();
                ApplicationPackage application = new MockApplicationPackage.Builder().withEmptyHosts().withEmptyServices().build();
                DeployState deployState = new DeployState.Builder().applicationPackage(application).now(now).build();
                return new VespaModel(deployState);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public ModelCreateResult createAndValidateModel(ModelContext modelContext, ValidationParameters validationParameters) {
            if ( ! validationParameters.ignoreValidationErrors())
                throw new IllegalArgumentException("Validation fails");
            return new ModelCreateResult(createModel(modelContext), Collections.emptyList());
        }

    }

    /** A wrapper of the regular model factory which counts the number of models it has created */
    public static class CountingModelFactory implements ModelFactory {

        private final VespaModelFactory wrapped;
        private int creationCount;

        public CountingModelFactory(Clock clock) {
            this.wrapped = new VespaModelFactory(new NullConfigModelRegistry(), clock);
        }

        public CountingModelFactory(Version version, Clock clock, Zone zone) {
            this.wrapped = new VespaModelFactory(version, new NullConfigModelRegistry(), clock, zone);
        }

        /** Returns the number of models created successfully by this instance */
        public int creationCount() { return creationCount; }

        @Override
        public Version version() { return wrapped.version(); }

        @Override
        public Model createModel(ModelContext modelContext) {
            Model model = wrapped.createModel(modelContext);
            creationCount++;
            return model;
        }

        @Override
        public ModelCreateResult createAndValidateModel(ModelContext modelContext, ValidationParameters validationParameters) {
            ModelCreateResult result = wrapped.createAndValidateModel(modelContext, validationParameters);
            creationCount++;
            return result;
        }

    }

}