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");
* 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)

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");
* 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<ServicePort> 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<String, String> labels = new HashMap<>();
labels.put("secured", "true");
labels.put("label1", "123");
Map<String, String> 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<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");
* 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<Service, ServiceList, ServiceResource<Service>> serviceOperation;
@SuppressWarnings("unchecked")
private final MixedOperation<Service, ServiceList, ServiceResource<Service>> serviceOperation =
Mockito.mock(MixedOperation.class);
@Mock
NonNamespaceOperation<Service, ServiceList, ServiceResource<Service>> namespaceOperation;
@SuppressWarnings("unchecked")
private final NonNamespaceOperation<Service, ServiceList, ServiceResource<Service>> namespaceOperation =
Mockito.mock(NonNamespaceOperation.class);
@Mock
ServiceResource<Service> serviceResource;
@SuppressWarnings("unchecked")
private final ServiceResource<Service> serviceResource = Mockito.mock(ServiceResource.class);
@Mock
AnyNamespaceOperation<Service, ServiceList, ServiceResource<Service>> multiDeletable;
@SuppressWarnings("unchecked")
private final AnyNamespaceOperation<Service, ServiceList, ServiceResource<Service>> 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);