Skip to content

Commit

Permalink
Merge pull request #71 from sheroy/master
Browse files Browse the repository at this point in the history
Replace the usage of the namespace list API call with get
  • Loading branch information
arvindsv authored Dec 14, 2018
2 parents 37fa9dc + 1ea3ac4 commit 11d0451
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,17 +79,17 @@ private void validateNamespaceExistence() {
final String namespace = validatePluginSettingsRequest.getPluginSettingsMap().getNamespace();
try {
final KubernetesClient client = factory.client(validatePluginSettingsRequest.getPluginSettingsMap());
final List<Namespace> 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;
}

result.add(error(NAMESPACE.key(), format("Namespace `{0}` does not exist in you cluster. Run \"kubectl create namespace {1}\" to create a namespace.", namespace, namespace)));
} 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.")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,11 +51,17 @@ public class ValidateConfigurationExecutorTest {
@Mock
private KubernetesClient client;
@Mock
private NonNamespaceOperation<Namespace, NamespaceList, DoneableNamespace, Resource<Namespace, DoneableNamespace>> mockedOperation;
private NonNamespaceOperation<Namespace, NamespaceList, DoneableNamespace, Resource<Namespace, DoneableNamespace>> mockedClient;
@Mock
NamespaceList namespaceList;
private ServerInfo serverInfo;

@Mock
private Resource<Namespace, DoneableNamespace> mockNamespaceResource;

@Mock
private Namespace mockValidNamespace;

@Before
public void setUp() {
initMocks(this);
Expand All @@ -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();
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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(");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -204,13 +213,4 @@ public void shouldValidateNamespaceExistence() throws JSONException {
"]", response.responseBody(), true);
}

private List<Namespace> 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());
}
}

0 comments on commit 11d0451

Please sign in to comment.