aboutsummaryrefslogtreecommitdiffstats
path: root/service-monitor/src/test/java/com/yahoo/vespa/service/health/StateV1HealthUpdaterTest.java
blob: d780acb544c0fe8b2eb3cd8e633cadbc9170ecb5 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.health;

import com.yahoo.vespa.applicationmodel.ServiceStatus;
import com.yahoo.vespa.applicationmodel.ServiceStatusInfo;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URL;
import java.util.function.Function;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class StateV1HealthUpdaterTest {
    private URL url;

    @Before
    public void setUp() throws Exception{
        url = new URL("http://host.com:19071");
    }

    @Test
    public void successfulRequestResponse() throws IOException {
        ServiceStatusInfo info = getServiceStatusInfoFromJsonResponse("{\n" +
                "    \"metrics\": {\n" +
                "        \"snapshot\": {\n" +
                "            \"from\": 1.528789829249E9,\n" +
                "            \"to\": 1.528789889249E9\n" +
                "        }\n" +
                "    },\n" +
                "    \"status\": {\"code\": \"up\"},\n" +
                "    \"time\": 1528789889364\n" +
                "}");
        assertEquals(ServiceStatus.UP, info.serviceStatus());
        assertNull(info.errorOrNull());
    }

    @Test
    public void notUpResponse() throws IOException {
        ServiceStatusInfo info = getServiceStatusInfoFromJsonResponse("{\n" +
                "    \"metrics\": {\n" +
                "        \"snapshot\": {\n" +
                "            \"from\": 1.528789829249E9,\n" +
                "            \"to\": 1.528789889249E9\n" +
                "        }\n" +
                "    },\n" +
                "    \"status\": {\"code\": \"initializing\"},\n" +
                "    \"time\": 1528789889364\n" +
                "}");
        assertEquals(ServiceStatus.DOWN, info.serviceStatus());
        assertEquals("Bad health status code 'initializing'", info.errorOrNull());
    }

    @Test
    public void noCodeInResponse() throws IOException {
        ServiceStatusInfo info = getServiceStatusInfoFromJsonResponse("{\n" +
                "    \"metrics\": {\n" +
                "        \"snapshot\": {\n" +
                "            \"from\": 1.528789829249E9,\n" +
                "            \"to\": 1.528789889249E9\n" +
                "        }\n" +
                "    },\n" +
                "    \"status\": {\"foo\": \"bar\"},\n" +
                "    \"time\": 1528789889364\n" +
                "}");
        assertEquals(ServiceStatus.DOWN, info.serviceStatus());
        assertEquals("Bad health status code 'down'", info.errorOrNull());
    }

    @Test
    public void noStatusInResponse() throws IOException {
        ServiceStatusInfo info = getServiceStatusInfoFromJsonResponse("{\n" +
                "    \"metrics\": {\n" +
                "        \"snapshot\": {\n" +
                "            \"from\": 1.528789829249E9,\n" +
                "            \"to\": 1.528789889249E9\n" +
                "        }\n" +
                "    },\n" +
                "    \"time\": 1528789889364\n" +
                "}");
        assertEquals(ServiceStatus.DOWN, info.serviceStatus());
        assertEquals("Bad health status code 'down'", info.errorOrNull());
    }

    @Test
    public void badJson() throws IOException {
        ServiceStatusInfo info = getServiceStatusInfoFromJsonResponse("} foo bar");
        assertEquals(ServiceStatus.DOWN, info.serviceStatus());
        assertTrue(info.errorOrNull().startsWith("Exception: Unexpected close marker '}': "));
    }

    private ServiceStatusInfo getServiceStatusInfoFromJsonResponse(String content)
            throws IOException {
        CloseableHttpClient client = mock(CloseableHttpClient.class);

        CloseableHttpResponse response = mock(CloseableHttpResponse.class);
        when(client.execute(any())).thenReturn(response);

        StatusLine statusLine = mock(StatusLine.class);
        when(response.getStatusLine()).thenReturn(statusLine);

        when(statusLine.getStatusCode()).thenReturn(200);

        HttpEntity httpEntity = mock(HttpEntity.class);
        when(response.getEntity()).thenReturn(httpEntity);

        try (StateV1HealthUpdater updater = makeUpdater(client, entry -> content)) {
            when(httpEntity.getContentLength()).thenReturn((long) content.length());
            updater.run();
            return updater.getServiceStatusInfo();
        }
    }

    @Test
    public void testRequestException() throws IOException {
        CloseableHttpClient client = mock(CloseableHttpClient.class);

        when(client.execute(any())).thenThrow(new ConnectTimeoutException("exception string"));

        try (StateV1HealthUpdater updater = makeUpdater(client, entry -> "")) {
            updater.run();
            ServiceStatusInfo info = updater.getServiceStatusInfo();
            assertEquals(ServiceStatus.DOWN, info.serviceStatus());
            assertEquals("Exception: exception string", info.errorOrNull());
        }
    }

    @Test
    public void testBadHttpResponseCode()
            throws IOException {
        CloseableHttpClient client = mock(CloseableHttpClient.class);

        CloseableHttpResponse response = mock(CloseableHttpResponse.class);
        when(client.execute(any())).thenReturn(response);

        StatusLine statusLine = mock(StatusLine.class);
        when(response.getStatusLine()).thenReturn(statusLine);

        when(statusLine.getStatusCode()).thenReturn(500);

        HttpEntity httpEntity = mock(HttpEntity.class);
        when(response.getEntity()).thenReturn(httpEntity);

        String content = "{}";
        try (HealthUpdater updater = makeUpdater(client, entry -> content)) {
            when(httpEntity.getContentLength()).thenReturn((long) content.length());
            updater.run();
            ServiceStatusInfo info = updater.getServiceStatusInfo();
            assertEquals(ServiceStatus.DOWN, info.serviceStatus());
            assertEquals("Bad HTTP response status code 500", info.errorOrNull());
        }
    }

    private StateV1HealthUpdater makeUpdater(CloseableHttpClient client, Function<HttpEntity, String> getContentFunction) {
        ApacheHttpClient apacheHttpClient = new ApacheHttpClient(url, client);
        StateV1HealthClient healthClient = new StateV1HealthClient(apacheHttpClient, getContentFunction);
        return new StateV1HealthUpdater(url.toString(), healthClient);
    }
}