From 031f63ae546a60048d4e7967811ee4fdf4f49ef6 Mon Sep 17 00:00:00 2001 From: Jon Marius Venstad Date: Tue, 23 Apr 2019 13:18:07 +0200 Subject: Trimmed down TestConfig, which depends on config-provisioning --- tenant-cd/pom.xml | 6 ++ .../java/com/yahoo/vespa/tenant/cd/TestConfig.java | 75 ++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tenant-cd/src/main/java/com/yahoo/vespa/tenant/cd/TestConfig.java (limited to 'tenant-cd') diff --git a/tenant-cd/pom.xml b/tenant-cd/pom.xml index 072fdbff869..2fdf5adfb57 100644 --- a/tenant-cd/pom.xml +++ b/tenant-cd/pom.xml @@ -31,6 +31,12 @@ ${project.version} + + com.yahoo.vespa + config-provisioning + ${project.version} + + junit junit diff --git a/tenant-cd/src/main/java/com/yahoo/vespa/tenant/cd/TestConfig.java b/tenant-cd/src/main/java/com/yahoo/vespa/tenant/cd/TestConfig.java new file mode 100644 index 00000000000..9edb76cfe35 --- /dev/null +++ b/tenant-cd/src/main/java/com/yahoo/vespa/tenant/cd/TestConfig.java @@ -0,0 +1,75 @@ +package com.yahoo.vespa.tenant.cd; + +import com.yahoo.config.provision.ApplicationId; +import com.yahoo.slime.ArrayTraverser; +import com.yahoo.slime.Inspector; +import com.yahoo.slime.JsonDecoder; +import com.yahoo.slime.ObjectTraverser; +import com.yahoo.config.provision.zone.ZoneId; +import com.yahoo.slime.Slime; + +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * The place to obtain environment-dependent configuration for the current test run. + * + * If the system property 'vespa.test.config' is set, this class attempts to parse config + * from a JSON file at that location -- otherwise, attempts to access the config will return null. + * + * @author jvenstad + */ +public class TestConfig { + + private static final TestConfig theConfig = fromFile(Paths.get(System.getProperty("vespa.test.config"))); + + private final ApplicationId application; + private final ZoneId zone; + private final Map deployments; + + private TestConfig(ApplicationId application, ZoneId zone, Map deployments) { + this.application = application; + this.zone = zone; + this.deployments = Map.copyOf(deployments); + } + + /** Returns the config for this test, or null if it has not been provided. */ + public static TestConfig get() { return theConfig; } + + /** Returns the full id of the application to be tested. */ + public ApplicationId application() { return application; } + + /** Returns the zone of the deployment to test. */ + public ZoneId zone() { return zone; } + + /** Returns an immutable view of all configured endpoints for each zone of the application to test. */ + public Map deployments() { return deployments; } + + static TestConfig fromFile(Path path) { + if (path == null) + return null; + + try { + Inspector config = new JsonDecoder().decode(new Slime(), Files.readAllBytes(path)).get(); + ApplicationId application = ApplicationId.fromSerializedForm(config.field("application").asString()); + ZoneId zone = ZoneId.from(config.field("zone").asString()); + Map endpoints = new HashMap<>(); + config.field("endpoints").traverse((ObjectTraverser) (zoneId, endpointArray) -> { + List uris = new ArrayList<>(); + endpointArray.traverse((ArrayTraverser) (__, uri) -> uris.add(URI.create(uri.asString()))); + endpoints.put(ZoneId.from(zoneId), null); // TODO jvenstad + }); + return new TestConfig(application, zone, endpoints); + } + catch (Exception e) { + throw new IllegalArgumentException("Failed reading config from '" + path + "'!", e); + } + } + +} -- cgit v1.2.3