Simplify test 2 (#1224)

This commit is contained in:
erabii
2023-02-14 15:47:18 +02:00
committed by GitHub
parent 6d9fa50efb
commit d0fc86d04d
6 changed files with 84 additions and 193 deletions

View File

@@ -42,45 +42,45 @@ import static org.mockito.Mockito.when;
* @author Oleg Vyukov
* @author Tim Ysewyn
*/
public class KubernetesCatalogServicesWatchConfigurationTest {
class KubernetesCatalogServicesWatchConfigurationTest {
private ConfigurableApplicationContext context;
@AfterEach
void close() {
if (this.context != null) {
this.context.close();
if (context != null) {
context.close();
}
}
@Test
void kubernetesCatalogWatchDisabled() {
setup("spring.cloud.kubernetes.discovery.catalog-services-watch.enabled=false");
assertThat(this.context.containsBean("kubernetesCatalogWatch")).isFalse();
assertThat(context.containsBean("kubernetesCatalogWatch")).isFalse();
}
@Test
void kubernetesCatalogWatchWhenKubernetesDisabled() {
setup();
assertThat(this.context.containsBean("kubernetesCatalogWatch")).isFalse();
assertThat(context.containsBean("kubernetesCatalogWatch")).isFalse();
}
@Test
void kubernetesCatalogWatchWhenServiceDiscoveryDisabled() {
setup("spring.cloud.discovery.enabled=false");
assertThat(this.context.containsBean("kubernetesCatalogWatch")).isFalse();
assertThat(context.containsBean("kubernetesCatalogWatch")).isFalse();
}
@Test
void kubernetesCatalogWatchDefaultEnabled() {
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.discovery.use-endpoint-slices=false");
assertThat(this.context.containsBean("kubernetesCatalogWatch")).isTrue();
assertThat(context.containsBean("kubernetesCatalogWatch")).isTrue();
}
private void setup(String... env) {
List<String> envList = new ArrayList<>(Arrays.asList(env));
envList.add("spring.cloud.config.enabled=false");
this.context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class,
context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class,
KubernetesClientTestConfiguration.class, KubernetesCatalogWatchAutoConfiguration.class,
KubernetesDiscoveryClientAutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class)
.web(WebApplicationType.NONE).properties(envList.toArray(new String[0])).run();

View File

@@ -46,9 +46,9 @@ class KubernetesDiscoveryClientAutoConfigurationPropertiesTests {
private ConfigurableApplicationContext context;
@AfterEach
void close() {
if (this.context != null) {
this.context.close();
void afterEach() {
if (context != null) {
context.close();
}
}
@@ -56,31 +56,31 @@ class KubernetesDiscoveryClientAutoConfigurationPropertiesTests {
void kubernetesDiscoveryDisabled() {
setup("spring.cloud.kubernetes.discovery.enabled=false",
"spring.cloud.kubernetes.discovery.catalog-services-watch.enabled=false");
assertThat(this.context.getBeanNamesForType(KubernetesDiscoveryClient.class)).isEmpty();
assertThat(context.getBeanNamesForType(KubernetesDiscoveryClient.class)).isEmpty();
}
@Test
void kubernetesDiscoveryWhenKubernetesDisabled() {
setup();
assertThat(this.context.getBeanNamesForType(KubernetesDiscoveryClient.class)).isEmpty();
assertThat(context.getBeanNamesForType(KubernetesDiscoveryClient.class)).isEmpty();
}
@Test
void kubernetesDiscoveryWhenDiscoveryDisabled() {
setup("spring.cloud.discovery.enabled=false");
assertThat(this.context.getBeanNamesForType(KubernetesDiscoveryClient.class)).isEmpty();
assertThat(context.getBeanNamesForType(KubernetesDiscoveryClient.class)).isEmpty();
}
@Test
void kubernetesDiscoveryDefaultEnabled() {
setup("spring.main.cloud-platform=KUBERNETES");
assertThat(this.context.getBeanNamesForType(KubernetesDiscoveryClient.class)).hasSize(1);
assertThat(context.getBeanNamesForType(KubernetesDiscoveryClient.class)).hasSize(1);
}
private void setup(String... env) {
List<String> envList = new ArrayList<>(Arrays.asList(env));
envList.add("spring.cloud.config.enabled=false");
this.context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class,
context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class,
KubernetesClientTestConfiguration.class, KubernetesDiscoveryClientAutoConfiguration.class,
KubernetesDiscoveryPropertiesAutoConfiguration.class)
.web(org.springframework.boot.WebApplicationType.NONE)

View File

@@ -31,12 +31,12 @@ import static org.assertj.core.api.Assertions.assertThat;
"spring.cloud.config.enabled=false", "spring.cloud.kubernetes.discovery.use-endpoint-slices=false" })
class KubernetesDiscoveryClientAutoConfigurationTests {
@Autowired(required = false)
@Autowired
private DiscoveryClient discoveryClient;
@Test
void kubernetesDiscoveryClientCreated() {
assertThat(this.discoveryClient).isNotNull().isInstanceOf(CompositeDiscoveryClient.class);
assertThat(this.discoveryClient).isInstanceOf(CompositeDiscoveryClient.class);
CompositeDiscoveryClient composite = (CompositeDiscoveryClient) this.discoveryClient;
assertThat(composite.getDiscoveryClients().stream().anyMatch(dc -> dc instanceof KubernetesDiscoveryClient))

View File

@@ -48,12 +48,12 @@ class KubernetesDiscoveryClientConfigClientBootstrapConfigurationTests {
private AnnotationConfigApplicationContext context;
@AfterEach
public void close() {
if (this.context != null) {
if (this.context.getParent() != null) {
((AnnotationConfigApplicationContext) this.context.getParent()).close();
void afterEach() {
if (context != null) {
if (context.getParent() != null) {
((AnnotationConfigApplicationContext) context.getParent()).close();
}
this.context.close();
context.close();
}
}
@@ -62,10 +62,10 @@ class KubernetesDiscoveryClientConfigClientBootstrapConfigurationTests {
setup("server.port=7000", "spring.cloud.config.discovery.enabled=true",
"spring.cloud.kubernetes.discovery.enabled:true", "spring.application.name:test",
"spring.cloud.config.discovery.service-id:configserver");
Assertions.assertEquals(1, this.context.getParent().getBeanNamesForType(DiscoveryClient.class).length);
DiscoveryClient client = this.context.getParent().getBean(DiscoveryClient.class);
Assertions.assertEquals(1, context.getParent().getBeanNamesForType(DiscoveryClient.class).length);
DiscoveryClient client = context.getParent().getBean(DiscoveryClient.class);
verify(client, atLeast(2)).getInstances("configserver");
ConfigClientProperties locator = this.context.getBean(ConfigClientProperties.class);
ConfigClientProperties locator = context.getBean(ConfigClientProperties.class);
Assertions.assertEquals("http://fake:8888/", locator.getUri()[0]);
}
@@ -77,18 +77,18 @@ class KubernetesDiscoveryClientConfigClientBootstrapConfigurationTests {
KubernetesDiscoveryClientConfigClientBootstrapConfiguration.class,
DiscoveryClientConfigServiceBootstrapConfiguration.class, ConfigClientProperties.class);
parent.refresh();
this.context = new AnnotationConfigApplicationContext();
this.context.setParent(parent);
this.context.register(PropertyPlaceholderAutoConfiguration.class, KubernetesCommonsAutoConfiguration.class,
context = new AnnotationConfigApplicationContext();
context.setParent(parent);
context.register(PropertyPlaceholderAutoConfiguration.class, KubernetesCommonsAutoConfiguration.class,
KubernetesDiscoveryClientAutoConfiguration.class);
this.context.refresh();
context.refresh();
}
@Configuration(proxyBeanMethods = false)
protected static class EnvironmentKnobbler {
@Bean
public KubernetesDiscoveryClient kubernetesDiscoveryClient() {
KubernetesDiscoveryClient kubernetesDiscoveryClient() {
KubernetesDiscoveryClient client = mock(KubernetesDiscoveryClient.class);
ServiceInstance instance = new DefaultServiceInstance("configserver1", "configserver", "fake", 8888, false);
given(client.getInstances("configserver")).willReturn(Collections.singletonList(instance));

View File

@@ -17,7 +17,6 @@
package org.springframework.cloud.kubernetes.fabric8.discovery;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -39,11 +38,8 @@ import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.ServiceResource;
import org.assertj.core.util.Strings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
@@ -57,26 +53,24 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties.Metadata;
@RunWith(MockitoJUnitRunner.class)
public class KubernetesDiscoveryClientFilterMetadataTest {
class KubernetesDiscoveryClientFilterMetadataTest {
private static final KubernetesClient CLIENT = Mockito.mock(KubernetesClient.class);
@Mock
private MixedOperation<Service, ServiceList, ServiceResource<Service>> serviceOperation;
private final MixedOperation<Service, ServiceList, ServiceResource<Service>> serviceOperation = Mockito
.mock(MixedOperation.class);
@Mock
private MixedOperation<Endpoints, EndpointsList, Resource<Endpoints>> endpointsOperation;
private final MixedOperation<Endpoints, EndpointsList, Resource<Endpoints>> endpointsOperation = Mockito
.mock(MixedOperation.class);
@Mock
private ServiceResource<Service> serviceResource;
private final ServiceResource<Service> serviceResource = Mockito.mock(ServiceResource.class);
@Mock
FilterWatchListDeletable<Endpoints, EndpointsList, Resource<Endpoints>> filter;
private final FilterWatchListDeletable<Endpoints, EndpointsList, Resource<Endpoints>> filter = Mockito
.mock(FilterWatchListDeletable.class);
@Test
public void testAllExtraMetadataDisabled() {
final String serviceId = "s";
void testAllExtraMetadataDisabled() {
String serviceId = "s";
Metadata metadata = new Metadata(false, null, false, null, false, null);
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60,
@@ -84,29 +78,17 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", new HashMap<String, String>() {
{
put("l1", "lab");
}
}, new HashMap<String, String>() {
{
put("l1", "lab");
}
}, new HashMap<Integer, String>() {
{
put(80, "http");
put(5555, "");
}
});
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "lab"), Map.of("l1", "lab"),
Map.of(80, "http", 5555, ""));
final List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
assertThat(instances).hasSize(1);
assertThat(instances.get(0).getMetadata()).isEmpty();
}
@Test
public void testLabelsEnabled() {
final String serviceId = "s";
void testLabelsEnabled() {
String serviceId = "s";
Metadata metadata = new Metadata(true, null, false, null, false, null);
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60,
@@ -114,30 +96,17 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", new HashMap<String, String>() {
{
put("l1", "v1");
put("l2", "v2");
}
}, new HashMap<String, String>() {
{
put("l1", "lab");
}
}, new HashMap<Integer, String>() {
{
put(80, "http");
put(5555, "");
}
});
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1", "l2", "v2"),
Map.of("l1", "lab"), Map.of(80, "http", 5555, ""));
final List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
assertThat(instances).hasSize(1);
assertThat(instances.get(0).getMetadata()).containsOnly(entry("l1", "v1"), entry("l2", "v2"));
}
@Test
public void testLabelsEnabledWithPrefix() {
final String serviceId = "s";
void testLabelsEnabledWithPrefix() {
String serviceId = "s";
Metadata metadata = new Metadata(true, "l_", false, null, false, null);
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60,
@@ -145,30 +114,17 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", new HashMap<String, String>() {
{
put("l1", "v1");
put("l2", "v2");
}
}, new HashMap<String, String>() {
{
put("l1", "lab");
}
}, new HashMap<Integer, String>() {
{
put(80, "http");
put(5555, "");
}
});
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1", "l2", "v2"),
Map.of("l1", "lab"), Map.of(80, "http", 5555, ""));
final List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
assertThat(instances).hasSize(1);
assertThat(instances.get(0).getMetadata()).containsOnly(entry("l_l1", "v1"), entry("l_l2", "v2"));
}
@Test
public void testAnnotationsEnabled() {
final String serviceId = "s";
void testAnnotationsEnabled() {
String serviceId = "s";
Metadata metadata = new Metadata(false, null, true, null, false, null);
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60,
@@ -176,30 +132,17 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", new HashMap<String, String>() {
{
put("l1", "v1");
}
}, new HashMap<String, String>() {
{
put("a1", "v1");
put("a2", "v2");
}
}, new HashMap<Integer, String>() {
{
put(80, "http");
put(5555, "");
}
});
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"),
Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, ""));
final List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
assertThat(instances).hasSize(1);
assertThat(instances.get(0).getMetadata()).containsOnly(entry("a1", "v1"), entry("a2", "v2"));
}
@Test
public void testAnnotationsEnabledWithPrefix() {
final String serviceId = "s";
void testAnnotationsEnabledWithPrefix() {
String serviceId = "s";
Metadata metadata = new Metadata(false, null, true, "a_", false, null);
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60,
@@ -207,30 +150,17 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", new HashMap<String, String>() {
{
put("l1", "v1");
}
}, new HashMap<String, String>() {
{
put("a1", "v1");
put("a2", "v2");
}
}, new HashMap<Integer, String>() {
{
put(80, "http");
put(5555, "");
}
});
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"),
Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, ""));
final List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
assertThat(instances).hasSize(1);
assertThat(instances.get(0).getMetadata()).containsOnly(entry("a_a1", "v1"), entry("a_a2", "v2"));
}
@Test
public void testPortsEnabled() {
final String serviceId = "s";
void testPortsEnabled() {
String serviceId = "s";
Metadata metadata = new Metadata(false, null, false, null, true, null);
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60,
@@ -238,30 +168,17 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", new HashMap<String, String>() {
{
put("l1", "v1");
}
}, new HashMap<String, String>() {
{
put("a1", "v1");
put("a2", "v2");
}
}, new HashMap<Integer, String>() {
{
put(80, "http");
put(5555, "");
}
});
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"),
Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, ""));
final List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
assertThat(instances).hasSize(1);
assertThat(instances.get(0).getMetadata()).containsOnly(entry("http", "80"));
}
@Test
public void testPortsEnabledWithPrefix() {
final String serviceId = "s";
void testPortsEnabledWithPrefix() {
String serviceId = "s";
Metadata metadata = new Metadata(false, null, false, null, true, "p_");
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60,
@@ -269,30 +186,17 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", new HashMap<String, String>() {
{
put("l1", "v1");
}
}, new HashMap<String, String>() {
{
put("a1", "v1");
put("a2", "v2");
}
}, new HashMap<Integer, String>() {
{
put(80, "http");
put(5555, "");
}
});
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"),
Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, ""));
final List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
assertThat(instances).hasSize(1);
assertThat(instances.get(0).getMetadata()).containsOnly(entry("p_http", "80"));
}
@Test
public void testLabelsAndAnnotationsAndPortsEnabledWithPrefix() {
final String serviceId = "s";
void testLabelsAndAnnotationsAndPortsEnabledWithPrefix() {
String serviceId = "s";
Metadata metadata = new Metadata(true, "l_", true, "a_", true, "p_");
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60,
@@ -300,23 +204,10 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", new HashMap<String, String>() {
{
put("l1", "la1");
}
}, new HashMap<String, String>() {
{
put("a1", "an1");
put("a2", "an2");
}
}, new HashMap<Integer, String>() {
{
put(80, "http");
put(5555, "");
}
});
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "la1"),
Map.of("a1", "an1", "a2", "an2"), Map.of(80, "http", 5555, ""));
final List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
assertThat(instances).hasSize(1);
assertThat(instances.get(0).getMetadata()).containsOnly(entry("a_a1", "an1"), entry("a_a2", "an2"),
entry("l_l1", "la1"), entry("p_http", "80"));
@@ -324,7 +215,7 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
private void setupServiceWithLabelsAndAnnotationsAndPorts(String serviceId, String namespace,
Map<String, String> labels, Map<String, String> annotations, Map<Integer, String> ports) {
final Service service = new ServiceBuilder().withNewMetadata().withNamespace(namespace).withLabels(labels)
Service service = new ServiceBuilder().withNewMetadata().withNamespace(namespace).withLabels(labels)
.withAnnotations(annotations).endMetadata().withNewSpec().withPorts(getServicePorts(ports)).endSpec()
.build();
when(this.serviceOperation.withName(serviceId)).thenReturn(this.serviceResource);
@@ -335,7 +226,7 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
ObjectMeta objectMeta = new ObjectMeta();
objectMeta.setNamespace(namespace);
final Endpoints endpoints = new EndpointsBuilder().withMetadata(objectMeta).addNewSubset()
Endpoints endpoints = new EndpointsBuilder().withMetadata(objectMeta).addNewSubset()
.addAllToPorts(getEndpointPorts(ports)).addNewAddress().endAddress().endSubset().build();
when(CLIENT.endpoints()).thenReturn(this.endpointsOperation);

View File

@@ -46,12 +46,12 @@ public class KubernetesExtension implements ParameterResolver, BeforeEachCallbac
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
public void beforeEach(ExtensionContext context) {
mockServer.before();
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
public void afterEach(ExtensionContext context) {
mockServer.after();
}