From 3f7d270e81a56e531b1be7182a13d762f5950a48 Mon Sep 17 00:00:00 2001 From: erabii Date: Wed, 3 Jan 2024 22:41:11 +0200 Subject: [PATCH] Loadbalancer part 4 (#1556) --- ...ic8LoadBalancerAutoConfigurationTests.java | 57 ++++++++++--------- .../Fabric8ServiceInstanceMapperTests.java | 17 +++--- ...a => Fabric8ServiceListSupplierTests.java} | 41 +++++++------ 3 files changed, 57 insertions(+), 58 deletions(-) rename spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/{KubernetesServiceListSupplierTests.java => Fabric8ServiceListSupplierTests.java} (77%) diff --git a/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8LoadBalancerAutoConfigurationTests.java b/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8LoadBalancerAutoConfigurationTests.java index 52bc6bdf..0994bee6 100644 --- a/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8LoadBalancerAutoConfigurationTests.java +++ b/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8LoadBalancerAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2020 the original author or authors. + * Copyright 2013-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,13 @@ package org.springframework.cloud.kubernetes.fabric8.loadbalancer; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; -import org.springframework.context.ConfigurableApplicationContext; import static org.assertj.core.api.Assertions.assertThat; @@ -31,48 +31,53 @@ import static org.assertj.core.api.Assertions.assertThat; */ class Fabric8LoadBalancerAutoConfigurationTests { - private ConfigurableApplicationContext context; - - @AfterEach - public void close() { - if (this.context != null) { - this.context.close(); - } - } - @Test void kubernetesLoadBalancerWhenKubernetesDisabledAndLoadBalancerDisabled() { - setup("spring.cloud.kubernetes.loadbalancer.enabled=false"); - assertThat(this.context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).isEmpty(); + new ApplicationContextRunner().withUserConfiguration(Fabric8LoadBalancerAutoConfigurationTests.Config.class) + .withConfiguration(AutoConfigurations.of(Fabric8LoadBalancerAutoConfiguration.class)) + .withPropertyValues("spring.cloud.kubernetes.loadbalancer.enabled=false") + .run(this::assertInstanceMapperMissing); } @Test void kubernetesLoadBalancerWhenKubernetesDisabledAndLoadBalancerEnabled() { - setup("spring.cloud.kubernetes.loadbalancer.enabled=true"); - assertThat(this.context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).isEmpty(); + new ApplicationContextRunner().withUserConfiguration(Fabric8LoadBalancerAutoConfigurationTests.Config.class) + .withConfiguration(AutoConfigurations.of(Fabric8LoadBalancerAutoConfiguration.class)) + .withPropertyValues("spring.cloud.kubernetes.loadbalancer.enabled=true") + .run(this::assertInstanceMapperMissing); } @Test void kubernetesLoadBalancerWhenKubernetesEnabledAndLoadBalancerEnabled() { - setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.loadbalancer.enabled=true"); - assertThat(this.context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).hasSize(1); + new ApplicationContextRunner().withUserConfiguration(Fabric8LoadBalancerAutoConfigurationTests.Config.class) + .withConfiguration(AutoConfigurations.of(Fabric8LoadBalancerAutoConfiguration.class)) + .withPropertyValues("spring.cloud.kubernetes.loadbalancer.enabled=true", + "spring.main.cloud-platform=KUBERNETES") + .run(this::assertInstanceMapperPresent); } @Test void kubernetesLoadBalancerWhenKubernetesEnabledAndLoadBalancerDisabled() { - setup("spring.cloud.kubernetes.loadbalancer.enabled=false"); - assertThat(this.context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).isEmpty(); + new ApplicationContextRunner().withUserConfiguration(Fabric8LoadBalancerAutoConfigurationTests.Config.class) + .withConfiguration(AutoConfigurations.of(Fabric8LoadBalancerAutoConfiguration.class)) + .withPropertyValues("spring.cloud.kubernetes.loadbalancer.enabled=false", + "spring.main.cloud-platform=KUBERNETES") + .run(this::assertInstanceMapperMissing); } @Test void kubernetesLoadBalancerWhenDefaultProperties() { - setup("spring.main.cloud-platform=KUBERNETES"); - assertThat(this.context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).hasSize(1); + new ApplicationContextRunner().withUserConfiguration(Fabric8LoadBalancerAutoConfigurationTests.Config.class) + .withConfiguration(AutoConfigurations.of(Fabric8LoadBalancerAutoConfiguration.class)) + .withPropertyValues("spring.main.cloud-platform=KUBERNETES").run(this::assertInstanceMapperPresent); } - private void setup(String... env) { - this.context = new SpringApplicationBuilder(Fabric8LoadBalancerAutoConfiguration.class, Config.class) - .web(org.springframework.boot.WebApplicationType.NONE).properties(env).run(); + private void assertInstanceMapperMissing(AssertableApplicationContext context) { + assertThat(context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).isEmpty(); + } + + private void assertInstanceMapperPresent(AssertableApplicationContext context) { + assertThat(context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).hasSize(1); } @EnableConfigurationProperties(KubernetesDiscoveryProperties.class) diff --git a/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceInstanceMapperTests.java b/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceInstanceMapperTests.java index e8b18dce..14b1b945 100644 --- a/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceInstanceMapperTests.java +++ b/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceInstanceMapperTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2020 the original author or authors. + * Copyright 2013-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package org.springframework.cloud.kubernetes.fabric8.loadbalancer; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -37,9 +36,9 @@ import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadB class Fabric8ServiceInstanceMapperTests { @Test - public void testMapperSimple() { + void testMapperSimple() { KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties(); - Service service = buildService("test", "abc", 8080, null, new HashMap<>()); + Service service = buildService("test", "abc", 8080, null, Map.of()); KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties, KubernetesDiscoveryProperties.DEFAULT).map(service); Assertions.assertNotNull(instance); @@ -54,7 +53,7 @@ class Fabric8ServiceInstanceMapperTests { List ports = new ArrayList<>(); ports.add(new ServicePortBuilder().withPort(8080).withName("web").build()); ports.add(new ServicePortBuilder().withPort(9000).withName("http").build()); - Service service = buildService("test", "abc", ports, new HashMap<>()); + Service service = buildService("test", "abc", ports, Map.of()); KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties, KubernetesDiscoveryProperties.DEFAULT).map(service); Assertions.assertNotNull(instance); @@ -66,7 +65,7 @@ class Fabric8ServiceInstanceMapperTests { @Test void testMapperSecure() { KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties(); - Service service = buildService("test", "abc", 443, null, new HashMap<>()); + Service service = buildService("test", "abc", 443, null, Map.of()); KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties, KubernetesDiscoveryProperties.DEFAULT).map(service); Assertions.assertNotNull(instance); @@ -95,9 +94,7 @@ class Fabric8ServiceInstanceMapperTests { @Test void testMapperSecureWithLabels() { KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties(); - HashMap labels = new HashMap<>(); - labels.put("secured", "true"); - labels.put("label1", "123"); + Map labels = Map.of("secured", "true", "label1", "123"); Service service = buildService("test", "abc", 8080, null, labels); KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties, KubernetesDiscoveryProperties.DEFAULT).map(service); @@ -120,7 +117,7 @@ class Fabric8ServiceInstanceMapperTests { } private Service buildService(String name, String uid, List ports, Map labels) { - return buildService(name, uid, ports, labels, new HashMap<>(0)); + return buildService(name, uid, ports, labels, Map.of()); } } diff --git a/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/KubernetesServiceListSupplierTests.java b/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceListSupplierTests.java similarity index 77% rename from spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/KubernetesServiceListSupplierTests.java rename to spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceListSupplierTests.java index 40afa180..3626c864 100644 --- a/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/KubernetesServiceListSupplierTests.java +++ b/spring-cloud-kubernetes-fabric8-loadbalancer/src/test/java/org/springframework/cloud/kubernetes/fabric8/loadbalancer/Fabric8ServiceListSupplierTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2020 the original author or authors. + * Copyright 2013-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,46 +30,44 @@ import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; import io.fabric8.kubernetes.client.dsl.ServiceResource; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.Mockito; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServicesListSupplier; import org.springframework.core.env.Environment; +import org.springframework.mock.env.MockEnvironment; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; -@ExtendWith(MockitoExtension.class) -class KubernetesServiceListSupplierTests { +class Fabric8ServiceListSupplierTests { - @Mock - Environment environment; + private final Environment environment = + new MockEnvironment().withProperty("loadbalancer.client.name", "test-service"); - @Mock - Fabric8ServiceInstanceMapper mapper; + private final Fabric8ServiceInstanceMapper mapper = Mockito.mock(Fabric8ServiceInstanceMapper.class); - @Mock - KubernetesClient client; + private final KubernetesClient client = Mockito.mock(KubernetesClient.class); - @Mock - MixedOperation> serviceOperation; + @SuppressWarnings("unchecked") + private final MixedOperation> serviceOperation = + Mockito.mock(MixedOperation.class); - @Mock - NonNamespaceOperation> namespaceOperation; + @SuppressWarnings("unchecked") + private final NonNamespaceOperation> namespaceOperation = + Mockito.mock(NonNamespaceOperation.class); - @Mock - ServiceResource serviceResource; + @SuppressWarnings("unchecked") + private final ServiceResource serviceResource = Mockito.mock(ServiceResource.class); - @Mock - AnyNamespaceOperation> multiDeletable; + @SuppressWarnings("unchecked") + private final AnyNamespaceOperation> multiDeletable = + Mockito.mock(AnyNamespaceOperation.class); @Test void testPositiveMatch() { - when(environment.getProperty("loadbalancer.client.name")).thenReturn("test-service"); when(mapper.map(any(Service.class))) .thenReturn(new DefaultKubernetesServiceInstance("", "", "", 0, null, false)); when(this.client.getNamespace()).thenReturn("test"); @@ -86,7 +84,6 @@ class KubernetesServiceListSupplierTests { @Test void testPositiveMatchAllNamespaces() { - when(environment.getProperty("loadbalancer.client.name")).thenReturn("test-service"); when(mapper.map(any(Service.class))) .thenReturn(new DefaultKubernetesServiceInstance("", "", "", 0, null, false)); when(this.client.services()).thenReturn(this.serviceOperation);