12
12
*/
13
13
package io .kubernetes .client .util ;
14
14
15
+ import static com .github .stefanbirkner .systemlambda .SystemLambda .withEnvironmentVariable ;
15
16
import static io .kubernetes .client .util .Config .ENV_SERVICE_HOST ;
16
17
import static io .kubernetes .client .util .Config .ENV_SERVICE_PORT ;
17
18
import static org .hamcrest .MatcherAssert .assertThat ;
30
31
import java .io .IOException ;
31
32
import java .nio .file .Files ;
32
33
import java .nio .file .Paths ;
33
- import org .junit .Before ;
34
- import org .junit .Rule ;
35
34
import org .junit .Test ;
36
- import org .junit .contrib .java .lang .system .EnvironmentVariables ;
37
- import static com .github .stefanbirkner .systemlambda .SystemLambda .withEnvironmentVariable ;
38
35
39
36
/** Tests for the ConfigBuilder helper class */
40
37
public class ClientBuilderTest {
@@ -60,115 +57,127 @@ public class ClientBuilderTest {
60
57
public static final String KUBEDIR = ".kube" ;
61
58
public static final String KUBECONFIG = "config" ;
62
59
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
-
71
60
@ Test
72
61
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
+ });
79
70
assertEquals ("http://localhost:8080" , path );
80
71
}
81
72
82
73
@ Test
83
74
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
+ });
89
82
assertEquals ("http://home.dir.com" , path );
90
83
}
91
84
92
85
@ Test
93
86
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
+ });
99
94
assertEquals ("http://kubeconfig.dir.com" , path );
100
95
}
101
96
102
97
@ Test
103
98
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
+ });
109
106
assertEquals ("http://kubeconfig.dir.com" , path );
110
107
}
111
108
112
109
@ Test
113
110
public void testDefaultClientReadsKubeConfigMultiple () throws Exception {
114
111
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
+ });
120
119
assertEquals ("http://kubeconfig.dir.com" , path );
121
120
}
122
121
123
122
@ Test
124
123
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
+ });
131
132
// $KUBECONFIG should take precedence over $HOME/.kube/config
132
133
assertEquals ("http://kubeconfig.dir.com" , path );
133
134
}
134
135
135
136
@ Test
136
137
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
+ });
142
145
assertThat (path , is (Config .DEFAULT_FALLBACK_HOST ));
143
146
}
144
147
145
148
@ Test
146
149
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
+ });
152
157
assertThat (path , is ("https://localhost:443" ));
153
158
}
154
159
155
160
@ Test
156
161
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
+ });
162
169
assertThat (path , is ("http://localhost" ));
163
170
}
164
171
165
172
@ Test
166
173
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
+ });
172
181
assertThat (isVerifyingSsl , is (false ));
173
182
}
174
183
@@ -180,11 +189,13 @@ public void testBasePathTrailingSlash() throws Exception {
180
189
181
190
@ Test
182
191
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
+ });
188
199
assertThat (isVerifyingSsl , is (true ));
189
200
}
190
201
@@ -218,51 +229,57 @@ public void testSslCertCaBad() throws Exception {
218
229
219
230
@ Test
220
231
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
+ });
228
241
assertEquals (path , "http://home.dir.com" );
229
242
}
230
243
231
244
@ Test
232
245
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
+ });
247
262
assertEquals (path , "https://127.0.0.1:6443" );
248
263
}
249
264
250
265
@ Test
251
266
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
+ });
266
283
assertEquals (path , "https://[::1]:6443" );
267
284
}
268
285
0 commit comments