aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/pkg/TestPackage.java
blob: 120c0a89f45f55fa8c01260bb3478cb8bdcd520d (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
362
363
364
package com.yahoo.vespa.hosted.controller.application.pkg;

import com.yahoo.config.application.api.DeploymentInstanceSpec;
import com.yahoo.config.application.api.DeploymentSpec;
import com.yahoo.config.application.api.DeploymentSpec.Step;
import com.yahoo.config.provision.AthenzDomain;
import com.yahoo.config.provision.AthenzService;
import com.yahoo.config.provision.CloudAccount;
import com.yahoo.config.provision.CloudName;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.path.Path;
import com.yahoo.security.KeyAlgorithm;
import com.yahoo.security.KeyUtils;
import com.yahoo.security.SignatureAlgorithm;
import com.yahoo.security.X509CertificateBuilder;
import com.yahoo.security.X509CertificateUtils;
import com.yahoo.text.Text;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.RunId;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.TesterCloud.Suite;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.TesterId;
import com.yahoo.vespa.hosted.controller.application.pkg.ApplicationPackageStream.Replacer;
import com.yahoo.vespa.hosted.controller.config.ControllerConfig;
import com.yahoo.vespa.hosted.controller.config.ControllerConfig.Steprunner.Testerapp;
import com.yahoo.yolean.Exceptions;

import javax.security.auth.x500.X500Principal;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.regex.Pattern;

import static com.yahoo.vespa.hosted.controller.api.integration.deployment.TesterCloud.Suite.production;
import static com.yahoo.vespa.hosted.controller.api.integration.deployment.TesterCloud.Suite.staging;
import static com.yahoo.vespa.hosted.controller.api.integration.deployment.TesterCloud.Suite.staging_setup;
import static com.yahoo.vespa.hosted.controller.api.integration.deployment.TesterCloud.Suite.system;
import static com.yahoo.vespa.hosted.controller.application.pkg.ApplicationPackage.deploymentFile;
import static com.yahoo.vespa.hosted.controller.application.pkg.ApplicationPackage.servicesFile;
import static java.io.InputStream.nullInputStream;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.function.UnaryOperator.identity;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;

/**
 * Validation and manipulation of test package.
 *
 * @author jonmv
 */
public class TestPackage {

    // Must match exactly the advertised resources of an AWS instance type. Also consider that the container
    // will have ~1.8 GB less memory than equivalent resources in AWS (VESPA-16259).
    static final NodeResources DEFAULT_TESTER_RESOURCES_CLOUD = new NodeResources(2, 8, 50, 0.3, NodeResources.DiskSpeed.any);
    static final NodeResources DEFAULT_TESTER_RESOURCES = new NodeResources(1, 4, 50, 0.3, NodeResources.DiskSpeed.any);

    private final ApplicationPackageStream applicationPackageStream;
    private final X509Certificate certificate;

    public TestPackage(Supplier<InputStream> inZip, boolean isPublicSystem, CloudName cloud, RunId id, Testerapp testerApp,
                       DeploymentSpec spec, Instant certificateValidFrom, Duration certificateValidDuration) {
        KeyPair keyPair;
        if (certificateValidFrom != null) {
            keyPair = KeyUtils.generateKeypair(KeyAlgorithm.RSA, 2048);
            X500Principal subject = new X500Principal("CN=" + id.tester().id().toFullString() + "." + id.type() + "." + id.number());
            this.certificate = X509CertificateBuilder.fromKeypair(keyPair,
                                                                  subject,
                                                                  certificateValidFrom,
                                                                  certificateValidFrom.plus(certificateValidDuration),
                                                                  SignatureAlgorithm.SHA512_WITH_RSA,
                                                                  BigInteger.valueOf(1))
                                                     .build();
        }
        else {
            keyPair = null;
            this.certificate = null;
        }
        boolean isEnclave = isPublicSystem &&
                !spec.cloudAccount(cloud, id.application().instance(), id.type().zone()).isUnspecified();
        this.applicationPackageStream = new ApplicationPackageStream(inZip, () -> name -> name.endsWith(".xml"), () -> new Replacer() {

            // Initially skips all declared entries, ensuring they're generated and appended after all input entries.
            final Map<String, UnaryOperator<InputStream>> entries = new HashMap<>();
            final Map<String, UnaryOperator<InputStream>> replacements = new HashMap<>();
            boolean hasLegacyTests = false;

            @Override
            public String next() {
                if (entries.isEmpty()) return null;
                String next = entries.keySet().iterator().next();
                replacements.put(next, entries.remove(next));
                return next;
            }

            @Override
            public InputStream modify(String name, InputStream in) {
                hasLegacyTests |= name.startsWith("artifacts/") && name.endsWith("-tests.jar");
                return entries.containsKey(name) ? null : replacements.getOrDefault(name, identity()).apply(in);
            }

            {
                // Copy contents of submitted application-test.zip, and ensure required directories exist within the zip.
                entries.put("artifacts/.ignore-" + UUID.randomUUID(), __ -> nullInputStream());
                entries.put("tests/.ignore-" + UUID.randomUUID(), __ -> nullInputStream());

                entries.put(servicesFile,
                            __ -> new ByteArrayInputStream(servicesXml( ! isPublicSystem,
                                                                       certificateValidFrom != null,
                                                                       hasLegacyTests,
                                                                       testerResourcesFor(id.type().zone(), spec.requireInstance(id.application().instance()), isEnclave),
                                                                       testerApp)));

                entries.put(deploymentFile,
                            __ -> new ByteArrayInputStream(deploymentXml(id.tester(), id.application().instance(), cloud, id.type().zone(), spec)));

                if (certificate != null) {
                    entries.put("artifacts/key", __ -> new ByteArrayInputStream(KeyUtils.toPem(keyPair.getPrivate()).getBytes(UTF_8)));
                    entries.put("artifacts/cert", __ -> new ByteArrayInputStream(X509CertificateUtils.toPem(certificate).getBytes(UTF_8)));
                }
            }
        });
    }

    public ApplicationPackageStream asApplicationPackage() {
        return applicationPackageStream;
    }

    public X509Certificate certificate() {
        return Objects.requireNonNull(certificate);
    }

    public static TestSummary validateTests(DeploymentSpec spec, byte[] testPackage) {
        return validateTests(expectedSuites(spec.steps()), testPackage);
    }

    static TestSummary validateTests(Collection<Suite> expectedSuites, byte[] testPackage) {
        List<String> problems = new ArrayList<>();
        Set<Suite> suites = new LinkedHashSet<>();
        ZipEntries.from(testPackage, __ -> true, 0, false).asList().stream()
                  .map(entry -> Path.fromString(entry.name()))
                  .collect(groupingBy(path -> path.elements().size() > 1 ? path.elements().get(0) : "",
                                      mapping(path -> (path.elements().size() > 1 ? path.getChildPath() : path).getRelative(), toList())))
                  .forEach((directory, paths) -> {
                      switch (directory) {
                          case "components": {
                              for (String path : paths) {
                                  if (path.endsWith("-tests.jar")) {
                                      try {
                                          byte[] testsJar = ZipEntries.readFile(testPackage, "components/" + path, 1 << 30);
                                          Manifest manifest = new JarInputStream(new ByteArrayInputStream(testsJar)).getManifest();
                                          String bundleCategoriesHeader = manifest.getMainAttributes().getValue("X-JDisc-Test-Bundle-Categories");
                                          if (bundleCategoriesHeader == null) continue;
                                          for (String suite : bundleCategoriesHeader.split(","))
                                              if ( ! suite.isBlank()) switch (suite.trim()) {
                                                  case "SystemTest" -> suites.add(system);
                                                  case "StagingSetup" -> suites.add(staging_setup);
                                                  case "StagingTest" -> suites.add(staging);
                                                  case "ProductionTest" -> suites.add(production);
                                                  default -> problems.add("unexpected test suite name '" + suite + "' in bundle manifest");
                                              }
                                      }
                                      catch (Exception e) {
                                          problems.add("failed reading test bundle manifest: " + Exceptions.toMessageString(e));
                                      }
                                  }
                              }
                          }
                          break;
                          case "tests": {
                              if (paths.stream().anyMatch(Pattern.compile("system-test/.+\\.json").asMatchPredicate())) suites.add(system);
                              if (paths.stream().anyMatch(Pattern.compile("staging-setup/.+\\.json").asMatchPredicate())) suites.add(staging_setup);
                              if (paths.stream().anyMatch(Pattern.compile("staging-test/.+\\.json").asMatchPredicate())) suites.add(staging);
                              if (paths.stream().anyMatch(Pattern.compile("production-test/.+\\.json").asMatchPredicate())) suites.add(production);
                          }
                          break;
                          case "artifacts": {
                              if (paths.stream().anyMatch(Pattern.compile(".+-tests.jar").asMatchPredicate()))
                                  suites.addAll(expectedSuites); // ಠ_ಠ

                              for (String forbidden : List.of("key", "cert"))
                                  if (paths.contains(forbidden))
                                      problems.add("test package contains 'artifacts/" + forbidden +
                                                   "'; this conflicts with credentials used to run tests in Vespa Cloud");
                          }
                          break;
                      }
                  });

        if (expectedSuites.contains(system) && ! suites.contains(system))
            problems.add("test package has no system tests, but <test /> is declared in deployment.xml");

        if (suites.contains(staging) != suites.contains(staging_setup))
            problems.add("test package has " + (suites.contains(staging) ? "staging tests" : "staging setup") +
                         ", so it should also include " + (suites.contains(staging) ? "staging setup" : "staging tests"));
        else if (expectedSuites.contains(staging) && ! suites.contains(staging))
            problems.add("test package has no staging setup and tests, but <staging /> is declared in deployment.xml");

        if (suites.contains(production) != expectedSuites.contains(production))
            problems.add("test package has " + (suites.contains(production) ? "" : "no ") + "production tests, " +
                         "but " + (suites.contains(production) ? "no " : "") + "production tests are declared in deployment.xml");

        if ( ! problems.isEmpty())
            problems.add("see https://docs.vespa.ai/en/testing.html for details on how to write system tests for Vespa");

        return new TestSummary(problems, suites);
    }

    static NodeResources testerResourcesFor(ZoneId zone, DeploymentInstanceSpec spec, boolean isEnclave) {
        NodeResources nodeResources = spec.steps().stream()
                                          .filter(step -> step.concerns(zone.environment()))
                                          .findFirst()
                                          .flatMap(step -> step.zones().get(0).testerFlavor())
                                          .map(NodeResources::fromLegacyName)
                                          .orElse(zone.region().value().matches("^(aws|gcp)-.*") ? DEFAULT_TESTER_RESOURCES_CLOUD
                                                                                                 : DEFAULT_TESTER_RESOURCES);
        if (isEnclave) nodeResources = nodeResources.with(NodeResources.Architecture.x86_64);
        return nodeResources.with(NodeResources.DiskSpeed.any);
    }

    /** Returns the generated services.xml content for the tester application. */
    static byte[] servicesXml(boolean systemUsesAthenz, boolean useTesterCertificate, boolean hasLegacyTests,
                                     NodeResources resources, ControllerConfig.Steprunner.Testerapp config) {
        int jdiscMemoryGb = 2; // 2Gb memory for tester application which uses Maven.
        int jdiscMemoryPct = (int) Math.ceil(100 * jdiscMemoryGb / resources.memoryGb());

        // Of the remaining memory, split 50/50 between Surefire running the tests and the rest
        int testMemoryMb = (int) (1024 * (resources.memoryGb() - jdiscMemoryGb) / 2);

        String resourceString = Text.format("<resources vcpu=\"%.2f\" memory=\"%.2fGb\" disk=\"%.2fGb\" disk-speed=\"%s\" storage-type=\"%s\" architecture=\"%s\"/>",
                                            resources.vcpu(), resources.memoryGb(), resources.diskGb(), resources.diskSpeed().name(), resources.storageType().name(), resources.architecture().name());

        String runtimeProviderClass = config.runtimeProviderClass();
        String tenantCdBundle = config.tenantCdBundle();

        String servicesXml =
                "<?xml version='1.0' encoding='UTF-8'?>\n" +
                "<services xmlns:deploy='vespa' version='1.0'>\n" +
                "    <container version='1.0' id='tester'>\n" +
                "\n" +
                "        <component id=\"com.yahoo.vespa.hosted.testrunner.TestRunner\" bundle=\"vespa-testrunner-components\">\n" +
                "            <config name=\"com.yahoo.vespa.hosted.testrunner.test-runner\">\n" +
                "                <artifactsPath>artifacts</artifactsPath>\n" +
                "                <surefireMemoryMb>" + testMemoryMb + "</surefireMemoryMb>\n" +
                "                <useAthenzCredentials>" + systemUsesAthenz + "</useAthenzCredentials>\n" +
                "                <useTesterCertificate>" + useTesterCertificate + "</useTesterCertificate>\n" +
                "            </config>\n" +
                "        </component>\n" +
                "\n" +
                "        <handler id=\"com.yahoo.vespa.testrunner.TestRunnerHandler\" bundle=\"vespa-osgi-testrunner\">\n" +
                "            <binding>http://*/tester/v1/*</binding>\n" +
                "        </handler>\n" +
                "\n" +
                "        <component id=\"" + runtimeProviderClass + "\" bundle=\"" + tenantCdBundle + "\" />\n" +
                "\n" +
                "        <component id=\"com.yahoo.vespa.testrunner.JunitRunner\" bundle=\"vespa-osgi-testrunner\">\n" +
                "            <config name=\"com.yahoo.vespa.testrunner.junit-test-runner\">\n" +
                "                <artifactsPath>artifacts</artifactsPath>\n" +
                "                <useAthenzCredentials>" + systemUsesAthenz + "</useAthenzCredentials>\n" +
                "            </config>\n" +
                "        </component>\n" +
                "\n" +
                "        <component id=\"com.yahoo.vespa.testrunner.VespaCliTestRunner\" bundle=\"vespa-osgi-testrunner\">\n" +
                "            <config name=\"com.yahoo.vespa.testrunner.vespa-cli-test-runner\">\n" +
                "                <artifactsPath>artifacts</artifactsPath>\n" +
                "                <testsPath>tests</testsPath>\n" +
                "                <useAthenzCredentials>" + systemUsesAthenz + "</useAthenzCredentials>\n" +
                "            </config>\n" +
                "        </component>\n" +
                "\n" +
                "        <nodes count=\"1\">\n" +
                (hasLegacyTests ? "            <jvm allocated-memory=\"" + jdiscMemoryPct + "%\"/>\n" : "" ) +
                "            " + resourceString + "\n" +
                "        </nodes>\n" +
                "    </container>\n" +
                "</services>\n";

        return servicesXml.getBytes(UTF_8);
    }

    /** Returns a dummy deployment xml which sets up the service identity for the tester, if present. */
    static byte[] deploymentXml(TesterId id, InstanceName instance, CloudName cloud, ZoneId zone, DeploymentSpec original) {
        Optional<AthenzDomain> athenzDomain = original.athenzDomain();
        Optional<AthenzService> athenzService = original.requireInstance(instance)
                                                        .athenzService(zone.environment(), zone.region());
        Optional<CloudAccount> cloudAccount = Optional.of(original.cloudAccount(cloud, instance, zone))
                                                      .filter(account -> ! account.isUnspecified());
        Optional<Duration> hostTTL = (zone.environment().isProduction()
                                      ? original.requireInstance(instance)
                                                .steps().stream().filter(step -> step.isTest() && step.concerns(zone.environment(), Optional.of(zone.region())))
                                                .findFirst().flatMap(Step::hostTTL)
                                      : original.requireInstance(instance).hostTTL(zone.environment(), Optional.of(zone.region())))
                .filter(__ -> cloudAccount.isPresent());
        String deploymentSpec =
                "<?xml version='1.0' encoding='UTF-8'?>\n" +
                "<deployment version='1.0'" +
                athenzDomain.map(domain -> " athenz-domain='" + domain.value() + "'").orElse("") +
                athenzService.map(service -> " athenz-service='" + service.value() + "'").orElse("") +
                cloudAccount.map(account -> " cloud-account='" + account.value() + "'").orElse("") +
                hostTTL.map(ttl -> " empty-host-ttl='" + ttl.getSeconds() / 60 + "m'").orElse("") +
                ">" +
                "  <instance id='" + id.id().instance().value() + "' />" +
                "</deployment>";
        return deploymentSpec.getBytes(UTF_8);
    }

    static Set<Suite> expectedSuites(List<Step> steps) {
        Set<Suite> suites = new HashSet<>();
        if (steps.isEmpty()) return suites;
        for (Step step : steps) {
            if (step.isTest()) {
                if (step.concerns(Environment.prod)) suites.add(production);
                if (step.concerns(Environment.test)) suites.add(system);
                if (step.concerns(Environment.staging)) { suites.add(staging); suites.add(staging_setup); }
            }
            else
                suites.addAll(expectedSuites(step.steps()));
        }
        return suites;
    }


    public static class TestSummary {

        private final List<String> problems;
        private final List<Suite> suites;

        public TestSummary(List<String> problems, Set<Suite> suites) {
            this.problems = List.copyOf(problems);
            this.suites = List.copyOf(suites);
        }

        public List<String> problems() {
            return problems;
        }

        public List<Suite> suites() {
            return suites;
        }

    }

}