diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ResourceLock.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ResourceLock.java index eedc72e0acc..c9eea69c735 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ResourceLock.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extended/leaderelection/resourcelock/ResourceLock.java @@ -77,7 +77,7 @@ public synchronized void update(KubernetesClient client, LeaderElectionRecord le protected abstract LeaderElectionRecord toRecord(T resource); protected ObjectMetaBuilder getObjectMeta(String version) { - return new ObjectMetaBuilder(meta).withResourceVersion((String) version); + return new ObjectMetaBuilder(meta).withResourceVersion(version); } /** diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionCrudTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionCrudTest.java new file mode 100644 index 00000000000..e81a8cdbc14 --- /dev/null +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionCrudTest.java @@ -0,0 +1,38 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.kubernetes.client.mock; + +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.ConfigMapLock; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static io.fabric8.kubernetes.client.mock.LeaderElectionTest.testAndAssertSingleLeader; + +@EnableKubernetesMockClient(crud = true) +@DisplayName("LeaderElection runs on Kubernetes Mock Server in CRUD mode") +class LeaderElectionCrudTest { + + KubernetesClient client; + + @Test + void singleLeaderConfigMapLockTest() throws Exception { + testAndAssertSingleLeader(client, "lead-config-map-crud", + new ConfigMapLock("namespace", "name", "lead-config-map-crud")); + + } +} diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionTest.java index 19c0af84e49..b68d9a50409 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/LeaderElectionTest.java @@ -42,14 +42,14 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -@EnableKubernetesMockClient(crud = true) -public class LeaderElectionTest { +@EnableKubernetesMockClient +class LeaderElectionTest { KubernetesMockServer server; KubernetesClient client; @Test - public void singleLeaderConfigMapLockCreateTest() throws Exception { + void singleLeaderConfigMapLockCreateTest() throws Exception { // Given server.expect() .post() @@ -57,12 +57,12 @@ public void singleLeaderConfigMapLockCreateTest() throws Exception { .andReturn(200, null) .once(); // When - Then - testAndAssertSingleLeader("lead-config-map", + testAndAssertSingleLeader(client, "lead-config-map", new ConfigMapLock("namespace", "name", "lead-config-map")); } @Test - public void singleLeaderConfigMapLockUpdateTest() throws Exception { + void singleLeaderConfigMapLockUpdateTest() throws Exception { // Given server.expect() .get() @@ -82,17 +82,12 @@ public void singleLeaderConfigMapLockUpdateTest() throws Exception { .andReturn(200, null) .once(); // When - Then - testAndAssertSingleLeader("lead-config-map", - new ConfigMapLock("namespace", "name", "lead-config-map")); - - // verify crud mock behavior - server.clearExpectations(); - testAndAssertSingleLeader("lead-config-map", + testAndAssertSingleLeader(client, "lead-config-map", new ConfigMapLock("namespace", "name", "lead-config-map")); } @Test - public void singleLeaderLeaseLockCreateTest() throws Exception { + void singleLeaderLeaseLockCreateTest() throws Exception { // Given server.expect() .post() @@ -100,12 +95,12 @@ public void singleLeaderLeaseLockCreateTest() throws Exception { .andReturn(200, null) .once(); // When - Then - testAndAssertSingleLeader("lead-lease", + testAndAssertSingleLeader(client, "lead-lease", new LeaseLock("namespace", "name", "lead-lease")); } @Test - public void singleLeaderLeaseLockUpdateTest() throws Exception { + void singleLeaderLeaseLockUpdateTest() throws Exception { // Given server.expect() .get() @@ -129,11 +124,11 @@ public void singleLeaderLeaseLockUpdateTest() throws Exception { .andReturn(200, null) .once(); // When - Then - testAndAssertSingleLeader("lead-lease", + testAndAssertSingleLeader(client, "lead-lease", new LeaseLock("namespace", "name", "lead-lease")); } - private void testAndAssertSingleLeader(String id, Lock lock) throws Exception { + static void testAndAssertSingleLeader(KubernetesClient client, String id, Lock lock) throws Exception { // Given final CountDownLatch leaderLatch = new CountDownLatch(1); final AtomicReference newLeaderRecord = new AtomicReference<>(); @@ -161,7 +156,7 @@ private void testAndAssertSingleLeader(String id, Lock lock) throws Exception { assertEquals(0, leaderLatch.getCount()); leaderElectorTask.cancel(true); executorService.shutdownNow(); - executorService.awaitTermination(10, TimeUnit.SECONDS); + assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS)); assertTrue(stoppedLeading.await(10, TimeUnit.SECONDS)); } }