From b49ceed3c5f5221b0862a703e7fd6b0372c2c5ab Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 17 Mar 2015 13:13:28 +0000 Subject: [PATCH] Config server health indicator default status changed to UNKNOWN In the unlikely event that the property source locator throws an exception we go with DOWN, but if there are no property sources we just mark it as UNKNOWN. It's then a business decision whether or not to rely on the server to be there at runtime (e.g. in a /refresh). See gh-104 --- .../client/ConfigServerHealthIndicator.java | 6 +- .../ConfigServerHealthIndicatorTests.java | 60 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerHealthIndicatorTests.java diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerHealthIndicator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerHealthIndicator.java index 5fa005d8..f676a58a 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerHealthIndicator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerHealthIndicator.java @@ -20,6 +20,10 @@ public class ConfigServerHealthIndicator extends AbstractHealthIndicator { public ConfigServerHealthIndicator(ConfigServicePropertySourceLocator locator) { this.env = new AbstractEnvironment() { + @Override + public String[] getActiveProfiles() { + return new String[] {"default"}; + } }; this.locator = locator; } @@ -38,7 +42,7 @@ public class ConfigServerHealthIndicator extends AbstractHealthIndicator { } else if (propertySource!=null) { builder.withDetail("propertySources", propertySource.toString()); } else { - builder.down().withDetail("error", "no property sources located"); + builder.unknown().withDetail("error", "no property sources located"); } } catch (Exception e) { builder.down(e); diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerHealthIndicatorTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerHealthIndicatorTests.java new file mode 100644 index 00000000..1e87b9ac --- /dev/null +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerHealthIndicatorTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2014-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.config.client; + +import static org.junit.Assert.assertEquals; + +import java.util.Collections; + +import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.boot.actuate.health.Status; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; + +/** + * @author Dave Syer + * + */ +public class ConfigServerHealthIndicatorTests { + + private ConfigServicePropertySourceLocator locator = Mockito + .mock(ConfigServicePropertySourceLocator.class); + private ConfigServerHealthIndicator indicator = new ConfigServerHealthIndicator( + locator); + + @Test + public void testDefaultStatus() { + // UNKNOWN is better than DOWN since it doesn't stop the app from working + assertEquals(Status.UNKNOWN, indicator.health().getStatus()); + } + + @Test + public void testExceptionStatus() { + Mockito.doThrow(new IllegalStateException()).when(locator).locate(Mockito.any(Environment.class)); + assertEquals(Status.DOWN, indicator.health().getStatus()); + } + + @Test + public void testServerUp() { + PropertySource source = new MapPropertySource("foo", Collections.emptyMap()); + Mockito.doReturn(source).when(locator).locate(Mockito.any(Environment.class)); + assertEquals(Status.UP, indicator.health().getStatus()); + } + +}