1616
1717package org .springframework .cloud .kubernetes .client .discovery ;
1818
19- import java .io . StringReader ;
19+ import java .util . List ;
2020
21+ import com .github .tomakehurst .wiremock .junit5 .WireMockExtension ;
2122import io .kubernetes .client .openapi .ApiClient ;
22- import io .kubernetes .client .util .Config ;
23- import org .junit .jupiter .api .AfterAll ;
23+ import io .kubernetes .client .util .ClientBuilder ;
24+ import org .junit .jupiter .api .AfterEach ;
2425import org .junit .jupiter .api .Test ;
25- import org .testcontainers . k3s . K3sContainer ;
26+ import org .junit . jupiter . api . extension . RegisterExtension ;
2627
2728import org .springframework .boot .autoconfigure .AutoConfigurations ;
2829import org .springframework .boot .health .contributor .HealthIndicator ;
3536import org .springframework .cloud .kubernetes .commons .KubernetesCommonsAutoConfiguration ;
3637import org .springframework .cloud .kubernetes .commons .discovery .KubernetesDiscoveryClientHealthIndicatorInitializer ;
3738import org .springframework .cloud .kubernetes .commons .discovery .KubernetesDiscoveryPropertiesAutoConfiguration ;
38- import org .springframework .cloud .kubernetes .integration .tests .commons .Commons ;
3939import org .springframework .context .annotation .Bean ;
4040import org .springframework .context .annotation .Configuration ;
4141import org .springframework .context .annotation .Primary ;
4242
43+ import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
4344import static org .assertj .core .api .Assertions .assertThat ;
4445import static org .springframework .cloud .kubernetes .client .discovery .TestUtils .assertInformerBeansMissing ;
4546import static org .springframework .cloud .kubernetes .client .discovery .TestUtils .assertInformerBeansPresent ;
47+ import static org .springframework .cloud .kubernetes .client .discovery .TestUtils .mockEndpointsAndServices ;
4648
4749/**
4850 * Test various conditionals for
@@ -54,15 +56,21 @@ class KubernetesClientInformerDiscoveryClientAutoConfigurationApplicationContext
5456
5557 private ApplicationContextRunner applicationContextRunner ;
5658
57- private static K3sContainer container ;
59+ @ RegisterExtension
60+ private static final WireMockExtension API_SERVER = WireMockExtension .newInstance ()
61+ .options (options ().dynamicPort ())
62+ .build ();
5863
59- @ AfterAll
60- static void afterAll () {
61- container . stop ();
64+ @ AfterEach
65+ void afterEach () {
66+ API_SERVER . resetAll ();
6267 }
6368
6469 @ Test
6570 void discoveryEnabledDefault () {
71+
72+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
73+
6674 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
6775 "spring.cloud.kubernetes.client.namespace=default" );
6876 applicationContextRunner .run (context -> {
@@ -82,6 +90,9 @@ void discoveryEnabledDefault() {
8290
8391 @ Test
8492 void discoveryEnabledDefaultWithSelectiveNamespaces () {
93+
94+ mockEndpointsAndServices (List .of ("a" , "b" , "c" ), API_SERVER );
95+
8596 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
8697 "spring.cloud.kubernetes.discovery.namespaces=a,b,c" );
8798 applicationContextRunner .run (context -> {
@@ -102,6 +113,9 @@ void discoveryEnabledDefaultWithSelectiveNamespaces() {
102113
103114 @ Test
104115 void discoveryEnabled () {
116+
117+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
118+
105119 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
106120 "spring.cloud.discovery.enabled=true" , "spring.cloud.kubernetes.client.namespace=default" );
107121 applicationContextRunner .run (context -> {
@@ -121,6 +135,9 @@ void discoveryEnabled() {
121135
122136 @ Test
123137 void discoveryEnabledWithSelectiveNamespaces () {
138+
139+ mockEndpointsAndServices (List .of ("a" , "b" ), API_SERVER );
140+
124141 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
125142 "spring.cloud.discovery.enabled=true" , "spring.cloud.kubernetes.discovery.namespaces=a,b" );
126143 applicationContextRunner .run (context -> {
@@ -155,6 +172,7 @@ void discoveryDisabled() {
155172
156173 @ Test
157174 void discoveryDisabledWithSelectiveNamespaces () {
175+
158176 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
159177 "spring.cloud.discovery.enabled=false" , "spring.cloud.kubernetes.discovery.namespaces=a,b" );
160178 applicationContextRunner .run (context -> {
@@ -185,6 +203,9 @@ void kubernetesDiscoveryEnabled() {
185203
186204 @ Test
187205 void kubernetesDiscoveryEnabledWithSelectiveNamespaces () {
206+
207+ mockEndpointsAndServices (List .of ("a" , "b" , "c" , "d" ), API_SERVER );
208+
188209 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
189210 "spring.cloud.kubernetes.discovery.enabled=true" ,
190211 "spring.cloud.kubernetes.discovery.namespaces=a,b,c,d" );
@@ -239,6 +260,9 @@ void kubernetesDiscoveryDisabledWithSelectiveNamespaces() {
239260
240261 @ Test
241262 void kubernetesDiscoveryBlockingEnabled () {
263+
264+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
265+
242266 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
243267 "spring.cloud.discovery.blocking.enabled=true" , "spring.cloud.kubernetes.client.namespace=default" );
244268 applicationContextRunner .run (context -> {
@@ -259,6 +283,9 @@ void kubernetesDiscoveryBlockingEnabled() {
259283
260284 @ Test
261285 void kubernetesDiscoveryBlockingEnabledWithSelectiveNamespaces () {
286+
287+ mockEndpointsAndServices (List .of ("a" ), API_SERVER );
288+
262289 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
263290 "spring.cloud.discovery.blocking.enabled=true" , "spring.cloud.kubernetes.discovery.namespaces=a" );
264291 applicationContextRunner .run (context -> {
@@ -279,6 +306,9 @@ void kubernetesDiscoveryBlockingEnabledWithSelectiveNamespaces() {
279306
280307 @ Test
281308 void kubernetesDiscoveryBlockingDisabled () {
309+
310+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
311+
282312 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
283313 "spring.cloud.discovery.blocking.enabled=false" , "spring.cloud.kubernetes.client.namespace=default" );
284314 applicationContextRunner .run (context -> {
@@ -298,6 +328,9 @@ void kubernetesDiscoveryBlockingDisabled() {
298328
299329 @ Test
300330 void kubernetesDiscoveryBlockingDisabledWithSelectiveNamespaces () {
331+
332+ mockEndpointsAndServices (List .of ("a" , "b" ), API_SERVER );
333+
301334 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
302335 "spring.cloud.discovery.blocking.enabled=false" , "spring.cloud.kubernetes.discovery.namespaces=a,b" );
303336 applicationContextRunner .run (context -> {
@@ -317,6 +350,9 @@ void kubernetesDiscoveryBlockingDisabledWithSelectiveNamespaces() {
317350
318351 @ Test
319352 void kubernetesDiscoveryHealthIndicatorEnabled () {
353+
354+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
355+
320356 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
321357 "spring.cloud.discovery.client.health-indicator.enabled=true" ,
322358 "spring.cloud.kubernetes.client.namespace=default" );
@@ -337,6 +373,9 @@ void kubernetesDiscoveryHealthIndicatorEnabled() {
337373
338374 @ Test
339375 void kubernetesDiscoveryHealthIndicatorEnabledWithSelectiveNamespaces () {
376+
377+ mockEndpointsAndServices (List .of ("b" ), API_SERVER );
378+
340379 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
341380 "spring.cloud.discovery.client.health-indicator.enabled=true" ,
342381 "spring.cloud.kubernetes.discovery.namespaces=b" );
@@ -357,6 +396,9 @@ void kubernetesDiscoveryHealthIndicatorEnabledWithSelectiveNamespaces() {
357396
358397 @ Test
359398 void kubernetesDiscoveryHealthIndicatorDisabled () {
399+
400+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
401+
360402 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
361403 "spring.cloud.discovery.client.health-indicator.enabled=false" ,
362404 "spring.cloud.kubernetes.client.namespace=default" );
@@ -373,6 +415,9 @@ void kubernetesDiscoveryHealthIndicatorDisabled() {
373415
374416 @ Test
375417 void kubernetesDiscoveryHealthIndicatorDisabledWithSelectiveNamespaces () {
418+
419+ mockEndpointsAndServices (List .of ("b" ), API_SERVER );
420+
376421 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
377422 "spring.cloud.discovery.client.health-indicator.enabled=false" ,
378423 "spring.cloud.kubernetes.discovery.namespaces=b" );
@@ -389,6 +434,9 @@ void kubernetesDiscoveryHealthIndicatorDisabledWithSelectiveNamespaces() {
389434
390435 @ Test
391436 void kubernetesDiscoveryHealthIndicatorEnabledHealthIndicatorMissing () {
437+
438+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
439+
392440 setupWithFilteredClassLoader (HealthIndicator .class , "spring.main.cloud-platform=KUBERNETES" ,
393441 "spring.cloud.config.enabled=false" , "spring.cloud.discovery.client.health-indicator.enabled=true" ,
394442 "spring.cloud.kubernetes.client.namespace=default" );
@@ -411,6 +459,9 @@ void kubernetesDiscoveryHealthIndicatorEnabledHealthIndicatorMissing() {
411459
412460 @ Test
413461 void kubernetesDiscoveryHealthIndicatorEnabledHealthIndicatorMissingWithSelectiveNamespaces () {
462+
463+ mockEndpointsAndServices (List .of ("a" , "b" , "c" , "d" , "e" ), API_SERVER );
464+
414465 setupWithFilteredClassLoader (HealthIndicator .class , "spring.main.cloud-platform=KUBERNETES" ,
415466 "spring.cloud.config.enabled=false" , "spring.cloud.discovery.client.health-indicator.enabled=true" ,
416467 "spring.cloud.kubernetes.discovery.namespaces=a,b,c,d,e" );
@@ -437,6 +488,9 @@ void kubernetesDiscoveryHealthIndicatorEnabledHealthIndicatorMissingWithSelectiv
437488 */
438489 @ Test
439490 void kubernetesDiscoveryReactiveCacheableEnabledBlockingDisabled () {
491+
492+ mockEndpointsAndServices (List .of ("a" , "b" ), API_SERVER );
493+
440494 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
441495 "spring.cloud.discovery.blocking.enabled=false" ,
442496 "spring.cloud.kubernetes.discovery.cacheable.reactive.enabled=true" ,
@@ -462,6 +516,9 @@ void kubernetesDiscoveryReactiveCacheableEnabledBlockingDisabled() {
462516 */
463517 @ Test
464518 void reactiveDisabled () {
519+
520+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
521+
465522 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
466523 "spring.cloud.discovery.reactive.enabled=false" , "spring.cloud.kubernetes.client.namespace=default" );
467524 applicationContextRunner .run (context -> {
@@ -480,6 +537,9 @@ void reactiveDisabled() {
480537 */
481538 @ Test
482539 void reactiveDisabledWithSelectiveNamespaces () {
540+
541+ mockEndpointsAndServices (List .of ("a" , "b" , "c" , "d" , "e" ), API_SERVER );
542+
483543 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
484544 "spring.cloud.discovery.reactive.enabled=false" ,
485545 "spring.cloud.kubernetes.discovery.namespaces=a,b,c,d,e" );
@@ -503,6 +563,9 @@ void reactiveDisabledWithSelectiveNamespaces() {
503563 */
504564 @ Test
505565 void blockingCacheableDefault () {
566+
567+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
568+
506569 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
507570 "spring.cloud.kubernetes.client.namespace=default" );
508571 applicationContextRunner .run (context -> {
@@ -520,6 +583,9 @@ void blockingCacheableDefault() {
520583 */
521584 @ Test
522585 void blockingCacheableDisabled () {
586+
587+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
588+
523589 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
524590 "spring.cloud.kubernetes.discovery.cacheable.blocking.enabled=false" ,
525591 "spring.cloud.kubernetes.client.namespace=default" );
@@ -538,6 +604,9 @@ void blockingCacheableDisabled() {
538604 */
539605 @ Test
540606 void blockingCacheableEnabled () {
607+
608+ mockEndpointsAndServices (List .of ("default" ), API_SERVER );
609+
541610 setup ("spring.main.cloud-platform=KUBERNETES" , "spring.cloud.config.enabled=false" ,
542611 "spring.cloud.kubernetes.discovery.cacheable.blocking.enabled=true" ,
543612 "spring.cloud.kubernetes.client.namespace=default" );
@@ -585,10 +654,7 @@ static class ApiClientConfig {
585654 @ Bean
586655 @ Primary
587656 ApiClient apiClient () throws Exception {
588- container = Commons .container ();
589- container .start ();
590-
591- return Config .fromConfig (new StringReader (container .getKubeConfigYaml ()));
657+ return new ClientBuilder ().setBasePath ("http://localhost:" + API_SERVER .getPort ()).build ();
592658 }
593659
594660 }
0 commit comments