From a2bea457e569d781e76abae994dad1de61b42be9 Mon Sep 17 00:00:00 2001 From: Ivo Smid Date: Thu, 13 Apr 2023 19:57:25 +0200 Subject: [PATCH] Improve Kubernetes liveness and readiness probes customization documentation Add tests for liveness and readiness probes --- .../src/docs/asciidoc/actuator/endpoints.adoc | 6 +- ...ManagementPortAvailabilityProbesTests.java | 75 +++++++++++++++++++ ...PortAvailabilityProbesCustomPathTests.java | 64 ++++++++++++++++ ...obesCustomPathWithoutFlagEnabledTests.java | 65 ++++++++++++++++ ...ortAvailabilityProbesDefaultPathTests.java | 47 ++++++++++++ 5 files changed, 253 insertions(+), 4 deletions(-) create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/AbstractManagementPortAvailabilityProbesTests.java create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesCustomPathTests.java create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesCustomPathWithoutFlagEnabledTests.java create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesDefaultPathTests.java diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc index 195c05ab9886..1b31a49c8142 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc @@ -904,10 +904,8 @@ This can be done by setting the following property: management.endpoint.health.probes.add-additional-paths=true ---- -This would make `liveness` available at `/livez` and `readiness` at `readyz` on the main server port. - - - +This would make `liveness` group available at `/livez` and `readiness` group at `readyz` on the main server port. +Paths could be customized using `additional-path` property on each group, see <> for more detail. [[actuator.endpoints.kubernetes-probes.external-state]] ==== Checking External State With Kubernetes Probes diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/AbstractManagementPortAvailabilityProbesTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/AbstractManagementPortAvailabilityProbesTests.java new file mode 100644 index 000000000000..2e490fca2b90 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/AbstractManagementPortAvailabilityProbesTests.java @@ -0,0 +1,75 @@ +/* + * Copyright 2012-2023 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 + * + * https://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 smoketest.actuator; + +import java.util.function.Function; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalManagementPort; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Base class for integration tests for adding Health probes additional paths. + * + * @author Ivo Smid + */ +abstract class AbstractManagementPortAvailabilityProbesTests { + + @LocalServerPort + protected int port; + + @LocalManagementPort + protected int managementPort; + + @Test + void testLiveness() { + ResponseEntity entity = callHttpGetManagement("/actuator/health/liveness"); + assertUp(entity); + } + + @Test + void testReadiness() { + ResponseEntity entity = callHttpGetManagement("/actuator/health/readiness"); + assertUp(entity); + } + + protected ResponseEntity callHttpGetManagement(String urlPath) { + return callHttpGet(urlPath, this.managementPort, Function.identity()); + } + + protected ResponseEntity callHttpGetServer(String urlPath) { + return callHttpGet(urlPath, this.port, (it) -> it.withBasicAuth("user", "password")); + } + + private ResponseEntity callHttpGet(String urlPath, int port, + Function customizer) { + var testRestTemplate = customizer.apply(new TestRestTemplate()); + return testRestTemplate.getForEntity("http://localhost:" + port + urlPath, String.class); + } + + protected static void assertUp(ResponseEntity entity) { + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(entity.getBody()).contains("\"status\":\"UP\""); + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesCustomPathTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesCustomPathTests.java new file mode 100644 index 000000000000..fb57193f4871 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesCustomPathTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012-2023 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 + * + * https://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 smoketest.actuator; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for adding Health probes additional paths. + * + * @author Ivo Smid + */ +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, + properties = { "management.server.port=0", "management.endpoint.health.probes.enabled=true", + "management.endpoint.health.probes.add-additional-paths=true", + "management.endpoint.health.group.liveness.additional-path=server:/live-custom", + "management.endpoint.health.group.readiness.additional-path=server:/ready-custom" }) +class ManagementPortAvailabilityProbesCustomPathTests extends AbstractManagementPortAvailabilityProbesTests { + + @Test + void testCustomLivez() { + ResponseEntity entity = callHttpGetServer("/live-custom"); + assertUp(entity); + } + + @Test + void testDefaultLivezNotFound() { + ResponseEntity entity = callHttpGetServer("/livez"); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } + + @Test + void testCustomReadyz() { + ResponseEntity entity = callHttpGetServer("/ready-custom"); + assertUp(entity); + } + + @Test + void testDefaultReadyzNotFound() { + ResponseEntity entity = callHttpGetServer("/readyz"); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesCustomPathWithoutFlagEnabledTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesCustomPathWithoutFlagEnabledTests.java new file mode 100644 index 000000000000..6d59050486df --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesCustomPathWithoutFlagEnabledTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2012-2023 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 + * + * https://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 smoketest.actuator; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for adding Health probes additional paths. + * + * @author Ivo Smid + */ +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, + properties = { "management.server.port=0", "management.endpoint.health.probes.enabled=true", + "management.endpoint.health.probes.add-additional-paths=false", + "management.endpoint.health.group.liveness.additional-path=server:/live-custom", + "management.endpoint.health.group.readiness.additional-path=server:/ready-custom" }) +class ManagementPortAvailabilityProbesCustomPathWithoutFlagEnabledTests + extends AbstractManagementPortAvailabilityProbesTests { + + @Test + void testCustomLivez() { + ResponseEntity entity = callHttpGetServer("/live-custom"); + assertUp(entity); + } + + @Test + void testDefaultLivezNotFound() { + ResponseEntity entity = callHttpGetServer("/livez"); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } + + @Test + void testCustomReadyz() { + ResponseEntity entity = callHttpGetServer("/ready-custom"); + assertUp(entity); + } + + @Test + void testDefaultReadyzNotFound() { + ResponseEntity entity = callHttpGetServer("/readyz"); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesDefaultPathTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesDefaultPathTests.java new file mode 100644 index 000000000000..8fa8fa6b55ab --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortAvailabilityProbesDefaultPathTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2023 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 + * + * https://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 smoketest.actuator; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.http.ResponseEntity; + +/** + * Integration tests for adding Health probes additional paths. + * + * @author Ivo Smid + */ +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, + properties = { "management.server.port=0", "management.endpoint.health.probes.enabled=true", + "management.endpoint.health.probes.add-additional-paths=true" }) +class ManagementPortAvailabilityProbesDefaultPathTests extends AbstractManagementPortAvailabilityProbesTests { + + @Test + void testLivez() { + ResponseEntity entity = callHttpGetServer("/livez"); + assertUp(entity); + } + + @Test + void testReadyz() { + ResponseEntity entity = callHttpGetServer("/readyz"); + assertUp(entity); + } + +}