summaryrefslogtreecommitdiffstats
path: root/service-monitor/src/test/java/com/yahoo/vespa/service/health/ApplicationHealthMonitorTest.java
blob: 0dfca12099e88528086b8ffa700e3c439627185a (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
// Copyright 2018 Yahoo Holdings. 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.config.provision.HostName;
import com.yahoo.vespa.applicationmodel.ServiceStatus;
import com.yahoo.vespa.service.duper.ConfigServerApplication;
import com.yahoo.vespa.service.monitor.ConfigserverUtil;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ApplicationHealthMonitorTest {
    private final ConfigServerApplication configServerApplication = new ConfigServerApplication();

    @Test
    public void sanityCheck() {
        MonitorFactory monitorFactory = new MonitorFactory();

        HealthMonitor monitor1 = mock(HealthMonitor.class);
        HealthMonitor monitor2 = mock(HealthMonitor.class);
        HealthMonitor monitor3 = mock(HealthMonitor.class);

        monitorFactory.expectEndpoint("http://cfg1:19071/state/v1/health", monitor1);
        monitorFactory.expectEndpoint("http://cfg2:19071/state/v1/health", monitor2);
        monitorFactory.expectEndpoint("http://cfg3:19071/state/v1/health", monitor3);

        when(monitor1.getStatus()).thenReturn(ServiceStatus.UP);
        when(monitor2.getStatus()).thenReturn(ServiceStatus.DOWN);
        when(monitor3.getStatus()).thenReturn(ServiceStatus.NOT_CHECKED);

        ApplicationHealthMonitor applicationMonitor = ApplicationHealthMonitor.startMonitoring(
                ConfigserverUtil.makeExampleConfigServer(),
                monitorFactory);

        assertEquals(ServiceStatus.UP, getStatus(applicationMonitor, "cfg1"));
        assertEquals(ServiceStatus.DOWN, getStatus(applicationMonitor, "cfg2"));
        assertEquals(ServiceStatus.NOT_CHECKED, getStatus(applicationMonitor, "cfg3"));
    }

    private ServiceStatus getStatus(ApplicationHealthMonitor monitor, String hostname) {
        return monitor.getStatus(
                configServerApplication.getApplicationId(),
                configServerApplication.getClusterId(),
                configServerApplication.getServiceType(),
                configServerApplication.configIdFor(HostName.from(hostname)));
    }

    private static class MonitorFactory implements Function<HealthEndpoint, HealthMonitor> {
        private Map<String, EndpointInfo> endpointMonitors = new HashMap<>();

        public void expectEndpoint(String url, HealthMonitor monitorToReturn) {
            endpointMonitors.put(url, new EndpointInfo(url, monitorToReturn));
        }

        @Override
        public HealthMonitor apply(HealthEndpoint endpoint) {
            String url = endpoint.getStateV1HealthUrl().toString();
            EndpointInfo info = endpointMonitors.get(url);
            if (info == null) {
                throw new IllegalArgumentException("Endpoint not expected: " + url);
            }

            if (info.isEndpointDiscovered()) {
                throw new IllegalArgumentException("A HealthMonitor has already been created to " + url);
            }

            info.setEndpointDiscovered(true);

            return info.getMonitorToReturn();
        }
    }

    private static class EndpointInfo {
        private final String url;
        private final HealthMonitor monitorToReturn;

        private boolean endpointDiscovered = false;

        private EndpointInfo(String url, HealthMonitor monitorToReturn) {
            this.url = url;
            this.monitorToReturn = monitorToReturn;
        }

        public String getUrl() {
            return url;
        }

        public boolean isEndpointDiscovered() {
            return endpointDiscovered;
        }

        public void setEndpointDiscovered(boolean endpointDiscovered) {
            this.endpointDiscovered = endpointDiscovered;
        }

        public HealthMonitor getMonitorToReturn() {
            return monitorToReturn;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            EndpointInfo that = (EndpointInfo) o;
            return Objects.equals(url, that.url);
        }

        @Override
        public int hashCode() {
            return Objects.hash(url);
        }
    }
}