Search in sources :

Example 51 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testCreateDeploymentResourceForInvalidResource.

@Test
public void testCreateDeploymentResourceForInvalidResource() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    HasMetadata invalidMetadata = Mockito.mock(Service.class);
    List<HasMetadata> deploymentResources = new ArrayList<>();
    deploymentResources.add(invalidMetadata);
    Mockito.when(openShiftClient.load(Mockito.any()).get()).thenReturn(getServiceResources(), deploymentResources);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    ScalableResource scalableResource = Mockito.mock(ScalableResource.class);
    Mockito.when(scalableResource.get()).thenReturn(null);
    Service service = createService(openShiftClient, nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.create(Mockito.any())).thenReturn(service);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    try {
        kubernetesGateway.createContainerGateway(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX + LABEL_SUFFIX, api);
    } catch (ContainerBasedGatewayException e) {
        Assert.assertEquals(e.getErrorHandler(), ExceptionCodes.LOADED_RESOURCE_DEFINITION_IS_NOT_VALID);
    }
}
Also used : ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 52 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project carbon-apimgt by wso2.

the class KubernetesGatewayImplTestCase method testRemoveContainerBasedGateway.

@Test
public void testRemoveContainerBasedGateway() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    KubernetesGatewayImpl kubernetesGateway = getKubernetesGatewayImpl(openShiftClient);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    BaseOperation baseOperation = Mockito.mock(BaseOperation.class);
    Mockito.when(openShiftClient.services().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withLabel(Mockito.anyString(), Mockito.anyString())).thenReturn(baseOperation);
    Mockito.when(baseOperation.delete()).thenReturn(true);
    Mockito.when(openShiftClient.extensions().deployments().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    Mockito.when(openShiftClient.extensions().ingresses().inNamespace(NAMESPACE)).thenReturn(nonNamespaceOperation);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    kubernetesGateway.removeContainerBasedGateway("label", api);
    Mockito.verify(openShiftClient, Mockito.times(2)).services();
    Mockito.verify(openShiftClient, Mockito.times(4)).extensions();
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) API(org.wso2.carbon.apimgt.core.models.API) BaseOperation(io.fabric8.kubernetes.client.dsl.base.BaseOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.junit.Test)

Example 53 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testListServicesWithWrongPortType.

@Test(description = "Test .listServices() while the list has only one service and its port is not http nor https")
public void testListServicesWithWrongPortType() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    // Include ExternalNames (includeClusterIP not checked)
    sdKubernetes.setIncludeExternalNameTypeServices(true);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    Mockito.when(openShiftClient.services().inNamespace(null)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.list()).thenReturn(createMalformedServiceList("somePort"));
    List<Endpoint> endpoints = sdKubernetes.listServices();
    Assert.assertTrue(endpoints.isEmpty());
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 54 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testListServicesOfLoadBalancerTypeWithoutIngress.

@Test(description = "Test .listServices() while the list only has a LoadBalancer type service without any ingress")
public void testListServicesOfLoadBalancerTypeWithoutIngress() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    // Not include ClusterIPs
    sdKubernetes.setIncludeClusterIP(false);
    // Not include ExternalNames
    sdKubernetes.setIncludeExternalNameTypeServices(false);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    Mockito.when(openShiftClient.services().inNamespace(null)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.list()).thenReturn(createMalformedServiceList("http"));
    Mockito.when(openShiftClient.getMasterUrl()).thenReturn(new URL(MASTER_URL));
    List<Endpoint> endpoints = sdKubernetes.listServices();
    Assert.assertEquals(endpoints.size(), 1);
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) URL(java.net.URL) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 55 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testListServicesWithCriteria.

@Test(description = "Test .listServices(Criteria) method")
public void testListServicesWithCriteria() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    // Include ClusterIPs
    sdKubernetes.setIncludeClusterIP(true);
    // Not include ExternalNames
    sdKubernetes.setIncludeExternalNameTypeServices(false);
    Map<String, String> oneLabel = createOneLabelHashMap();
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    BaseOperation baseOperation = Mockito.mock(BaseOperation.class);
    Mockito.when(openShiftClient.services().inNamespace(null)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.withLabels(oneLabel)).thenReturn(baseOperation);
    Mockito.when(baseOperation.list()).thenReturn(createServiceListWithCriteria());
    Mockito.when(openShiftClient.getMasterUrl()).thenReturn(new URL(MASTER_URL));
    List<Endpoint> endpoints = sdKubernetes.listServices(oneLabel);
    Assert.assertEquals(endpoints.size(), 3);
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) BaseOperation(io.fabric8.kubernetes.client.dsl.base.BaseOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) URL(java.net.URL) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)65 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)18 IOException (java.io.IOException)18 Test (org.junit.Test)17 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)15 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)14 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)12 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)12 API (org.wso2.carbon.apimgt.core.models.API)12 FileNotFoundException (java.io.FileNotFoundException)11 Service (io.fabric8.kubernetes.api.model.Service)10 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)10 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)10 BeforeTest (org.testng.annotations.BeforeTest)9 Test (org.testng.annotations.Test)9 Controller (io.fabric8.kubernetes.api.Controller)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)8 Route (io.fabric8.openshift.api.model.Route)8 JSONObject (org.json.JSONObject)8