Skip to content

Commit fe9b3f5

Browse files
committed
dirty
1 parent 566dfd6 commit fe9b3f5

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/discovery/it/Fabric8DiscoveryPodMetadataIT.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,23 @@
1616

1717
package org.springframework.cloud.kubernetes.fabric8.client.discovery.it;
1818

19-
import java.util.List;
20-
import java.util.Map;
21-
2219
import io.fabric8.kubernetes.client.KubernetesClient;
2320
import org.junit.jupiter.api.AfterEach;
24-
import org.junit.jupiter.api.Assertions;
2521
import org.junit.jupiter.api.BeforeEach;
2622
import org.junit.jupiter.api.Test;
2723

24+
import org.springframework.beans.factory.annotation.Autowired;
2825
import org.springframework.boot.test.context.SpringBootTest;
2926
import org.springframework.boot.test.context.TestConfiguration;
30-
import org.springframework.boot.test.web.server.LocalServerPort;
31-
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
27+
import org.springframework.cloud.client.discovery.DiscoveryClient;
3228
import org.springframework.cloud.kubernetes.fabric8.client.discovery.Fabric8DiscoveryApp;
3329
import org.springframework.cloud.kubernetes.integration.tests.commons.Images;
3430
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
3531
import org.springframework.context.annotation.Bean;
3632
import org.springframework.context.annotation.Primary;
37-
import org.springframework.core.ParameterizedTypeReference;
38-
import org.springframework.http.HttpMethod;
39-
import org.springframework.web.reactive.function.client.WebClient;
4033

41-
import static org.springframework.cloud.kubernetes.fabric8.client.discovery.it.TestAssertions.builder;
42-
import static org.springframework.cloud.kubernetes.fabric8.client.discovery.it.TestAssertions.retrySpec;
4334
import static org.springframework.cloud.kubernetes.fabric8.client.discovery.it.Fabric8DiscoveryPodMetadataIT.TestConfig;
35+
import static org.springframework.cloud.kubernetes.fabric8.client.discovery.it.TestAssertions.assertPodMetadata;
4436

4537
/**
4638
* @author wind57
@@ -49,8 +41,8 @@
4941
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
5042
class Fabric8DiscoveryPodMetadataIT extends Fabric8DiscoveryBase {
5143

52-
@LocalServerPort
53-
private int port;
44+
@Autowired
45+
private DiscoveryClient discoveryClient;
5446

5547
@BeforeEach
5648
void beforeEach() {
@@ -81,6 +73,8 @@ void test() throws Exception {
8173

8274
K3S.execInContainer("sh", "-c", "kubectl label pods " + podOne + " my-label=my-value");
8375
K3S.execInContainer("sh", "-c", "kubectl annotate pods " + podTwo + " my-annotation=my-value");
76+
77+
assertPodMetadata(discoveryClient);
8478
}
8579

8680
@TestConfiguration

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/discovery/it/TestAssertions.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,60 @@
1616

1717
package org.springframework.cloud.kubernetes.fabric8.client.discovery.it;
1818

19-
import java.time.Duration;
20-
import java.util.Objects;
19+
import java.util.List;
20+
import java.util.Map;
2121

22+
import org.springframework.cloud.client.ServiceInstance;
2223
import org.springframework.cloud.client.discovery.DiscoveryClient;
23-
import reactor.netty.http.client.HttpClient;
24-
import reactor.util.retry.Retry;
25-
import reactor.util.retry.RetryBackoffSpec;
24+
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
2625

27-
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
28-
import org.springframework.web.reactive.function.client.WebClient;
26+
import static org.assertj.core.api.Assertions.assertThat;
2927

3028
/**
3129
* @author wind57
3230
*/
33-
class TestAssertions {
31+
final class TestAssertions {
3432

35-
static void assertPodMetadata(DiscoveryClient discoveryClient) {
33+
private TestAssertions() {
3634

3735
}
3836

39-
static WebClient.Builder builder() {
40-
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
41-
}
37+
static void assertPodMetadata(DiscoveryClient discoveryClient) {
38+
39+
List<ServiceInstance> serviceInstances = discoveryClient.getInstances("busybox-service");
40+
41+
DefaultKubernetesServiceInstance withCustomLabel = serviceInstances.stream()
42+
.map(instance -> (DefaultKubernetesServiceInstance) instance)
43+
.filter(x -> x.podMetadata().getOrDefault("annotations", Map.of()).isEmpty())
44+
.toList()
45+
.get(0);
46+
47+
Assertions.assertEquals(withCustomLabel.getServiceId(), "busybox-service");
48+
Assertions.assertNotNull(withCustomLabel.getInstanceId());
49+
Assertions.assertNotNull(withCustomLabel.getHost());
50+
Assertions.assertEquals(withCustomLabel.getMetadata(),
51+
Map.of("k8s_namespace", "default", "type", "ClusterIP", "port.busybox-port", "80"));
52+
Assertions.assertTrue(withCustomLabel.podMetadata()
53+
.get("labels")
54+
.entrySet()
55+
.stream()
56+
.anyMatch(x -> x.getKey().equals("custom-label") && x.getValue().equals("custom-label-value")));
4257

43-
static RetryBackoffSpec retrySpec() {
44-
return Retry.fixedDelay(15, Duration.ofSeconds(1)).filter(Objects::nonNull);
58+
DefaultKubernetesServiceInstance withCustomAnnotation = serviceInstances.stream()
59+
.map(instance -> (DefaultKubernetesServiceInstance) instance)
60+
.filter(x -> !x.podMetadata().getOrDefault("annotations", Map.of()).isEmpty())
61+
.toList()
62+
.get(0);
63+
Assertions.assertEquals(withCustomAnnotation.getServiceId(), "busybox-service");
64+
Assertions.assertNotNull(withCustomAnnotation.getInstanceId());
65+
Assertions.assertNotNull(withCustomAnnotation.getHost());
66+
Assertions.assertEquals(withCustomAnnotation.getMetadata(),
67+
Map.of("k8s_namespace", "default", "type", "ClusterIP", "port.busybox-port", "80"));
68+
Assertions.assertTrue(withCustomAnnotation.podMetadata()
69+
.get("annotations")
70+
.entrySet()
71+
.stream()
72+
.anyMatch(x -> x.getKey().equals("custom-annotation") && x.getValue().equals("custom-annotation-value")));
4573
}
4674

4775
}

0 commit comments

Comments
 (0)