aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/configserver/ConfigServerApiImplTest.java
blob: 910fd8e670a7d33f8cc09011d8e462150769b69b (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.configserver;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicStatusLine;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Basic testing of retry logic.
 *
 * @author dybis
 */
public class ConfigServerApiImplTest {

    private static final int FAIL_RETURN_CODE = 100000;
    private static final int TIMEOUT_RETURN_CODE = 100001;

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class TestPojo {
        @JsonProperty("foo")
        String foo;
        @JsonProperty("error-code")
        Integer errorCode;
    }

    private final String uri1 = "http://host1:666";
    private final String uri2 = "http://host2:666";
    private final List<URI> configServers = List.of(URI.create(uri1), URI.create(uri2));
    private final StringBuilder mockLog = new StringBuilder();

    private ConfigServerApiImpl configServerApi;
    private int mockReturnCode = 200;

    @BeforeEach
    public void initExecutor() throws IOException {
        CloseableHttpClient httpMock = mock(CloseableHttpClient.class);
        when(httpMock.execute(any())).thenAnswer(invocationOnMock -> {
            HttpGet get = (HttpGet) invocationOnMock.getArguments()[0];
            mockLog.append(get.getMethod()).append(" ").append(get.getURI()).append("  ");

            switch (mockReturnCode) {
                case FAIL_RETURN_CODE -> throw new RuntimeException("FAIL");
                case TIMEOUT_RETURN_CODE -> throw new SocketTimeoutException("read timed out");
            }

            BasicStatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, mockReturnCode, null);
            BasicHttpEntity entity = new BasicHttpEntity();
            String returnMessage = "{\"foo\":\"bar\", \"no\":3, \"error-code\": " + mockReturnCode + "}";
            InputStream stream = new ByteArrayInputStream(returnMessage.getBytes(StandardCharsets.UTF_8));
            entity.setContent(stream);

            CloseableHttpResponse response = mock(CloseableHttpResponse.class);
            when(response.getEntity()).thenReturn(entity);
            when(response.getStatusLine()).thenReturn(statusLine);

            return response;
        });
        configServerApi = ConfigServerApiImpl.createForTestingWithClient(configServers, httpMock);
    }

    @Test
    void testBasicParsingSingleServer() {
        TestPojo answer = configServerApi.get("/path", TestPojo.class);
        assertEquals(answer.foo, "bar");
        assertLogStringContainsGETForAHost();
    }

    @Test
    void testBasicFailure() {
        assertThrows(HttpException.class, () -> {
            // Server is returning 400, no retries.
            mockReturnCode = 400;

            TestPojo testPojo = configServerApi.get("/path", TestPojo.class);
            assertEquals(testPojo.errorCode.intValue(), mockReturnCode);
            assertLogStringContainsGETForAHost();
        });
    }

    @Test
    void testBasicSuccessWithNoRetries() {
        // Server is returning 201, no retries.
        mockReturnCode = 201;

        TestPojo testPojo = configServerApi.get("/path", TestPojo.class);
        assertEquals(testPojo.errorCode.intValue(), mockReturnCode);
        assertLogStringContainsGETForAHost();
    }

    @Test
    void testBasicSuccessWithCustomTimeouts() {
        mockReturnCode = TIMEOUT_RETURN_CODE;

        var params = new ConfigServerApi.Params<TestPojo>();
        params.setConnectionTimeout(Duration.ofSeconds(3));

        try {
            configServerApi.get("/path", TestPojo.class, params);
            fail();
        } catch (ConnectionException e) {
            assertNotNull(e.getCause());
            assertEquals("read timed out", e.getCause().getMessage());
        }
    }

    @Test
    void testRetries() {
        // Client is throwing exception, should be retries.
        mockReturnCode = FAIL_RETURN_CODE;
        try {
            configServerApi.get("/path", TestPojo.class);
            fail("Expected failure");
        } catch (Exception e) {
            // ignore
        }

        List<String> log = List.of(mockLog.toString().split("  "));
        assertTrue(log.containsAll(List.of("GET http://host1:666/path", "GET http://host2:666/path")));
    }

    @Test
    void testNoRetriesOnBadHttpResponseCode() {
        // Client is throwing exception, should be retries.
        mockReturnCode = 503;
        try {
            configServerApi.get("/path", TestPojo.class);
            fail("Expected failure");
        } catch (Exception e) {
            // ignore
        }

        assertLogStringContainsGETForAHost();
    }

    @Test
    void testForbidden() {
        mockReturnCode = 403;
        try {
            configServerApi.get("/path", TestPojo.class);
            fail("Expected exception");
        } catch (HttpException.ForbiddenException e) {
            // ignore
        }
        assertLogStringContainsGETForAHost();
    }

    @Test
    void testNotFound() {
        // Server is returning 404, special exception is thrown.
        mockReturnCode = 404;
        try {
            configServerApi.get("/path", TestPojo.class);
            fail("Expected exception");
        } catch (HttpException.NotFoundException e) {
            // ignore
        }
        assertLogStringContainsGETForAHost();
    }

    @Test
    void testConflict() {
        // Server is returning 409, no exception is thrown.
        mockReturnCode = 409;
        configServerApi.get("/path", TestPojo.class);
        assertLogStringContainsGETForAHost();
    }

    private void assertLogStringContainsGETForAHost() {
        String logString = mockLog.toString();
        assertTrue((logString.equals("GET http://host1:666/path  ") || logString.equals("GET http://host2:666/path  ")),
                   "log does not contain expected entries:" + logString);
    }
}