|
| 1 | +/* |
| 2 | + * Copyright 2013-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.kubernetes.fabric8.discovery; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.assertj.core.api.Assertions; |
| 22 | +import org.testcontainers.k3s.K3sContainer; |
| 23 | + |
| 24 | +import org.springframework.boot.test.json.BasicJsonTester; |
| 25 | +import org.springframework.core.ParameterizedTypeReference; |
| 26 | +import org.springframework.http.HttpMethod; |
| 27 | +import org.springframework.web.reactive.function.client.WebClient; |
| 28 | + |
| 29 | +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtil.builder; |
| 30 | +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtil.retrySpec; |
| 31 | +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtil.waitForLogStatement; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author wind57 |
| 35 | + */ |
| 36 | +final class Fabric8DiscoveryClientHealthDelegate { |
| 37 | + |
| 38 | + private Fabric8DiscoveryClientHealthDelegate() { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + private static final String REACTIVE_STATUS = "$.components.reactiveDiscoveryClients.components.['Fabric8 Kubernetes Reactive Discovery Client'].status"; |
| 43 | + |
| 44 | + private static final String BLOCKING_STATUS = "$.components.discoveryComposite.components.discoveryClient.status"; |
| 45 | + |
| 46 | + private static final BasicJsonTester BASIC_JSON_TESTER = new BasicJsonTester( |
| 47 | + Fabric8DiscoveryClientHealthDelegate.class); |
| 48 | + |
| 49 | + /** |
| 50 | + * Reactive is disabled, only blocking is active. As such, |
| 51 | + * KubernetesInformerDiscoveryClientAutoConfiguration::indicatorInitializer will post |
| 52 | + * an InstanceRegisteredEvent. |
| 53 | + * |
| 54 | + * We assert for logs and call '/health' endpoint to see that blocking discovery |
| 55 | + * client was initialized. |
| 56 | + */ |
| 57 | + static void testBlockingConfiguration(K3sContainer k3sContainer, String imageName) { |
| 58 | + |
| 59 | + waitForLogStatement("Will publish InstanceRegisteredEvent from blocking implementation", k3sContainer, |
| 60 | + imageName); |
| 61 | + waitForLogStatement("publishing InstanceRegisteredEvent", k3sContainer, imageName); |
| 62 | + waitForLogStatement("Discovery Client has been initialized", k3sContainer, imageName); |
| 63 | + waitForLogStatement( |
| 64 | + "received InstanceRegisteredEvent from pod with 'app' label value : spring-cloud-kubernetes-fabric8-client-discovery", |
| 65 | + k3sContainer, imageName); |
| 66 | + |
| 67 | + WebClient healthClient = builder().baseUrl("http://localhost/actuator/health").build(); |
| 68 | + |
| 69 | + String healthResult = healthClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class) |
| 70 | + .retryWhen(retrySpec()).block(); |
| 71 | + |
| 72 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)) |
| 73 | + .extractingJsonPathStringValue("$.components.discoveryComposite.status").isEqualTo("UP"); |
| 74 | + |
| 75 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathStringValue(BLOCKING_STATUS) |
| 76 | + .isEqualTo("UP"); |
| 77 | + |
| 78 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)) |
| 79 | + .extractingJsonPathArrayValue( |
| 80 | + "$.components.discoveryComposite.components.discoveryClient.details.services") |
| 81 | + .containsExactlyInAnyOrder("spring-cloud-kubernetes-fabric8-client-discovery", "kubernetes", |
| 82 | + "busybox-service", "external-name-service", "service-wiremock"); |
| 83 | + |
| 84 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)).doesNotHaveJsonPath(REACTIVE_STATUS); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Both blocking and reactive are enabled. |
| 90 | + */ |
| 91 | + static void testDefaultConfiguration(K3sContainer k3sContainer, String imageName) { |
| 92 | + |
| 93 | + waitForLogStatement("Will publish InstanceRegisteredEvent from blocking implementation", k3sContainer, |
| 94 | + imageName); |
| 95 | + waitForLogStatement("publishing InstanceRegisteredEvent", k3sContainer, imageName); |
| 96 | + waitForLogStatement("Discovery Client has been initialized", k3sContainer, imageName); |
| 97 | + waitForLogStatement("received InstanceRegisteredEvent from pod with 'app' label value : " |
| 98 | + + "spring-cloud-kubernetes-fabric8-client-discovery", k3sContainer, imageName); |
| 99 | + |
| 100 | + WebClient healthClient = builder().baseUrl("http://localhost/actuator/health").build(); |
| 101 | + |
| 102 | + String healthResult = healthClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class) |
| 103 | + .retryWhen(retrySpec()).block(); |
| 104 | + |
| 105 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)) |
| 106 | + .extractingJsonPathStringValue("$.components.discoveryComposite.status").isEqualTo("UP"); |
| 107 | + |
| 108 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)) |
| 109 | + .extractingJsonPathStringValue("$.components.discoveryComposite.components.discoveryClient.status") |
| 110 | + .isEqualTo("UP"); |
| 111 | + |
| 112 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)) |
| 113 | + .extractingJsonPathArrayValue( |
| 114 | + "$.components.discoveryComposite.components.discoveryClient.details.services") |
| 115 | + .containsExactlyInAnyOrder("spring-cloud-kubernetes-fabric8-client-discovery", "kubernetes", |
| 116 | + "external-name-service", "service-wiremock", "busybox-service"); |
| 117 | + |
| 118 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)) |
| 119 | + .extractingJsonPathStringValue("$.components.reactiveDiscoveryClients.status").isEqualTo("UP"); |
| 120 | + |
| 121 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathStringValue( |
| 122 | + "$.components.reactiveDiscoveryClients.components.['Fabric8 Kubernetes Reactive Discovery Client'].status") |
| 123 | + .isEqualTo("UP"); |
| 124 | + |
| 125 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathArrayValue( |
| 126 | + "$.components.reactiveDiscoveryClients.components.['Fabric8 Kubernetes Reactive Discovery Client'].details.services") |
| 127 | + .containsExactlyInAnyOrder("spring-cloud-kubernetes-fabric8-client-discovery", "kubernetes", |
| 128 | + "external-name-service", "service-wiremock", "busybox-service"); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Reactive is enabled, blocking is disabled. As such, |
| 133 | + * KubernetesInformerDiscoveryClientAutoConfiguration::indicatorInitializer will post |
| 134 | + * an InstanceRegisteredEvent. |
| 135 | + * |
| 136 | + * We assert for logs and call '/health' endpoint to see that blocking discovery |
| 137 | + * client was initialized. |
| 138 | + */ |
| 139 | + static void testReactiveConfiguration(K3sContainer k3sContainer, String imageName) { |
| 140 | + |
| 141 | + waitForLogStatement("Will publish InstanceRegisteredEvent from reactive implementation", k3sContainer, |
| 142 | + imageName); |
| 143 | + waitForLogStatement("publishing InstanceRegisteredEvent", k3sContainer, imageName); |
| 144 | + waitForLogStatement("Discovery Client has been initialized", k3sContainer, imageName); |
| 145 | + waitForLogStatement( |
| 146 | + "received InstanceRegisteredEvent from pod with 'app' label value : spring-cloud-kubernetes-fabric8-client-discovery", |
| 147 | + k3sContainer, imageName); |
| 148 | + |
| 149 | + WebClient healthClient = builder().baseUrl("http://localhost/actuator/health").build(); |
| 150 | + |
| 151 | + String healthResult = healthClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class) |
| 152 | + .retryWhen(retrySpec()).block(); |
| 153 | + |
| 154 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)) |
| 155 | + .extractingJsonPathStringValue("$.components.reactiveDiscoveryClients.status").isEqualTo("UP"); |
| 156 | + |
| 157 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathStringValue(REACTIVE_STATUS) |
| 158 | + .isEqualTo("UP"); |
| 159 | + |
| 160 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathArrayValue( |
| 161 | + "$.components.reactiveDiscoveryClients.components.['Fabric8 Kubernetes Reactive Discovery Client'].details.services") |
| 162 | + .containsExactlyInAnyOrder("spring-cloud-kubernetes-fabric8-client-discovery", "kubernetes", |
| 163 | + "external-name-service", "service-wiremock", "busybox-service"); |
| 164 | + |
| 165 | + Assertions.assertThat(BASIC_JSON_TESTER.from(healthResult)).doesNotHaveJsonPath(BLOCKING_STATUS); |
| 166 | + |
| 167 | + // test for services also: |
| 168 | + |
| 169 | + WebClient servicesClient = builder().baseUrl("http://localhost/reactive/services").build(); |
| 170 | + |
| 171 | + List<String> servicesResult = servicesClient.method(HttpMethod.GET).retrieve() |
| 172 | + .bodyToMono(new ParameterizedTypeReference<List<String>>() { |
| 173 | + }).retryWhen(retrySpec()).block(); |
| 174 | + |
| 175 | + Assertions.assertThat(servicesResult).contains("spring-cloud-kubernetes-fabric8-client-discovery"); |
| 176 | + Assertions.assertThat(servicesResult).contains("kubernetes"); |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments