3434import org .junit .Rule ;
3535import org .junit .Test ;
3636import org .junit .contrib .java .lang .system .EnvironmentVariables ;
37+ import static com .github .stefanbirkner .systemlambda .SystemLambda .withEnvironmentVariable ;
3738
3839/** Tests for the ConfigBuilder helper class */
3940public 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