|
| 1 | +package org.lognet.springboot.grpc.actuator; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 5 | +import com.jayway.jsonpath.Configuration; |
| 6 | +import com.jayway.jsonpath.DocumentContext; |
| 7 | +import com.jayway.jsonpath.JsonPath; |
| 8 | +import com.jayway.jsonpath.TypeRef; |
| 9 | +import com.jayway.jsonpath.spi.json.JacksonJsonProvider; |
| 10 | +import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; |
| 11 | +import io.micrometer.prometheus.PrometheusConfig; |
| 12 | +import org.awaitility.Awaitility; |
| 13 | +import org.hamcrest.Matchers; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.lognet.springboot.grpc.GrpcServerTestBase; |
| 17 | +import org.lognet.springboot.grpc.TestConfig; |
| 18 | +import org.lognet.springboot.grpc.demo.DemoApp; |
| 19 | +import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.boot.actuate.health.Status; |
| 21 | +import org.springframework.boot.test.context.SpringBootTest; |
| 22 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 23 | +import org.springframework.http.HttpStatus; |
| 24 | +import org.springframework.http.ResponseEntity; |
| 25 | +import org.springframework.test.context.ActiveProfiles; |
| 26 | +import org.springframework.test.context.junit4.SpringRunner; |
| 27 | + |
| 28 | +import java.time.Duration; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.Spliterator; |
| 31 | +import java.util.Spliterators; |
| 32 | +import java.util.concurrent.Callable; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | +import java.util.stream.Stream; |
| 35 | +import java.util.stream.StreamSupport; |
| 36 | + |
| 37 | +import static org.hamcrest.CoreMatchers.is; |
| 38 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | +import static org.junit.Assert.assertEquals; |
| 40 | +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
| 41 | + |
| 42 | +/** |
| 43 | + * Created by alexf on 28-Jan-16. |
| 44 | + */ |
| 45 | +@RunWith(SpringRunner.class) |
| 46 | +@SpringBootTest(classes = {DemoApp.class, TestConfig.class}, webEnvironment = RANDOM_PORT |
| 47 | + , properties = { |
| 48 | + |
| 49 | + "management.endpoint.health.show-details=always" |
| 50 | + , "management.endpoint.health.show-components=always" |
| 51 | + , "spring.main.web-application-type=servlet" |
| 52 | +}) |
| 53 | +@ActiveProfiles({"disable-security", "measure"}) |
| 54 | +public class ActuatorTest extends GrpcServerTestBase { |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private PrometheusConfig prometheusConfig; |
| 58 | + |
| 59 | + @Autowired |
| 60 | + private TestRestTemplate restTemplate; |
| 61 | + |
| 62 | + |
| 63 | + @Test |
| 64 | + public void actuatorEnvTest() throws ExecutionException, InterruptedException { |
| 65 | + ResponseEntity<String> response = restTemplate.getForEntity("/actuator/env", String.class); |
| 66 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void actuatorGrpcTest() throws ExecutionException, InterruptedException { |
| 71 | + ResponseEntity<String> response = restTemplate.getForEntity("/actuator/grpc", String.class); |
| 72 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 73 | + final DocumentContext json = JsonPath.parse(response.getBody(), Configuration.builder() |
| 74 | + .mappingProvider(new JacksonMappingProvider()) |
| 75 | + .jsonProvider(new JacksonJsonProvider()) |
| 76 | + .build()); |
| 77 | + final String[] statuses = json.read("services.*name", new TypeRef<String[]>() {}); |
| 78 | + assertThat(statuses,Matchers.arrayWithSize(Matchers.greaterThan(0))); |
| 79 | + for(String s:statuses) { |
| 80 | + assertThat(s, Matchers.not(Matchers.blankOrNullString())); |
| 81 | + } |
| 82 | + |
| 83 | + final Integer port = json.read("port", Integer.class); |
| 84 | + assertThat(port,Matchers.greaterThan(0)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void actuatorHealthTest() throws ExecutionException, InterruptedException { |
| 89 | + ResponseEntity<String> response = restTemplate.getForEntity("/actuator/health", String.class); |
| 90 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 91 | + |
| 92 | + final DocumentContext json = JsonPath.parse(response.getBody(), Configuration.builder() |
| 93 | + .mappingProvider(new JacksonMappingProvider()) |
| 94 | + .jsonProvider(new JacksonJsonProvider()) |
| 95 | + .build()); |
| 96 | + final String[] statuses = json.read("components.grpc.components.*status", new TypeRef<String[]>() { |
| 97 | + }); |
| 98 | + assertThat(statuses,Matchers.arrayWithSize(Matchers.greaterThan(0))); |
| 99 | + for(String s:statuses) { |
| 100 | + assertThat(s, is(Status.UP.getCode())); |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected void afterGreeting() throws Exception { |
| 107 | + |
| 108 | + |
| 109 | + ResponseEntity<ObjectNode> metricsResponse = restTemplate.getForEntity("/actuator/metrics", ObjectNode.class); |
| 110 | + assertEquals(HttpStatus.OK, metricsResponse.getStatusCode()); |
| 111 | + final String metricName = "grpc.server.calls"; |
| 112 | + final Optional<String> containsGrpcServerCallsMetric = StreamSupport.stream(Spliterators.spliteratorUnknownSize(metricsResponse.getBody().withArray("names") |
| 113 | + .elements(), Spliterator.NONNULL), false) |
| 114 | + .map(JsonNode::asText) |
| 115 | + .filter(metricName::equals) |
| 116 | + .findFirst(); |
| 117 | + assertThat("Should contain " + metricName, containsGrpcServerCallsMetric.isPresent()); |
| 118 | + |
| 119 | + |
| 120 | + Callable<Long> getPrometheusMetrics = () -> { |
| 121 | + ResponseEntity<String> response = restTemplate.getForEntity("/actuator/prometheus", String.class); |
| 122 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 123 | + return Stream.of(response.getBody().split(System.lineSeparator())) |
| 124 | + .filter(s -> s.contains(metricName.replace('.', '_'))) |
| 125 | + .count(); |
| 126 | + }; |
| 127 | + |
| 128 | + Awaitility |
| 129 | + .waitAtMost(Duration.ofMillis(prometheusConfig.step().toMillis() * 2)) |
| 130 | + .until(getPrometheusMetrics, Matchers.greaterThan(0L)); |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | +} |
0 commit comments