Skip to content

Commit fb656dd

Browse files
authored
Refactor loadbalancer it test (14) (#1443)
1 parent 99a455e commit fb656dd

File tree

5 files changed

+238
-73
lines changed

5 files changed

+238
-73
lines changed

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-client-loadbalancer-it/src/test/java/org/springframework/cloud/kubernetes/client/loadbalancer/it/LoadBalancerIT.java

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,61 @@
2323
import io.kubernetes.client.openapi.models.V1Deployment;
2424
import io.kubernetes.client.openapi.models.V1Ingress;
2525
import io.kubernetes.client.openapi.models.V1Service;
26+
import org.assertj.core.api.Assertions;
2627
import org.junit.jupiter.api.AfterAll;
2728
import org.junit.jupiter.api.AfterEach;
28-
import org.junit.jupiter.api.Assertions;
2929
import org.junit.jupiter.api.BeforeAll;
3030
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.MethodOrderer;
32+
import org.junit.jupiter.api.Order;
3133
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.TestMethodOrder;
3235
import org.testcontainers.k3s.K3sContainer;
3336
import reactor.netty.http.client.HttpClient;
3437
import reactor.util.retry.Retry;
3538
import reactor.util.retry.RetryBackoffSpec;
3639

40+
import org.springframework.boot.test.json.BasicJsonTester;
3741
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
3842
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
3943
import org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util;
40-
import org.springframework.core.ParameterizedTypeReference;
41-
import org.springframework.core.ResolvableType;
4244
import org.springframework.http.HttpMethod;
4345
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
4446
import org.springframework.web.reactive.function.client.WebClient;
4547

48+
import static org.springframework.cloud.kubernetes.integration.tests.commons.native_client.Util.patchWithMerge;
49+
4650
/**
4751
* @author Ryan Baxter
4852
*/
53+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
4954
class LoadBalancerIT {
5055

56+
private static final BasicJsonTester BASIC_JSON_TESTER = new BasicJsonTester(LoadBalancerIT.class);
57+
58+
private static final String BODY_FOR_MERGE = """
59+
{
60+
"spec": {
61+
"template": {
62+
"spec": {
63+
"containers": [{
64+
"name": "spring-cloud-kubernetes-client-loadbalancer-it",
65+
"env": [
66+
{
67+
"name": "SPRING_CLOUD_KUBERNETES_LOADBALANCER_MODE",
68+
"value": "SERVICE"
69+
}
70+
]
71+
}]
72+
}
73+
}
74+
}
75+
}
76+
""";
77+
78+
private static final Map<String, String> POD_LABELS = Map.of("app",
79+
"spring-cloud-kubernetes-client-loadbalancer-it");
80+
5181
private static final String SERVICE_URL = "http://localhost:80/loadbalancer-it/service";
5282

5383
private static final String SPRING_CLOUD_K8S_LOADBALANCER_APP_NAME = "spring-cloud-kubernetes-client-loadbalancer-it";
@@ -65,10 +95,12 @@ static void beforeAll() throws Exception {
6595
Commons.loadSpringCloudKubernetesImage(SPRING_CLOUD_K8S_LOADBALANCER_APP_NAME, K3S);
6696
util = new Util(K3S);
6797
util.setUp(NAMESPACE);
98+
loadbalancerIt(Phase.CREATE);
6899
}
69100

70101
@AfterAll
71102
static void afterAll() throws Exception {
103+
loadbalancerIt(Phase.DELETE);
72104
Commons.cleanUp(SPRING_CLOUD_K8S_LOADBALANCER_APP_NAME, K3S);
73105
Commons.systemPrune();
74106
}
@@ -84,39 +116,32 @@ void afterEach() {
84116
}
85117

86118
@Test
87-
void testLoadBalancerServiceMode() {
88-
loadbalancerIt(false, Phase.CREATE);
119+
@Order(1)
120+
void testLoadBalancerPodMode() {
89121
testLoadBalancer();
90-
loadbalancerIt(false, Phase.DELETE);
91122
}
92123

93124
@Test
94-
void testLoadBalancerPodMode() {
95-
loadbalancerIt(true, Phase.CREATE);
125+
@Order(2)
126+
void testLoadBalancerServiceMode() {
127+
patchForServiceMode("spring-cloud-kubernetes-client-loadbalancer-it-deployment", NAMESPACE);
96128
testLoadBalancer();
97-
loadbalancerIt(true, Phase.DELETE);
98129
}
99130

100131
private void testLoadBalancer() {
101132

102133
WebClient.Builder builder = builder();
103134
WebClient serviceClient = builder.baseUrl(SERVICE_URL).build();
104135

105-
ResolvableType resolvableType = ResolvableType.forClassWithGenerics(Map.class, String.class, Object.class);
106-
@SuppressWarnings("unchecked")
107-
Map<String, Object> result = (Map<String, Object>) serviceClient.method(HttpMethod.GET).retrieve()
108-
.bodyToMono(ParameterizedTypeReference.forType(resolvableType.getType())).retryWhen(retrySpec())
109-
.block();
110-
111-
Assertions.assertTrue(result.containsKey("mappings"));
112-
Assertions.assertTrue(result.containsKey("meta"));
113-
136+
String result = serviceClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).block();
137+
Assertions.assertThat(BASIC_JSON_TESTER.from(result)).extractingJsonPathArrayValue("$.mappings").isEmpty();
138+
Assertions.assertThat(BASIC_JSON_TESTER.from(result)).extractingJsonPathNumberValue("$.meta.total")
139+
.isEqualTo(0);
114140
}
115141

116-
private void loadbalancerIt(boolean podBased, Phase phase) {
117-
V1Deployment deployment = podBased
118-
? (V1Deployment) util.yaml("spring-cloud-kubernetes-client-loadbalancer-pod-it-deployment.yaml")
119-
: (V1Deployment) util.yaml("spring-cloud-kubernetes-client-loadbalancer-service-it-deployment.yaml");
142+
private static void loadbalancerIt(Phase phase) {
143+
V1Deployment deployment = (V1Deployment) util
144+
.yaml("spring-cloud-kubernetes-client-loadbalancer-pod-it-deployment.yaml");
120145
V1Service service = (V1Service) util.yaml("spring-cloud-kubernetes-client-loadbalancer-it-service.yaml");
121146
V1Ingress ingress = (V1Ingress) util.yaml("spring-cloud-kubernetes-client-loadbalancer-it-ingress.yaml");
122147

@@ -136,4 +161,8 @@ private RetryBackoffSpec retrySpec() {
136161
return Retry.fixedDelay(15, Duration.ofSeconds(1)).filter(Objects::nonNull);
137162
}
138163

164+
private static void patchForServiceMode(String deploymentName, String namespace) {
165+
patchWithMerge(deploymentName, namespace, BODY_FOR_MERGE, POD_LABELS);
166+
}
167+
139168
}

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-client-loadbalancer-it/src/test/resources/spring-cloud-kubernetes-client-loadbalancer-service-it-deployment.yaml

Lines changed: 0 additions & 31 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2013-2021 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.discoveryclient.it;
18+
19+
import java.time.Duration;
20+
import java.util.Objects;
21+
22+
import org.assertj.core.api.Assertions;
23+
import reactor.netty.http.client.HttpClient;
24+
import reactor.util.retry.Retry;
25+
import reactor.util.retry.RetryBackoffSpec;
26+
27+
import org.springframework.boot.test.json.BasicJsonTester;
28+
import org.springframework.http.HttpMethod;
29+
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
30+
import org.springframework.web.reactive.function.client.WebClient;
31+
32+
/**
33+
* @author mbialkowski1
34+
*/
35+
final class DiscoveryClientFilterNamespaceDelegate {
36+
37+
private static final BasicJsonTester BASIC_JSON_TESTER = new BasicJsonTester(
38+
DiscoveryClientFilterNamespaceDelegate.class);
39+
40+
static void testNamespaceDiscoveryClient() {
41+
testLoadBalancer();
42+
testHealth();
43+
}
44+
45+
private static void testLoadBalancer() {
46+
47+
WebClient.Builder builder = builder();
48+
WebClient serviceClient = builder.baseUrl("http://localhost:80/discoveryclient-it/services").build();
49+
String result = serviceClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(retrySpec())
50+
.block();
51+
52+
Assertions.assertThat(BASIC_JSON_TESTER.from(result)).extractingJsonPathArrayValue("$")
53+
.contains("service-wiremock");
54+
55+
// ServiceInstance
56+
WebClient serviceInstanceClient = builder
57+
.baseUrl("http://localhost:80/discoveryclient-it/service/service-wiremock").build();
58+
String serviceInstances = serviceInstanceClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class)
59+
.retryWhen(retrySpec()).block();
60+
61+
Assertions.assertThat(BASIC_JSON_TESTER.from(serviceInstances)).extractingJsonPathStringValue("$.[0].serviceId")
62+
.isEqualTo("service-wiremock");
63+
64+
Assertions.assertThat(BASIC_JSON_TESTER.from(serviceInstances)).extractingJsonPathStringValue("$.[0].namespace")
65+
.isEqualTo("left");
66+
}
67+
68+
private static void testHealth() {
69+
WebClient.Builder builder = builder();
70+
WebClient serviceClient = builder.baseUrl("http://localhost:80/discoveryclient-it/actuator/health").build();
71+
72+
String health = serviceClient.method(HttpMethod.GET).retrieve().bodyToMono(String.class).retryWhen(retrySpec())
73+
.block();
74+
75+
Assertions.assertThat(BASIC_JSON_TESTER.from(health))
76+
.extractingJsonPathStringValue("$.components.discoveryComposite.status").isEqualTo("UP");
77+
}
78+
79+
private static WebClient.Builder builder() {
80+
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create()));
81+
}
82+
83+
private static RetryBackoffSpec retrySpec() {
84+
return Retry.fixedDelay(15, Duration.ofSeconds(1)).filter(Objects::nonNull);
85+
}
86+
87+
}

0 commit comments

Comments
 (0)