Search in sources :

Example 26 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project kubernetes by ballerinax.

the class NoAnnotationsTest method serviceWithNoAnnotationTest.

@Test(timeOut = 90000)
public void serviceWithNoAnnotationTest() throws IOException, InterruptedException, DockerTestException, KubernetesPluginException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "no_annotation_service.bal"), 0);
    File yamlFile = KUBERNETES_TARGET_PATH.resolve("no_annotation_service.yaml").toFile();
    Assert.assertTrue(yamlFile.exists());
    KubernetesClient client = new DefaultKubernetesClient();
    List<HasMetadata> k8sItems = client.load(new FileInputStream(yamlFile)).get();
    for (HasMetadata data : k8sItems) {
        if ("Service".equals(data.getKind())) {
            Service service = (Service) data;
            Assert.assertEquals(service.getMetadata().getName(), "helloworld-svc");
            Assert.assertEquals(service.getMetadata().getLabels().get(KubernetesConstants.KUBERNETES_SELECTOR_KEY), "no_annotation_service");
            Assert.assertEquals(service.getSpec().getType(), KubernetesConstants.ServiceType.NodePort.name());
            Assert.assertEquals(service.getSpec().getPorts().size(), 1);
            Assert.assertEquals(service.getSpec().getPorts().get(0).getPort().intValue(), 9090);
        }
        if ("Deployment".equals(data.getKind())) {
            Deployment deployment = (Deployment) data;
            Assert.assertEquals(deployment.getMetadata().getName(), "no-annotation-service-deployment");
            Assert.assertEquals(deployment.getSpec().getReplicas().intValue(), 1);
            Assert.assertEquals(deployment.getMetadata().getLabels().get(KubernetesConstants.KUBERNETES_SELECTOR_KEY), "no_annotation_service");
            Assert.assertEquals(deployment.getSpec().getTemplate().getSpec().getContainers().size(), 1);
            Container container = deployment.getSpec().getTemplate().getSpec().getContainers().get(0);
            Assert.assertEquals(container.getImage(), "no_annotation_service:latest");
            Assert.assertEquals(container.getImagePullPolicy(), KubernetesConstants.ImagePullPolicy.IfNotPresent.name());
            Assert.assertEquals(container.getPorts().size(), 1);
            Assert.assertEquals(container.getEnv().size(), 0);
        }
    }
    validateDockerfile();
    validateDockerImage("no_annotation_service:latest");
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage("no_annotation_service:latest");
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Container(io.fabric8.kubernetes.api.model.Container) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 27 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project kubernetes by ballerinax.

the class NoAnnotationsTest method listenerWithNoAnnotationTest.

@Test(timeOut = 90000)
public void listenerWithNoAnnotationTest() throws IOException, InterruptedException, DockerTestException, KubernetesPluginException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "no_annotation_listener.bal"), 0);
    File yamlFile = KUBERNETES_TARGET_PATH.resolve("no_annotation_listener.yaml").toFile();
    Assert.assertTrue(yamlFile.exists());
    KubernetesClient client = new DefaultKubernetesClient();
    List<HasMetadata> k8sItems = client.load(new FileInputStream(yamlFile)).get();
    for (HasMetadata data : k8sItems) {
        if ("Service".equals(data.getKind())) {
            Service service = (Service) data;
            Assert.assertEquals(service.getMetadata().getName(), "helloworldep-svc");
            Assert.assertEquals(service.getMetadata().getLabels().get(KubernetesConstants.KUBERNETES_SELECTOR_KEY), "no_annotation_listener");
            Assert.assertEquals(service.getSpec().getType(), KubernetesConstants.ServiceType.NodePort.name());
            Assert.assertEquals(service.getSpec().getPorts().size(), 1);
            Assert.assertEquals(service.getSpec().getPorts().get(0).getPort().intValue(), 9090);
        }
        if ("Deployment".equals(data.getKind())) {
            Deployment deployment = (Deployment) data;
            Assert.assertEquals(deployment.getMetadata().getName(), "no-annotation-listener-deployment");
            Assert.assertEquals(deployment.getSpec().getReplicas().intValue(), 1);
            Assert.assertEquals(deployment.getMetadata().getLabels().get(KubernetesConstants.KUBERNETES_SELECTOR_KEY), "no_annotation_listener");
            Assert.assertEquals(deployment.getSpec().getTemplate().getSpec().getContainers().size(), 1);
            Container container = deployment.getSpec().getTemplate().getSpec().getContainers().get(0);
            Assert.assertEquals(container.getImage(), "no_annotation_listener:latest");
            Assert.assertEquals(container.getImagePullPolicy(), KubernetesConstants.ImagePullPolicy.IfNotPresent.name());
            Assert.assertEquals(container.getPorts().size(), 1);
            Assert.assertEquals(container.getEnv().size(), 0);
        }
    }
    validateDockerfile();
    validateDockerImage("no_annotation_listener:latest");
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage("no_annotation_listener:latest");
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Container(io.fabric8.kubernetes.api.model.Container) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 28 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project kubernetes by ballerinax.

the class NoAnnotationsTest method mainWithNoAnnotationTest.

@Test(timeOut = 90000)
public void mainWithNoAnnotationTest() throws IOException, InterruptedException, DockerTestException, KubernetesPluginException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "no_annotations_main.bal"), 0);
    File yamlFile = KUBERNETES_TARGET_PATH.resolve("no_annotations_main.yaml").toFile();
    Assert.assertTrue(yamlFile.exists());
    KubernetesClient client = new DefaultKubernetesClient();
    List<HasMetadata> k8sItems = client.load(new FileInputStream(yamlFile)).get();
    for (HasMetadata data : k8sItems) {
        if ("Job".equals(data.getKind())) {
            Job job = (Job) data;
            Assert.assertEquals(job.getMetadata().getName(), "no-annotations-main-job");
            Assert.assertEquals(job.getSpec().getTemplate().getSpec().getContainers().size(), 1);
            Container container = job.getSpec().getTemplate().getSpec().getContainers().get(0);
            Assert.assertEquals(container.getImage(), "no_annotations_main:latest");
            Assert.assertEquals(container.getImagePullPolicy(), KubernetesConstants.ImagePullPolicy.IfNotPresent.name());
            Assert.assertEquals(job.getSpec().getTemplate().getSpec().getRestartPolicy(), KubernetesConstants.RestartPolicy.Never.name());
        }
    }
    validateDockerfile();
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage("no_annotations_main:latest");
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Container(io.fabric8.kubernetes.api.model.Container) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Job(io.fabric8.kubernetes.api.model.batch.Job) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 29 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project kubernetes by ballerinax.

the class OpenShiftRouteTest method simpleRoute.

/**
 * Test case openshift route with host.
 */
@Test(groups = { "openshift" })
public void simpleRoute() throws IOException, InterruptedException, KubernetesPluginException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "simple_route.bal"), 0);
    File yamlFile = new File(KUBERNETES_TARGET_PATH + File.separator + "simple_route.yaml");
    Assert.assertTrue(yamlFile.exists());
    KubernetesClient client = new DefaultKubernetesClient();
    List<HasMetadata> k8sItems = client.load(new FileInputStream(yamlFile)).get();
    for (HasMetadata data : k8sItems) {
        switch(data.getKind()) {
            case "Service":
            case "Deployment":
                break;
            case "Route":
                Route route = (Route) data;
                // metadata
                Assert.assertNotNull(route.getMetadata());
                Assert.assertEquals(route.getMetadata().getName(), "helloep-openshift-route", "Invalid name found.");
                // spec
                Assert.assertNotNull(route.getSpec(), "Spec is missing.");
                Assert.assertEquals(route.getSpec().getHost(), "www.bxoc.com", "Invalid host");
                Assert.assertNotNull(route.getSpec().getPort());
                Assert.assertEquals(route.getSpec().getPort().getTargetPort().getIntVal().intValue(), 9090, "Invalid port found");
                Assert.assertNotNull(route.getSpec().getTo(), "To is missing.");
                Assert.assertEquals(route.getSpec().getTo().getKind(), "Service", "Kind is missing.");
                Assert.assertEquals(route.getSpec().getTo().getName(), "helloep-svc", "Service name is invalid.");
                Assert.assertEquals(route.getSpec().getTo().getWeight().intValue(), 100, "Invalid route weight.");
                break;
            default:
                Assert.fail("Unexpected k8s resource found: " + data.getKind());
                break;
        }
    }
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage("simple_route:latest");
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File) FileInputStream(java.io.FileInputStream) Route(io.fabric8.openshift.api.model.Route) Test(org.testng.annotations.Test)

Example 30 with DefaultKubernetesClient

use of io.fabric8.kubernetes.client.DefaultKubernetesClient in project kubernetes by ballerinax.

the class ServiceTest method portAndTargetPortTest.

@Test
public void portAndTargetPortTest() throws IOException, InterruptedException, KubernetesPluginException, DockerTestException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "port_and_target_port.bal"), 0);
    File yamlFile = KUBERNETES_TARGET_PATH.resolve("port_and_target_port.yaml").toFile();
    Assert.assertTrue(yamlFile.exists());
    KubernetesClient client = new DefaultKubernetesClient();
    List<HasMetadata> k8sItems = client.load(new FileInputStream(yamlFile)).get();
    for (HasMetadata data : k8sItems) {
        if ("Service".equals(data.getKind())) {
            Service service = (Service) data;
            Assert.assertNotNull(service);
            Assert.assertEquals("hello", service.getMetadata().getName());
            Assert.assertEquals("port_and_target_port", service.getMetadata().getLabels().get(KubernetesConstants.KUBERNETES_SELECTOR_KEY));
            Assert.assertEquals(KubernetesConstants.ServiceType.ClusterIP.name(), service.getSpec().getType());
            Assert.assertEquals(1, service.getSpec().getPorts().size());
            Assert.assertEquals(8080, service.getSpec().getPorts().get(0).getPort().intValue());
            Assert.assertEquals(9090, service.getSpec().getPorts().get(0).getTargetPort().getIntVal().intValue());
        }
    }
    validateDockerfile();
    validateDockerImage(DOCKER_IMAGE);
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Service(io.fabric8.kubernetes.api.model.Service) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Aggregations

DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)101 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)77 File (java.io.File)27 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)22 FileInputStream (java.io.FileInputStream)22 HashMap (java.util.HashMap)13 Test (org.testng.annotations.Test)12 Config (io.fabric8.kubernetes.client.Config)11 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)11 BeforeClass (org.testng.annotations.BeforeClass)10 Service (io.fabric8.kubernetes.api.model.Service)8 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)7 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)7 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)6 TaskAction (org.gradle.api.tasks.TaskAction)6 Pod (io.fabric8.kubernetes.api.model.Pod)5 Map (java.util.Map)5 Test (org.junit.Test)5 KubernetesModelGenerator (com.github.isdream.chameleon.container.kubernetes.KubernetesModelGenerator)4 ImmutableMap (com.google.common.collect.ImmutableMap)4