Skip to content

Commit b44cf33

Browse files
committed
test:refactor: extract LeaderElection CRUD specific test
Signed-off-by: Marc Nuri <[email protected]>
1 parent f91e0bd commit b44cf33

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ResourceLock.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public synchronized void update(KubernetesClient client, LeaderElectionRecord le
7777
protected abstract LeaderElectionRecord toRecord(T resource);
7878

7979
protected ObjectMetaBuilder getObjectMeta(String version) {
80-
return new ObjectMetaBuilder(meta).withResourceVersion((String) version);
80+
return new ObjectMetaBuilder(meta).withResourceVersion(version);
8181
}
8282

8383
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
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+
* http://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+
package io.fabric8.kubernetes.client.mock;
17+
18+
import io.fabric8.kubernetes.client.KubernetesClient;
19+
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.ConfigMapLock;
20+
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
21+
import org.junit.jupiter.api.DisplayName;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static io.fabric8.kubernetes.client.mock.LeaderElectionTest.testAndAssertSingleLeader;
25+
26+
@EnableKubernetesMockClient(crud = true)
27+
@DisplayName("LeaderElection runs on Kubernetes Mock Server in CRUD mode")
28+
class LeaderElectionCrudTest {
29+
30+
KubernetesClient client;
31+
32+
@Test
33+
void singleLeaderConfigMapLockTest() throws Exception {
34+
testAndAssertSingleLeader(client, "lead-config-map-crud",
35+
new ConfigMapLock("namespace", "name", "lead-config-map-crud"));
36+
37+
}
38+
}

kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionTest.java

+12-17
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,27 @@
4242
import static org.junit.jupiter.api.Assertions.assertThrows;
4343
import static org.junit.jupiter.api.Assertions.assertTrue;
4444

45-
@EnableKubernetesMockClient(crud = true)
46-
public class LeaderElectionTest {
45+
@EnableKubernetesMockClient
46+
class LeaderElectionTest {
4747

4848
KubernetesMockServer server;
4949
KubernetesClient client;
5050

5151
@Test
52-
public void singleLeaderConfigMapLockCreateTest() throws Exception {
52+
void singleLeaderConfigMapLockCreateTest() throws Exception {
5353
// Given
5454
server.expect()
5555
.post()
5656
.withPath("/api/v1/namespaces/namespace/configmaps")
5757
.andReturn(200, null)
5858
.once();
5959
// When - Then
60-
testAndAssertSingleLeader("lead-config-map",
60+
testAndAssertSingleLeader(client, "lead-config-map",
6161
new ConfigMapLock("namespace", "name", "lead-config-map"));
6262
}
6363

6464
@Test
65-
public void singleLeaderConfigMapLockUpdateTest() throws Exception {
65+
void singleLeaderConfigMapLockUpdateTest() throws Exception {
6666
// Given
6767
server.expect()
6868
.get()
@@ -82,30 +82,25 @@ public void singleLeaderConfigMapLockUpdateTest() throws Exception {
8282
.andReturn(200, null)
8383
.once();
8484
// When - Then
85-
testAndAssertSingleLeader("lead-config-map",
86-
new ConfigMapLock("namespace", "name", "lead-config-map"));
87-
88-
// verify crud mock behavior
89-
server.clearExpectations();
90-
testAndAssertSingleLeader("lead-config-map",
85+
testAndAssertSingleLeader(client, "lead-config-map",
9186
new ConfigMapLock("namespace", "name", "lead-config-map"));
9287
}
9388

9489
@Test
95-
public void singleLeaderLeaseLockCreateTest() throws Exception {
90+
void singleLeaderLeaseLockCreateTest() throws Exception {
9691
// Given
9792
server.expect()
9893
.post()
9994
.withPath("/apis/coordination.k8s.io/v1/namespaces/namespace/leases")
10095
.andReturn(200, null)
10196
.once();
10297
// When - Then
103-
testAndAssertSingleLeader("lead-lease",
98+
testAndAssertSingleLeader(client, "lead-lease",
10499
new LeaseLock("namespace", "name", "lead-lease"));
105100
}
106101

107102
@Test
108-
public void singleLeaderLeaseLockUpdateTest() throws Exception {
103+
void singleLeaderLeaseLockUpdateTest() throws Exception {
109104
// Given
110105
server.expect()
111106
.get()
@@ -129,11 +124,11 @@ public void singleLeaderLeaseLockUpdateTest() throws Exception {
129124
.andReturn(200, null)
130125
.once();
131126
// When - Then
132-
testAndAssertSingleLeader("lead-lease",
127+
testAndAssertSingleLeader(client, "lead-lease",
133128
new LeaseLock("namespace", "name", "lead-lease"));
134129
}
135130

136-
private void testAndAssertSingleLeader(String id, Lock lock) throws Exception {
131+
static void testAndAssertSingleLeader(KubernetesClient client, String id, Lock lock) throws Exception {
137132
// Given
138133
final CountDownLatch leaderLatch = new CountDownLatch(1);
139134
final AtomicReference<String> newLeaderRecord = new AtomicReference<>();
@@ -161,7 +156,7 @@ private void testAndAssertSingleLeader(String id, Lock lock) throws Exception {
161156
assertEquals(0, leaderLatch.getCount());
162157
leaderElectorTask.cancel(true);
163158
executorService.shutdownNow();
164-
executorService.awaitTermination(10, TimeUnit.SECONDS);
159+
assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS));
165160
assertTrue(stoppedLeading.await(10, TimeUnit.SECONDS));
166161
}
167162
}

0 commit comments

Comments
 (0)