summaryrefslogtreecommitdiffstats
path: root/tenant-cd/src/main/java/ai/vespa/hosted/cd/TestRuntime.java
blob: fa09d7037c9fa0b4b4fd27cba685078f25ae412b (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
package ai.vespa.hosted.cd;

import ai.vespa.hosted.api.Authenticator;
import ai.vespa.hosted.api.ControllerHttpClient;
import ai.vespa.hosted.api.Properties;
import ai.vespa.hosted.api.TestConfig;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.zone.ZoneId;

import java.nio.file.Files;
import java.nio.file.Paths;

import static ai.vespa.hosted.api.TestConfig.fromJson;

/**
 * The place to obtain environment-dependent configuration for test of a Vespa deployment.
 *
 * @author jvenstad
 */
public class TestRuntime {

    private static TestRuntime theRuntime;

    private final TestConfig config;
    private final Authenticator authenticator;

    private TestRuntime(TestConfig config, Authenticator authenticator) {
        this.config = config;
        this.authenticator = authenticator;
    }

    /**
     * Returns the config for this test, or null if it has not been provided.
     *
     * If the system property {@code "vespa.test.config"} is set (to a file path), a file at that location
     * is attempted read, and config parsed from it.
     * Otherwise, config is fetched over HTTP from the hosted Vespa API, assuming the deployment indicated
     * by the optional {@code "environment"} and {@code "region"} system properties exists.
     * When environment is not specified, it defaults to {@link Environment#dev},
     * while region must be set unless the environment is {@link Environment#dev} or {@link Environment#perf}.
     */
    public static synchronized TestRuntime get() {
        if (theRuntime == null) {
            String configPath = System.getProperty("vespa.test.config");
            Authenticator authenticator = new ai.vespa.hosted.auth.Authenticator();
            theRuntime = new TestRuntime(configPath != null ? fromFile(configPath) : fromController(authenticator),
                                         authenticator);
        }
        return theRuntime;
    }

    /** Returns a copy of this runtime, with the given authenticator. */
    public TestRuntime with(Authenticator authenticator) {
        return new TestRuntime(config, authenticator);
    }

    private static TestConfig fromFile(String path) {
        try {
            return TestConfig.fromJson(Files.readAllBytes(Paths.get(path)));
        }
        catch (Exception e) {
            throw new IllegalArgumentException("Failed reading config from '" + path + "'!", e);
        }
    }

    private static TestConfig fromController(Authenticator authenticator) {
        ControllerHttpClient controller = authenticator.controller();
        ApplicationId id = Properties.application();
        Environment environment = Properties.environment().orElse(Environment.dev);
        ZoneId zone = Properties.region().map(region -> ZoneId.from(environment, region))
                                .orElseGet(() -> controller.defaultZone(environment));
        return controller.testConfig(id, zone);
    }

}