unit tests fixes gh-465

This commit is contained in:
Haytham Mohamed
2019-09-12 16:36:38 -05:00
parent bfd6772705
commit a84c7873e0
2 changed files with 33 additions and 23 deletions

View File

@@ -27,6 +27,7 @@ import io.fabric8.kubernetes.api.model.EndpointPortBuilder;
import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.EndpointsBuilder;
import io.fabric8.kubernetes.api.model.EndpointsList;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
import io.fabric8.kubernetes.api.model.ServiceList;
@@ -48,6 +49,7 @@ import org.springframework.cloud.client.ServiceInstance;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
@@ -89,7 +91,7 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
when(this.metadata.isAddAnnotations()).thenReturn(false);
when(this.metadata.isAddPorts()).thenReturn(false);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId,
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns",
new HashMap<String, String>() {
{
put("l1", "lab");
@@ -119,7 +121,7 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
when(this.metadata.isAddAnnotations()).thenReturn(false);
when(this.metadata.isAddPorts()).thenReturn(false);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId,
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns",
new HashMap<String, String>() {
{
put("l1", "v1");
@@ -152,7 +154,7 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
when(this.metadata.isAddAnnotations()).thenReturn(false);
when(this.metadata.isAddPorts()).thenReturn(false);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId,
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns",
new HashMap<String, String>() {
{
put("l1", "v1");
@@ -184,7 +186,7 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
when(this.metadata.isAddAnnotations()).thenReturn(true);
when(this.metadata.isAddPorts()).thenReturn(false);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId,
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns",
new HashMap<String, String>() {
{
put("l1", "v1");
@@ -217,7 +219,7 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
when(this.metadata.getAnnotationsPrefix()).thenReturn("a_");
when(this.metadata.isAddPorts()).thenReturn(false);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId,
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns",
new HashMap<String, String>() {
{
put("l1", "v1");
@@ -249,7 +251,7 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
when(this.metadata.isAddAnnotations()).thenReturn(false);
when(this.metadata.isAddPorts()).thenReturn(true);
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId,
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns",
new HashMap<String, String>() {
{
put("l1", "v1");
@@ -281,7 +283,7 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
when(this.metadata.isAddPorts()).thenReturn(true);
when(this.metadata.getPortsPrefix()).thenReturn("p_");
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId,
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns",
new HashMap<String, String>() {
{
put("l1", "v1");
@@ -315,7 +317,7 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
when(this.metadata.isAddPorts()).thenReturn(true);
when(this.metadata.getPortsPrefix()).thenReturn("p_");
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId,
setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns",
new HashMap<String, String>() {
{
put("l1", "la1");
@@ -339,18 +341,24 @@ public class KubernetesDiscoveryClientFilterMetadataTest {
}
private void setupServiceWithLabelsAndAnnotationsAndPorts(String serviceId,
Map<String, String> labels, Map<String, String> annotations,
String namespace, Map<String, String> labels, Map<String, String> annotations,
Map<Integer, String> ports) {
final Service service = new ServiceBuilder().withNewMetadata().withLabels(labels)
.withAnnotations(annotations).endMetadata().withNewSpec()
.withPorts(getServicePorts(ports)).endSpec().build();
final 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);
when(this.serviceResource.get()).thenReturn(service);
when(this.kubernetesClient.services()).thenReturn(this.serviceOperation);
when(this.kubernetesClient.services().inNamespace(anyString()))
.thenReturn(this.serviceOperation);
final Endpoints endpoints = new EndpointsBuilder().addNewSubset()
.addAllToPorts(getEndpointPorts(ports)).addNewAddress().endAddress()
.endSubset().build();
ObjectMeta objectMeta = new ObjectMeta();
objectMeta.setNamespace(namespace);
final Endpoints endpoints = new EndpointsBuilder().withMetadata(objectMeta)
.addNewSubset().addAllToPorts(getEndpointPorts(ports)).addNewAddress()
.endAddress().endSubset().build();
when(this.endpointsResource.get()).thenReturn(endpoints);
when(this.endpointsOperation.withName(serviceId))

View File

@@ -116,13 +116,14 @@ public class KubernetesDiscoveryClientTest {
.andReturn(200, services).once();
mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint")
.andReturn(200, service1).once();
.andReturn(200, service1).always();
mockServer.expect().get().withPath("/api/v1/namespaces/test2/services/endpoint")
.andReturn(200, service2).once();
.andReturn(200, service2).always();
final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
properties.setAllNamespaces(true);
final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient,
properties, KubernetesClient::services,
new DefaultIsServicePortSecureResolver(properties));
@@ -149,16 +150,17 @@ public class KubernetesDiscoveryClientTest {
.endAddress().addNewPort("http", 80, "TCP").endSubset().build())
.once();
mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint")
mockServer.expect().get().withPath("/api/v1/services/endpoint")
.andReturn(200, new ServiceBuilder().withNewMetadata()
.withName("endpoint").withLabels(new HashMap<String, String>() {
{
put("l", "v");
}
}).endMetadata().build())
.once();
.always();
final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient,
properties, KubernetesClient::services,
new DefaultIsServicePortSecureResolver(properties));
@@ -180,14 +182,14 @@ public class KubernetesDiscoveryClientTest {
.addNewPort("http", 80, "TCP").endSubset().build())
.once();
mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint")
mockServer.expect().get().withPath("/api/v1/services/endpoint")
.andReturn(200, new ServiceBuilder().withNewMetadata()
.withName("endpoint").withLabels(new HashMap<String, String>() {
{
put("l", "v");
}
}).endMetadata().build())
.once();
.always();
final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
properties.setPrimaryPortName("http");
@@ -212,14 +214,14 @@ public class KubernetesDiscoveryClientTest {
.endAddress().addNewPort("https", 443, "TCP").endSubset().build())
.once();
mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint")
mockServer.expect().get().withPath("/api/v1/services/endpoint")
.andReturn(200, new ServiceBuilder().withNewMetadata()
.withName("endpoint").withLabels(new HashMap<String, String>() {
{
put("l", "v");
}
}).endMetadata().build())
.once();
.always();
final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient,