34
34
import org .junit .Rule ;
35
35
import org .junit .Test ;
36
36
import org .junit .contrib .java .lang .system .EnvironmentVariables ;
37
+ import static com .github .stefanbirkner .systemlambda .SystemLambda .withEnvironmentVariable ;
37
38
38
39
/** Tests for the ConfigBuilder helper class */
39
40
public class ClientBuilderTest {
@@ -68,76 +69,107 @@ public void setup() {
68
69
}
69
70
70
71
@ 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 );
75
80
}
76
81
77
82
@ Test
78
83
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 );
82
90
}
83
91
84
92
@ Test
85
93
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 );
89
100
}
90
101
91
102
@ Test
92
103
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 );
96
110
}
97
111
98
112
@ Test
99
113
public void testDefaultClientReadsKubeConfigMultiple () throws Exception {
100
114
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 );
104
121
}
105
122
106
123
@ Test
107
124
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
+ });
111
131
// $KUBECONFIG should take precedence over $HOME/.kube/config
112
- assertEquals ("http://kubeconfig.dir.com" , client . getBasePath () );
132
+ assertEquals ("http://kubeconfig.dir.com" , path );
113
133
}
114
134
115
135
@ Test
116
136
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 ));
120
143
}
121
144
122
145
@ Test
123
146
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" ));
127
153
}
128
154
129
155
@ Test
130
156
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" ));
134
163
}
135
164
136
165
@ Test
137
166
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 ));
141
173
}
142
174
143
175
@ Test
@@ -147,10 +179,13 @@ public void testBasePathTrailingSlash() throws Exception {
147
179
}
148
180
149
181
@ 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 ));
154
189
}
155
190
156
191
@ Test
@@ -183,45 +218,52 @@ public void testSslCertCaBad() throws Exception {
183
218
184
219
@ Test
185
220
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" );
191
229
}
192
230
193
231
@ 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" );
208
248
}
209
249
210
250
@ 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" );
225
267
}
226
268
227
269
@ Test
0 commit comments