From b44a7e242c8373da2ae4bf78f792c55a4ee5d7fb Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Thu, 15 Sep 2022 14:07:47 -0700 Subject: [PATCH] Support Mongo's Stable API in MongoHealthIndicator Closes gh-30849 --- .../actuate/data/mongo/MongoHealthIndicator.java | 4 ++-- .../data/mongo/MongoReactiveHealthIndicator.java | 4 ++-- .../actuate/mongo/MongoHealthIndicatorTests.java | 14 +++++++------- .../mongo/MongoReactiveHealthIndicatorTests.java | 10 +++++----- .../src/main/resources/application.properties | 1 + .../SampleSessionMongoApplicationTests.java | 13 +++++++++++++ 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/mongo/MongoHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/mongo/MongoHealthIndicator.java index a320adf255..75fba5119f 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/mongo/MongoHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/mongo/MongoHealthIndicator.java @@ -43,8 +43,8 @@ public class MongoHealthIndicator extends AbstractHealthIndicator { @Override protected void doHealthCheck(Health.Builder builder) throws Exception { - Document result = this.mongoTemplate.executeCommand("{ buildInfo: 1 }"); - builder.up().withDetail("version", result.getString("version")); + Document result = this.mongoTemplate.executeCommand("{ isMaster: 1 }"); + builder.up().withDetail("maxWireVersion", result.getInteger("maxWireVersion")); } } diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/mongo/MongoReactiveHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/mongo/MongoReactiveHealthIndicator.java index 6ad9cdd5e3..aaa96e93a5 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/mongo/MongoReactiveHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/mongo/MongoReactiveHealthIndicator.java @@ -43,12 +43,12 @@ public class MongoReactiveHealthIndicator extends AbstractReactiveHealthIndicato @Override protected Mono doHealthCheck(Health.Builder builder) { - Mono buildInfo = this.reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }"); + Mono buildInfo = this.reactiveMongoTemplate.executeCommand("{ isMaster: 1 }"); return buildInfo.map((document) -> up(builder, document)); } private Health up(Health.Builder builder, Document document) { - return builder.up().withDetail("version", document.getString("version")).build(); + return builder.up().withDetail("maxWireVersion", document.getInteger("maxWireVersion")).build(); } } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mongo/MongoHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mongo/MongoHealthIndicatorTests.java index fd5c5d4ec7..0cc6e283b4 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mongo/MongoHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mongo/MongoHealthIndicatorTests.java @@ -40,26 +40,26 @@ class MongoHealthIndicatorTests { @Test void mongoIsUp() { Document commandResult = mock(Document.class); - given(commandResult.getString("version")).willReturn("2.6.4"); + given(commandResult.getInteger("maxWireVersion")).willReturn(10); MongoTemplate mongoTemplate = mock(MongoTemplate.class); - given(mongoTemplate.executeCommand("{ buildInfo: 1 }")).willReturn(commandResult); + given(mongoTemplate.executeCommand("{ isMaster: 1 }")).willReturn(commandResult); MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate); Health health = healthIndicator.health(); assertThat(health.getStatus()).isEqualTo(Status.UP); - assertThat(health.getDetails().get("version")).isEqualTo("2.6.4"); - then(commandResult).should().getString("version"); - then(mongoTemplate).should().executeCommand("{ buildInfo: 1 }"); + assertThat(health.getDetails().get("maxWireVersion")).isEqualTo(10); + then(commandResult).should().getInteger("maxWireVersion"); + then(mongoTemplate).should().executeCommand("{ isMaster: 1 }"); } @Test void mongoIsDown() { MongoTemplate mongoTemplate = mock(MongoTemplate.class); - given(mongoTemplate.executeCommand("{ buildInfo: 1 }")).willThrow(new MongoException("Connection failed")); + given(mongoTemplate.executeCommand("{ isMaster: 1 }")).willThrow(new MongoException("Connection failed")); MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate); Health health = healthIndicator.health(); assertThat(health.getStatus()).isEqualTo(Status.DOWN); assertThat((String) health.getDetails().get("error")).contains("Connection failed"); - then(mongoTemplate).should().executeCommand("{ buildInfo: 1 }"); + then(mongoTemplate).should().executeCommand("{ isMaster: 1 }"); } } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mongo/MongoReactiveHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mongo/MongoReactiveHealthIndicatorTests.java index 7c63abd11b..2afd83823e 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mongo/MongoReactiveHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mongo/MongoReactiveHealthIndicatorTests.java @@ -41,23 +41,23 @@ class MongoReactiveHealthIndicatorTests { @Test void testMongoIsUp() { Document buildInfo = mock(Document.class); - given(buildInfo.getString("version")).willReturn("2.6.4"); + given(buildInfo.getInteger("maxWireVersion")).willReturn(10); ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class); - given(reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }")).willReturn(Mono.just(buildInfo)); + given(reactiveMongoTemplate.executeCommand("{ isMaster: 1 }")).willReturn(Mono.just(buildInfo)); MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator( reactiveMongoTemplate); Mono health = mongoReactiveHealthIndicator.health(); StepVerifier.create(health).consumeNextWith((h) -> { assertThat(h.getStatus()).isEqualTo(Status.UP); - assertThat(h.getDetails()).containsOnlyKeys("version"); - assertThat(h.getDetails().get("version")).isEqualTo("2.6.4"); + assertThat(h.getDetails()).containsOnlyKeys("maxWireVersion"); + assertThat(h.getDetails().get("maxWireVersion")).isEqualTo(10); }).verifyComplete(); } @Test void testMongoIsDown() { ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class); - given(reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }")) + given(reactiveMongoTemplate.executeCommand("{ isMaster: 1 }")) .willThrow(new MongoException("Connection failed")); MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator( reactiveMongoTemplate); diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/main/resources/application.properties index 98058a93e6..c57212adcc 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/main/resources/application.properties +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/main/resources/application.properties @@ -1,3 +1,4 @@ management.endpoints.web.exposure.include=* +management.endpoint.health.show-details=always spring.security.user.name=user spring.security.user.password=password \ No newline at end of file diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/test/java/smoketest/session/mongodb/SampleSessionMongoApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/test/java/smoketest/session/mongodb/SampleSessionMongoApplicationTests.java index ae6ffc2555..cda1b9a7e0 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/test/java/smoketest/session/mongodb/SampleSessionMongoApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/test/java/smoketest/session/mongodb/SampleSessionMongoApplicationTests.java @@ -29,6 +29,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.boot.testsupport.testcontainers.DockerImageNames; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpHeaders; @@ -53,6 +54,9 @@ public class SampleSessionMongoApplicationTests { @Autowired private TestRestTemplate restTemplate; + @LocalServerPort + private int port; + @Container static MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3) .withStartupTimeout(Duration.ofMinutes(2)); @@ -73,6 +77,15 @@ public class SampleSessionMongoApplicationTests { assertThat(sessions.size()).isEqualTo(1); } + @Test + @SuppressWarnings("unchecked") + void health() { + ResponseEntity entity = this.restTemplate + .getForEntity("http://localhost:" + this.port + "/actuator/health", String.class); + assertThat(entity.getBody()).contains("\"status\":\"UP\""); + assertThat(entity.getBody()).contains("maxWireVersion"); + } + private void createSession(URI uri) { RequestEntity request = getRequestEntity(uri); this.restTemplate.exchange(request, String.class);