aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/VespaServiceTest.java
blob: c69fe7539df966efba06b38a2244e2d2d60360eb (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.service;

import ai.vespa.metricsproxy.metric.Metric;
import ai.vespa.metricsproxy.metric.Metrics;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static ai.vespa.metricsproxy.TestUtil.getFileContents;
import static ai.vespa.metricsproxy.metric.model.MetricId.toMetricId;
import static ai.vespa.metricsproxy.service.RemoteMetricsFetcher.METRICS_PATH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * @author Unknown
 */
public class VespaServiceTest {
    private MockHttpServer httpServer;
    private static final String response;

    static {
        response = getFileContents("metrics-state.json");
        HttpMetricFetcher.CONNECTION_TIMEOUT = 60000; // 60 secs in unit tests
    }

    @Before
    public void setupHTTPServer() {
        try {
            httpServer = new MockHttpServer(response, METRICS_PATH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testService() {
        VespaService service = new VespaService("container", "container/container.0");
        assertEquals("container", service.getServiceName());
        assertEquals("container", service.getInstanceName());
        assertEquals(-1, service.getPid());
        assertEquals("container/container.0", service.getConfigId());


        service = VespaService.create("container2", "container/container.0", -1);
        assertEquals("container", service.getServiceName());
        assertEquals("container2", service.getInstanceName());
        assertEquals(-1, service.getPid());
        assertEquals("container/container.0", service.getConfigId());
    }

    @Test
    // TODO: Make it possible to test this without running a HTTP server to create the response
    public void testMetricsFetching() {
        VespaService service = VespaService.create("service1", "id", httpServer.port());
        assertEquals(28, getMetric("queries.count", service.getMetrics()).getValue().intValue());

        // Shutdown server and check that no metrics are returned (should use empty metrics
        // when unable to fetch new metrics)
        shutdown();

        assertTrue(service.getMetrics().list().isEmpty());
    }

    @After
    public void shutdown() {
        this.httpServer.close();
    }

    public Metric getMetric(String metric, Metrics metrics) {
        for (Metric m: metrics.list()) {
            if (m.getName().equals(toMetricId(metric)))
                return m;
        }
        return null;
    }

}