summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/application/ConfigConvergenceCheckerTest.java
blob: 67a04e0b3d6c25784a2f99a341f5cea0ff7cc30e (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
// 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.application;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.config.model.api.Model;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationName;
import com.yahoo.config.provision.InstanceName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Version;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.vespa.config.server.ServerCache;
import com.yahoo.vespa.config.server.http.SessionHandlerTest;
import com.yahoo.vespa.config.server.monitoring.MetricUpdater;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static com.yahoo.vespa.config.server.application.ConfigConvergenceChecker.ServiceResponse;
import static org.junit.Assert.assertEquals;

/**
 * @author Ulf Lilleengen
 */
public class ConfigConvergenceCheckerTest {

    private static final ObjectMapper mapper = new ObjectMapper();

    private final TenantName tenant = TenantName.from("mytenant");
    private final ApplicationId appId = ApplicationId.from(tenant, ApplicationName.from("myapp"), InstanceName.from("myinstance"));
    private Application application;
    private ConfigConvergenceChecker checker;
    private Map<URI, Long> currentGeneration;

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Before
    public void setup() {
        Model mockModel = MockModel.createContainer("localhost", 1337);
        application = new Application(mockModel,
                                      new ServerCache(),
                                      3,
                                      false,
                                      Version.fromIntValues(0, 0, 0),
                                      MetricUpdater.createTestUpdater(), appId);
        currentGeneration = new HashMap<>();
        checker = new ConfigConvergenceChecker(
                (client, serviceUri) -> () -> asJson("{\"config\":{\"generation\":"
                                                     + currentGeneration.getOrDefault(serviceUri, 3L)
                                                     + "}}"));
    }

    @Test
    public void service_convergence() throws Exception {
        ServiceResponse serviceResponse = checker.checkService(application,
                                                               "localhost:1337",
                                                               URI.create("http://foo:234/serviceconverge/localhost:1337"), Duration.ofSeconds(5));
        assertEquals(200, serviceResponse.getStatus());
        assertJsonEquals("{\n" +
                         "  \"url\": \"http://foo:234/serviceconverge/localhost:1337\",\n" +
                         "  \"host\": \"localhost:1337\",\n" +
                         "  \"wantedGeneration\": 3,\n" +
                         "  \"converged\": true,\n" +
                         "  \"currentGeneration\": 3\n" +
                         "}",
                         SessionHandlerTest.getRenderedString(serviceResponse));

        ServiceResponse hostMissingResponse = checker.checkService(application,
                                                                   "notPresent:1337",
                                                                   URI.create("http://foo:234/serviceconverge/notPresent:1337"), Duration.ofSeconds(5));
        assertEquals(410, hostMissingResponse.getStatus());
        assertJsonEquals("{\n" +
                         "  \"url\": \"http://foo:234/serviceconverge/notPresent:1337\",\n" +
                         "  \"host\": \"notPresent:1337\",\n" +
                         "  \"wantedGeneration\": 3,\n" +
                         "  \"problem\": \"Host:port (service) no longer part of application, refetch list of services.\"\n" +
                         "}",
                     SessionHandlerTest.getRenderedString(hostMissingResponse));
    }

    @Test
    public void service_list_convergence() throws Exception {
        HttpResponse serviceListResponse = checker.servicesToCheck(application, URI.create("http://foo:234/serviceconverge"), Duration.ofSeconds(5));
        assertEquals(200, serviceListResponse.getStatus());
        assertJsonEquals("{\n" +
                         "  \"services\": [\n" +
                         "    {\n" +
                         "      \"host\": \"localhost\",\n" +
                         "      \"port\": 1337,\n" +
                         "      \"type\": \"container\",\n" +
                         "      \"url\": \"http://foo:234/serviceconverge/localhost:1337\"\n" +
                         "    }\n" +
                         "  ],\n" +
                         "  \"url\": \"http://foo:234/serviceconverge\",\n" +
                         "  \"currentGeneration\": 3,\n" +
                         "  \"wantedGeneration\": 3,\n" +
                         "  \"converged\": true\n" +
                         "}",
                         SessionHandlerTest.getRenderedString(serviceListResponse));

        // Model with two hosts on different generations
        MockModel model = new MockModel(Arrays.asList(
                MockModel.createContainerHost("host1", 1234),
                MockModel.createContainerHost("host2", 1234))
        );
        Application application = new Application(model, new ServerCache(), 4,
                                                  false,
                                                  Version.fromIntValues(0, 0, 0),
                                                  MetricUpdater.createTestUpdater(), appId);
        currentGeneration.put(URI.create("http://host2:1234"), 4L);
        serviceListResponse = checker.servicesToCheck(application, URI.create("http://foo:234/serviceconverge"), Duration.ofSeconds(5));
        assertEquals(200, serviceListResponse.getStatus());
        assertJsonEquals("{\n" +
                         "  \"services\": [\n" +
                         "    {\n" +
                         "      \"host\": \"host1\",\n" +
                         "      \"port\": 1234,\n" +
                         "      \"type\": \"container\",\n" +
                         "      \"url\": \"http://foo:234/serviceconverge/host1:1234\"\n" +
                         "    },\n" +
                         "    {\n" +
                         "      \"host\": \"host2\",\n" +
                         "      \"port\": 1234,\n" +
                         "      \"type\": \"container\",\n" +
                         "      \"url\": \"http://foo:234/serviceconverge/host2:1234\"\n" +
                         "    }\n" +
                         "  ],\n" +
                         "  \"url\": \"http://foo:234/serviceconverge\",\n" +
                         "  \"currentGeneration\": 3,\n" +
                         "  \"wantedGeneration\": 4,\n" +
                         "  \"converged\": false\n" +
                         "}",
                     SessionHandlerTest.getRenderedString(serviceListResponse));
    }

    private static void assertJsonEquals(String expected, String actual) {
        assertEquals(asJson(expected), asJson(actual));
    }

    private static JsonNode asJson(String data) {
        try {
            return mapper.readTree(data);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

}