Skip to content

Commit

Permalink
Merge pull request #555 from harshanL/master
Browse files Browse the repository at this point in the history
Persist TestPlan to database at generate-TestPlan step
  • Loading branch information
harshanL authored Mar 8, 2018
2 parents cedab01 + 56e3892 commit 517b47c
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 149 deletions.
101 changes: 100 additions & 1 deletion common/src/main/java/org/wso2/testgrid/common/util/TestGridUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.testgrid.common.util;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -28,8 +29,12 @@
import org.wso2.testgrid.common.DeploymentPattern;
import org.wso2.testgrid.common.Host;
import org.wso2.testgrid.common.Product;
import org.wso2.testgrid.common.Status;
import org.wso2.testgrid.common.TestGridConstants;
import org.wso2.testgrid.common.TestPlan;
import org.wso2.testgrid.common.TestScenario;
import org.wso2.testgrid.common.config.DeploymentConfig;
import org.wso2.testgrid.common.config.InfrastructureConfig;
import org.wso2.testgrid.common.exception.CommandExecutionException;
import org.wso2.testgrid.common.exception.TestGridException;

Expand All @@ -42,6 +47,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -243,7 +252,7 @@ private static String getInfraParamUUID(String infraParams) throws TestGridExcep
}

/**
* This method builds and returns the parameter string from given properties
* This method builds and returns the parameter string from given properties.
*
* @param properties {@link Properties} with required paramters as key value pairs
* @return the String representation of input paramters
Expand All @@ -258,4 +267,94 @@ public static String getParameterString(String fileInput, Properties properties)
}
return parameterBuilder.toString();
}

/**
* This method generates TestPlan object model that from the given input parameters.
*
* @param deploymentPattern deployment pattern
* @param testPlan testPlan object
* @return TestPlan object model
*/
public static TestPlan toTestPlanEntity(DeploymentPattern deploymentPattern, TestPlan testPlan)
throws CommandExecutionException {
try {
String jsonInfraParams = new ObjectMapper()
.writeValueAsString(testPlan.getInfrastructureConfig().getParameters());
TestPlan testPlanEntity = testPlan.clone();
testPlanEntity.setStatus(Status.PENDING);
testPlanEntity.setDeploymentPattern(deploymentPattern);

// TODO: this code need to use enum valueOf instead of doing if checks for each deployer-type.
if (testPlan.getInfrastructureConfig().getInfrastructureProvider()
== InfrastructureConfig.InfrastructureProvider.SHELL) {
testPlanEntity.setDeployerType(TestPlan.DeployerType.SHELL);
}
testPlanEntity.setInfraParameters(jsonInfraParams);
deploymentPattern.addTestPlan(testPlanEntity);

// Set test run number
int latestTestRunNumber = getLatestTestRunNumber(deploymentPattern, testPlanEntity.getInfraParameters());
testPlanEntity.setTestRunNumber(latestTestRunNumber + 1);

// Set test scenarios
List<TestScenario> testScenarios = new ArrayList<>();
for (String name : testPlan.getScenarioConfig().getScenarios()) {
TestScenario testScenario = new TestScenario();
testScenario.setName(name);
testScenario.setTestPlan(testPlanEntity);
testScenario.setStatus(Status.PENDING);
testScenarios.add(testScenario);
}
testPlanEntity.setTestScenarios(testScenarios);
return testPlanEntity;
} catch (JsonProcessingException e) {
throw new CommandExecutionException(StringUtil
.concatStrings("Error in preparing a JSON object from the given test plan infra " +
"parameters: ", testPlan.getInfrastructureConfig().getParameters()), e);
}
}

/**
* Returns the latest test run number.
*
* @param deploymentPattern deployment pattern to get the latest test run number
* @param infraParams infrastructure parameters to get the latest test run number
* @return latest test run number
*/
private static int getLatestTestRunNumber(DeploymentPattern deploymentPattern, String infraParams) {
// Get test plans with the same infra param
List<TestPlan> testPlans = new ArrayList<>();
for (TestPlan testPlan : deploymentPattern.getTestPlans()) {
if (testPlan.getInfraParameters().equals(infraParams)) {
testPlans.add(testPlan);
}
}

// Get the Test Plan with the latest test run number for the given infra combination
TestPlan latestTestPlan = Collections.max(testPlans, Comparator.comparingInt(
TestPlan::getTestRunNumber));

return latestTestPlan.getTestRunNumber();
}

/**
* Return the deployment pattern name under the DeploymentConfig of a {@link TestPlan}.
* If not found, return the provisioner name under Infrastructure.
*
* @param testPlan the test-plan config
* @return the deployment pattern name.
*/
public static String getDeploymentPatternName(TestPlan testPlan) {
List<DeploymentConfig.DeploymentPattern> deploymentPatterns = testPlan.getDeploymentConfig()
.getDeploymentPatterns();
if (!deploymentPatterns.isEmpty()) {
return deploymentPatterns.get(0).getName();
}
List<InfrastructureConfig.Provisioner> provisioners = testPlan.getInfrastructureConfig().getProvisioners();
if (!provisioners.isEmpty()) {
return provisioners.get(0).getName();
}

return TestGridConstants.DEFAULT_DEPLOYMENT_PATTERN_NAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.testgrid.common.DeploymentPattern;
import org.wso2.testgrid.common.Product;
import org.wso2.testgrid.common.TestGridConstants;
import org.wso2.testgrid.common.TestPlan;
Expand All @@ -38,7 +39,9 @@
import org.wso2.testgrid.common.util.StringUtil;
import org.wso2.testgrid.common.util.TestGridUtil;
import org.wso2.testgrid.dao.TestGridDAOException;
import org.wso2.testgrid.dao.uow.DeploymentPatternUOW;
import org.wso2.testgrid.dao.uow.ProductUOW;
import org.wso2.testgrid.dao.uow.TestPlanUOW;
import org.wso2.testgrid.infrastructure.InfrastructureCombinationsProvider;
import org.wso2.testgrid.logging.plugins.LogFilePathLookup;
import org.yaml.snakeyaml.DumperOptions;
Expand Down Expand Up @@ -104,10 +107,14 @@ public class GenerateTestPlanCommand implements Command {
private InfrastructureCombinationsProvider infrastructureCombinationsProvider;

private ProductUOW productUOW;
private DeploymentPatternUOW deploymentPatternUOW;
private TestPlanUOW testPlanUOW;

public GenerateTestPlanCommand() {
this.infrastructureCombinationsProvider = new InfrastructureCombinationsProvider();
this.productUOW = new ProductUOW();
this.deploymentPatternUOW = new DeploymentPatternUOW();
this.testPlanUOW = new TestPlanUOW();
}

/**
Expand All @@ -121,12 +128,15 @@ public GenerateTestPlanCommand() {
* @param productUOW the ProductUOW
*/
GenerateTestPlanCommand(String productName, String jobConfigFile, String workingDir,
InfrastructureCombinationsProvider combinationsProvider, ProductUOW productUOW) {
InfrastructureCombinationsProvider combinationsProvider, ProductUOW productUOW,
DeploymentPatternUOW deploymentPatternUOW, TestPlanUOW testPlanUOW) {
this.productName = productName;
this.jobConfigFile = jobConfigFile;
this.workingDir = workingDir;
this.infrastructureCombinationsProvider = combinationsProvider;
this.productUOW = productUOW;
this.deploymentPatternUOW = deploymentPatternUOW;
this.testPlanUOW = testPlanUOW;
}

@Override
Expand Down Expand Up @@ -200,6 +210,19 @@ private void processTestgridYaml(TestgridYaml testgridYaml)
}
for (int i = 0; i < testPlans.size(); i++) {
TestPlan testPlan = testPlans.get(i);
DeploymentPattern deploymentPattern = getDeploymentPattern(product,
TestGridUtil.getDeploymentPatternName(testPlan));

// Generate test plan from config
TestPlan testPlanEntity = TestGridUtil.toTestPlanEntity(deploymentPattern, testPlan);

// Product, deployment pattern, test plan and test scenarios should be persisted
TestPlan persistedTestPlan = testPlanUOW.persistTestPlan(testPlanEntity);
testPlan.setId(persistedTestPlan.getId());
testPlan.setTestRunNumber(persistedTestPlan.getTestRunNumber());
//Need to set this as converting to TestPlan entity changes deployerType based on infra provisioner.
testPlan.setDeployerType(persistedTestPlan.getDeployerType());

String fileName = String
.format("%s-%02d%s", TestGridConstants.TEST_PLAN_YAML_PREFIX, (i + 1), FileUtil.YAML_EXTENSION);
String output = yaml.dump(testPlan);
Expand All @@ -213,6 +236,36 @@ private void processTestgridYaml(TestgridYaml testgridYaml)
}
}

/**
* Returns the existing deployment pattern for the given name and product or creates a new deployment pattern for
* the given product and deployment pattern name.
*
* @param product product to get deployment pattern
* @param deploymentPatternName deployment pattern name
* @return deployment pattern for the given product and deployment pattern name
* @throws CommandExecutionException thrown when error on retrieving deployment pattern
*/
private DeploymentPattern getDeploymentPattern(Product product, String deploymentPatternName)
throws CommandExecutionException {
try {
Optional<DeploymentPattern> optionalDeploymentPattern =
deploymentPatternUOW.getDeploymentPattern(product, deploymentPatternName);

if (optionalDeploymentPattern.isPresent()) {
return optionalDeploymentPattern.get();
}

DeploymentPattern deploymentPattern = new DeploymentPattern();
deploymentPattern.setName(deploymentPatternName);
deploymentPattern.setProduct(product);
return deploymentPattern;
} catch (TestGridDAOException e) {
throw new CommandExecutionException(StringUtil
.concatStrings("Error while retrieving deployment pattern for { product: ", product,
", deploymentPatternName: ", deploymentPatternName, "}"));
}
}

/**
* This method will resolve the relative paths in the job-config.yaml
* to absolute paths by taking into account factors such as the workingDir.
Expand Down
Loading

0 comments on commit 517b47c

Please sign in to comment.