Fix issue where Kubernetes specific info did not show up in /health

Fixes #250
This commit is contained in:
Georgios Andrianakis
2018-10-12 10:37:26 +03:00
committed by Ioannis Canellos
parent c2dc809469
commit a3dcd2f4d8
6 changed files with 143 additions and 1 deletions

View File

@@ -164,6 +164,11 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -0,0 +1,59 @@
package org.springframework.cloud.kubernetes.config;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.StringContains.containsString;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.config.example.App;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class,
properties = {
"management.endpoint.health.show-details=always"
})
public class HealthIndicatorTest {
@ClassRule
public static KubernetesServer server = new KubernetesServer();
private static KubernetesClient mockClient;
@Value("${local.server.port}")
private int port;
@BeforeClass
public static void setUpBeforeClass() {
mockClient = server.getClient();
//Configure the kubernetes master url to point to the mock server
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl());
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");
}
@Test
public void healthEndpointShouldContainKubernetes() {
RestAssured.baseURI = String.format("http://localhost:%d/actuator/health", port);
given()
.contentType("application/json")
.get()
.then()
.statusCode(200)
.body(containsString("kubernetes"));
}
}

View File

@@ -108,12 +108,24 @@
<artifactId>spock-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -112,7 +112,6 @@ public class KubernetesAutoConfiguration {
@ConditionalOnClass(HealthIndicator.class)
protected static class KubernetesActuatorConfiguration {
@Bean
@ConditionalOnMissingBean
public KubernetesHealthIndicator kubernetesHealthIndicator(PodUtils podUtils) {
return new KubernetesHealthIndicator(podUtils);
}

View File

@@ -0,0 +1,59 @@
package org.springframework.cloud.kubernetes;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.StringContains.containsString;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.example.App;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class,
properties = {
"management.endpoint.health.show-details=always"
})
public class HealthIndicatorTest {
@ClassRule
public static KubernetesServer server = new KubernetesServer();
private static KubernetesClient mockClient;
@Value("${local.server.port}")
private int port;
@BeforeClass
public static void setUpBeforeClass() {
mockClient = server.getClient();
//Configure the kubernetes master url to point to the mock server
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl());
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");
}
@Test
public void healthEndpointShouldContainKubernetes() {
RestAssured.baseURI = String.format("http://localhost:%d/actuator/health", port);
given()
.contentType("application/json")
.get()
.then()
.statusCode(200)
.body(containsString("kubernetes"));
}
}

View File

@@ -0,0 +1,8 @@
package org.springframework.cloud.kubernetes.example;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
}