2323import io .kubernetes .client .openapi .models .V1Deployment ;
2424import io .kubernetes .client .openapi .models .V1Ingress ;
2525import io .kubernetes .client .openapi .models .V1Service ;
26+ import org .assertj .core .api .Assertions ;
2627import org .junit .jupiter .api .AfterAll ;
2728import org .junit .jupiter .api .AfterEach ;
28- import org .junit .jupiter .api .Assertions ;
2929import org .junit .jupiter .api .BeforeAll ;
3030import org .junit .jupiter .api .BeforeEach ;
31+ import org .junit .jupiter .api .MethodOrderer ;
32+ import org .junit .jupiter .api .Order ;
3133import org .junit .jupiter .api .Test ;
34+ import org .junit .jupiter .api .TestMethodOrder ;
3235import org .testcontainers .k3s .K3sContainer ;
3336import reactor .netty .http .client .HttpClient ;
3437import reactor .util .retry .Retry ;
3538import reactor .util .retry .RetryBackoffSpec ;
3639
40+ import org .springframework .boot .test .json .BasicJsonTester ;
3741import org .springframework .cloud .kubernetes .integration .tests .commons .Commons ;
3842import org .springframework .cloud .kubernetes .integration .tests .commons .Phase ;
3943import org .springframework .cloud .kubernetes .integration .tests .commons .native_client .Util ;
40- import org .springframework .core .ParameterizedTypeReference ;
41- import org .springframework .core .ResolvableType ;
4244import org .springframework .http .HttpMethod ;
4345import org .springframework .http .client .reactive .ReactorClientHttpConnector ;
4446import 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 )
4954class 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}
0 commit comments