Skip to content

Commit

Permalink
Added db to readiness probe
Browse files Browse the repository at this point in the history
  • Loading branch information
roar-skinderviken committed Dec 7, 2024
1 parent 486a5ed commit 0486d26
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
5 changes: 5 additions & 0 deletions backend-spring-boot/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ spring:
enabled: true
locations: classpath:db/migration
default-schema: vicx

management:
endpoint:
health:
show-details: never
24 changes: 19 additions & 5 deletions backend-spring-boot/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,29 @@ spring:
management:
server:
port: 8082
# health: # alternative, disable all and enable those required
# defaults:
# enabled: false
# livenessstate:
# enabled: true
# readinessState:
# enabled: true
# db:
# enabled: true
endpoints:
web:
exposure:
include: health,info
include: health
endpoint:
health:
show-details: always # to show all groups on localhost
probes:
enabled: true
info:
git:
enabled: true
mode: simple
group:
liveness:
include:
- livenessState
readiness:
include:
- readinessState
- db
Original file line number Diff line number Diff line change
@@ -1,43 +1,64 @@
package no.vicx.backend;

import no.vicx.backend.testconfiguration.TestSecurityConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalManagementPort;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.test.web.reactive.server.WebTestClient;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
@Import(TestSecurityConfig.class)
class ActuatorHealthTest {

@LocalManagementPort
private int managementPort;

@Autowired
TestRestTemplate restTemplate;
WebTestClient webClient;

private String baseManagementUrl;
@ParameterizedTest
@MethodSource("probeEndpoints")
void getHealthProbe_givenExpectedComponents_expectComponentsAsInConfig(
String probeEndpoint, List<String> expectedComponents) {

@BeforeEach
void setup() {
baseManagementUrl = "http://localhost:" + managementPort + "/actuator/health";
}
var currentProbeUrl = "http://localhost:%d/actuator/health%s"
.formatted(managementPort, probeEndpoint);

@ParameterizedTest
@ValueSource(strings = {"/liveness", "/readiness"})
@DisplayName("Health Endpoint Probes Should Return HTTP 200")
void testHealthProbes(String probePath) {
var response = restTemplate.getForEntity(baseManagementUrl + probePath, String.class);
webClient.get()
.uri(currentProbeUrl)
.exchange()
.expectStatus().isOk()
.expectBody(new ParameterizedTypeReference<Map<String, Object>>() {
})
.consumeWith(result -> {
@SuppressWarnings({"unchecked", "DataFlowIssue"})
var actualComponents = (Map<String, Object>) result.getResponseBody().get("components");

assertEquals(expectedComponents.size(), actualComponents.size());
assertThat(actualComponents.keySet(), containsInAnyOrder(expectedComponents.toArray()));
});
}

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals("{\"status\":\"UP\"}", response.getBody());
private static Stream<Arguments> probeEndpoints() {
return Stream.of(
Arguments.of("/readiness", List.of("db", "readinessState")),
Arguments.of("/liveness", Collections.singletonList("livenessState"))
);
}
}

0 comments on commit 0486d26

Please sign in to comment.