aboutsummaryrefslogtreecommitdiffstats
path: root/routing-generator/src/test/java/com/yahoo/vespa/hosted/routing/nginx/NginxHealthClientTest.java
blob: c70569eb60cf7aecafbf7c87f97cfe3b5fdf05cb (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.routing.nginx;

import com.yahoo.vespa.hosted.routing.mock.HttpClientMock;
import org.junit.Test;

import java.nio.file.Paths;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * @author oyving
 * @author mpolden
 */
public class NginxHealthClientTest {

    @Test
    public void unknown_endpoint_is_down() {
        NginxHealthClient client = createClient("nginx-health-output.json");
        assertFalse(client.servers().isHealthy("no.such.endpoint"));
    }

    @Test
    public void all_down_endpoint_is_down() {
        NginxHealthClient service = createClient("nginx-health-output-all-down.json");
        assertFalse(service.servers().isHealthy("gateway.prod.music.vespa.us-east-2.prod"));
    }

    @Test
    public void all_up_endpoint_is_up() {
        NginxHealthClient service = createClient("nginx-health-output-all-up.json");
        assertTrue(service.servers().isHealthy("gateway.prod.music.vespa.us-east-2.prod"));
    }

    @Test
    public void two_down_endpoint_is_down() {
        NginxHealthClient service = createClient("nginx-health-output-policy-down.json");
        assertFalse(service.servers().isHealthy("gateway.prod.music.vespa.us-east-2.prod"));
    }

    @Test
    public void one_down_endpoint_is_up() {
        NginxHealthClient service = createClient("nginx-health-output-policy-up.json");
        assertTrue(service.servers().isHealthy("gateway.prod.music.vespa.us-east-2.prod"));
    }

    @Test
    public void all_up_but_other_endpoint_down() {
        NginxHealthClient service = createClient("nginx-health-output-all-up-but-other-down.json");
        assertTrue(service.servers().isHealthy("gateway.prod.music.vespa.us-east-2.prod"));
        assertFalse(service.servers().isHealthy("frog.prod.music.vespa.us-east-2.prod"));
    }

    private static NginxHealthClient createClient(String file) {
        HttpClientMock httpClient = new HttpClientMock().setResponse("GET",
                                                                     "http://localhost:4080/health-status/?format=json",
                                                                     response(file));
        return new NginxHealthClient(httpClient);
    }

    private static HttpClientMock.JsonResponse response(String file) {
        return new HttpClientMock.JsonResponse(Paths.get("src/test/resources/", file), 200);
    }

}