Skip to content

Commit 14cc2b3

Browse files
authored
test: added tests for loading proxy URLs in KubernetesClient Config
Add tests to ensure that current precedence of loading proxy Urls from various sources is preserved: - proxy-url in cluster of kubeconfig (not supported) - `all.proxy` System Property - `http.proxy` System Property - `configBuilder.withHttpProxy()` in ConfigBuilder Signed-off-by: Rohan Kumar <[email protected]>
1 parent 7b728e7 commit 14cc2b3

File tree

2 files changed

+291
-0
lines changed

2 files changed

+291
-0
lines changed
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
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+
* http://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+
package io.fabric8.kubernetes.client;
17+
18+
import io.fabric8.kubernetes.client.utils.Utils;
19+
import org.junit.jupiter.api.AfterEach;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Disabled;
22+
import org.junit.jupiter.api.DisplayName;
23+
import org.junit.jupiter.api.Nested;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
27+
28+
class ConfigProxySourceTest {
29+
@Nested
30+
@DisplayName("http.proxy System property")
31+
class HttpProxySystemProperty {
32+
@BeforeEach
33+
void setup() {
34+
System.setProperty("http.proxy", "http://proxy-via-http-proxy-property:3128");
35+
}
36+
37+
@AfterEach
38+
void tearDown() {
39+
System.clearProperty("http.proxy");
40+
}
41+
42+
@Test
43+
@DisplayName("no other proxy configuration, then http.proxy property takes precedence")
44+
void noOtherConfiguration() {
45+
assertThat(new ConfigBuilder().build())
46+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-http-proxy-property:3128")
47+
.hasFieldOrPropertyWithValue("httpsProxy", null);
48+
}
49+
50+
@Test
51+
@DisplayName("all.proxy property configuration, then http.proxy property takes precedence")
52+
void allProxySystemProperty() {
53+
try {
54+
System.setProperty("all.proxy", "http://proxy-via-all-proxy-property:3128");
55+
56+
assertThat(new ConfigBuilder().build())
57+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-http-proxy-property:3128")
58+
.hasFieldOrPropertyWithValue("httpsProxy", "http://proxy-via-all-proxy-property:3128");
59+
} finally {
60+
System.clearProperty("all.proxy");
61+
}
62+
}
63+
64+
@Test
65+
@DisplayName("user configures proxy via builder, then user configuration via builder takes precedence")
66+
void httpProxyViaBuilder() {
67+
assertThat(new ConfigBuilder().withHttpProxy("http://proxy-via-builder:3128").build())
68+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-builder:3128")
69+
.hasFieldOrPropertyWithValue("httpsProxy", null);
70+
}
71+
72+
@Test
73+
@DisplayName("kubeconfig cluster has proxy-url, then http.proxy property takes precedence")
74+
void kubeConfigProxyUrl() {
75+
try {
76+
// Given
77+
System.setProperty("kubeconfig", Utils
78+
.filePath(ConfigTest.class.getResource("/config-proxy-source/kubeconfig-with-proxy-url")));
79+
// When + Then
80+
assertThat(new ConfigBuilder().build())
81+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-http-proxy-property:3128")
82+
.hasFieldOrPropertyWithValue("httpsProxy", null);
83+
} finally {
84+
System.clearProperty("kubeconfig");
85+
}
86+
}
87+
}
88+
89+
@Nested
90+
@DisplayName("all.proxy System property")
91+
class AllProxySystemProperty {
92+
@BeforeEach
93+
void setup() {
94+
System.setProperty("all.proxy", "http://proxy-via-all-proxy-property:3128");
95+
}
96+
97+
@AfterEach
98+
void tearDown() {
99+
System.clearProperty("all.proxy");
100+
}
101+
102+
@Test
103+
@DisplayName("no other proxy configuration, then all.proxy property takes precedence")
104+
void noOtherConfiguration() {
105+
assertThat(new ConfigBuilder().build())
106+
.hasFieldOrPropertyWithValue("httpsProxy", "http://proxy-via-all-proxy-property:3128")
107+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-all-proxy-property:3128");
108+
}
109+
110+
@Test
111+
@DisplayName("http.proxy property configuration, then http.proxy property takes precedence")
112+
void allProxySystemProperty() {
113+
try {
114+
System.setProperty("http.proxy", "http://proxy-via-http-proxy-property:3128");
115+
116+
assertThat(new ConfigBuilder().build())
117+
.hasFieldOrPropertyWithValue("httpsProxy", "http://proxy-via-all-proxy-property:3128")
118+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-http-proxy-property:3128");
119+
} finally {
120+
System.clearProperty("http.proxy");
121+
}
122+
}
123+
124+
@Test
125+
@DisplayName("user configures proxy via builder, then user configuration via builder takes precedence")
126+
void httpProxyViaBuilder() {
127+
assertThat(new ConfigBuilder().withHttpProxy("http://proxy-via-builder:3128").build())
128+
.hasFieldOrPropertyWithValue("httpsProxy", "http://proxy-via-all-proxy-property:3128")
129+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-builder:3128");
130+
}
131+
132+
@Test
133+
@DisplayName("kubeconfig cluster has proxy-url, then all.proxy property takes precedence")
134+
void kubeConfigProxyUrl() {
135+
try {
136+
// Given
137+
System.setProperty("kubeconfig", Utils
138+
.filePath(ConfigTest.class.getResource("/config-proxy-source/kubeconfig-with-proxy-url")));
139+
// When + Then
140+
assertThat(new ConfigBuilder().build())
141+
.hasFieldOrPropertyWithValue("httpsProxy", "http://proxy-via-all-proxy-property:3128")
142+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-all-proxy-property:3128");
143+
} finally {
144+
System.clearProperty("kubeconfig");
145+
}
146+
}
147+
}
148+
149+
@Nested
150+
@DisplayName("withHttpProxy via builder")
151+
class ProxyViaBuilder {
152+
private ConfigBuilder configBuilder;
153+
154+
@BeforeEach
155+
void setUp() {
156+
configBuilder = new ConfigBuilder().withHttpProxy("http://proxy-via-builder:3128");
157+
}
158+
159+
@Test
160+
@DisplayName("no other proxy configuration, then user configuration via builder takes precedence")
161+
void noOtherConfiguration() {
162+
assertThat(configBuilder.build())
163+
.hasFieldOrPropertyWithValue("httpsProxy", null)
164+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-builder:3128");
165+
}
166+
167+
@Test
168+
@DisplayName("http.proxy property configuration, then user configuration via builder takes precedence")
169+
void httpProxySystemProperty() {
170+
try {
171+
System.setProperty("http.proxy", "http://proxy-via-http-proxy-property:3128");
172+
173+
assertThat(configBuilder.build())
174+
.hasFieldOrPropertyWithValue("httpsProxy", null)
175+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-builder:3128");
176+
} finally {
177+
System.clearProperty("http.proxy");
178+
}
179+
}
180+
181+
@Test
182+
@DisplayName("all.proxy property configuration, then user configuration via builder takes precedence")
183+
void allProxySystemProperty() {
184+
try {
185+
System.setProperty("all.proxy", "http://proxy-via-all-proxy-property:3128");
186+
187+
assertThat(configBuilder.build())
188+
.hasFieldOrPropertyWithValue("httpsProxy", "http://proxy-via-all-proxy-property:3128")
189+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-builder:3128");
190+
} finally {
191+
System.clearProperty("all.proxy");
192+
}
193+
}
194+
195+
@Test
196+
@DisplayName("kubeconfig cluster has proxy-url, then user configuration via builder takes precedence")
197+
void kubeConfigProxyUrl() {
198+
try {
199+
// Given
200+
System.setProperty("kubeconfig", Utils
201+
.filePath(ConfigTest.class.getResource("/config-proxy-source/kubeconfig-with-proxy-url")));
202+
// When + Then
203+
assertThat(configBuilder.build())
204+
.hasFieldOrPropertyWithValue("httpsProxy", null)
205+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-builder:3128");
206+
} finally {
207+
System.clearProperty("kubeconfig");
208+
}
209+
}
210+
}
211+
212+
@Nested
213+
@DisplayName("kubeconfig contains proxy-url in cluster configuration")
214+
class KubeConfigWithProxyUrl {
215+
@BeforeEach
216+
void setup() {
217+
System.setProperty("kubeconfig", Utils
218+
.filePath(ConfigTest.class.getResource("/config-proxy-source/kubeconfig-with-proxy-url")));
219+
}
220+
221+
@AfterEach
222+
void tearDown() {
223+
System.clearProperty("kubeconfig");
224+
}
225+
226+
@Test
227+
@Disabled("https://github.com/fabric8io/kubernetes-client/issues/6150")
228+
void noOtherConfiguration() {
229+
// When + Then
230+
assertThat(new ConfigBuilder().build())
231+
.hasFieldOrPropertyWithValue("httpProxy", null)
232+
.hasFieldOrPropertyWithValue("httpsProxy", "socks5://proxy-via-kubeconfig-proxy-url:1080");
233+
}
234+
235+
@Test
236+
@DisplayName("http.proxy property configuration, then http.proxy takes precedence")
237+
void httpProxySystemProperty() {
238+
try {
239+
System.setProperty("http.proxy", "http://proxy-via-http-proxy-property:3128");
240+
241+
assertThat(new ConfigBuilder().build())
242+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-http-proxy-property:3128")
243+
.hasFieldOrPropertyWithValue("httpsProxy", null);
244+
} finally {
245+
System.clearProperty("http.proxy");
246+
}
247+
}
248+
249+
@Test
250+
@DisplayName("all.proxy property configuration, then all.proxy takes precedence")
251+
void allProxySystemProperty() {
252+
try {
253+
System.setProperty("all.proxy", "http://proxy-via-all-proxy-property:3128");
254+
255+
assertThat(new ConfigBuilder().build())
256+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-all-proxy-property:3128")
257+
.hasFieldOrPropertyWithValue("httpsProxy", "http://proxy-via-all-proxy-property:3128");
258+
} finally {
259+
System.clearProperty("all.proxy");
260+
}
261+
}
262+
263+
@Test
264+
@DisplayName("user configures proxy via builder, then user configuration via builder takes precedence")
265+
void httpProxyViaBuilder() {
266+
assertThat(new ConfigBuilder().withHttpProxy("http://proxy-via-builder:3128").build())
267+
.hasFieldOrPropertyWithValue("httpProxy", "http://proxy-via-builder:3128")
268+
.hasFieldOrPropertyWithValue("httpsProxy", null);
269+
}
270+
}
271+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: v1
2+
clusters:
3+
- cluster:
4+
certificate-authority-data: LRMEMMW2
5+
server: https://kubernetes-remote-server.example:6443
6+
proxy-url: socks5://proxy-via-kubeconfig-proxy-url:1080
7+
name: default
8+
contexts:
9+
- context:
10+
cluster: default
11+
user: default
12+
name: default
13+
current-context: default
14+
kind: Config
15+
preferences: {}
16+
users:
17+
- name: default
18+
user:
19+
client-certificate-data: LS0tLS1CR==
20+
client-key-data: LS0tLS1CRUdJT=

0 commit comments

Comments
 (0)