1212*/
1313package io .kubernetes .client .util ;
1414
15+ import static com .github .stefanbirkner .systemlambda .SystemLambda .withEnvironmentVariable ;
1516import static io .kubernetes .client .util .Config .ENV_SERVICE_HOST ;
1617import static io .kubernetes .client .util .Config .ENV_SERVICE_PORT ;
1718import static org .hamcrest .MatcherAssert .assertThat ;
3031import java .io .IOException ;
3132import java .nio .file .Files ;
3233import java .nio .file .Paths ;
33- import org .junit .Before ;
34- import org .junit .Rule ;
3534import org .junit .Test ;
36- import org .junit .contrib .java .lang .system .EnvironmentVariables ;
37- import static com .github .stefanbirkner .systemlambda .SystemLambda .withEnvironmentVariable ;
3835
3936/** Tests for the ConfigBuilder helper class */
4037public class ClientBuilderTest {
@@ -60,115 +57,127 @@ public class ClientBuilderTest {
6057 public static final String KUBEDIR = ".kube" ;
6158 public static final String KUBECONFIG = "config" ;
6259
63- @ Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables ();
64-
65- @ Before
66- public void setup () {
67- environmentVariables .set ("HOME" , "/non-existent" );
68- environmentVariables .set ("KUBECONFIG" , null );
69- }
70-
7160 @ Test
7261 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- });
62+ String path =
63+ withEnvironmentVariable ("HOME" , "/non-existent" )
64+ .and ("KUBECONFIG" , null )
65+ .execute (
66+ () -> {
67+ final ApiClient client = ClientBuilder .defaultClient ();
68+ return client .getBasePath ();
69+ });
7970 assertEquals ("http://localhost:8080" , path );
8071 }
8172
8273 @ Test
8374 public void testDefaultClientReadsHomeDir () throws Exception {
84- String path = withEnvironmentVariable ("HOME" , HOME_PATH )
85- .execute (() -> {
86- ApiClient client = ClientBuilder .defaultClient ();
87- return client .getBasePath ();
88- });
75+ String path =
76+ withEnvironmentVariable ("HOME" , HOME_PATH )
77+ .execute (
78+ () -> {
79+ ApiClient client = ClientBuilder .defaultClient ();
80+ return client .getBasePath ();
81+ });
8982 assertEquals ("http://home.dir.com" , path );
9083 }
9184
9285 @ Test
9386 public void testDefaultClientReadsKubeConfig () throws Exception {
94- String path = withEnvironmentVariable ("KUBECONFIG" , KUBECONFIG_FILE_PATH )
95- .execute (() -> {
96- final ApiClient client = ClientBuilder .defaultClient ();
97- return client .getBasePath ();
98- });
87+ String path =
88+ withEnvironmentVariable ("KUBECONFIG" , KUBECONFIG_FILE_PATH )
89+ .execute (
90+ () -> {
91+ final ApiClient client = ClientBuilder .defaultClient ();
92+ return client .getBasePath ();
93+ });
9994 assertEquals ("http://kubeconfig.dir.com" , path );
10095 }
10196
10297 @ Test
10398 public void testDefaultClientUTF8EncodedConfig () throws Exception {
104- String path = withEnvironmentVariable ("KUBECONFIG" , KUBECONFIG_UTF8_FILE_PATH )
105- .execute (() -> {
106- final ApiClient client = ClientBuilder .defaultClient ();
107- return client .getBasePath ();
108- });
99+ String path =
100+ withEnvironmentVariable ("KUBECONFIG" , KUBECONFIG_UTF8_FILE_PATH )
101+ .execute (
102+ () -> {
103+ final ApiClient client = ClientBuilder .defaultClient ();
104+ return client .getBasePath ();
105+ });
109106 assertEquals ("http://kubeconfig.dir.com" , path );
110107 }
111108
112109 @ Test
113110 public void testDefaultClientReadsKubeConfigMultiple () throws Exception {
114111 final String kubeConfigEnv = KUBECONFIG_FILE_PATH + File .pathSeparator + "/non-existent" ;
115- String path = withEnvironmentVariable ("KUBECONFIG" , kubeConfigEnv )
116- .execute (() -> {
117- final ApiClient client = ClientBuilder .defaultClient ();
118- return client .getBasePath ();
119- });
112+ String path =
113+ withEnvironmentVariable ("KUBECONFIG" , kubeConfigEnv )
114+ .execute (
115+ () -> {
116+ final ApiClient client = ClientBuilder .defaultClient ();
117+ return client .getBasePath ();
118+ });
120119 assertEquals ("http://kubeconfig.dir.com" , path );
121120 }
122121
123122 @ Test
124123 public void testKubeconfigPreferredOverHomeDir () throws Exception {
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- });
124+ String path =
125+ withEnvironmentVariable ("HOME" , HOME_PATH )
126+ .and ("KUBECONFIG" , KUBECONFIG_FILE_PATH )
127+ .execute (
128+ () -> {
129+ final ApiClient client = ClientBuilder .standard ().build ();
130+ return client .getBasePath ();
131+ });
131132 // $KUBECONFIG should take precedence over $HOME/.kube/config
132133 assertEquals ("http://kubeconfig.dir.com" , path );
133134 }
134135
135136 @ Test
136137 public void testInvalidKubeconfig () throws Exception {
137- String path = withEnvironmentVariable ("KUBECONFIG" , "/non-existent" )
138- .execute (() -> {
139- final ApiClient client = ClientBuilder .standard ().build ();
140- return client .getBasePath ();
141- });
138+ String path =
139+ withEnvironmentVariable ("KUBECONFIG" , "/non-existent" )
140+ .execute (
141+ () -> {
142+ final ApiClient client = ClientBuilder .standard ().build ();
143+ return client .getBasePath ();
144+ });
142145 assertThat (path , is (Config .DEFAULT_FALLBACK_HOST ));
143146 }
144147
145148 @ Test
146149 public void testKubeconfigAddsSchemeHttps () throws Exception {
147- String path = withEnvironmentVariable ("KUBECONFIG" , KUBECONFIG_HTTPS_FILE_PATH )
148- .execute (() -> {
149- final ApiClient client = ClientBuilder .standard ().build ();
150- return client .getBasePath ();
151- });
150+ String path =
151+ withEnvironmentVariable ("KUBECONFIG" , KUBECONFIG_HTTPS_FILE_PATH )
152+ .execute (
153+ () -> {
154+ final ApiClient client = ClientBuilder .standard ().build ();
155+ return client .getBasePath ();
156+ });
152157 assertThat (path , is ("https://localhost:443" ));
153158 }
154159
155160 @ Test
156161 public void testKubeconfigAddsSchemeHttp () throws Exception {
157- String path = withEnvironmentVariable ("KUBECONFIG" , KUBECONFIG_HTTP_FILE_PATH )
158- .execute (() -> {
159- final ApiClient client = ClientBuilder .standard ().build ();
160- return client .getBasePath ();
161- });
162+ String path =
163+ withEnvironmentVariable ("KUBECONFIG" , KUBECONFIG_HTTP_FILE_PATH )
164+ .execute (
165+ () -> {
166+ final ApiClient client = ClientBuilder .standard ().build ();
167+ return client .getBasePath ();
168+ });
162169 assertThat (path , is ("http://localhost" ));
163170 }
164171
165172 @ Test
166173 public void testKubeconfigDisablesVerifySsl () throws Exception {
167- boolean isVerifyingSsl = withEnvironmentVariable ("KUBECONFIG" , KUBECONFIG_HTTP_FILE_PATH )
168- .execute (() -> {
169- final ApiClient client = ClientBuilder .standard ().build ();
170- return client .isVerifyingSsl ();
171- });
174+ boolean isVerifyingSsl =
175+ withEnvironmentVariable ("KUBECONFIG" , KUBECONFIG_HTTP_FILE_PATH )
176+ .execute (
177+ () -> {
178+ final ApiClient client = ClientBuilder .standard ().build ();
179+ return client .isVerifyingSsl ();
180+ });
172181 assertThat (isVerifyingSsl , is (false ));
173182 }
174183
@@ -180,11 +189,13 @@ public void testBasePathTrailingSlash() throws Exception {
180189
181190 @ Test
182191 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- });
192+ boolean isVerifyingSsl =
193+ withEnvironmentVariable ("HOME" , "/non-existent" )
194+ .execute (
195+ () -> {
196+ final ApiClient client = ClientBuilder .standard ().build ();
197+ return client .isVerifyingSsl ();
198+ });
188199 assertThat (isVerifyingSsl , is (true ));
189200 }
190201
@@ -218,51 +229,57 @@ public void testSslCertCaBad() throws Exception {
218229
219230 @ Test
220231 public void testHomeDirPreferredOverKubeConfig () throws Exception {
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- });
232+ String path =
233+ withEnvironmentVariable ("HOME" , HOME_PATH )
234+ .and ("KUBEDIR" , KUBEDIR )
235+ .and ("KUBECONFIG" , KUBECONFIG )
236+ .execute (
237+ () -> {
238+ final ApiClient client = ClientBuilder .standard ().build ();
239+ return client .getBasePath ();
240+ });
228241 assertEquals (path , "http://home.dir.com" );
229242 }
230243
231244 @ Test
232245 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- });
246+ String path =
247+ withEnvironmentVariable (ENV_SERVICE_HOST , "127.0.0.1" )
248+ .and (ENV_SERVICE_PORT , "6443" )
249+ .execute (
250+ () -> {
251+ String ipv4Host = "127.0.0.1" ;
252+ String port = "6443" ;
253+ ClientBuilder builder =
254+ new ClientBuilder () {
255+ @ Override
256+ public ClientBuilder setBasePath (String host , String port ) {
257+ return super .setBasePath (host , port );
258+ }
259+ }.setBasePath (ipv4Host , port );
260+ return builder .getBasePath ();
261+ });
247262 assertEquals (path , "https://127.0.0.1:6443" );
248263 }
249264
250265 @ Test
251266 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- });
267+ String path =
268+ withEnvironmentVariable (ENV_SERVICE_HOST , "127.0.0.1" )
269+ .and (ENV_SERVICE_PORT , "6443" )
270+ .execute (
271+ () -> {
272+ String ipv4Host = "::1" ;
273+ String port = "6443" ;
274+ ClientBuilder builder =
275+ new ClientBuilder () {
276+ @ Override
277+ public ClientBuilder setBasePath (String host , String port ) {
278+ return super .setBasePath (host , port );
279+ }
280+ }.setBasePath (ipv4Host , port );
281+ return builder .getBasePath ();
282+ });
266283 assertEquals (path , "https://[::1]:6443" );
267284 }
268285
0 commit comments