From e139d892e51a684a869cf23846ca6779eb946b52 Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Fri, 5 Apr 2024 16:58:39 +0200 Subject: [PATCH] fix: corrected check for OpenShift cluster (#216) Signed-off-by: Andre Dietisheim --- .../common/kubernetes/ClusterHelper.java | 7 +--- .../common/kubernetes/ClusterHelperTest.java | 35 +++---------------- 2 files changed, 5 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/redhat/devtools/intellij/common/kubernetes/ClusterHelper.java b/src/main/java/com/redhat/devtools/intellij/common/kubernetes/ClusterHelper.java index dc3f1bc..d2ebe8c 100644 --- a/src/main/java/com/redhat/devtools/intellij/common/kubernetes/ClusterHelper.java +++ b/src/main/java/com/redhat/devtools/intellij/common/kubernetes/ClusterHelper.java @@ -25,12 +25,7 @@ private ClusterHelper() { } public static boolean isOpenShift(KubernetesClient client) { - OpenShiftClient osClient = client.adapt(OpenShiftClient.class); - try { - return osClient.isSupported(); - } catch (KubernetesClientException e) { - return e.getCode() == HttpURLConnection.HTTP_UNAUTHORIZED; - } + return client.hasApiGroup(OpenShiftClient.BASE_API_GROUP, false); } public static ClusterInfo getClusterInfo(KubernetesClient client) { diff --git a/src/test/java/com/redhat/devtools/intellij/common/kubernetes/ClusterHelperTest.java b/src/test/java/com/redhat/devtools/intellij/common/kubernetes/ClusterHelperTest.java index d558fa0..8faa975 100644 --- a/src/test/java/com/redhat/devtools/intellij/common/kubernetes/ClusterHelperTest.java +++ b/src/test/java/com/redhat/devtools/intellij/common/kubernetes/ClusterHelperTest.java @@ -11,19 +11,14 @@ package com.redhat.devtools.intellij.common.kubernetes; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.KubernetesClientException; import io.fabric8.kubernetes.client.VersionInfo; import io.fabric8.openshift.client.OpenShiftClient; import org.junit.Test; -import java.net.HttpURLConnection; - import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -77,13 +72,10 @@ public void testOCP3OrOCP4AdminUserCluster() { } @Test - public void isOpenShift_should_return_true_if_isSupported() { + public void isOpenShift_should_return_true_if_has_api_group() { // given - OpenShiftClient oclient = mock(OpenShiftClient.class); - doReturn(true) - .when(oclient).isSupported(); KubernetesClient client = mock(KubernetesClient.class); - when(client.adapt(OpenShiftClient.class)).thenReturn(oclient); + when(client.hasApiGroup(OpenShiftClient.BASE_API_GROUP, false)).thenReturn(true); // when boolean isOpenShift = ClusterHelper.isOpenShift(client); // then @@ -91,32 +83,13 @@ public void isOpenShift_should_return_true_if_isSupported() { } @Test - public void isOpenShift_should_return_false_if_isSupported_throws() { + public void isOpenShift_should_return_false_if_has_not_api_group() { // given - OpenShiftClient oclient = mock(OpenShiftClient.class); - doThrow(KubernetesClientException.class) - .when(oclient).isSupported(); KubernetesClient client = mock(KubernetesClient.class); - when(client.adapt(OpenShiftClient.class)).thenReturn(oclient); + when(client.hasApiGroup(OpenShiftClient.BASE_API_GROUP, false)).thenReturn(false); // when boolean isOpenShift = ClusterHelper.isOpenShift(client); // then assertThat(isOpenShift).isFalse(); } - - @Test - public void isOpenShift_should_return_true_if_isSupported_throws_unauthorized() { - // given - OpenShiftClient oclient = mock(OpenShiftClient.class); - KubernetesClientException e = new KubernetesClientException("ouch", HttpURLConnection.HTTP_UNAUTHORIZED, null); - doThrow(e) - .when(oclient).isSupported(); - KubernetesClient client = mock(KubernetesClient.class); - when(client.adapt(OpenShiftClient.class)).thenReturn(oclient); - // when - boolean isOpenShift = ClusterHelper.isOpenShift(client); - // then - assertThat(isOpenShift).isTrue(); - } - }