Skip to content

Commit 3708301

Browse files
committed
add tests
Signed-off-by: wind57 <[email protected]>
1 parent c33f20c commit 3708301

File tree

2 files changed

+246
-0
lines changed

2 files changed

+246
-0
lines changed

spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2728
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2829
import org.springframework.cloud.kubernetes.commons.leader.LeaderInfoContributor;
2930
import org.springframework.cloud.kubernetes.commons.leader.LeaderInitiator;
@@ -45,6 +46,7 @@
4546
@EnableConfigurationProperties(LeaderProperties.class)
4647
@ConditionalOnBean(KubernetesClient.class)
4748
@ConditionalOnLeaderElectionDisabled
49+
@ConditionalOnProperty(value = "spring.cloud.kubernetes.leader.enabled", havingValue = "true", matchIfMissing = true)
4850
public class Fabric8LeaderAutoConfiguration {
4951

5052
/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
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.fabric8.leader.election;
18+
19+
import io.fabric8.kubernetes.api.model.APIGroupList;
20+
import io.fabric8.kubernetes.api.model.APIGroupListBuilder;
21+
import io.fabric8.kubernetes.api.model.APIResourceBuilder;
22+
import io.fabric8.kubernetes.api.model.APIResourceListBuilder;
23+
import io.fabric8.kubernetes.api.model.ConfigMap;
24+
import io.fabric8.kubernetes.api.model.GroupVersionForDiscoveryBuilder;
25+
import io.fabric8.kubernetes.client.KubernetesClient;
26+
import io.fabric8.kubernetes.client.dsl.MixedOperation;
27+
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
28+
import io.fabric8.kubernetes.client.dsl.PodResource;
29+
import io.fabric8.kubernetes.client.dsl.Resource;
30+
import org.junit.jupiter.api.Test;
31+
import org.mockito.Mockito;
32+
33+
import org.springframework.boot.autoconfigure.AutoConfigurations;
34+
import org.springframework.boot.test.context.TestConfiguration;
35+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
36+
import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration;
37+
import org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration;
38+
import org.springframework.cloud.kubernetes.fabric8.leader.Fabric8LeaderAutoConfiguration;
39+
import org.springframework.context.annotation.Bean;
40+
41+
import static org.assertj.core.api.Assertions.assertThat;
42+
43+
/**
44+
* Tests that prove that previous and new leader implementation works based on the flags
45+
* we set.
46+
*
47+
* @author wind57
48+
*/
49+
class Fabric8LeaderOldAndNewImplementationTests {
50+
51+
private ApplicationContextRunner applicationContextRunner;
52+
53+
/**
54+
* <pre>
55+
* - 'spring.cloud.kubernetes.leader.enabled' is not set
56+
* - 'spring.cloud.kubernetes.leader.election.enabled' is not set
57+
*
58+
* As such :
59+
*
60+
* - 'Fabric8LeaderAutoConfiguration' is active
61+
* - 'Fabric8LeaderElectionAutoConfiguration' is not active
62+
* </pre>
63+
*/
64+
@Test
65+
void noFlagsSet() {
66+
setup("spring.main.cloud-platform=KUBERNETES");
67+
applicationContextRunner.run(context -> {
68+
assertThat(context).hasSingleBean(Fabric8LeaderAutoConfiguration.class);
69+
assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
70+
});
71+
}
72+
73+
/**
74+
* <pre>
75+
* - 'spring.cloud.kubernetes.leader.enabled' = true
76+
* - 'spring.cloud.kubernetes.leader.election.enabled' is not set
77+
*
78+
* As such :
79+
*
80+
* - 'Fabric8LeaderAutoConfiguration' is active
81+
* - 'Fabric8LeaderElectionAutoConfiguration' is not active
82+
* </pre>
83+
*/
84+
@Test
85+
void oldImplementationEnabled() {
86+
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.enabled=true");
87+
applicationContextRunner.run(context -> {
88+
assertThat(context).hasSingleBean(Fabric8LeaderAutoConfiguration.class);
89+
assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
90+
});
91+
}
92+
93+
/**
94+
* <pre>
95+
* - 'spring.cloud.kubernetes.leader.enabled' = false
96+
* - 'spring.cloud.kubernetes.leader.election.enabled' is not set
97+
*
98+
* As such :
99+
*
100+
* - 'Fabric8LeaderAutoConfiguration' is not active
101+
* - 'Fabric8LeaderElectionAutoConfiguration' is not active
102+
* </pre>
103+
*/
104+
@Test
105+
void oldImplementationDisabled() {
106+
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.enabled=false");
107+
applicationContextRunner.run(context -> {
108+
assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
109+
assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
110+
});
111+
}
112+
113+
/**
114+
* <pre>
115+
* - 'spring.cloud.kubernetes.leader.enabled' is not set
116+
* - 'spring.cloud.kubernetes.leader.election.enabled' = false
117+
*
118+
* As such :
119+
*
120+
* - 'Fabric8LeaderAutoConfiguration' is active
121+
* - 'Fabric8LeaderElectionAutoConfiguration' is not active
122+
* </pre>
123+
*/
124+
@Test
125+
void newImplementationDisabled() {
126+
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.election.enabled=false");
127+
applicationContextRunner.run(context -> {
128+
assertThat(context).hasSingleBean(Fabric8LeaderAutoConfiguration.class);
129+
assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
130+
});
131+
}
132+
133+
/**
134+
* <pre>
135+
* - 'spring.cloud.kubernetes.leader.enabled' is not set
136+
* - 'spring.cloud.kubernetes.leader.election.enabled' = true
137+
*
138+
* As such :
139+
*
140+
* - 'Fabric8LeaderAutoConfiguration' is not active
141+
* - 'Fabric8LeaderElectionAutoConfiguration' is active
142+
* </pre>
143+
*/
144+
@Test
145+
void newImplementationEnabled() {
146+
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.election.enabled=true");
147+
applicationContextRunner.run(context -> {
148+
assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
149+
assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
150+
});
151+
}
152+
153+
/**
154+
* <pre>
155+
* - 'spring.cloud.kubernetes.leader.enabled' = false
156+
* - 'spring.cloud.kubernetes.leader.election.enabled' = false
157+
*
158+
* As such :
159+
*
160+
* - 'Fabric8LeaderAutoConfiguration' is not active
161+
* - 'Fabric8LeaderElectionAutoConfiguration' is not active
162+
* </pre>
163+
*/
164+
@Test
165+
void bothDisabled() {
166+
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.enabled=false",
167+
"spring.cloud.kubernetes.leader.election.enabled=false");
168+
applicationContextRunner.run(context -> {
169+
assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
170+
assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
171+
});
172+
}
173+
174+
/**
175+
* <pre>
176+
* - 'spring.cloud.kubernetes.leader.enabled' = true
177+
* - 'spring.cloud.kubernetes.leader.election.enabled' = true
178+
*
179+
* As such :
180+
*
181+
* - 'Fabric8LeaderAutoConfiguration' is not active
182+
* - 'Fabric8LeaderElectionAutoConfiguration' is active
183+
*
184+
* You can't enable both of them, only the new one will work.
185+
* </pre>
186+
*/
187+
@Test
188+
void bothEnabled() {
189+
setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.enabled=true",
190+
"spring.cloud.kubernetes.leader.election.enabled=true");
191+
applicationContextRunner.run(context -> {
192+
assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
193+
assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
194+
});
195+
}
196+
197+
private void setup(String... properties) {
198+
applicationContextRunner = new ApplicationContextRunner()
199+
.withConfiguration(AutoConfigurations.of(Fabric8LeaderElectionCallbacksAutoConfiguration.class,
200+
Fabric8AutoConfiguration.class, KubernetesCommonsAutoConfiguration.class,
201+
Fabric8LeaderElectionAutoConfiguration.class, Fabric8LeaderAutoConfiguration.class))
202+
.withUserConfiguration(Fabric8LeaderOldAndNewImplementationTests.Configuration.class)
203+
.withPropertyValues(properties);
204+
}
205+
206+
@TestConfiguration
207+
static class Configuration {
208+
209+
@Bean
210+
@SuppressWarnings({ "rawtypes", "unchecked" })
211+
KubernetesClient mockKubernetesClient() {
212+
KubernetesClient client = Mockito.mock(KubernetesClient.class);
213+
214+
Mockito.when(client.getNamespace()).thenReturn("namespace");
215+
216+
MixedOperation mixedOperation = Mockito.mock(MixedOperation.class);
217+
NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
218+
Mockito.when(client.configMaps()).thenReturn(mixedOperation);
219+
220+
Mockito.when(mixedOperation.inNamespace(Mockito.anyString())).thenReturn(nonNamespaceOperation);
221+
Resource<ConfigMap> configMapResource = Mockito.mock(Resource.class);
222+
Mockito.when(nonNamespaceOperation.withName(Mockito.anyString())).thenReturn(configMapResource);
223+
224+
Mockito.when(client.pods()).thenReturn(mixedOperation);
225+
PodResource podResource = Mockito.mock(PodResource.class);
226+
Mockito.when(mixedOperation.withName(Mockito.anyString())).thenReturn(podResource);
227+
228+
Mockito.when(client.getApiResources("coordination.k8s.io/v1"))
229+
.thenReturn(
230+
new APIResourceListBuilder().withResources(new APIResourceBuilder().withKind("Lease").build())
231+
.build());
232+
233+
APIGroupList apiGroupList = new APIGroupListBuilder().addNewGroup()
234+
.withVersions(new GroupVersionForDiscoveryBuilder().withGroupVersion("coordination.k8s.io/v1").build())
235+
.endGroup()
236+
.build();
237+
238+
Mockito.when(client.getApiGroups()).thenReturn(apiGroupList);
239+
return client;
240+
}
241+
242+
}
243+
244+
}

0 commit comments

Comments
 (0)