aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/stubs/MockTesterCloud.java
blob: c148afb91901c1fb6f9312d9559fa3b1f3e3a486 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.stubs;

import ai.vespa.http.DomainName;
import com.google.common.net.InetAddresses;
import com.yahoo.config.provision.EndpointsChecker;
import com.yahoo.config.provision.EndpointsChecker.Endpoint;
import com.yahoo.config.provision.EndpointsChecker.Availability;
import com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId;
import com.yahoo.vespa.hosted.controller.api.integration.LogEntry;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.TestReport;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.TesterCloud;
import com.yahoo.vespa.hosted.controller.api.integration.dns.NameService;
import com.yahoo.vespa.hosted.controller.api.integration.dns.Record;
import com.yahoo.vespa.hosted.controller.api.integration.dns.RecordName;

import java.net.InetAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.yahoo.vespa.hosted.controller.api.integration.deployment.TesterCloud.Status.NOT_STARTED;
import static com.yahoo.vespa.hosted.controller.api.integration.deployment.TesterCloud.Status.RUNNING;

public class MockTesterCloud implements TesterCloud {

    private final NameService nameService;
    private final EndpointsChecker endpointsChecker = EndpointsChecker.mock(this::resolveHostName, this::resolveCname, __ -> Availability.ready);

    private List<LogEntry> log = new ArrayList<>();
    private Status status = NOT_STARTED;
    private byte[] config;
    private Optional<TestReport> testReport = Optional.empty();

    public MockTesterCloud(NameService nameService) {
        this.nameService = nameService;
    }

    @Override
    public void startTests(DeploymentId deploymentId, Suite suite, byte[] config) {
        this.status = RUNNING;
        this.config = config;
    }

    @Override
    public List<LogEntry> getLog(DeploymentId deploymentId, long after) {
        return log.stream().filter(entry -> entry.id() > after).toList();
    }

    @Override
    public Status getStatus(DeploymentId deploymentId) { return status; }

    @Override
    public boolean testerReady(DeploymentId deploymentId) {
        return true;
    }

    @Override
    public Availability verifyEndpoints(DeploymentId deploymentId, List<Endpoint> endpoints) {
        return endpointsChecker.endpointsAvailable(endpoints);
    }

    private Optional<InetAddress> resolveHostName(DomainName hostname) {
        return nameService.findRecords(Record.Type.A, RecordName.from(hostname.value())).stream()
                .findFirst()
                .map(record -> InetAddresses.forString(record.data().asString()))
                .or(() -> Optional.of(InetAddresses.forString("1.2.3.4")));
    }

    private Optional<DomainName> resolveCname(DomainName hostName) {
        return nameService.findRecords(Record.Type.CNAME, RecordName.from(hostName.value())).stream()
                          .findFirst()
                          .map(record -> DomainName.of(record.data().asString().substring(0, record.data().asString().length() - 1)));
    }

    @Override
    public Optional<TestReport> getTestReport(DeploymentId deploymentId) {
        return testReport;
    }

    public void testReport(TestReport testReport) {
        this.testReport = Optional.ofNullable(testReport);
    }

    public void add(LogEntry entry) {
        log.add(entry);
    }

    public void clearLog() {
        log.clear();
    }

    public void set(Status status) {
        this.status = status;
    }

    public byte[] config() {
        return config;
    }

}