From 4b11b56739a64623dec825029f7693a636d035e3 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Thu, 6 Mar 2025 08:25:57 -0600 Subject: [PATCH] Health indicator should show down if acceptEmpty false and property sources are empty (#2778) Fixes #2749 --- .../config/ConfigServerHealthIndicator.java | 3 +- .../ConfigServerHealthIndicatorTests.java | 30 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerHealthIndicator.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerHealthIndicator.java index 1274a62e..180e10b5 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerHealthIndicator.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/ConfigServerHealthIndicator.java @@ -112,9 +112,10 @@ public class ConfigServerHealthIndicator extends AbstractHealthIndicator { return; } } - if (!this.acceptEmpty && details.isEmpty()) { + if (!this.acceptEmpty && (details.isEmpty() || details.stream().noneMatch(d -> d.containsKey("sources")))) { // If accept-empty is false and no repositories are found, meaning details is // empty, then set status to DOWN + // If there are details but none of them have sources, then set status to DOWN builder.down().withDetail("acceptEmpty", this.acceptEmpty); } builder.withDetail("repositories", details); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/ConfigServerHealthIndicatorTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/ConfigServerHealthIndicatorTests.java index adef8e01..8536eae0 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/ConfigServerHealthIndicatorTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/ConfigServerHealthIndicatorTests.java @@ -90,7 +90,7 @@ public class ConfigServerHealthIndicatorTests { } @Test - public void acceptEmptyFalse() { + public void acceptEmptyFalseNoRepos() { ConfigServerProperties configServerProperties = new ConfigServerProperties(); configServerProperties.setAcceptEmpty(false); this.indicator = new ConfigServerHealthIndicator(this.repository, configServerProperties); @@ -98,4 +98,32 @@ public class ConfigServerHealthIndicatorTests { assertThat(this.indicator.health().getStatus()).as("wrong default status").isEqualTo(Status.DOWN); } + @Test + public void acceptEmptyFalseNoPropertySources() { + Repository repo = new Repository(); + repo.setName("myname"); + repo.setProfiles("myprofile"); + repo.setLabel("mylabel"); + ConfigServerProperties configServerProperties = new ConfigServerProperties(); + configServerProperties.setAcceptEmpty(false); + this.indicator = new ConfigServerHealthIndicator(this.repository, configServerProperties); + this.indicator.setRepositories(Collections.singletonMap("myname", repo)); + when(this.repository.findOne("myname", "myprofile", "mylabel", false)).thenReturn(this.environment); + assertThat(this.indicator.health().getStatus()).as("wrong default status").isEqualTo(Status.DOWN); + } + + @Test + public void acceptEmptyTrueNoPropertySources() { + Repository repo = new Repository(); + repo.setName("myname"); + repo.setProfiles("myprofile"); + repo.setLabel("mylabel"); + ConfigServerProperties configServerProperties = new ConfigServerProperties(); + configServerProperties.setAcceptEmpty(true); + this.indicator = new ConfigServerHealthIndicator(this.repository, configServerProperties); + this.indicator.setRepositories(Collections.singletonMap("myname", repo)); + when(this.repository.findOne("myname", "myprofile", "mylabel", false)).thenReturn(this.environment); + assertThat(this.indicator.health().getStatus()).as("wrong default status").isEqualTo(Status.UP); + } + }