|
18 | 18 |
|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.io.StringReader; |
21 | | -import java.util.LinkedHashMap; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.List; |
22 | 23 | import java.util.Map; |
23 | 24 | import java.util.Set; |
24 | | -import java.util.concurrent.TimeUnit; |
25 | 25 |
|
26 | 26 | import io.kubernetes.client.openapi.ApiClient; |
27 | 27 | import io.kubernetes.client.openapi.apis.CoreV1Api; |
|
30 | 30 | import io.kubernetes.client.openapi.models.V1Deployment; |
31 | 31 | import io.kubernetes.client.openapi.models.V1Service; |
32 | 32 | import io.kubernetes.client.util.Config; |
33 | | -import org.assertj.core.api.Assertions; |
34 | | -import org.assertj.core.api.Condition; |
| 33 | +import org.awaitility.Awaitility; |
35 | 34 | import org.junit.jupiter.api.BeforeAll; |
36 | 35 | import org.junit.jupiter.api.extension.ExtendWith; |
37 | | -import org.springframework.boot.test.system.CapturedOutput; |
38 | | -import org.springframework.http.HttpMethod; |
39 | | -import org.springframework.web.reactive.function.client.WebClient; |
40 | 36 | import org.testcontainers.k3s.K3sContainer; |
41 | 37 |
|
| 38 | +import org.springframework.boot.test.system.CapturedOutput; |
42 | 39 | import org.springframework.boot.test.system.OutputCaptureExtension; |
| 40 | +import org.springframework.cloud.kubernetes.commons.discovery.EndpointNameAndNamespace; |
43 | 41 | import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; |
44 | 42 | import org.springframework.cloud.kubernetes.integration.tests.commons.Commons; |
45 | 43 | import org.springframework.cloud.kubernetes.integration.tests.commons.Phase; |
@@ -112,52 +110,41 @@ protected static void serviceAccount(Phase phase) { |
112 | 110 | rbacApi.createClusterRoleBinding(clusterRoleBinding, null, null, null, null); |
113 | 111 | } |
114 | 112 | else { |
115 | | - rbacApi.deleteClusterRoleBinding(clusterRoleBinding.getMetadata().getName(), |
116 | | - null, null, null, null, null, null); |
| 113 | + rbacApi.deleteClusterRoleBinding(clusterRoleBinding.getMetadata().getName(), null, null, null, null, |
| 114 | + null, null); |
117 | 115 | } |
118 | 116 | } |
119 | | - catch(Exception e) { |
| 117 | + catch (Exception e) { |
120 | 118 | throw new RuntimeException(e); |
121 | 119 | } |
122 | 120 |
|
123 | 121 | } |
124 | 122 |
|
125 | | - protected static void testHeartBeat(K3sContainer container, |
126 | | - HeartbeatListener heartbeatListener, CapturedOutput output) { |
| 123 | + protected static void testHeartBeat(HeartbeatListener heartbeatListener, CapturedOutput output) { |
127 | 124 |
|
128 | 125 | // 1. logs from discovery server |
129 | | - Commons.waitForLogStatement("using delay : 3000", container, DISCOVERY_SERVER_LABEL); |
130 | | - Commons.waitForLogStatement("received heartbeat event", container, DISCOVERY_SERVER_LABEL); |
131 | | - Commons.waitForLogStatement("state received :", container, DISCOVERY_SERVER_LABEL); |
| 126 | + Commons.waitForLogStatement("using delay : 3000", K3S, DISCOVERY_SERVER_LABEL); |
| 127 | + Commons.waitForLogStatement("received heartbeat event", K3S, DISCOVERY_SERVER_LABEL); |
| 128 | + Commons.waitForLogStatement("state received :", K3S, DISCOVERY_SERVER_LABEL); |
132 | 129 |
|
133 | 130 | // 2. logs from discovery client |
134 | 131 | assertThat(output.getOut()).contains("using delay : 3000"); |
135 | 132 | assertThat(output.getOut()).contains("state received : "); |
| 133 | + assertThat(output.getOut()).contains("received heartbeat event in listener"); |
| 134 | + |
| 135 | + // 3. heartbeat listener message |
| 136 | + List<EndpointNameAndNamespace> result = heartbeatListener.state.get(); |
| 137 | + Awaitility.await() |
| 138 | + .atMost(Duration.ofSeconds(60)) |
| 139 | + .pollInterval(Duration.ofSeconds(1)) |
| 140 | + .until(() -> result.size() == 2); |
| 141 | + |
| 142 | + List<String> namespaces = result.stream().map(EndpointNameAndNamespace::namespace).toList(); |
| 143 | + assertThat(namespaces).containsExactlyInAnyOrder(NAMESPACE_LEFT, NAMESPACE_RIGHT); |
136 | 144 |
|
137 | | -// // 3. heartbeat listener message |
138 | | -// WebClient.Builder builder = builder(); |
139 | | -// WebClient client = builder.baseUrl("http://localhost:80/discoveryclient-it/state").build(); |
140 | | -// String result = client.method(HttpMethod.GET) |
141 | | -// .retrieve() |
142 | | -// .bodyToMono(String.class) |
143 | | -// .retryWhen(retrySpec()) |
144 | | -// .block(); |
145 | | -// |
146 | | -// Condition<LinkedHashMap<String, String>> wireMockService = new Condition<>( |
147 | | -// map -> map.entrySet().stream().anyMatch(en -> en.getValue().contains("service-wiremock-deployment")), |
148 | | -// ""); |
149 | | -// |
150 | | -// Condition<LinkedHashMap<String, String>> discoveryServerService = new Condition<>(map -> map.entrySet() |
151 | | -// .stream() |
152 | | -// .anyMatch(en -> en.getValue().contains("spring-cloud-kubernetes-k8s-client-discovery-server")), ""); |
153 | | -// |
154 | | -// Assertions.assertThat(BASIC_JSON_TESTER.from(result)) |
155 | | -// .<LinkedHashMap<String, String>>extractingJsonPathArrayValue("$.[*]") |
156 | | -// .areAtLeastOne(wireMockService); |
157 | | -// |
158 | | -// Assertions.assertThat(BASIC_JSON_TESTER.from(result)) |
159 | | -// .<LinkedHashMap<String, String>>extractingJsonPathArrayValue("$.[*]") |
160 | | -// .areAtLeastOne(discoveryServerService); |
| 145 | + List<String> endpointNames = result.stream().map(EndpointNameAndNamespace::endpointName).toList(); |
| 146 | + assertThat(endpointNames.get(0)).contains("service-wiremock-deployment"); |
| 147 | + assertThat(endpointNames.get(1)).contains("service-wiremock-deployment"); |
161 | 148 | } |
162 | 149 |
|
163 | 150 | } |
0 commit comments