Skip to content

Commit 81335ce

Browse files
brendandburnsyue9944882
authored andcommitted
Change the env variables implementation for tests.
1 parent 63de2d2 commit 81335ce

File tree

11 files changed

+142
-113
lines changed

11 files changed

+142
-113
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@
262262
</dependency>
263263
<dependency>
264264
<groupId>com.github.stefanbirkner</groupId>
265-
<artifactId>system-rules</artifactId>
266-
<version>1.19.0</version>
265+
<artifactId>system-lambda</artifactId>
266+
<version>1.2.0</version>
267267
<scope>test</scope>
268268
</dependency>
269269
<dependency>

util/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
</dependency>
9898
<dependency>
9999
<groupId>com.github.stefanbirkner</groupId>
100-
<artifactId>system-rules</artifactId>
100+
<artifactId>system-lambda</artifactId>
101101
<scope>test</scope>
102102
</dependency>
103103
<dependency>

util/src/test/java/io/kubernetes/client/informer/cache/CacheTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@
2626
import java.util.function.Function;
2727
import org.junit.Rule;
2828
import org.junit.Test;
29-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
3029
import org.junit.runner.RunWith;
3130
import org.junit.runners.Parameterized;
3231

3332
@RunWith(Parameterized.class)
3433
public class CacheTest {
3534

36-
@Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
37-
3835
private static String mockIndexName = "mock";
3936

4037
private static List<String> mockIndexFunc(Object obj) {

util/src/test/java/io/kubernetes/client/informer/cache/ControllerTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@
2626
import java.util.concurrent.atomic.AtomicInteger;
2727
import org.junit.Rule;
2828
import org.junit.Test;
29-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
3029

3130
public class ControllerTest {
3231

33-
@Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
34-
3532
@Test
3633
public void testControllerProcessDeltas() throws InterruptedException {
3734

util/src/test/java/io/kubernetes/client/informer/cache/DeltaFIFOTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@
2525
import org.apache.commons.lang3.tuple.MutablePair;
2626
import org.junit.Rule;
2727
import org.junit.Test;
28-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
2928

3029
public class DeltaFIFOTest {
31-
32-
@Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
33-
3430
@Test
3531
public void testDeltaFIFOBasic() throws InterruptedException {
3632
Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>> receivingDeltas = new LinkedList<>();

util/src/test/java/io/kubernetes/client/informer/cache/ListerTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
import java.util.List;
2121
import org.junit.Rule;
2222
import org.junit.Test;
23-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
2423

2524
public class ListerTest {
26-
@Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
27-
2825
@Test
2926
public void testListerBasic() {
3027
Cache<V1Pod> podCache = new Cache<>();

util/src/test/java/io/kubernetes/client/informer/cache/ProcessorListenerTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919
import io.kubernetes.client.openapi.models.V1Pod;
2020
import org.junit.Rule;
2121
import org.junit.Test;
22-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
2322

2423
public class ProcessorListenerTest {
2524

26-
@Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
27-
2825
private static boolean addNotificationReceived;
2926
private static boolean updateNotificationReceived;
3027
private static boolean deleteNotificationReceived;

util/src/test/java/io/kubernetes/client/informer/cache/ReflectorRunnableTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@
3636
import org.hamcrest.core.IsEqual;
3737
import org.junit.Rule;
3838
import org.junit.Test;
39-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
4039
import org.junit.runner.RunWith;
4140
import org.mockito.Mock;
4241
import org.mockito.junit.MockitoJUnitRunner;
4342

4443
@RunWith(MockitoJUnitRunner.class)
4544
public class ReflectorRunnableTest {
4645

47-
@Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
48-
4946
@Mock private DeltaFIFO deltaFIFO;
5047

5148
@Mock private ListerWatcher<V1Pod, V1PodList> listerWatcher;

util/src/test/java/io/kubernetes/client/informer/cache/SharedProcessorTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@
2222
import java.util.concurrent.Executors;
2323
import org.junit.Rule;
2424
import org.junit.Test;
25-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
2625

2726
public class SharedProcessorTest {
2827

29-
@Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
30-
3128
@Test
3229
public void testListenerAddition() throws InterruptedException {
3330

util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java

Lines changed: 111 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.junit.Rule;
3535
import org.junit.Test;
3636
import org.junit.contrib.java.lang.system.EnvironmentVariables;
37+
import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
3738

3839
/** Tests for the ConfigBuilder helper class */
3940
public class ClientBuilderTest {
@@ -68,76 +69,107 @@ public void setup() {
6869
}
6970

7071
@Test
71-
public void testDefaultClientWithNoFiles() throws IOException {
72-
environmentVariables.set("HOME", "/non-existent");
73-
final ApiClient client = ClientBuilder.defaultClient();
74-
assertEquals("http://localhost:8080", client.getBasePath());
72+
public void testDefaultClientWithNoFiles() throws Exception {
73+
String path = withEnvironmentVariable("HOME", "/non-existent")
74+
.execute(() -> {
75+
environmentVariables.set("HOME", "/non-existent");
76+
final ApiClient client = ClientBuilder.defaultClient();
77+
return client.getBasePath();
78+
});
79+
assertEquals("http://localhost:8080", path);
7580
}
7681

7782
@Test
7883
public void testDefaultClientReadsHomeDir() throws Exception {
79-
environmentVariables.set("HOME", HOME_PATH);
80-
ApiClient client = ClientBuilder.defaultClient();
81-
assertEquals("http://home.dir.com", client.getBasePath());
84+
String path = withEnvironmentVariable("HOME", HOME_PATH)
85+
.execute(() -> {
86+
ApiClient client = ClientBuilder.defaultClient();
87+
return client.getBasePath();
88+
});
89+
assertEquals("http://home.dir.com", path);
8290
}
8391

8492
@Test
8593
public void testDefaultClientReadsKubeConfig() throws Exception {
86-
environmentVariables.set("KUBECONFIG", KUBECONFIG_FILE_PATH);
87-
final ApiClient client = ClientBuilder.defaultClient();
88-
assertEquals("http://kubeconfig.dir.com", client.getBasePath());
94+
String path = withEnvironmentVariable("KUBECONFIG", KUBECONFIG_FILE_PATH)
95+
.execute(() -> {
96+
final ApiClient client = ClientBuilder.defaultClient();
97+
return client.getBasePath();
98+
});
99+
assertEquals("http://kubeconfig.dir.com", path);
89100
}
90101

91102
@Test
92103
public void testDefaultClientUTF8EncodedConfig() throws Exception {
93-
environmentVariables.set("KUBECONFIG", KUBECONFIG_UTF8_FILE_PATH);
94-
final ApiClient client = ClientBuilder.defaultClient();
95-
assertEquals("http://kubeconfig.dir.com", client.getBasePath());
104+
String path = withEnvironmentVariable("KUBECONFIG", KUBECONFIG_UTF8_FILE_PATH)
105+
.execute(() -> {
106+
final ApiClient client = ClientBuilder.defaultClient();
107+
return client.getBasePath();
108+
});
109+
assertEquals("http://kubeconfig.dir.com", path);
96110
}
97111

98112
@Test
99113
public void testDefaultClientReadsKubeConfigMultiple() throws Exception {
100114
final String kubeConfigEnv = KUBECONFIG_FILE_PATH + File.pathSeparator + "/non-existent";
101-
environmentVariables.set("KUBECONFIG", kubeConfigEnv);
102-
final ApiClient client = ClientBuilder.defaultClient();
103-
assertEquals("http://kubeconfig.dir.com", client.getBasePath());
115+
String path = withEnvironmentVariable("KUBECONFIG", kubeConfigEnv)
116+
.execute(() -> {
117+
final ApiClient client = ClientBuilder.defaultClient();
118+
return client.getBasePath();
119+
});
120+
assertEquals("http://kubeconfig.dir.com", path);
104121
}
105122

106123
@Test
107124
public void testKubeconfigPreferredOverHomeDir() throws Exception {
108-
environmentVariables.set("HOME", HOME_PATH);
109-
environmentVariables.set("KUBECONFIG", KUBECONFIG_FILE_PATH);
110-
final ApiClient client = ClientBuilder.standard().build();
125+
String path = withEnvironmentVariable("HOME", HOME_PATH)
126+
.and("KUBECONFIG", KUBECONFIG_FILE_PATH)
127+
.execute(() -> {
128+
final ApiClient client = ClientBuilder.standard().build();
129+
return client.getBasePath();
130+
});
111131
// $KUBECONFIG should take precedence over $HOME/.kube/config
112-
assertEquals("http://kubeconfig.dir.com", client.getBasePath());
132+
assertEquals("http://kubeconfig.dir.com", path);
113133
}
114134

115135
@Test
116136
public void testInvalidKubeconfig() throws Exception {
117-
environmentVariables.set("KUBECONFIG", "/non-existent");
118-
final ApiClient client = ClientBuilder.standard().build();
119-
assertThat(client.getBasePath(), is(Config.DEFAULT_FALLBACK_HOST));
137+
String path = withEnvironmentVariable("KUBECONFIG", "/non-existent")
138+
.execute(() -> {
139+
final ApiClient client = ClientBuilder.standard().build();
140+
return client.getBasePath();
141+
});
142+
assertThat(path, is(Config.DEFAULT_FALLBACK_HOST));
120143
}
121144

122145
@Test
123146
public void testKubeconfigAddsSchemeHttps() throws Exception {
124-
environmentVariables.set("KUBECONFIG", KUBECONFIG_HTTPS_FILE_PATH);
125-
final ApiClient client = ClientBuilder.standard().build();
126-
assertThat(client.getBasePath(), is("https://localhost:443"));
147+
String path = withEnvironmentVariable("KUBECONFIG", KUBECONFIG_HTTPS_FILE_PATH)
148+
.execute(() -> {
149+
final ApiClient client = ClientBuilder.standard().build();
150+
return client.getBasePath();
151+
});
152+
assertThat(path, is("https://localhost:443"));
127153
}
128154

129155
@Test
130156
public void testKubeconfigAddsSchemeHttp() throws Exception {
131-
environmentVariables.set("KUBECONFIG", KUBECONFIG_HTTP_FILE_PATH);
132-
final ApiClient client = ClientBuilder.standard().build();
133-
assertThat(client.getBasePath(), is("http://localhost"));
157+
String path = withEnvironmentVariable("KUBECONFIG", KUBECONFIG_HTTP_FILE_PATH)
158+
.execute(() -> {
159+
final ApiClient client = ClientBuilder.standard().build();
160+
return client.getBasePath();
161+
});
162+
assertThat(path, is("http://localhost"));
134163
}
135164

136165
@Test
137166
public void testKubeconfigDisablesVerifySsl() throws Exception {
138-
environmentVariables.set("KUBECONFIG", KUBECONFIG_HTTP_FILE_PATH);
139-
final ApiClient client = ClientBuilder.standard().build();
140-
assertThat(client.isVerifyingSsl(), is(false));
167+
boolean isVerifyingSsl = withEnvironmentVariable("KUBECONFIG", KUBECONFIG_HTTP_FILE_PATH)
168+
.execute(() -> {
169+
final ApiClient client = ClientBuilder.standard().build();
170+
return client.isVerifyingSsl();
171+
});
172+
assertThat(isVerifyingSsl, is(false));
141173
}
142174

143175
@Test
@@ -147,10 +179,13 @@ public void testBasePathTrailingSlash() throws Exception {
147179
}
148180

149181
@Test
150-
public void testStandardVerifiesSsl() throws IOException {
151-
environmentVariables.set("HOME", "/non-existent");
152-
final ApiClient client = ClientBuilder.standard().build();
153-
assertThat(client.isVerifyingSsl(), is(true));
182+
public void testStandardVerifiesSsl() throws Exception {
183+
boolean isVerifyingSsl = withEnvironmentVariable("HOME", "/non-existent")
184+
.execute(() -> {
185+
final ApiClient client = ClientBuilder.standard().build();
186+
return client.isVerifyingSsl();
187+
});
188+
assertThat(isVerifyingSsl, is(true));
154189
}
155190

156191
@Test
@@ -183,45 +218,52 @@ public void testSslCertCaBad() throws Exception {
183218

184219
@Test
185220
public void testHomeDirPreferredOverKubeConfig() throws Exception {
186-
environmentVariables.set("HOME", HOME_PATH);
187-
environmentVariables.set("KUBEDIR", KUBEDIR);
188-
environmentVariables.set("KUBECONFIG", KUBECONFIG);
189-
final ApiClient client = ClientBuilder.standard().build();
190-
assertEquals("http://home.dir.com", client.getBasePath());
221+
String path = withEnvironmentVariable("HOME", HOME_PATH)
222+
.and("KUBEDIR", KUBEDIR)
223+
.and("KUBECONFIG", KUBECONFIG)
224+
.execute(() -> {
225+
final ApiClient client = ClientBuilder.standard().build();
226+
return client.getBasePath();
227+
});
228+
assertEquals(path, "http://home.dir.com");
191229
}
192230

193231
@Test
194-
public void testIPv4AddressParsingShouldWork() {
195-
environmentVariables.set(ENV_SERVICE_HOST, "127.0.0.1");
196-
environmentVariables.set(ENV_SERVICE_PORT, "6443");
197-
String ipv4Host = "127.0.0.1";
198-
String port = "6443";
199-
ClientBuilder builder =
200-
new ClientBuilder() {
201-
@Override
202-
public ClientBuilder setBasePath(String host, String port) {
203-
return super.setBasePath(host, port);
204-
}
205-
}.setBasePath(ipv4Host, port);
206-
207-
assertEquals("https://127.0.0.1:6443", builder.getBasePath());
232+
public void testIPv4AddressParsingShouldWork() throws Exception {
233+
String path = withEnvironmentVariable(ENV_SERVICE_HOST, "127.0.0.1")
234+
.and(ENV_SERVICE_PORT, "6443")
235+
.execute(() -> {
236+
String ipv4Host = "127.0.0.1";
237+
String port = "6443";
238+
ClientBuilder builder =
239+
new ClientBuilder() {
240+
@Override
241+
public ClientBuilder setBasePath(String host, String port) {
242+
return super.setBasePath(host, port);
243+
}
244+
}.setBasePath(ipv4Host, port);
245+
return builder.getBasePath();
246+
});
247+
assertEquals(path, "https://127.0.0.1:6443");
208248
}
209249

210250
@Test
211-
public void testIPv6AddressParsingShouldWork() {
212-
environmentVariables.set(ENV_SERVICE_HOST, "127.0.0.1");
213-
environmentVariables.set(ENV_SERVICE_PORT, "6443");
214-
String ipv4Host = "::1";
215-
String port = "6443";
216-
ClientBuilder builder =
217-
new ClientBuilder() {
218-
@Override
219-
public ClientBuilder setBasePath(String host, String port) {
220-
return super.setBasePath(host, port);
221-
}
222-
}.setBasePath(ipv4Host, port);
223-
224-
assertEquals("https://[::1]:6443", builder.getBasePath());
251+
public void testIPv6AddressParsingShouldWork() throws Exception {
252+
String path = withEnvironmentVariable(ENV_SERVICE_HOST, "127.0.0.1")
253+
.and(ENV_SERVICE_PORT, "6443")
254+
.execute(() -> {
255+
String ipv4Host = "::1";
256+
String port = "6443";
257+
ClientBuilder builder =
258+
new ClientBuilder() {
259+
@Override
260+
public ClientBuilder setBasePath(String host, String port) {
261+
return super.setBasePath(host, port);
262+
}
263+
}.setBasePath(ipv4Host, port);
264+
return builder.getBasePath();
265+
});
266+
assertEquals(path, "https://[::1]:6443");
225267
}
226268

227269
@Test

0 commit comments

Comments
 (0)