Loadbalancer part 4 (#1556)

This commit is contained in:
erabii
2024-01-03 22:41:11 +02:00
committed by GitHub
parent 6262c76c84
commit 3f7d270e81
3 changed files with 57 additions and 58 deletions

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; package org.springframework.cloud.kubernetes.fabric8.loadbalancer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; 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.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.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@@ -31,48 +31,53 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
class Fabric8LoadBalancerAutoConfigurationTests { class Fabric8LoadBalancerAutoConfigurationTests {
private ConfigurableApplicationContext context;
@AfterEach
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
void kubernetesLoadBalancerWhenKubernetesDisabledAndLoadBalancerDisabled() { void kubernetesLoadBalancerWhenKubernetesDisabledAndLoadBalancerDisabled() {
setup("spring.cloud.kubernetes.loadbalancer.enabled=false"); new ApplicationContextRunner().withUserConfiguration(Fabric8LoadBalancerAutoConfigurationTests.Config.class)
assertThat(this.context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).isEmpty(); .withConfiguration(AutoConfigurations.of(Fabric8LoadBalancerAutoConfiguration.class))
.withPropertyValues("spring.cloud.kubernetes.loadbalancer.enabled=false")
.run(this::assertInstanceMapperMissing);
} }
@Test @Test
void kubernetesLoadBalancerWhenKubernetesDisabledAndLoadBalancerEnabled() { void kubernetesLoadBalancerWhenKubernetesDisabledAndLoadBalancerEnabled() {
setup("spring.cloud.kubernetes.loadbalancer.enabled=true"); new ApplicationContextRunner().withUserConfiguration(Fabric8LoadBalancerAutoConfigurationTests.Config.class)
assertThat(this.context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).isEmpty(); .withConfiguration(AutoConfigurations.of(Fabric8LoadBalancerAutoConfiguration.class))
.withPropertyValues("spring.cloud.kubernetes.loadbalancer.enabled=true")
.run(this::assertInstanceMapperMissing);
} }
@Test @Test
void kubernetesLoadBalancerWhenKubernetesEnabledAndLoadBalancerEnabled() { void kubernetesLoadBalancerWhenKubernetesEnabledAndLoadBalancerEnabled() {
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.loadbalancer.enabled=true"); new ApplicationContextRunner().withUserConfiguration(Fabric8LoadBalancerAutoConfigurationTests.Config.class)
assertThat(this.context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).hasSize(1); .withConfiguration(AutoConfigurations.of(Fabric8LoadBalancerAutoConfiguration.class))
.withPropertyValues("spring.cloud.kubernetes.loadbalancer.enabled=true",
"spring.main.cloud-platform=KUBERNETES")
.run(this::assertInstanceMapperPresent);
} }
@Test @Test
void kubernetesLoadBalancerWhenKubernetesEnabledAndLoadBalancerDisabled() { void kubernetesLoadBalancerWhenKubernetesEnabledAndLoadBalancerDisabled() {
setup("spring.cloud.kubernetes.loadbalancer.enabled=false"); new ApplicationContextRunner().withUserConfiguration(Fabric8LoadBalancerAutoConfigurationTests.Config.class)
assertThat(this.context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).isEmpty(); .withConfiguration(AutoConfigurations.of(Fabric8LoadBalancerAutoConfiguration.class))
.withPropertyValues("spring.cloud.kubernetes.loadbalancer.enabled=false",
"spring.main.cloud-platform=KUBERNETES")
.run(this::assertInstanceMapperMissing);
} }
@Test @Test
void kubernetesLoadBalancerWhenDefaultProperties() { void kubernetesLoadBalancerWhenDefaultProperties() {
setup("spring.main.cloud-platform=KUBERNETES"); new ApplicationContextRunner().withUserConfiguration(Fabric8LoadBalancerAutoConfigurationTests.Config.class)
assertThat(this.context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).hasSize(1); .withConfiguration(AutoConfigurations.of(Fabric8LoadBalancerAutoConfiguration.class))
.withPropertyValues("spring.main.cloud-platform=KUBERNETES").run(this::assertInstanceMapperPresent);
} }
private void setup(String... env) { private void assertInstanceMapperMissing(AssertableApplicationContext context) {
this.context = new SpringApplicationBuilder(Fabric8LoadBalancerAutoConfiguration.class, Config.class) assertThat(context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).isEmpty();
.web(org.springframework.boot.WebApplicationType.NONE).properties(env).run(); }
private void assertInstanceMapperPresent(AssertableApplicationContext context) {
assertThat(context.getBeanNamesForType(Fabric8ServiceInstanceMapper.class)).hasSize(1);
} }
@EnableConfigurationProperties(KubernetesDiscoveryProperties.class) @EnableConfigurationProperties(KubernetesDiscoveryProperties.class)

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -37,9 +36,9 @@ import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadB
class Fabric8ServiceInstanceMapperTests { class Fabric8ServiceInstanceMapperTests {
@Test @Test
public void testMapperSimple() { void testMapperSimple() {
KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties(); 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, KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties,
KubernetesDiscoveryProperties.DEFAULT).map(service); KubernetesDiscoveryProperties.DEFAULT).map(service);
Assertions.assertNotNull(instance); Assertions.assertNotNull(instance);
@@ -54,7 +53,7 @@ class Fabric8ServiceInstanceMapperTests {
List<ServicePort> ports = new ArrayList<>(); List<ServicePort> ports = new ArrayList<>();
ports.add(new ServicePortBuilder().withPort(8080).withName("web").build()); ports.add(new ServicePortBuilder().withPort(8080).withName("web").build());
ports.add(new ServicePortBuilder().withPort(9000).withName("http").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, KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties,
KubernetesDiscoveryProperties.DEFAULT).map(service); KubernetesDiscoveryProperties.DEFAULT).map(service);
Assertions.assertNotNull(instance); Assertions.assertNotNull(instance);
@@ -66,7 +65,7 @@ class Fabric8ServiceInstanceMapperTests {
@Test @Test
void testMapperSecure() { void testMapperSecure() {
KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties(); 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, KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties,
KubernetesDiscoveryProperties.DEFAULT).map(service); KubernetesDiscoveryProperties.DEFAULT).map(service);
Assertions.assertNotNull(instance); Assertions.assertNotNull(instance);
@@ -95,9 +94,7 @@ class Fabric8ServiceInstanceMapperTests {
@Test @Test
void testMapperSecureWithLabels() { void testMapperSecureWithLabels() {
KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties(); KubernetesLoadBalancerProperties properties = new KubernetesLoadBalancerProperties();
HashMap<String, String> labels = new HashMap<>(); Map<String, String> labels = Map.of("secured", "true", "label1", "123");
labels.put("secured", "true");
labels.put("label1", "123");
Service service = buildService("test", "abc", 8080, null, labels); Service service = buildService("test", "abc", 8080, null, labels);
KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties, KubernetesServiceInstance instance = new Fabric8ServiceInstanceMapper(properties,
KubernetesDiscoveryProperties.DEFAULT).map(service); KubernetesDiscoveryProperties.DEFAULT).map(service);
@@ -120,7 +117,7 @@ class Fabric8ServiceInstanceMapperTests {
} }
private Service buildService(String name, String uid, List<ServicePort> ports, Map<String, String> labels) { private Service buildService(String name, String uid, List<ServicePort> ports, Map<String, String> labels) {
return buildService(name, uid, ports, labels, new HashMap<>(0)); return buildService(name, uid, ports, labels, Map.of());
} }
} }

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 io.fabric8.kubernetes.client.dsl.ServiceResource;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance; import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServicesListSupplier; import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServicesListSupplier;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.mock.env.MockEnvironment;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class) class Fabric8ServiceListSupplierTests {
class KubernetesServiceListSupplierTests {
@Mock private final Environment environment =
Environment environment; new MockEnvironment().withProperty("loadbalancer.client.name", "test-service");
@Mock private final Fabric8ServiceInstanceMapper mapper = Mockito.mock(Fabric8ServiceInstanceMapper.class);
Fabric8ServiceInstanceMapper mapper;
@Mock private final KubernetesClient client = Mockito.mock(KubernetesClient.class);
KubernetesClient client;
@Mock @SuppressWarnings("unchecked")
MixedOperation<Service, ServiceList, ServiceResource<Service>> serviceOperation; private final MixedOperation<Service, ServiceList, ServiceResource<Service>> serviceOperation =
Mockito.mock(MixedOperation.class);
@Mock @SuppressWarnings("unchecked")
NonNamespaceOperation<Service, ServiceList, ServiceResource<Service>> namespaceOperation; private final NonNamespaceOperation<Service, ServiceList, ServiceResource<Service>> namespaceOperation =
Mockito.mock(NonNamespaceOperation.class);
@Mock @SuppressWarnings("unchecked")
ServiceResource<Service> serviceResource; private final ServiceResource<Service> serviceResource = Mockito.mock(ServiceResource.class);
@Mock @SuppressWarnings("unchecked")
AnyNamespaceOperation<Service, ServiceList, ServiceResource<Service>> multiDeletable; private final AnyNamespaceOperation<Service, ServiceList, ServiceResource<Service>> multiDeletable =
Mockito.mock(AnyNamespaceOperation.class);
@Test @Test
void testPositiveMatch() { void testPositiveMatch() {
when(environment.getProperty("loadbalancer.client.name")).thenReturn("test-service");
when(mapper.map(any(Service.class))) when(mapper.map(any(Service.class)))
.thenReturn(new DefaultKubernetesServiceInstance("", "", "", 0, null, false)); .thenReturn(new DefaultKubernetesServiceInstance("", "", "", 0, null, false));
when(this.client.getNamespace()).thenReturn("test"); when(this.client.getNamespace()).thenReturn("test");
@@ -86,7 +84,6 @@ class KubernetesServiceListSupplierTests {
@Test @Test
void testPositiveMatchAllNamespaces() { void testPositiveMatchAllNamespaces() {
when(environment.getProperty("loadbalancer.client.name")).thenReturn("test-service");
when(mapper.map(any(Service.class))) when(mapper.map(any(Service.class)))
.thenReturn(new DefaultKubernetesServiceInstance("", "", "", 0, null, false)); .thenReturn(new DefaultKubernetesServiceInstance("", "", "", 0, null, false));
when(this.client.services()).thenReturn(this.serviceOperation); when(this.client.services()).thenReturn(this.serviceOperation);