diff --git a/src/main/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutor.java b/src/main/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutor.java index 9f4abcf1..4859528c 100644 --- a/src/main/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutor.java +++ b/src/main/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutor.java @@ -25,8 +25,10 @@ import cd.go.contrib.elasticagent.requests.ValidatePluginSettingsRequest; import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse; import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse; +import io.fabric8.kubernetes.api.model.DoneableNamespace; import io.fabric8.kubernetes.api.model.Namespace; import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.Resource; import java.util.ArrayList; import java.util.HashMap; @@ -77,9 +79,9 @@ private void validateNamespaceExistence() { final String namespace = validatePluginSettingsRequest.getPluginSettingsMap().getNamespace(); try { final KubernetesClient client = factory.client(validatePluginSettingsRequest.getPluginSettingsMap()); - final List namespaceList = client.namespaces().list().getItems(); - if (namespaceList.stream().anyMatch(n -> n.getMetadata().getName().equals(namespace))) { + Namespace ns = client.namespaces().withName(namespace).get(); + if (ns != null) { return; } @@ -87,7 +89,7 @@ private void validateNamespaceExistence() { } catch (Exception e) { String message = "Failed validation of plugin settings. The reasons could be - " + "Cluster Url is configured incorrectly or " + - "the service account token might not have enough permissions to list namespaces or " + + "the service account token might not have enough permissions to get namespaces or " + "incorrect CA certificate."; LOG.error(message, e); result.add(error(NAMESPACE.key(), format(message + "Please check the plugin log for more details."))); diff --git a/src/test/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutorTest.java b/src/test/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutorTest.java index 3196463d..357ac174 100644 --- a/src/test/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutorTest.java +++ b/src/test/java/cd/go/contrib/elasticagent/executors/ValidateConfigurationExecutorTest.java @@ -21,10 +21,7 @@ import cd.go.contrib.elasticagent.model.ServerInfo; import cd.go.contrib.elasticagent.requests.ValidatePluginSettingsRequest; import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse; -import io.fabric8.kubernetes.api.model.DoneableNamespace; -import io.fabric8.kubernetes.api.model.Namespace; -import io.fabric8.kubernetes.api.model.NamespaceBuilder; -import io.fabric8.kubernetes.api.model.NamespaceList; +import io.fabric8.kubernetes.api.model.*; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; import io.fabric8.kubernetes.client.dsl.Resource; @@ -54,11 +51,17 @@ public class ValidateConfigurationExecutorTest { @Mock private KubernetesClient client; @Mock - private NonNamespaceOperation> mockedOperation; + private NonNamespaceOperation> mockedClient; @Mock NamespaceList namespaceList; private ServerInfo serverInfo; + @Mock + private Resource mockNamespaceResource; + + @Mock + private Namespace mockValidNamespace; + @Before public void setUp() { initMocks(this); @@ -69,13 +72,13 @@ public void setUp() { "}"); when(pluginRequest.getSeverInfo()).thenReturn(serverInfo); when(factory.client(any())).thenReturn(client); - when(client.namespaces()).thenReturn(mockedOperation); - when(mockedOperation.list()).thenReturn(namespaceList); + when(client.namespaces()).thenReturn(mockedClient); } @Test public void shouldValidateABadConfiguration() throws Exception { - when(namespaceList.getItems()).thenReturn(getNamespaceList("default")); + when(mockedClient.withName("default")).thenReturn(mockNamespaceResource); + when(mockNamespaceResource.get()).thenReturn(mockValidNamespace); ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest(); GoPluginApiResponse response = new ValidateConfigurationExecutor(settings, pluginRequest, factory).execute(); @@ -95,7 +98,8 @@ public void shouldValidateABadConfiguration() throws Exception { @Test public void shouldValidateAGoodConfiguration() throws Exception { - when(namespaceList.getItems()).thenReturn(getNamespaceList("default")); + when(mockedClient.withName("default")).thenReturn(mockNamespaceResource); + when(mockNamespaceResource.get()).thenReturn(mockValidNamespace); ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest(); settings.put("go_server_url", "https://ci.example.com/go"); @@ -109,7 +113,8 @@ public void shouldValidateAGoodConfiguration() throws Exception { @Test public void shouldValidateGoServerUrl() throws Exception { - when(namespaceList.getItems()).thenReturn(getNamespaceList("default")); + when(mockedClient.withName("default")).thenReturn(mockNamespaceResource); + when(mockNamespaceResource.get()).thenReturn(mockValidNamespace); ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest(); serverInfo.setSecureSiteUrl(null); @@ -128,7 +133,8 @@ public void shouldValidateGoServerUrl() throws Exception { @Test public void shouldValidateGoServerHTTPSUrlFormat() throws Exception { - when(namespaceList.getItems()).thenReturn(getNamespaceList("default")); + when(mockedClient.withName("default")).thenReturn(mockNamespaceResource); + when(mockNamespaceResource.get()).thenReturn(mockValidNamespace); ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest(); settings.put("go_server_url", "foo.com/go("); @@ -147,7 +153,8 @@ public void shouldValidateGoServerHTTPSUrlFormat() throws Exception { @Test public void shouldValidateGoServerUrlFormat() throws Exception { - when(namespaceList.getItems()).thenReturn(getNamespaceList("default")); + when(mockedClient.withName("default")).thenReturn(mockNamespaceResource); + when(mockNamespaceResource.get()).thenReturn(mockValidNamespace); ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest(); settings.put("go_server_url", "https://foo.com"); @@ -166,7 +173,8 @@ public void shouldValidateGoServerUrlFormat() throws Exception { @Test public void shouldValidateOAuthTokenWhenAuthenticationStrategyIsSetToOauthToken() throws JSONException { - when(namespaceList.getItems()).thenReturn(getNamespaceList("default")); + when(mockedClient.withName("default")).thenReturn(mockNamespaceResource); + when(mockNamespaceResource.get()).thenReturn(mockValidNamespace); ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest(); settings.put("go_server_url", "https://foo.com/go"); @@ -186,7 +194,8 @@ public void shouldValidateOAuthTokenWhenAuthenticationStrategyIsSetToOauthToken( @Test public void shouldValidateNamespaceExistence() throws JSONException { - when(namespaceList.getItems()).thenReturn(getNamespaceList("default")); + when(mockedClient.withName("gocd")).thenReturn(mockNamespaceResource); + when(mockNamespaceResource.get()).thenReturn(null); ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest(); settings.put("go_server_url", "https://ci.example.com/go"); @@ -204,13 +213,4 @@ public void shouldValidateNamespaceExistence() throws JSONException { "]", response.responseBody(), true); } - private List getNamespaceList(String... namespaces) { - if (namespaces == null || namespaces.length == 0) { - return Collections.emptyList(); - } - - return Arrays.asList(namespaces).stream() - .map(namespaceName -> new NamespaceBuilder().withNewMetadata().withName("default").endMetadata().build()) - .collect(Collectors.toList()); - } }