Skip to content

Commit

Permalink
deps: bump kubernetes-client to 6.3.1
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Jan 4, 2023
1 parent 751a8aa commit 74c61cd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,101 +13,84 @@
*/
package org.eclipse.jkube.kit.config.access;

import java.net.UnknownHostException;
import java.util.function.Consumer;

import org.eclipse.jkube.kit.common.KitLogger;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.api.model.APIGroupListBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.openshift.client.OpenShiftClient;
import org.junit.jupiter.api.AfterEach;
import org.eclipse.jkube.kit.common.KitLogger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@EnableKubernetesMockClient(https = false)
class ClusterAccessTest {

private KitLogger logger;
private MockedConstruction<DefaultKubernetesClient> kubernetesClientMockedConstruction;
private Consumer<OpenShiftClient> onInstantiate;
private KubernetesMockServer mockServer;
private ClusterConfiguration clusterConfiguration;

@BeforeEach
void setUp() {
logger = spy(new KitLogger.SilentLogger());
onInstantiate = oc -> {
};
kubernetesClientMockedConstruction = mockConstruction(DefaultKubernetesClient.class, (mock, ctx) -> {
final OpenShiftClient oc = mock(OpenShiftClient.class);
when(mock.adapt(OpenShiftClient.class)).thenReturn(oc);
onInstantiate.accept(oc);
});
}

@AfterEach
void tearDown() {
kubernetesClientMockedConstruction.close();
clusterConfiguration = ClusterConfiguration.builder()
.masterUrl(mockServer.url("/"))
.build();
}

@Test
void isOpenShiftOpenShiftClusterShouldReturnTrue() {
// Given
onInstantiate = oc -> when(oc.isSupported()).thenReturn(true);
mockServer.expect().get().withPath("/apis").andReturn(200,
new APIGroupListBuilder().addNewGroup().withName("project.openshift.io").withApiVersion("v1").endGroup().build()).once();
// When
final boolean result = new ClusterAccess(logger, null).isOpenShift();
final boolean result = new ClusterAccess(logger, clusterConfiguration).isOpenShift();
// Then
assertThat(result).isTrue();
}

@Test
void isOpenShiftKubernetesClusterShouldReturnFalse() {
// Given
onInstantiate = oc -> when(oc.isSupported()).thenReturn(false);
// When
final boolean result = new ClusterAccess(logger, null).isOpenShift();
final boolean result = new ClusterAccess(logger, clusterConfiguration).isOpenShift();
// Then
assertThat(result).isFalse();
}

@Test
void isOpenShiftThrowsExceptionShouldReturnFalse() {
// Given
onInstantiate = oc -> when(oc.isSupported())
.thenThrow(new KubernetesClientException("ERROR", new UnknownHostException()));
clusterConfiguration = ClusterConfiguration.builder().masterUrl("https://unknown.example.com").build();
// When
final boolean result = new ClusterAccess(logger, null).isOpenShift();
final boolean result = new ClusterAccess(logger, clusterConfiguration).isOpenShift();
// Then
assertThat(result).isFalse();
verify(logger, times(1))
.warn(startsWith("Cannot access cluster for detecting mode"), eq("Unknown host "), isNull());
.warn(startsWith("Cannot access cluster for detecting mode"), eq(""), contains("unknown.example.com"));
}

@Test
void createDefaultClientInKubernetesShouldReturnKubernetesClient() {
// When
final KubernetesClient result = new ClusterAccess(logger, null).createDefaultClient();
final KubernetesClient result = new ClusterAccess(logger, clusterConfiguration).createDefaultClient();
// Then
assertThat(result).isNotNull().isNotInstanceOf(OpenShiftClient.class);
}

@Test
void createDefaultClientInOpenShiftShouldReturnOpenShiftClient() {
// Given
onInstantiate = oc -> when(oc.isSupported()).thenReturn(true);
mockServer.expect().get().withPath("/apis").andReturn(200,
new APIGroupListBuilder().addNewGroup().withName("project.openshift.io").withApiVersion("v1").endGroup().build()).once();
// When
final KubernetesClient result = new ClusterAccess(logger, null).createDefaultClient();
final KubernetesClient result = new ClusterAccess(logger, clusterConfiguration).createDefaultClient();
// Then
assertThat(result).isNotNull().isInstanceOf(OpenShiftClient.class);
}
Expand Down
Loading

0 comments on commit 74c61cd

Please sign in to comment.