|
| 1 | +/* |
| 2 | + * Copyright 2013-2025 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.k8s.client.catalog.watcher.it; |
| 18 | + |
| 19 | +import org.assertj.core.api.Assertions; |
| 20 | +import org.awaitility.Awaitility; |
| 21 | +import org.springframework.boot.test.system.CapturedOutput; |
| 22 | +import org.springframework.cloud.kubernetes.commons.discovery.EndpointNameAndNamespace; |
| 23 | +import org.springframework.cloud.kubernetes.integration.tests.commons.Phase; |
| 24 | +import org.springframework.cloud.kubernetes.integration.tests.commons.fabric8_client.Util; |
| 25 | +import org.springframework.core.ParameterizedTypeReference; |
| 26 | +import org.springframework.core.ResolvableType; |
| 27 | +import org.springframework.http.HttpMethod; |
| 28 | +import org.springframework.web.reactive.function.client.WebClient; |
| 29 | + |
| 30 | +import java.time.Duration; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.Set; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.awaitility.Awaitility.await; |
| 37 | +import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.builder; |
| 38 | +import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.retrySpec; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author wind57 |
| 42 | + */ |
| 43 | +final class TestAssertions { |
| 44 | + |
| 45 | + private TestAssertions() { |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + static void assertLogStatement(CapturedOutput output, String textToAssert) { |
| 50 | + Awaitility.await() |
| 51 | + .during(Duration.ofSeconds(5)) |
| 52 | + .pollInterval(Duration.ofMillis(200)) |
| 53 | + .untilAsserted(() -> Assertions.assertThat(output.getOut()).contains(textToAssert)); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * the checks are the same for both endpoints and endpoint slices, while the set-up |
| 58 | + * for them is different. |
| 59 | + */ |
| 60 | + @SuppressWarnings("unchecked") |
| 61 | + static void invokeAndAssert(Util util, Set<String> namespaces, int port, String assertionNamespace) { |
| 62 | + |
| 63 | + WebClient client = builder().baseUrl("http://localhost:" + port + "/result").build(); |
| 64 | + EndpointNameAndNamespace[] holder = new EndpointNameAndNamespace[2]; |
| 65 | + ResolvableType resolvableType = ResolvableType.forClassWithGenerics(List.class, EndpointNameAndNamespace.class); |
| 66 | + |
| 67 | + await().pollInterval(Duration.ofMillis(200)).atMost(Duration.ofSeconds(30)).until(() -> { |
| 68 | + List<EndpointNameAndNamespace> result = (List<EndpointNameAndNamespace>) client.method(HttpMethod.GET) |
| 69 | + .retrieve() |
| 70 | + .bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType())) |
| 71 | + .retryWhen(retrySpec()) |
| 72 | + .block(); |
| 73 | + |
| 74 | + if (result != null) { |
| 75 | + if (result.size() != 2) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + holder[0] = result.get(0); |
| 79 | + holder[1] = result.get(1); |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + return false; |
| 84 | + }); |
| 85 | + |
| 86 | + EndpointNameAndNamespace resultOne = holder[0]; |
| 87 | + EndpointNameAndNamespace resultTwo = holder[1]; |
| 88 | + |
| 89 | + assertThat(resultOne).isNotNull(); |
| 90 | + assertThat(resultTwo).isNotNull(); |
| 91 | + |
| 92 | + assertThat(resultOne.endpointName()).contains("busybox"); |
| 93 | + assertThat(resultTwo.endpointName()).contains("busybox"); |
| 94 | + |
| 95 | + assertThat(resultOne.namespace()).isEqualTo(assertionNamespace); |
| 96 | + assertThat(resultTwo.namespace()).isEqualTo(assertionNamespace); |
| 97 | + |
| 98 | + namespaces.forEach(namespace -> util.busybox(namespace, Phase.DELETE)); |
| 99 | + |
| 100 | + await().pollInterval(Duration.ofSeconds(1)).atMost(Duration.ofSeconds(240)).until(() -> { |
| 101 | + List<EndpointNameAndNamespace> result = (List<EndpointNameAndNamespace>) client.method(HttpMethod.GET) |
| 102 | + .retrieve() |
| 103 | + .bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType())) |
| 104 | + .retryWhen(retrySpec()) |
| 105 | + .block(); |
| 106 | + |
| 107 | + // we need to get the event from KubernetesCatalogWatch, but that happens |
| 108 | + // on periodic bases. So in order to be sure we got the event we care about |
| 109 | + // we wait until there is no entry, which means busybox was deleted |
| 110 | + // and KubernetesCatalogWatch received that update. |
| 111 | + return Objects.requireNonNull(result).isEmpty(); |
| 112 | + }); |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments