-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1583
- Loading branch information
Showing
19 changed files
with
369 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...ava/org/springframework/cloud/kubernetes/client/ActuatorEnabledFailFastExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2013-2024 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 org.springframework.cloud.kubernetes.client; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import io.kubernetes.client.openapi.ApiException; | ||
import io.kubernetes.client.openapi.apis.CoreV1Api; | ||
import io.kubernetes.client.util.Config; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.Status; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.cloud.kubernetes.client.example.App; | ||
import org.springframework.cloud.kubernetes.commons.EnvReader; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* @author wind57 | ||
*/ | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, | ||
classes = { App.class, ActuatorEnabledFailFastExceptionTest.ActuatorConfig.class }, | ||
properties = { "management.endpoint.health.show-details=always", | ||
"management.endpoint.health.show-components=always", "management.endpoints.web.exposure.include=health", | ||
"spring.main.cloud-platform=KUBERNETES" }) | ||
class ActuatorEnabledFailFastExceptionTest { | ||
|
||
private static final boolean FAIL_FAST = true; | ||
|
||
private static MockedStatic<EnvReader> envReaderMockedStatic; | ||
|
||
private static MockedStatic<Paths> pathsMockedStatic; | ||
|
||
private static final CoreV1Api coreV1Api = Mockito.mock(CoreV1Api.class); | ||
|
||
@Autowired | ||
private KubernetesClientHealthIndicator healthIndicator; | ||
|
||
@AfterEach | ||
void afterEach() { | ||
envReaderMockedStatic.close(); | ||
pathsMockedStatic.close(); | ||
} | ||
|
||
@Test | ||
void test() throws ApiException { | ||
Health health = healthIndicator.getHealth(true); | ||
Assertions.assertEquals(health.getStatus(), Status.DOWN); | ||
Mockito.verify(coreV1Api).readNamespacedPod("host", "my-namespace", null); | ||
} | ||
|
||
private static void mocks() { | ||
envReaderMockedStatic = Mockito.mockStatic(EnvReader.class); | ||
pathsMockedStatic = Mockito.mockStatic(Paths.class); | ||
|
||
envReaderMockedStatic.when(() -> EnvReader.getEnv(KubernetesClientPodUtils.KUBERNETES_SERVICE_HOST)) | ||
.thenReturn("k8s-host"); | ||
envReaderMockedStatic.when(() -> EnvReader.getEnv(KubernetesClientPodUtils.HOSTNAME)).thenReturn("host"); | ||
|
||
Path serviceAccountTokenPath = Mockito.mock(Path.class); | ||
File serviceAccountTokenFile = Mockito.mock(File.class); | ||
Mockito.when(serviceAccountTokenPath.toFile()).thenReturn(serviceAccountTokenFile); | ||
Mockito.when(serviceAccountTokenFile.exists()).thenReturn(true); | ||
pathsMockedStatic.when(() -> Paths.get(Config.SERVICEACCOUNT_TOKEN_PATH)).thenReturn(serviceAccountTokenPath); | ||
|
||
Path serviceAccountCAPath = Mockito.mock(Path.class); | ||
File serviceAccountCAFile = Mockito.mock(File.class); | ||
Mockito.when(serviceAccountCAPath.toFile()).thenReturn(serviceAccountCAFile); | ||
Mockito.when(serviceAccountCAFile.exists()).thenReturn(true); | ||
pathsMockedStatic.when(() -> Paths.get(Config.SERVICEACCOUNT_CA_PATH)).thenReturn(serviceAccountCAPath); | ||
} | ||
|
||
@TestConfiguration | ||
static class ActuatorConfig { | ||
|
||
// will be created "instead" of | ||
// KubernetesClientAutoConfiguration::kubernetesPodUtils | ||
@Bean | ||
KubernetesClientPodUtils kubernetesPodUtils() throws ApiException { | ||
|
||
mocks(); | ||
|
||
Mockito.when(coreV1Api.readNamespacedPod("host", "my-namespace", null)) | ||
.thenThrow(new RuntimeException("just because")); | ||
|
||
return new KubernetesClientPodUtils(coreV1Api, "my-namespace", FAIL_FAST); | ||
} | ||
|
||
} | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
...a/org/springframework/cloud/kubernetes/client/ActuatorEnabledNoFailFastExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright 2013-2024 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 org.springframework.cloud.kubernetes.client; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import io.kubernetes.client.openapi.ApiException; | ||
import io.kubernetes.client.openapi.apis.CoreV1Api; | ||
import io.kubernetes.client.util.Config; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.Status; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.cloud.kubernetes.client.example.App; | ||
import org.springframework.cloud.kubernetes.commons.EnvReader; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* @author wind57 | ||
*/ | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, | ||
classes = { App.class, ActuatorEnabledNoFailFastExceptionTest.ActuatorConfig.class }, | ||
properties = { "management.endpoint.health.show-details=always", | ||
"management.endpoint.health.show-components=always", "management.endpoints.web.exposure.include=health", | ||
"spring.main.cloud-platform=KUBERNETES" }) | ||
|
||
class ActuatorEnabledNoFailFastExceptionTest { | ||
|
||
private static final boolean FAIL_FAST = false; | ||
|
||
private static MockedStatic<EnvReader> envReaderMockedStatic; | ||
|
||
private static MockedStatic<Paths> pathsMockedStatic; | ||
|
||
private static final CoreV1Api coreV1Api = Mockito.mock(CoreV1Api.class); | ||
|
||
@Autowired | ||
private KubernetesClientHealthIndicator healthIndicator; | ||
|
||
@AfterEach | ||
void afterEach() { | ||
envReaderMockedStatic.close(); | ||
pathsMockedStatic.close(); | ||
} | ||
|
||
// without a fail-fast, we would not fail and actuator would return "UP" | ||
// This is not a real case we have, it just makes sure | ||
@Test | ||
void test() throws ApiException { | ||
Health health = healthIndicator.getHealth(true); | ||
Assertions.assertEquals(health.getStatus(), Status.UP); | ||
Mockito.verify(coreV1Api).readNamespacedPod("host", "my-namespace", null); | ||
} | ||
|
||
private static void mocks() { | ||
envReaderMockedStatic = Mockito.mockStatic(EnvReader.class); | ||
pathsMockedStatic = Mockito.mockStatic(Paths.class); | ||
|
||
envReaderMockedStatic.when(() -> EnvReader.getEnv(KubernetesClientPodUtils.KUBERNETES_SERVICE_HOST)) | ||
.thenReturn("k8s-host"); | ||
envReaderMockedStatic.when(() -> EnvReader.getEnv(KubernetesClientPodUtils.HOSTNAME)).thenReturn("host"); | ||
|
||
Path serviceAccountTokenPath = Mockito.mock(Path.class); | ||
File serviceAccountTokenFile = Mockito.mock(File.class); | ||
Mockito.when(serviceAccountTokenPath.toFile()).thenReturn(serviceAccountTokenFile); | ||
Mockito.when(serviceAccountTokenFile.exists()).thenReturn(true); | ||
pathsMockedStatic.when(() -> Paths.get(Config.SERVICEACCOUNT_TOKEN_PATH)).thenReturn(serviceAccountTokenPath); | ||
|
||
Path serviceAccountCAPath = Mockito.mock(Path.class); | ||
File serviceAccountCAFile = Mockito.mock(File.class); | ||
Mockito.when(serviceAccountCAPath.toFile()).thenReturn(serviceAccountCAFile); | ||
Mockito.when(serviceAccountCAFile.exists()).thenReturn(true); | ||
pathsMockedStatic.when(() -> Paths.get(Config.SERVICEACCOUNT_CA_PATH)).thenReturn(serviceAccountCAPath); | ||
} | ||
|
||
@TestConfiguration | ||
static class ActuatorConfig { | ||
|
||
// will be created "instead" of | ||
// KubernetesClientAutoConfiguration::kubernetesPodUtils | ||
@Bean | ||
KubernetesClientPodUtils kubernetesPodUtils() throws ApiException { | ||
|
||
mocks(); | ||
|
||
Mockito.when(coreV1Api.readNamespacedPod("host", "my-namespace", null)) | ||
.thenThrow(new RuntimeException("just because")); | ||
|
||
return new KubernetesClientPodUtils(coreV1Api, "my-namespace", FAIL_FAST); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.