Skip to content

Commit

Permalink
Improve unit tests for the http handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
gjoranv committed Jun 22, 2019
1 parent 599758d commit 1911df6
Showing 1 changed file with 97 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import ai.vespa.metricsproxy.TestUtil;
import ai.vespa.metricsproxy.core.ConsumersConfig;
import ai.vespa.metricsproxy.core.ConsumersConfig.Consumer;
import ai.vespa.metricsproxy.core.MetricsConsumers;
import ai.vespa.metricsproxy.core.MetricsManager;
import ai.vespa.metricsproxy.metric.HealthMetric;
Expand All @@ -29,6 +30,7 @@
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.Executors;
Expand All @@ -43,6 +45,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
* @author gjoranv
Expand All @@ -55,11 +58,15 @@ public class GenericMetricsHandlerTest {
new DummyService(1, ""),
new DownService(HealthMetric.getDown("No response")));

private static final VespaServices vespaServices = new VespaServices(testServices);

private static final String DEFAULT_CONSUMER = "default";
private static final String CUSTOM_CONSUMER = "custom-consumer";

private static final String CPU_METRIC = "cpu";

private static final String URI = "http://localhost/metrics/v1/values";

private static final VespaServices vespaServices = new VespaServices(testServices);

private static RequestHandlerTestDriver testDriver;

Expand All @@ -74,47 +81,106 @@ public static void setup() {
testDriver = new RequestHandlerTestDriver(handler);
}

private GenericJsonModel getResponseAsJsonModel(String consumer) {
String response = testDriver.sendRequest(URI + "?consumer=" + consumer).readAll();
try {
return createObjectMapper().readValue(response, GenericJsonModel.class);
} catch (IOException e) {
fail("Failed to create json model: " + e.getMessage());
throw new RuntimeException(e);
}
}

@Ignore
@Test
public void visually_inspect_response() throws Exception{
String response = testDriver.sendRequest(URI).readAll();
String response = testDriver.sendRequest(URI+ "?consumer=default").readAll();
ObjectMapper mapper = createObjectMapper();
var jsonModel = mapper.readValue(response, GenericJsonModel.class);
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonModel));
}

@Test
public void response_contains_node_metrics() throws Exception {
public void no_explicit_consumer_gives_the_default_consumer() {
String responseDefaultConsumer = testDriver.sendRequest(URI + "?consumer=default").readAll();
String responseNoConsumer = testDriver.sendRequest(URI).readAll();
assertEqualsExceptTimestamps(responseDefaultConsumer, responseNoConsumer);
}

@Test
public void unknown_consumer_gives_the_default_consumer() {
String response = testDriver.sendRequest(URI).readAll();
var jsonModel = createObjectMapper().readValue(response, GenericJsonModel.class);
String responseUnknownConsumer = testDriver.sendRequest(URI + "?consumer=not_defined").readAll();
assertEqualsExceptTimestamps(response, responseUnknownConsumer);
}

private void assertEqualsExceptTimestamps(String s1, String s2) {
assertEquals(replaceTimestamps(s1), replaceTimestamps(s2));
}

@Test
public void response_contains_node_metrics() {
GenericJsonModel jsonModel = getResponseAsJsonModel(DEFAULT_CONSUMER);

assertNotNull(jsonModel.node);
assertEquals(1, jsonModel.node.metrics.size());
assertEquals(12.345, jsonModel.node.metrics.get(0).values.get(CPU_METRIC), 0.0001d);
}

@Test
public void response_contains_service_metrics() throws Exception {
String response = testDriver.sendRequest(URI).readAll();
var jsonModel = createObjectMapper().readValue(response, GenericJsonModel.class);
public void response_contains_service_metrics() {
GenericJsonModel jsonModel = getResponseAsJsonModel(DEFAULT_CONSUMER);

assertEquals(2, jsonModel.services.size());
GenericService dummyService = jsonModel.services.get(0);
assertEquals(2, dummyService.metrics.size());

GenericMetrics dummy0Metrics = getMetricsForInstance("dummy0", dummyService);
assertEquals(1L, dummy0Metrics.values.get(METRIC_1).longValue());
assertEquals("metric-dim", dummy0Metrics.dimensions.get("dim0"));
assertEquals("default-val", dummy0Metrics.dimensions.get("consumer-dim"));

GenericMetrics dummy1Metrics = getMetricsForInstance("dummy1", dummyService);
assertEquals(6L, dummy1Metrics.values.get(METRIC_1).longValue());
assertEquals("metric-dim", dummy1Metrics.dimensions.get("dim0"));
assertEquals("default-val", dummy1Metrics.dimensions.get("consumer-dim"));
}

@Test
public void response_contains_health_from_service_that_is_down() throws Exception {
String response = testDriver.sendRequest(URI).readAll();
var jsonModel = createObjectMapper().readValue(response, GenericJsonModel.class);
public void all_consumers_get_health_from_service_that_is_down() {
assertDownServiceHealth(DEFAULT_CONSUMER);
assertDownServiceHealth(CUSTOM_CONSUMER);
}

@Test
public void all_timestamps_are_equal_and_non_zero() {
GenericJsonModel jsonModel = getResponseAsJsonModel(DEFAULT_CONSUMER);

Long nodeTimestamp = jsonModel.node.timestamp;
assertNotEquals(0L, (long) nodeTimestamp);
for (var service : jsonModel.services)
assertEquals(nodeTimestamp, service.timestamp);
}

@Test
public void custom_consumer_gets_only_its_whitelisted_metrics() {
GenericJsonModel jsonModel = getResponseAsJsonModel(CUSTOM_CONSUMER);

assertNotNull(jsonModel.node);
// TODO: see comment in ExternalMetrics.setExtraMetrics
// assertEquals(0, jsonModel.node.metrics.size());

assertEquals(2, jsonModel.services.size());
GenericService dummyService = jsonModel.services.get(0);
assertEquals(2, dummyService.metrics.size());

GenericMetrics dummy0Metrics = getMetricsForInstance("dummy0", dummyService);
assertEquals("custom-val", dummy0Metrics.dimensions.get("consumer-dim"));

GenericMetrics dummy1Metrics = getMetricsForInstance("dummy1", dummyService);
assertEquals("custom-val", dummy1Metrics.dimensions.get("consumer-dim"));
}

private void assertDownServiceHealth(String consumer) {
GenericJsonModel jsonModel = getResponseAsJsonModel(consumer);

GenericService downService = jsonModel.services.get(1);
assertEquals(DOWN.status, downService.status.code);
Expand All @@ -127,39 +193,42 @@ public void response_contains_health_from_service_that_is_down() throws Exceptio
assertEquals(DownService.NAME, downService.metrics.get(0).dimensions.get(INSTANCE_DIMENSION_ID.id));
}

@Test
public void all_timestamps_are_equal_and_non_zero() throws Exception {
String response = testDriver.sendRequest(URI).readAll();
var jsonModel = createObjectMapper().readValue(response, GenericJsonModel.class);

Long nodeTimestamp = jsonModel.node.timestamp;
assertNotEquals(0L, (long) nodeTimestamp);
for (var service : jsonModel.services)
assertEquals(nodeTimestamp, service.timestamp);
private String replaceTimestamps(String s) {
return s.replaceAll("timestamp\":\\d+,", "timestamp\":1,");
}

private static GenericMetrics getMetricsForInstance(String instance, GenericService service) {
for (var metrics : service.metrics) {
if (metrics.dimensions.get(INSTANCE_DIMENSION_ID.id).equals(instance))
return metrics;
}
throw new RuntimeException("Could not find metrics for service instance " + instance);
fail("Could not find metrics for service instance " + instance);
throw new RuntimeException();
}

private static MetricsConsumers getMetricsConsumers() {
ConsumersConfig.Consumer.Metric.Dimension.Builder metricDimension = new ConsumersConfig.Consumer.Metric.Dimension.Builder()
.key("dim0").value("metric-dim");
var defaultConsumerDimension = new Consumer.Metric.Dimension.Builder()
.key("consumer-dim").value("default-val");

var customConsumerDimension = new Consumer.Metric.Dimension.Builder()
.key("consumer-dim").value("custom-val");

return new MetricsConsumers(new ConsumersConfig.Builder()
.consumer(new ConsumersConfig.Consumer.Builder()
.consumer(new Consumer.Builder()
.name(DEFAULT_PUBLIC_CONSUMER_ID.id)
.metric(new ConsumersConfig.Consumer.Metric.Builder()
.metric(new Consumer.Metric.Builder()
.name(CPU_METRIC)
.outputname(CPU_METRIC))
.metric(new ConsumersConfig.Consumer.Metric.Builder()
.metric(new Consumer.Metric.Builder()
.name(METRIC_1)
.outputname(METRIC_1)
.dimension(defaultConsumerDimension)))
.consumer(new Consumer.Builder()
.name(CUSTOM_CONSUMER)
.metric(new Consumer.Metric.Builder()
.name(METRIC_1)
.outputname(METRIC_1)
.dimension(metricDimension)))
.dimension(customConsumerDimension)))
.build());
}

Expand Down

0 comments on commit 1911df6

Please sign in to comment.