Skip to content

Commit

Permalink
fix: corrected check for OpenShift cluster (#216)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Apr 9, 2024
1 parent c487d8f commit e139d89
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -77,46 +72,24 @@ 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
assertThat(isOpenShift).isTrue();
}

@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();
}

}

0 comments on commit e139d89

Please sign in to comment.