EnvReader to commons (#706)

This commit is contained in:
erabii
2021-01-15 14:15:17 -05:00
committed by GitHub
parent 19ec4a0d06
commit 84c05702f5
3 changed files with 39 additions and 16 deletions

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2013-2020 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.commons;
/**
* @author wind57
*
* a class that should be used instead of System::getEnv, uselfull for testing
*/
public final class EnvReader {
private EnvReader() {
}
public static String getEnv(String property) {
return System.getenv(property);
}
}

View File

@@ -25,6 +25,7 @@ import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.kubernetes.commons.EnvReader;
import org.springframework.cloud.kubernetes.commons.LazilyInstantiate;
import org.springframework.cloud.kubernetes.commons.PodUtils;
@@ -107,16 +108,4 @@ public class Fabric8PodUtils implements PodUtils<Pod> {
return serviceAccountPathPresent && Paths.get(Config.KUBERNETES_SERVICE_ACCOUNT_CA_CRT_PATH).toFile().exists();
}
/**
* @author wind57 A class useful for testing. At some point this should be moved to
* commons
*/
public static class EnvReader {
public static String getEnv(String property) {
return System.getenv(property);
}
}
}

View File

@@ -35,6 +35,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.cloud.kubernetes.commons.EnvReader;
import org.springframework.cloud.kubernetes.fabric8.Fabric8PodUtils;
@SuppressWarnings("unchecked")
@@ -69,13 +70,13 @@ public class Fabric8PodUtilsTest {
private final PodResource<Pod, DoneablePod> podResource = Mockito.mock(PodResource.class);
private MockedStatic<Fabric8PodUtils.EnvReader> envReader;
private MockedStatic<EnvReader> envReader;
private MockedStatic<Paths> paths;
@BeforeEach
public void before() {
envReader = Mockito.mockStatic(Fabric8PodUtils.EnvReader.class);
envReader = Mockito.mockStatic(EnvReader.class);
paths = Mockito.mockStatic(Paths.class);
}
@@ -148,11 +149,11 @@ public class Fabric8PodUtilsTest {
}
private void mockHost(String host) {
envReader.when(() -> Fabric8PodUtils.EnvReader.getEnv(KUBERNETES_SERVICE_HOST)).thenReturn(host);
envReader.when(() -> EnvReader.getEnv(KUBERNETES_SERVICE_HOST)).thenReturn(host);
}
private void mockHostname(String name) {
envReader.when(() -> Fabric8PodUtils.EnvReader.getEnv(HOSTNAME)).thenReturn(name);
envReader.when(() -> EnvReader.getEnv(HOSTNAME)).thenReturn(name);
}
private void mockTokenPath(boolean result) {