diff --git a/components/cellery-component-test/src/test/java/org/cellery/components/test/scenarios/probes/ProbesTest.java b/components/cellery-component-test/src/test/java/org/cellery/components/test/scenarios/probes/ProbesTest.java new file mode 100644 index 00000000..b783c895 --- /dev/null +++ b/components/cellery-component-test/src/test/java/org/cellery/components/test/scenarios/probes/ProbesTest.java @@ -0,0 +1,256 @@ +/* + * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.cellery.components.test.scenarios.probes; + +import io.cellery.models.API; +import io.cellery.models.Cell; +import io.cellery.models.ServiceTemplate; +import io.fabric8.kubernetes.api.model.Container; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.api.model.Probe; +import org.ballerinax.kubernetes.exceptions.KubernetesPluginException; +import org.ballerinax.kubernetes.utils.KubernetesUtils; +import org.cellery.components.test.models.CellImageInfo; +import org.cellery.components.test.utils.CelleryUtils; +import org.cellery.components.test.utils.LangTestUtils; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.cellery.components.test.utils.CelleryTestConstants.ARTIFACTS; +import static org.cellery.components.test.utils.CelleryTestConstants.BAL; +import static org.cellery.components.test.utils.CelleryTestConstants.CELLERY; +import static org.cellery.components.test.utils.CelleryTestConstants.CELLERY_IMAGE_NAME; +import static org.cellery.components.test.utils.CelleryTestConstants.CELLERY_IMAGE_ORG; +import static org.cellery.components.test.utils.CelleryTestConstants.CELLERY_IMAGE_VERSION; +import static org.cellery.components.test.utils.CelleryTestConstants.CELLERY_MESH_VERSION; +import static org.cellery.components.test.utils.CelleryTestConstants.TARGET; +import static org.cellery.components.test.utils.CelleryTestConstants.YAML; + +public class ProbesTest { + + private static final Path SAMPLE_DIR = Paths.get(System.getProperty("sample.dir")); + private static final Path SOURCE_DIR_PATH = SAMPLE_DIR.resolve("probes"); + private static final Path TARGET_PATH = SOURCE_DIR_PATH.resolve(TARGET); + private static final Path CELLERY_PATH = TARGET_PATH.resolve(CELLERY); + private Cell cell; + private Cell runtimeCell; + private CellImageInfo cellImageInfo = new CellImageInfo("myorg", "probes", "1.0.0", "probe-inst"); + private Map dependencyCells = new HashMap<>(); + + @Test(groups = "build") + public void compileCellBuild() throws IOException, InterruptedException { + Assert.assertEquals(LangTestUtils.compileCellBuildFunction(SOURCE_DIR_PATH, "probes" + BAL, cellImageInfo), 0); + File artifactYaml = CELLERY_PATH.resolve(cellImageInfo.getName() + YAML).toFile(); + Assert.assertTrue(artifactYaml.exists()); + cell = CelleryUtils.getInstance(CELLERY_PATH.resolve(cellImageInfo.getName() + YAML).toString()); + } + + @Test(groups = "build") + public void validateBuildTimeCellAvailability() { + Assert.assertNotNull(cell); + } + + @Test(groups = "build") + public void validateBuildTimeAPIVersion() { + Assert.assertEquals(cell.getApiVersion(), CELLERY_MESH_VERSION); + } + + @Test(groups = "build") + public void validateBuildTimeMetaData() { + Assert.assertEquals(cell.getMetadata().getName(), cellImageInfo.getName()); + Assert.assertEquals(cell.getMetadata().getAnnotations().get(CELLERY_IMAGE_ORG), cellImageInfo.getOrg()); + Assert.assertEquals(cell.getMetadata().getAnnotations().get(CELLERY_IMAGE_NAME), cellImageInfo.getName()); + Assert.assertEquals(cell.getMetadata().getAnnotations().get(CELLERY_IMAGE_VERSION), cellImageInfo.getVer()); + } + + @Test(groups = "build") + public void validateBuildTimeGatewayTemplate() { + final List httpAPI = cell.getSpec().getGatewayTemplate().getSpec().getHttp(); + Assert.assertEquals(httpAPI.get(0).getBackend(), "employee"); + Assert.assertEquals(httpAPI.get(0).getContext(), "employee"); + Assert.assertEquals(httpAPI.get(0).getDefinitions().get(0).getMethod(), "GET"); + Assert.assertEquals(httpAPI.get(0).getDefinitions().get(0).getPath(), "/details"); + Assert.assertEquals(httpAPI.get(1).getBackend(), "salary"); + Assert.assertEquals(httpAPI.get(1).getContext(), "payroll"); + Assert.assertEquals(httpAPI.get(1).getDefinitions().get(0).getMethod(), "GET"); + Assert.assertEquals(httpAPI.get(1).getDefinitions().get(0).getPath(), "salary"); + Assert.assertEquals(cell.getSpec().getGatewayTemplate().getSpec().getType(), "MicroGateway"); + } + + @Test(groups = "build") + public void validateBuildTimeServiceTemplates() { + final List servicesTemplates = cell.getSpec().getServicesTemplates(); + Assert.assertEquals(servicesTemplates.get(0).getMetadata().getName(), "employee"); + final Container container = servicesTemplates.get(0).getSpec().getContainer(); + Assert.assertEquals(container.getEnv().get(0).getName(), "SALARY_HOST"); + Assert.assertEquals(container.getEnv().get(0).getValue(), "{{instance_name}}--salary-service"); + Assert.assertEquals(container.getImage(), "docker.io/celleryio/sampleapp-employee"); + Assert.assertEquals(container.getPorts().get(0).getContainerPort().intValue(), 8080); + Assert.assertEquals(servicesTemplates.get(0).getSpec().getReplicas(), 1); + Assert.assertEquals(servicesTemplates.get(0).getSpec().getServicePort(), 80); + Assert.assertEquals(servicesTemplates.get(1).getMetadata().getName(), "salary"); + Assert.assertEquals(servicesTemplates.get(1).getSpec().getContainer().getPorts() + .get(0).getContainerPort().intValue(), 8080); + Assert.assertEquals(servicesTemplates.get(1).getSpec().getReplicas(), 1); + Assert.assertEquals(servicesTemplates.get(1).getSpec().getServicePort(), 80); + } + + @Test(groups = "build") + public void validateBuildTimeProbes() { + final List servicesTemplates = cell.getSpec().getServicesTemplates(); + Assert.assertEquals(servicesTemplates.get(0).getMetadata().getName(), "employee"); + final Container empContainer = servicesTemplates.get(0).getSpec().getContainer(); + final Probe livenessProbe = empContainer.getLivenessProbe(); + Assert.assertNotNull(livenessProbe); + Assert.assertEquals(livenessProbe.getPeriodSeconds(), new Integer(10)); + Assert.assertEquals(livenessProbe.getInitialDelaySeconds(), new Integer(30)); + Assert.assertEquals(livenessProbe.getSuccessThreshold(), new Integer(1)); + Assert.assertEquals(livenessProbe.getTimeoutSeconds(), new Integer(1)); + Assert.assertEquals(livenessProbe.getFailureThreshold(), new Integer(3)); + Assert.assertEquals(livenessProbe.getTcpSocket().getPort().getIntVal(), new Integer(8080)); + + final Probe readinessProbe = empContainer.getReadinessProbe(); + Assert.assertNotNull(readinessProbe); + Assert.assertEquals(readinessProbe.getPeriodSeconds(), new Integer(10)); + Assert.assertEquals(readinessProbe.getInitialDelaySeconds(), new Integer(10)); + Assert.assertEquals(readinessProbe.getSuccessThreshold(), new Integer(1)); + Assert.assertEquals(readinessProbe.getTimeoutSeconds(), new Integer(50)); + Assert.assertEquals(readinessProbe.getFailureThreshold(), new Integer(3)); + Assert.assertEquals(readinessProbe.getExec().getCommand().size(), 3); + + final Container salaryContainer = servicesTemplates.get(1).getSpec().getContainer(); + final Probe livenessProbeSalary = salaryContainer.getLivenessProbe(); + Assert.assertNotNull(livenessProbeSalary); + Assert.assertEquals(livenessProbeSalary.getPeriodSeconds(), new Integer(10)); + Assert.assertEquals(livenessProbeSalary.getInitialDelaySeconds(), new Integer(30)); + Assert.assertEquals(livenessProbeSalary.getSuccessThreshold(), new Integer(1)); + Assert.assertEquals(livenessProbeSalary.getTimeoutSeconds(), new Integer(1)); + Assert.assertEquals(livenessProbeSalary.getFailureThreshold(), new Integer(3)); + Assert.assertEquals(livenessProbeSalary.getTcpSocket().getPort().getIntVal(), new Integer(8080)); + } + + @Test(groups = "run") + public void compileCellRun() throws IOException, InterruptedException { + String tmpDir = LangTestUtils.createTempImageDir(SOURCE_DIR_PATH, cellImageInfo.getName()); + Path tempPath = Paths.get(tmpDir); + Assert.assertEquals(LangTestUtils.compileCellRunFunction(SOURCE_DIR_PATH, "probes" + BAL, + cellImageInfo, dependencyCells, tmpDir), 0); + File newYaml = tempPath.resolve(ARTIFACTS).resolve(CELLERY).resolve(cellImageInfo.getName() + YAML).toFile(); + runtimeCell = CelleryUtils.getInstance(newYaml.getAbsolutePath()); + } + + @Test(groups = "run") + public void validateRunTimeCellAvailability() { + Assert.assertNotNull(runtimeCell); + } + + @Test(groups = "run") + public void validateRunTimeAPIVersion() { + Assert.assertEquals(runtimeCell.getApiVersion(), CELLERY_MESH_VERSION); + } + + @Test(groups = "run") + public void validateRunTimeMetaData() { + final ObjectMeta metadata = runtimeCell.getMetadata(); + Assert.assertEquals(metadata.getName(), cellImageInfo.getName()); + Assert.assertEquals(metadata.getAnnotations().get(CELLERY_IMAGE_ORG), cellImageInfo.getOrg()); + Assert.assertEquals(metadata.getAnnotations().get(CELLERY_IMAGE_NAME), cellImageInfo.getName()); + Assert.assertEquals(metadata.getAnnotations().get(CELLERY_IMAGE_VERSION), cellImageInfo.getVer()); + } + + @Test(groups = "run") + public void validateRunTimeGatewayTemplate() { + final List httpList = runtimeCell.getSpec().getGatewayTemplate().getSpec().getHttp(); + Assert.assertEquals(httpList.get(0).getBackend(), "employee"); + Assert.assertEquals(httpList.get(0).getContext(), "employee"); + Assert.assertEquals(httpList.get(0).getDefinitions().get(0).getMethod(), "GET"); + Assert.assertEquals(httpList.get(0).getDefinitions().get(0).getPath(), "/details"); + Assert.assertEquals(httpList.get(1).getBackend(), "salary"); + Assert.assertEquals(httpList.get(1).getContext(), "payroll"); + Assert.assertEquals(httpList.get(1).getDefinitions().get(0).getMethod(), "GET"); + Assert.assertEquals(httpList.get(1).getDefinitions().get(0).getPath(), "salary"); + Assert.assertEquals(runtimeCell.getSpec().getGatewayTemplate().getSpec().getType(), "MicroGateway"); + } + + @Test(groups = "run") + public void validateRunTimeServiceTemplates() { + final List servicesTemplates = runtimeCell.getSpec().getServicesTemplates(); + Assert.assertEquals(servicesTemplates.get(0).getMetadata().getName(), "employee"); + final Container container = servicesTemplates.get(0).getSpec().getContainer(); + Assert.assertEquals(container.getEnv().get(0).getName(), "SALARY_HOST"); + Assert.assertEquals(container.getEnv().get(0).getValue(), "probe-inst--salary-service"); + Assert.assertEquals(container.getImage(), "docker.io/celleryio/sampleapp-employee"); + Assert.assertEquals(container.getPorts().get(0).getContainerPort().intValue(), 8080); + Assert.assertEquals(servicesTemplates.get(0).getSpec().getReplicas(), 1); + Assert.assertEquals(servicesTemplates.get(0).getSpec().getServicePort(), 80); + Assert.assertEquals(servicesTemplates.get(1).getMetadata().getName(), "salary"); + Assert.assertEquals(servicesTemplates.get(1).getSpec().getContainer().getPorts() + .get(0).getContainerPort().intValue(), 8080); + Assert.assertEquals(servicesTemplates.get(1).getSpec().getReplicas(), 1); + Assert.assertEquals(servicesTemplates.get(1).getSpec().getServicePort(), 80); + } + + + @Test(groups = "run") + public void validateRuntimeProbes() { + final List servicesTemplates = runtimeCell.getSpec().getServicesTemplates(); + Assert.assertEquals(servicesTemplates.get(0).getMetadata().getName(), "employee"); + final Container empContainer = servicesTemplates.get(0).getSpec().getContainer(); + final Probe livenessProbe = empContainer.getLivenessProbe(); + Assert.assertNotNull(livenessProbe); + Assert.assertEquals(livenessProbe.getPeriodSeconds(), new Integer(10)); + Assert.assertEquals(livenessProbe.getInitialDelaySeconds(), new Integer(30)); + Assert.assertEquals(livenessProbe.getSuccessThreshold(), new Integer(1)); + Assert.assertEquals(livenessProbe.getTimeoutSeconds(), new Integer(1)); + Assert.assertEquals(livenessProbe.getFailureThreshold(), new Integer(5)); + Assert.assertEquals(livenessProbe.getTcpSocket().getPort().getIntVal(), new Integer(8080)); + + final Probe readinessProbe = empContainer.getReadinessProbe(); + Assert.assertNotNull(readinessProbe); + Assert.assertEquals(readinessProbe.getPeriodSeconds(), new Integer(10)); + Assert.assertEquals(readinessProbe.getInitialDelaySeconds(), new Integer(10)); + Assert.assertEquals(readinessProbe.getSuccessThreshold(), new Integer(1)); + Assert.assertEquals(readinessProbe.getTimeoutSeconds(), new Integer(50)); + Assert.assertEquals(readinessProbe.getFailureThreshold(), new Integer(3)); + Assert.assertEquals(readinessProbe.getExec().getCommand().size(), 3); + + final Container salaryContainer = servicesTemplates.get(1).getSpec().getContainer(); + final Probe livenessProbeSalary = salaryContainer.getLivenessProbe(); + Assert.assertNotNull(livenessProbeSalary); + Assert.assertEquals(livenessProbeSalary.getPeriodSeconds(), new Integer(10)); + Assert.assertEquals(livenessProbeSalary.getInitialDelaySeconds(), new Integer(30)); + Assert.assertEquals(livenessProbeSalary.getSuccessThreshold(), new Integer(1)); + Assert.assertEquals(livenessProbeSalary.getTimeoutSeconds(), new Integer(1)); + Assert.assertEquals(livenessProbeSalary.getFailureThreshold(), new Integer(3)); + Assert.assertEquals(livenessProbeSalary.getTcpSocket().getPort().getIntVal(), new Integer(8080)); + } + + @AfterClass + public void cleanUp() throws KubernetesPluginException { + KubernetesUtils.deleteDirectory(TARGET_PATH); + } +} diff --git a/components/cellery-component-test/src/test/java/org/cellery/components/test/utils/CelleryTestConstants.java b/components/cellery-component-test/src/test/java/org/cellery/components/test/utils/CelleryTestConstants.java index 351fee70..a1f2d6d4 100644 --- a/components/cellery-component-test/src/test/java/org/cellery/components/test/utils/CelleryTestConstants.java +++ b/components/cellery-component-test/src/test/java/org/cellery/components/test/utils/CelleryTestConstants.java @@ -40,6 +40,7 @@ public class CelleryTestConstants { public static final String CELLERY_IMAGE_ORG = "mesh.cellery.io/cell-image-org"; public static final String CELLERY_IMAGE_NAME = "mesh.cellery.io/cell-image-name"; public static final String CELLERY_IMAGE_VERSION = "mesh.cellery.io/cell-image-version"; + public static final String CELLERY_MESH_VERSION = "mesh.cellery.io/v1alpha1"; public static final String YAML = ".yaml"; public static final String BAL = ".bal"; public static final String JSON = ".json"; diff --git a/components/cellery-component-test/src/test/java/org/cellery/components/test/utils/LangTestUtils.java b/components/cellery-component-test/src/test/java/org/cellery/components/test/utils/LangTestUtils.java index cba249d0..a4014141 100644 --- a/components/cellery-component-test/src/test/java/org/cellery/components/test/utils/LangTestUtils.java +++ b/components/cellery-component-test/src/test/java/org/cellery/components/test/utils/LangTestUtils.java @@ -92,9 +92,8 @@ private static void logOutput(InputStream inputStream) throws IOException { * @throws InterruptedException if an error occurs while compiling * @throws IOException if an error occurs while writing file */ - public static int compileCellBuildFunction(Path sourceDirectory, String fileName, - CellImageInfo cellImageInfo - , Map envVar) throws InterruptedException, IOException { + public static int compileCellBuildFunction(Path sourceDirectory, String fileName, CellImageInfo cellImageInfo, + Map envVar) throws InterruptedException, IOException { return compileBallerinaFunction(BUILD, sourceDirectory, fileName, cellImageInfo, new HashMap<>(), envVar); @@ -110,8 +109,7 @@ public static int compileCellBuildFunction(Path sourceDirectory, String fileName * @throws InterruptedException if an error occurs while compiling * @throws IOException if an error occurs while writing file */ - public static int compileCellBuildFunction(Path sourceDirectory, String fileName, - CellImageInfo cellImageInfo) + public static int compileCellBuildFunction(Path sourceDirectory, String fileName, CellImageInfo cellImageInfo) throws InterruptedException, IOException { return compileCellBuildFunction(sourceDirectory, fileName, cellImageInfo, new HashMap<>()); @@ -128,9 +126,9 @@ public static int compileCellBuildFunction(Path sourceDirectory, String fileName * @throws InterruptedException if an error occurs while compiling * @throws IOException if an error occurs while writing file */ - public static int compileCellRunFunction(Path sourceDirectory, String fileName, - CellImageInfo cellImageInfo - , Map envVar, Map instanceData, String tmpDir) + public static int compileCellRunFunction(Path sourceDirectory, String fileName, CellImageInfo cellImageInfo, + Map envVar, Map instanceData, + String tmpDir) throws InterruptedException, IOException { envVar.put("CELLERY_IMAGE_DIR", tmpDir); return compileBallerinaFunction(RUN, sourceDirectory, fileName, cellImageInfo, instanceData, envVar); @@ -299,7 +297,7 @@ private static void moveRefJsonToCelleryHome(Path sourceDirectory, CellImageInfo private static String createExecutableBalFiles(Path sourcePath, String fileName, String action) throws IOException { String executableBalName = fileName.replace(BAL, "") + "_" + action + BAL; - Path targetDir = sourcePath.resolve("target"); + Path targetDir = sourcePath.resolve(TARGET); if (!Files.exists(targetDir)) { Files.createDirectory(targetDir); } diff --git a/components/cellery-component-test/src/test/resources/testng.xml b/components/cellery-component-test/src/test/resources/testng.xml index f71edf59..b92df281 100644 --- a/components/cellery-component-test/src/test/resources/testng.xml +++ b/components/cellery-component-test/src/test/resources/testng.xml @@ -37,6 +37,7 @@ + diff --git a/components/lang/src/main/java/io/cellery/impl/CreateInstance.java b/components/lang/src/main/java/io/cellery/impl/CreateInstance.java index e975ebc7..11147d35 100644 --- a/components/lang/src/main/java/io/cellery/impl/CreateInstance.java +++ b/components/lang/src/main/java/io/cellery/impl/CreateInstance.java @@ -184,7 +184,7 @@ private void updateProbes(ServiceTemplate serviceTemplate, Component updatedComp probe.setPeriodSeconds(readinessProbe.getPeriodSeconds()); probe.setSuccessThreshold(readinessProbe.getSuccessThreshold()); probe.setTimeoutSeconds(readinessProbe.getTimeoutSeconds()); - serviceTemplate.getSpec().getContainer().setLivenessProbe(probe); + serviceTemplate.getSpec().getContainer().setReadinessProbe(probe); } } diff --git a/test-cases/employee-portal/cellery/employee/employee.bal b/test-cases/employee-portal/cellery/employee/employee.bal index 60b92be0..10146d66 100644 --- a/test-cases/employee-portal/cellery/employee/employee.bal +++ b/test-cases/employee-portal/cellery/employee/employee.bal @@ -50,9 +50,6 @@ public function build(cellery:ImageName iName) returns error? { envVars: { SALARY_HOST: { value: cellery:getHost(salaryComponent) - }, - PORT: { - value: salaryContainerPort } }, labels: { diff --git a/test-cases/probes/probes.bal b/test-cases/probes/probes.bal index 46a36368..783a688d 100644 --- a/test-cases/probes/probes.bal +++ b/test-cases/probes/probes.bal @@ -22,7 +22,7 @@ public function build(cellery:ImageName iName) returns error? { } ] }, - expose: "local" + expose: "global" } }, probes: { @@ -46,7 +46,15 @@ public function build(cellery:ImageName iName) returns error? { employee: { port:empPort, context: "employee", - expose: "local" + definition: { + resources: [ + { + path: "/details", + method: "GET" + } + ] + }, + expose: "global" } }, probes: { @@ -84,6 +92,7 @@ public function build(cellery:ImageName iName) returns error? { public function run(cellery:ImageName iName, map instances) returns error? { cellery:CellImage employeeCell = check cellery:constructCellImage(untaint iName); + employeeCell.components.empComp.probes.liveness.failureThreshold = 5; return cellery:createInstance(employeeCell, iName, instances); }