Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: tests in appropriate packages after split #3886

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions kubernetes-client-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-node</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -144,7 +144,7 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-ext-jdk15on</artifactId>
Expand Down Expand Up @@ -190,6 +190,11 @@
<artifactId>junit-jupiter-migrationsupport</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
public interface Indexer<T> extends Store<T> {
/**
* Retrieve list of obejcts that match on the named indexing function.
* Retrieve list of objects that match on the named indexing function.
*
* @param indexName specific indexing function
* @param obj object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
* @param <T> the object type
*/
public class Lister<T> {
private String namespace;
private final String namespace;

private String indexName;
private final String indexName;

private Indexer<T> indexer;
private final Indexer<T> indexer;

public Lister(Indexer<T> indexer) {
this(indexer, null, Cache.NAMESPACE_INDEX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
import java.net.URL;

public class URLUtils {

public static class URLBuilder {
private StringBuilder url;

private final StringBuilder url;

public URLBuilder(String url) {
this.url = new StringBuilder(url);
}

public URLBuilder(URL url) {
this(url.toString());
}
Expand All @@ -53,14 +53,14 @@ public URL build() {
throw new IllegalArgumentException(e.getMessage(), e);
}
}

@Override
public String toString() {
return build().toString();
}

}

private URLUtils() {
throw new IllegalStateException("Utility class");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.fabric8.kubernetes.client.extended.leaderelection;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.ConfigMapLock;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -30,12 +29,12 @@

class LeaderElectorBuilderTest {

private DefaultKubernetesClient mockKubernetesClient;
private NamespacedKubernetesClient mockKubernetesClient;
private LeaderElectionConfigBuilder defaultConfigBuilder;

@BeforeEach
void setUp() {
mockKubernetesClient = mock(DefaultKubernetesClient.class, Answers.RETURNS_DEEP_STUBS);
mockKubernetesClient = mock(NamespacedKubernetesClient.class, Answers.RETURNS_DEEP_STUBS);
defaultConfigBuilder = new LeaderElectionConfigBuilder()
.withName("Valid Leader Election configuration")
.withLeaseDuration(Duration.ofSeconds(15L))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package io.fabric8.kubernetes.client.extended.leaderelection;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.LeaderElectionRecord;
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.Lock;
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.LockException;
Expand Down Expand Up @@ -68,7 +68,7 @@ void runShouldAbortAfterRenewDeadlineExpired() throws Exception {
}).when(mockedLock).update(any(), any());
// When
executor.submit(() -> {
new LeaderElector<>(mock(DefaultKubernetesClient.class), lec).run();
new LeaderElector<>(mock(NamespacedKubernetesClient.class), lec).run();
signal.countDown();
});
signal.await(10, TimeUnit.SECONDS);
Expand Down Expand Up @@ -96,7 +96,7 @@ void runShouldEndlesslyRun() throws Exception {
return null;
}).when(mockedLock).update(any(), any());
// When
executor.submit(() -> new LeaderElector<>(mock(DefaultKubernetesClient.class), lec).run());
executor.submit(() -> new LeaderElector<>(mock(NamespacedKubernetesClient.class), lec).run());
signal.await(10, TimeUnit.SECONDS);
// Then
assertEquals(0, signal.getCount());
Expand All @@ -118,7 +118,7 @@ void isLeaderAndIsLeaderShouldReturnTrue() {
final LeaderElectionRecord ler = mock(LeaderElectionRecord.class);
when(ler.getHolderIdentity()).thenReturn("1337");
// When
final boolean result = new LeaderElector<>(mock(DefaultKubernetesClient.class), lec).isLeader(ler);
final boolean result = new LeaderElector<>(mock(NamespacedKubernetesClient.class), lec).isLeader(ler);
// Then
assertTrue(result);
}
Expand All @@ -131,7 +131,7 @@ void isLeaderAndIsNotLeaderShouldReturnFalse() {
final LeaderElectionRecord ler = mock(LeaderElectionRecord.class);
when(ler.getHolderIdentity()).thenReturn("1337");
// When
final boolean result = new LeaderElector<>(mock(DefaultKubernetesClient.class), lec).isLeader(ler);
final boolean result = new LeaderElector<>(mock(NamespacedKubernetesClient.class), lec).isLeader(ler);
// Then
assertFalse(result);
}
Expand All @@ -144,7 +144,7 @@ void canBecomeLeaderAndDifferentLeaderWithExpiredLockShouldReturnTrue() {
final LeaderElectionRecord ler = mock(LeaderElectionRecord.class);
when(ler.getRenewTime()).thenReturn(ZonedDateTime.now(ZoneOffset.UTC).minusHours(1));
// When
final boolean result = new LeaderElector<>(mock(DefaultKubernetesClient.class), lec).canBecomeLeader(ler);
final boolean result = new LeaderElector<>(mock(NamespacedKubernetesClient.class), lec).canBecomeLeader(ler);
// Then
assertTrue(result);
}
Expand All @@ -157,7 +157,7 @@ void canBecomeLeaderAndDifferentLeaderWithActiveLockShouldReturnFalse() {
final LeaderElectionRecord ler = mock(LeaderElectionRecord.class);
when(ler.getRenewTime()).thenReturn(ZonedDateTime.now(ZoneOffset.UTC));
// When
final boolean result = new LeaderElector<>(mock(DefaultKubernetesClient.class), lec).canBecomeLeader(ler);
final boolean result = new LeaderElector<>(mock(NamespacedKubernetesClient.class), lec).canBecomeLeader(ler);
// Then
assertFalse(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ConfigMapList;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.ReplaceDeletable;
import io.fabric8.kubernetes.client.dsl.Resource;
Expand All @@ -28,6 +28,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.Answers;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

import java.time.Duration;
import java.time.ZoneId;
Expand All @@ -41,24 +43,25 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class ConfigMapLockTest {

private DefaultKubernetesClient kc;
private NamespacedKubernetesClient kc;
private MixedOperation<ConfigMap, ConfigMapList, Resource<ConfigMap>> configMaps;
private ConfigMapBuilder configMapBuilder;
private ConfigMapBuilder.MetadataNested<ConfigMapBuilder> metadata;

@BeforeEach
void setUp() {
kc = mock(DefaultKubernetesClient.class, Answers.RETURNS_DEEP_STUBS);
configMaps = mock(MixedOperation.class, Answers.RETURNS_DEEP_STUBS);
configMapBuilder = mock(ConfigMapBuilder.class, Answers.RETURNS_DEEP_STUBS);
metadata = mock(ConfigMapBuilder.MetadataNested.class, Answers.RETURNS_DEEP_STUBS);
kc = mock(NamespacedKubernetesClient.class, RETURNS_DEEP_STUBS);
configMaps = mock(MixedOperation.class, RETURNS_DEEP_STUBS);
configMapBuilder = Mockito.mock(ConfigMapBuilder.class, RETURNS_DEEP_STUBS);
metadata = mock(ConfigMapBuilder.MetadataNested.class, RETURNS_DEEP_STUBS);
when(kc.inNamespace(anyString()).configMaps()).thenReturn(configMaps);
when(configMapBuilder.editOrNewMetadata()).thenReturn(metadata);
}
Expand Down Expand Up @@ -99,7 +102,7 @@ void missingIdentityShouldThrowException() {
void getWithExistingConfigMapShouldReturnLeaderElectionRecord() {
// Given
final ConfigMap cm = new ConfigMap();
when(configMaps.withName(eq("name")).get()).thenReturn(cm);
when(configMaps.withName(ArgumentMatchers.eq("name")).get()).thenReturn(cm);
cm.setMetadata(new ObjectMetaBuilder()
.withAnnotations(
Collections.singletonMap("control-plane.alpha.kubernetes.io/leader",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.fabric8.kubernetes.api.model.coordination.v1.LeaseBuilder;
import io.fabric8.kubernetes.api.model.coordination.v1.LeaseList;
import io.fabric8.kubernetes.api.model.coordination.v1.LeaseSpec;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.ReplaceDeletable;
import io.fabric8.kubernetes.client.dsl.Resource;
Expand All @@ -39,26 +39,27 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class LeaseLockTest {

private DefaultKubernetesClient kc;
private NamespacedKubernetesClient kc;
private MixedOperation<Lease, LeaseList, Resource<Lease>> leases;
private LeaseBuilder leaserBuilder;
private LeaseBuilder.MetadataNested<LeaseBuilder> metadata;
private LeaseBuilder.SpecNested<LeaseBuilder> spec;

@BeforeEach
void setUp() {
kc = mock(DefaultKubernetesClient.class, Answers.RETURNS_DEEP_STUBS);
leases = mock(MixedOperation.class, Answers.RETURNS_DEEP_STUBS);
leaserBuilder = mock(LeaseBuilder.class, Answers.RETURNS_DEEP_STUBS);
metadata = mock(LeaseBuilder.MetadataNested.class, Answers.RETURNS_DEEP_STUBS);
spec = mock(LeaseBuilder.SpecNested.class, Answers.RETURNS_DEEP_STUBS);
kc = mock(NamespacedKubernetesClient.class, RETURNS_DEEP_STUBS);
leases = mock(MixedOperation.class, RETURNS_DEEP_STUBS);
leaserBuilder = mock(LeaseBuilder.class, RETURNS_DEEP_STUBS);
metadata = mock(LeaseBuilder.MetadataNested.class, RETURNS_DEEP_STUBS);
spec = mock(LeaseBuilder.SpecNested.class, RETURNS_DEEP_STUBS);
when(kc.inNamespace(anyString()).leases()).thenReturn(leases);
when(leaserBuilder.withNewMetadata()).thenReturn(metadata);
when(leaserBuilder.withNewSpec()).thenReturn(spec);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* 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.informers.cache;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ListerTest {

@Test
void testListerWithEmptyIndex() {
final Lister<String> namespacedPodLister = new Lister<>(new MapIndexer<>(), "default");
assertTrue(namespacedPodLister.list().isEmpty());
}

@Test
void testListerWithIndexValues() {
// Given
final MapIndexer<Integer> indexer = new MapIndexer<>();
indexer.put("default", "key1", 1);
indexer.put("other", "key2", 1);
indexer.put("other", "key3", 1);
// When
final Lister<Integer> result = new Lister<>(indexer, "default");
// Then
assertEquals(1, result.list().size());
}

@Test
void testListerWithIndexValuesInOther() {
// Given
final MapIndexer<Integer> indexer = new MapIndexer<>();
indexer.put("default", "key1", 1);
indexer.put("other", "key2", 1);
indexer.put("other", "key3", 1);
// When
final Lister<Integer> result = new Lister<>(indexer, "default");
// Then
assertEquals(2, result.namespace("other").list().size());
}

}
Loading