Skip to content

Commit a8e9060

Browse files
committed
Improve Kubernetes liveness and readiness probes customization documentation
Add tests for liveness and readiness probes
1 parent d4980ea commit a8e9060

File tree

4 files changed

+192
-2
lines changed

4 files changed

+192
-2
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,13 @@ This can be done by setting the following property:
902902
----
903903

904904
This would make `liveness` available at `/livez` and `readiness` at `readyz` on the main server port.
905+
Paths could be customized using `additional-path` property on group.
905906

906-
907-
907+
[source,properties,indent=0,subs="verbatim"]
908+
----
909+
management.endpoint.health.group.liveness.additional-path=server:/live-custom
910+
management.endpoint.health.group.readiness.additional-path=server:/ready-custom
911+
----
908912

909913
[[actuator.endpoints.kubernetes-probes.external-state]]
910914
==== Checking External State With Kubernetes Probes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.actuator;
18+
19+
import java.util.function.Function;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.boot.test.web.client.TestRestTemplate;
24+
import org.springframework.boot.test.web.server.LocalManagementPort;
25+
import org.springframework.boot.test.web.server.LocalServerPort;
26+
import org.springframework.http.HttpStatus;
27+
import org.springframework.http.ResponseEntity;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Base class for integration tests for adding Health probes additional paths.
33+
*
34+
* @author Ivo Smid
35+
*/
36+
class AbstractManagementPortAvailabilityProbesTests {
37+
38+
@LocalServerPort
39+
protected int port;
40+
41+
@LocalManagementPort
42+
protected int managementPort;
43+
44+
@Test
45+
void testLiveness() {
46+
ResponseEntity<String> entity = callHttpGetManagement("/actuator/health/liveness");
47+
assertUp(entity);
48+
}
49+
50+
@Test
51+
void testReadiness() {
52+
ResponseEntity<String> entity = callHttpGetManagement("/actuator/health/readiness");
53+
assertUp(entity);
54+
}
55+
56+
protected ResponseEntity<String> callHttpGetManagement(String urlPath) {
57+
return callHttpGet(urlPath, this.managementPort, Function.identity());
58+
}
59+
60+
protected ResponseEntity<String> callHttpGetServer(String urlPath) {
61+
return callHttpGet(urlPath, this.port, (it) -> it.withBasicAuth("user", "password"));
62+
}
63+
64+
private ResponseEntity<String> callHttpGet(String urlPath, int port,
65+
Function<TestRestTemplate, TestRestTemplate> customizer) {
66+
var testRestTemplate = customizer.apply(new TestRestTemplate());
67+
return testRestTemplate.getForEntity("http://localhost:" + port + urlPath, String.class);
68+
}
69+
70+
protected static void assertUp(ResponseEntity<String> entity) {
71+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
72+
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.actuator;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.test.context.SpringBootTest;
22+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
23+
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.ResponseEntity;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Integration tests for adding Health probes additional paths.
30+
*
31+
* @author Ivo Smid
32+
*/
33+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
34+
properties = { "management.server.port=0", "management.endpoint.health.probes.enabled=true",
35+
"management.endpoint.health.probes.add-additional-paths=true",
36+
"management.endpoint.health.group.liveness.additional-path=server:/live-custom",
37+
"management.endpoint.health.group.readiness.additional-path=server:/ready-custom" })
38+
class ManagementPortAvailabilityProbesCustomPathTests extends AbstractManagementPortAvailabilityProbesTests {
39+
40+
@Test
41+
void testCustomLivez() {
42+
ResponseEntity<String> entity = callHttpGetServer("/live-custom");
43+
assertUp(entity);
44+
}
45+
46+
@Test
47+
void testDefaultLivezNotFound() {
48+
ResponseEntity<String> entity = callHttpGetServer("/livez");
49+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
50+
}
51+
52+
@Test
53+
void testCustomReadyz() {
54+
ResponseEntity<String> entity = callHttpGetServer("/ready-custom");
55+
assertUp(entity);
56+
}
57+
58+
@Test
59+
void testDefaultReadyzNotFound() {
60+
ResponseEntity<String> entity = callHttpGetServer("/readyz");
61+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.actuator;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.test.context.SpringBootTest;
22+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
23+
import org.springframework.http.ResponseEntity;
24+
25+
/**
26+
* Integration tests for adding Health probes additional paths.
27+
*
28+
* @author Ivo Smid
29+
*/
30+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
31+
properties = { "management.server.port=0", "management.endpoint.health.probes.enabled=true",
32+
"management.endpoint.health.probes.add-additional-paths=true" })
33+
class ManagementPortAvailabilityProbesDefaultPathTests extends AbstractManagementPortAvailabilityProbesTests {
34+
35+
@Test
36+
void testLivez() {
37+
ResponseEntity<String> entity = callHttpGetServer("/livez");
38+
assertUp(entity);
39+
}
40+
41+
@Test
42+
void testReadyz() {
43+
ResponseEntity<String> entity = callHttpGetServer("/readyz");
44+
assertUp(entity);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)