Health indicator should show down if acceptEmpty false and property sources are empty (#2778)

Fixes #2749
This commit is contained in:
Ryan Baxter
2025-03-06 08:25:57 -06:00
committed by GitHub
parent 7efb905851
commit 4b11b56739
2 changed files with 31 additions and 2 deletions

View File

@@ -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);

View File

@@ -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);
}
}