Search in sources :

Example 51 with Deployment

use of io.fabric8.kubernetes.api.model.apps.Deployment in project kubernetes by ballerinax.

the class OpenShiftBuildConfigTest method serviceAnnotationTest.

/**
 * Test case openshift build config annotation with a service.
 */
@Test(groups = { "openshift" })
public void serviceAnnotationTest() throws IOException, InterruptedException, KubernetesPluginException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "annotation_on_service.bal"), 0);
    File yamlFile = new File(KUBERNETES_TARGET_PATH + File.separator + "annotation_on_service.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 "BuildConfig":
                BuildConfig bc = (BuildConfig) data;
                // metadata
                Assert.assertNotNull(bc.getMetadata());
                Assert.assertEquals(bc.getMetadata().getName(), "helloworld-openshift-bc", "Invalid name found.");
                Assert.assertNotNull(bc.getMetadata().getLabels(), "Labels are missing");
                Assert.assertEquals(bc.getMetadata().getLabels().size(), 1, "Labels are missing");
                Assert.assertNotNull(bc.getMetadata().getLabels().get("build"), "'build' label is missing");
                Assert.assertEquals(bc.getMetadata().getLabels().get("build"), "helloworld-openshift-bc", "Invalid label 'build' label value.");
                // spec
                Assert.assertNotNull(bc.getSpec());
                Assert.assertNotNull(bc.getSpec().getOutput());
                Assert.assertNotNull(bc.getSpec().getOutput().getTo());
                Assert.assertEquals(bc.getSpec().getOutput().getTo().getKind(), "ImageStreamTag", "Invalid output kind.");
                Assert.assertEquals(bc.getSpec().getOutput().getTo().getName(), "annotation_on_service:latest", "Invalid image stream name.");
                Assert.assertNotNull(bc.getSpec().getSource());
                Assert.assertNotNull(bc.getSpec().getSource().getBinary(), "Binary source is missing");
                Assert.assertNotNull(bc.getSpec().getStrategy());
                Assert.assertNotNull(bc.getSpec().getStrategy().getDockerStrategy(), "Docker strategy is missing.");
                Assert.assertEquals(bc.getSpec().getStrategy().getDockerStrategy().getBuildArgs().size(), 0, "Invalid number of build args.");
                Assert.assertEquals(bc.getSpec().getStrategy().getDockerStrategy().getDockerfilePath(), "kubernetes/docker/Dockerfile", "Invalid docker path.");
                Assert.assertFalse(bc.getSpec().getStrategy().getDockerStrategy().getForcePull(), "Force pull image set to false");
                Assert.assertFalse(bc.getSpec().getStrategy().getDockerStrategy().getNoCache(), "No cache for image build set to false");
                break;
            case "ImageStream":
                ImageStream is = (ImageStream) data;
                Assert.assertNotNull(is.getMetadata());
                Assert.assertEquals(is.getMetadata().getName(), "annotation_on_service", "Invalid name found.");
                Assert.assertEquals(is.getMetadata().getLabels().size(), 1, "Labels are missing");
                Assert.assertNotNull(is.getMetadata().getLabels().get("build"), "'build' label is missing");
                Assert.assertEquals(is.getMetadata().getLabels().get("build"), "helloworld-openshift-bc", "Invalid label 'build' label value.");
                Assert.assertNull(is.getSpec());
                break;
            default:
                Assert.fail("Unexpected k8s resource found: " + data.getKind());
                break;
        }
    }
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ImageStream(io.fabric8.openshift.api.model.ImageStream) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 52 with Deployment

use of io.fabric8.kubernetes.api.model.apps.Deployment in project kubernetes by ballerinax.

the class PrometheusTest method nodePortPrometheusTest.

/**
 * Build bal file with NodePort prometheus settings.
 *
 * @throws IOException               Error when loading the generated yaml.
 * @throws InterruptedException      Error when compiling the ballerina file.
 * @throws KubernetesPluginException Error when deleting the generated artifacts folder.
 */
@Test
public void nodePortPrometheusTest() throws IOException, InterruptedException, KubernetesPluginException, DockerTestException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "nodeport.bal"), 0);
    // Check if docker image exists and correct
    validateDockerfile();
    validateDockerImage();
    // Validate deployment yaml
    File deploymentYAML = KUBERNETES_TARGET_PATH.resolve("nodeport_deployment.yaml").toFile();
    Assert.assertTrue(deploymentYAML.exists());
    Deployment deployment = KubernetesTestUtils.loadYaml(deploymentYAML);
    Assert.assertNotNull(deployment.getSpec());
    Assert.assertNotNull(deployment.getSpec().getTemplate());
    Assert.assertNotNull(deployment.getSpec().getTemplate().getSpec());
    Assert.assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().size() > 0);
    final List<ContainerPort> ports = deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getPorts();
    Assert.assertEquals(ports.size(), 2);
    Assert.assertEquals(ports.get(0).getContainerPort().intValue(), 9090);
    Assert.assertEquals(ports.get(1).getContainerPort().intValue(), 9898);
    // Validate svc yaml
    File svcYAML = KUBERNETES_TARGET_PATH.resolve("nodeport_svc.yaml").toFile();
    Assert.assertTrue(svcYAML.exists());
    Service svc = KubernetesTestUtils.loadYaml(svcYAML);
    Assert.assertEquals(svc.getSpec().getPorts().size(), 1);
    final ServicePort servicePort = svc.getSpec().getPorts().get(0);
    Assert.assertEquals(servicePort.getPort().intValue(), 9090);
    Assert.assertEquals(servicePort.getProtocol(), "TCP");
    Assert.assertEquals(servicePort.getTargetPort().getIntVal().intValue(), 9090);
    Assert.assertEquals(svc.getSpec().getType(), "ClusterIP");
    Assert.assertEquals(svc.getMetadata().getName(), "helloep-svc");
    Assert.assertEquals(svc.getSpec().getSelector().get("app"), "nodeport");
    // Validate prometheus svc yaml
    File svcPrometheusYAML = KUBERNETES_TARGET_PATH.resolve("nodeport_prometheus_svc.yaml").toFile();
    Assert.assertTrue(svcPrometheusYAML.exists());
    Service svcPrometheus = KubernetesTestUtils.loadYaml(svcPrometheusYAML);
    Assert.assertEquals(svcPrometheus.getSpec().getPorts().size(), 1);
    final ServicePort prometheusSvcPort = svcPrometheus.getSpec().getPorts().get(0);
    Assert.assertEquals(prometheusSvcPort.getPort().intValue(), 9898);
    Assert.assertEquals(prometheusSvcPort.getProtocol(), "TCP");
    Assert.assertEquals(prometheusSvcPort.getTargetPort().getIntVal().intValue(), 9898);
    Assert.assertEquals(svcPrometheus.getSpec().getType(), "NodePort");
    Assert.assertEquals(svcPrometheus.getMetadata().getName(), "helloep-svc-prometheus");
    Assert.assertEquals(svcPrometheus.getSpec().getSelector().get("app"), "nodeport");
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Service(io.fabric8.kubernetes.api.model.Service) File(java.io.File) Test(org.testng.annotations.Test)

Example 53 with Deployment

use of io.fabric8.kubernetes.api.model.apps.Deployment in project kubernetes by ballerinax.

the class StrategyTest method rollingUpdateTest.

/**
 * Build bal file with deployment annotations having strategy annotations.
 *
 * @throws IOException               Error when loading the generated yaml.
 * @throws InterruptedException      Error when compiling the ballerina file.
 * @throws KubernetesPluginException Error when deleting the generated artifacts folder.
 */
@Test
public void rollingUpdateTest() throws IOException, InterruptedException, KubernetesPluginException, DockerTestException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "rolling_update.bal"), 0);
    // Check if docker image exists and correct
    validateDockerfile();
    validateDockerImage();
    // Validate deployment yaml
    File deploymentYAML = KUBERNETES_TARGET_PATH.resolve("rolling_update_deployment.yaml").toFile();
    Assert.assertTrue(deploymentYAML.exists());
    Deployment deployment = KubernetesTestUtils.loadYaml(deploymentYAML);
    Assert.assertEquals(deployment.getSpec().getStrategy().getType(), "RollingUpdate", "Invalid strategy found.");
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
Also used : Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) File(java.io.File) Test(org.testng.annotations.Test)

Example 54 with Deployment

use of io.fabric8.kubernetes.api.model.apps.Deployment in project kubernetes by ballerinax.

the class LivenessProbeTest method disabledTest.

/**
 * Build bal file with deployment having liveness disabled.
 *
 * @throws IOException               Error when loading the generated yaml.
 * @throws InterruptedException      Error when compiling the ballerina file.
 * @throws KubernetesPluginException Error when deleting the generated artifacts folder.
 */
@Test
public void disabledTest() throws IOException, InterruptedException, KubernetesPluginException, DockerTestException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "disabled.bal"), 0);
    // Check if docker image exists and correct
    validateDockerfile();
    validateDockerImage();
    // Validate deployment yaml
    File deploymentYAML = KUBERNETES_TARGET_PATH.resolve("disabled_deployment.yaml").toFile();
    Assert.assertTrue(deploymentYAML.exists());
    Deployment deployment = KubernetesTestUtils.loadYaml(deploymentYAML);
    Assert.assertNotNull(deployment.getSpec());
    Assert.assertNotNull(deployment.getSpec().getTemplate());
    Assert.assertNotNull(deployment.getSpec().getTemplate().getSpec());
    Assert.assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().size() > 0);
    Assert.assertNull(deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getLivenessProbe());
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
Also used : Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) File(java.io.File) Test(org.testng.annotations.Test)

Example 55 with Deployment

use of io.fabric8.kubernetes.api.model.apps.Deployment in project kubernetes by ballerinax.

the class LivenessProbeTest method configuredTest.

/**
 * Build bal file with deployment having liveness configured.
 *
 * @throws IOException               Error when loading the generated yaml.
 * @throws InterruptedException      Error when compiling the ballerina file.
 * @throws KubernetesPluginException Error when deleting the generated artifacts folder.
 */
@Test
public void configuredTest() throws IOException, InterruptedException, KubernetesPluginException, DockerTestException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "configured.bal"), 0);
    // Check if docker image exists and correct
    validateDockerfile();
    validateDockerImage();
    // Validate deployment yaml
    File deploymentYAML = KUBERNETES_TARGET_PATH.resolve("configured_deployment.yaml").toFile();
    Assert.assertTrue(deploymentYAML.exists());
    Deployment deployment = KubernetesTestUtils.loadYaml(deploymentYAML);
    Assert.assertNotNull(deployment.getSpec());
    Assert.assertNotNull(deployment.getSpec().getTemplate());
    Assert.assertNotNull(deployment.getSpec().getTemplate().getSpec());
    Assert.assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().size() > 0);
    Assert.assertNotNull(deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getLivenessProbe(), "Liveness probe is missing.");
    Assert.assertEquals(deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getLivenessProbe().getInitialDelaySeconds().intValue(), 20, "initialDelay in liveness probe is missing.");
    Assert.assertEquals(deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getLivenessProbe().getPeriodSeconds().intValue(), 10, "periodSeconds in liveness probe is missing.");
    Assert.assertEquals(deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getLivenessProbe().getTcpSocket().getPort().getIntVal().intValue(), 8080, "TCP port in liveness probe is missing.");
    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
Also used : Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

File (java.io.File)62 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)52 Test (org.testng.annotations.Test)50 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)47 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)43 Container (io.fabric8.kubernetes.api.model.Container)32 Service (io.fabric8.kubernetes.api.model.Service)28 Test (org.junit.Test)28 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)27 InputStream (java.io.InputStream)26 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)25 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)25 Deployment (org.jboss.arquillian.container.test.api.Deployment)21 OSGiManifestBuilder (org.jboss.osgi.metadata.OSGiManifestBuilder)21 Asset (org.jboss.shrinkwrap.api.asset.Asset)21 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)21 IOException (java.io.IOException)20 ServiceTracker (org.osgi.util.tracker.ServiceTracker)20 FileInputStream (java.io.FileInputStream)19 CommandSupport (io.fabric8.itests.support.CommandSupport)18