Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.

Commit

Permalink
Add probes test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
anuruddhal authored and isurulucky committed Jun 26, 2019
1 parent c79da95 commit 90f603b
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, CellImageInfo> 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<API> 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<ServiceTemplate> 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<ServiceTemplate> 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<API> 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<ServiceTemplate> 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<ServiceTemplate> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> envVar) throws InterruptedException, IOException {
public static int compileCellBuildFunction(Path sourceDirectory, String fileName, CellImageInfo cellImageInfo,
Map<String, String> envVar) throws InterruptedException, IOException {

return compileBallerinaFunction(BUILD, sourceDirectory, fileName, cellImageInfo, new HashMap<>(),
envVar);
Expand All @@ -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<>());
Expand All @@ -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<String, String> envVar, Map<String, CellImageInfo> instanceData, String tmpDir)
public static int compileCellRunFunction(Path sourceDirectory, String fileName, CellImageInfo cellImageInfo,
Map<String, String> envVar, Map<String, CellImageInfo> instanceData,
String tmpDir)
throws InterruptedException, IOException {
envVar.put("CELLERY_IMAGE_DIR", tmpDir);
return compileBallerinaFunction(RUN, sourceDirectory, fileName, cellImageInfo, instanceData, envVar);
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<class name="org.cellery.components.test.scenarios.reviews.DatabaseTest"/>
<class name="org.cellery.components.test.scenarios.reviews.CustomerProductTest"/>
<class name="org.cellery.components.test.scenarios.reviews.ReviewsTest"/>
<class name="org.cellery.components.test.scenarios.probes.ProbesTest"/>
</classes>
</test>
</suite>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
3 changes: 0 additions & 3 deletions test-cases/employee-portal/cellery/employee/employee.bal
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ public function build(cellery:ImageName iName) returns error? {
envVars: {
SALARY_HOST: {
value: cellery:getHost(salaryComponent)
},
PORT: {
value: salaryContainerPort
}
},
labels: {
Expand Down
Loading

0 comments on commit 90f603b

Please sign in to comment.