Skip to content

Commit

Permalink
Merge pull request #563 from kasunbg/workspace-prop-for-scenario-scripts
Browse files Browse the repository at this point in the history
Send the workspace as a property into the scenario test scripts.
  • Loading branch information
harshanL committed Mar 15, 2018
2 parents dfee11b + 350bc0f commit aec0b1a
Show file tree
Hide file tree
Showing 23 changed files with 407 additions and 134 deletions.
15 changes: 15 additions & 0 deletions automation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<scope>test</scope>
</dependency>

<!-- JMeter dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.wso2.testgrid.common.Port;
import org.wso2.testgrid.common.TestScenario;
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.TestScenarioUOW;

Expand All @@ -44,6 +45,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

/**
Expand All @@ -54,10 +56,19 @@
public class JMeterExecutor extends TestExecutor {

private static final Logger logger = LoggerFactory.getLogger(JMeterExecutor.class);
private final TestScenarioUOW testScenarioUOW;
private String testLocation;
private String testName;
private TestScenario testScenario;

public JMeterExecutor() {
testScenarioUOW = new TestScenarioUOW();
}

public JMeterExecutor(TestScenarioUOW testScenarioUOW) {
this.testScenarioUOW = testScenarioUOW;
}

@Override
public void execute(String script, DeploymentCreationResult deploymentCreationResult)
throws TestAutomationException {
Expand All @@ -67,7 +78,7 @@ public void execute(String script, DeploymentCreationResult deploymentCreationRe
StringUtil.concatStrings("JMeter Executor not initialised properly.", "{ Test Name: ", testName,
", Test Location: ", testLocation, "}"));
}
overrideJMeterConfig(testLocation,
overrideJMeterConfig(script,
deploymentCreationResult); // Override JMeter properties for current deployment.
//TODO change parameter replacing in jmx files to use JMeter properties file.
try {
Expand Down Expand Up @@ -108,7 +119,6 @@ public void execute(String script, DeploymentCreationResult deploymentCreationRe
jMeterEngine.exit();

//Persist test scenario
TestScenarioUOW testScenarioUOW = new TestScenarioUOW();
try {
testScenarioUOW.persistTestScenario(testScenario);
if (logger.isDebugEnabled()) {
Expand Down Expand Up @@ -189,23 +199,34 @@ private void copyResourceFile(Path fileCopyPath, String fileName) throws TestAut
/**
* Overrides the JMeter properties with the properties required for the current deployment.
*
* @param testLocation directory location of the tests
* @param deployment deployment details of the current pattern
* @param script jmeter script file path
* @param deployment deployment details of the current pattern
*/

private void overrideJMeterConfig(String testLocation, DeploymentCreationResult deployment) {
private void overrideJMeterConfig(String script, DeploymentCreationResult deployment) {
Path path = Paths.get(testLocation, "resources", "user.properties");
if (!Files.exists(path)) {
logger.info("JMeter user.properties file not specified - proceeding with JMeter default properties.");
return;
}
JMeterUtils.loadJMeterProperties(path.toAbsolutePath().toString());
for (Map.Entry<Object, Object> entry : deployment.getProperties().entrySet()) {
JMeterUtils.setProperty((String) entry.getKey(), (String) entry.getValue());
}

//deprecated
for (Host host : deployment.getHosts()) {
JMeterUtils.setProperty(host.getLabel(), host.getIp());
for (Port port : host.getPorts()) {
JMeterUtils.setProperty(port.getProtocol(), String.valueOf(port.getPortNumber()));
}
}

if (logger.isDebugEnabled()) {
String jmeterFile = script.replace(TestGridUtil.getTestGridHomePath(), "");
logger.debug(String.format("List of JMeter properties for %s: %s", jmeterFile, JMeterUtils
.getJMeterProperties()));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void sampleOccurred(SampleEvent sampleEvent) {
super.sampleOccurred(sampleEvent);
SampleResult result = sampleEvent.getResult();

String failureMessage = result.isSuccessful() ? "" :
String message = result.isSuccessful() ? "" :
StringUtil.concatStrings("{ \"Response Data\": \"",
result.getResponseDataAsString().replaceAll("\"", "\\\""),
"\", \"Status code\": \"",
Expand All @@ -71,23 +71,29 @@ public void sampleOccurred(SampleEvent sampleEvent) {
"\", \"Sampler Data\": \"",
result.getSamplerData().replaceAll("\"", "\\\""), "\"}");

//Log the test failure details to test-run.log
jmeterResultLogger.info(StringUtil.concatStrings(
"Test case :", result.getSampleLabel(), " failed for scenario: ",
testScenario.getName(), "\n", "Failure Message: ", failureMessage, "\"}"));
if (!result.isSuccessful()) {
//Truncate the failure message if it exceeds the desired limit
if (message.length() > FAILURE_MESSAGE_LIMIT) {
message = StringUtil.concatStrings(
message.substring(0, FAILURE_MESSAGE_LIMIT), "\nlog truncated...");
}

//Truncate the failure message if it exceeds the desired limit
if (failureMessage.length() > FAILURE_MESSAGE_LIMIT) {
failureMessage = StringUtil.concatStrings(
failureMessage.substring(0, FAILURE_MESSAGE_LIMIT), "\nlog truncated...");
jmeterResultLogger.warn(StringUtil.concatStrings(
"Test case :", result.getSampleLabel(), " failed for scenario: ",
testScenario.getName(), "\n", "Failure Message: ", message, "\"}"));

} else {
jmeterResultLogger.debug(StringUtil.concatStrings(
"Test case :", result.getSampleLabel(), " for :", testScenario.getName(),
" ran successfully."));
}

// Add result to concurrent list
TestCase testCase = new TestCase();
testCase.setName(result.getSampleLabel());
testCase.setTestScenario(testScenario);
testCase.setSuccess(result.isSuccessful());
testCase.setFailureMessage(failureMessage);
testCase.setFailureMessage(message);
testScenario.addTestCase(testCase);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
*/
package org.wso2.testgrid.automation.executor;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.testgrid.automation.TestAutomationException;
import org.wso2.testgrid.common.DeploymentCreationResult;
import org.wso2.testgrid.dao.uow.TestScenarioUOW;

import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -34,6 +38,14 @@
*/
public class JMeterExecutorTest {

@Mock
TestScenarioUOW testScenarioUOW;

@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
}

@DataProvider(name = "invalidJMeterFiles")
public Object[][] invalidJMeterFiles() {
// Set cloned test plan location.
Expand Down Expand Up @@ -67,7 +79,7 @@ public void testExecuteJMeter() throws URISyntaxException, TestAutomationExcepti

String testLocation = Paths.get(resource.getPath(), "SolutionPattern22", "Tests")
.toAbsolutePath().toString();
TestExecutor testExecutor = new JMeterExecutor();
TestExecutor testExecutor = new JMeterExecutor(testScenarioUOW);
testExecutor.init(testLocation, "solution22", null);
// testExecutor.execute(testScript, new Deployment());

Expand All @@ -80,7 +92,7 @@ public void testExecuteJMeter() throws URISyntaxException, TestAutomationExcepti
public void testExecuteInvalidTest(String testLocation, String scriptPath, String exceptionMessage)
throws URISyntaxException {
try {
TestExecutor testExecutor = new JMeterExecutor();
TestExecutor testExecutor = new JMeterExecutor(testScenarioUOW);
testExecutor.init(testLocation, "solution22", null);
testExecutor.execute(scriptPath, new DeploymentCreationResult());
} catch (TestAutomationException e) {
Expand All @@ -95,7 +107,7 @@ public void testExecuteInvalidTest(String testLocation, String scriptPath, Strin
expectedExceptionsMessageRegExp = "JMeter Executor not initialised properly.\\{ Test Name: null, " +
"Test Location: null\\}")
public void testNoInit() throws TestAutomationException {
TestExecutor testExecutor = new JMeterExecutor();
TestExecutor testExecutor = new JMeterExecutor(testScenarioUOW);
testExecutor.execute("Script", new DeploymentCreationResult());
}

Expand All @@ -114,7 +126,7 @@ public void testErrorLoadJMeterFile() throws TestAutomationException {

String testLocation = Paths.get(resource.getPath(), "SolutionPattern22", "Tests")
.toAbsolutePath().toString();
TestExecutor testExecutor = new JMeterExecutor();
TestExecutor testExecutor = new JMeterExecutor(testScenarioUOW);
testExecutor.init(testLocation, "solution22", null);
testExecutor.execute(testScript, new DeploymentCreationResult());
}
Expand All @@ -135,7 +147,7 @@ public void testJMeterFileIsDirectory() throws TestAutomationException {
String testLocation = Paths
.get(resource.getPath(), "SolutionPattern22", "Tests")
.toAbsolutePath().toString();
TestExecutor testExecutor = new JMeterExecutor();
TestExecutor testExecutor = new JMeterExecutor(testScenarioUOW);
testExecutor.init(testLocation, "solution22", null);
testExecutor.execute(testScript, new DeploymentCreationResult());
}
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion automation/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ limitations under the License.
<test name="executor-test" parallel="false">
<classes>
<class name="org.wso2.testgrid.automation.executor.JMeterExecutorTest"/>
<class name="org.wso2.testgrid.automation.executor.TestExecutorFactoryTest"/>
</classes>
</test>
</suite>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
* This is a DTO that contain the output of the
Expand All @@ -34,6 +35,7 @@ public class DeploymentCreationResult implements Serializable {

private boolean success = true;
private String name;
private Properties properties = new Properties();
private List<Host> hosts = Collections.emptyList();

public String getName() {
Expand All @@ -52,6 +54,21 @@ public void setSuccess(boolean success) {
this.success = success;
}

public Properties getProperties() {
if (properties == null) {
return new Properties();
}
return properties;
}

public void setProperties(Properties properties) {
this.properties = properties;
}

public void setProperty(String key, String value) {
properties.setProperty(key, value);
}

public List<Host> getHosts() {
return hosts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.testgrid.common.exception.CommandExecutionException;
import org.wso2.testgrid.common.util.TestGridUtil;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -30,6 +31,7 @@
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
Expand All @@ -46,7 +48,8 @@ public class ShellExecutor {
private Path workingDirectory;

public ShellExecutor() {
this(null);
//todo: Set this to the test-run workspace
this(Paths.get(TestGridUtil.getTestGridHomePath()));
}

public ShellExecutor(Path workingDirectory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class TestGridConstants {

public static final String TEST_LOG_FILE_NAME = "test-run.log";
public static final String PRODUCT_TEST_PLANS_DIR = "test-plans";

public static final String WORKSPACE = "workspace";
public static final String TESTGRID_JOB_DIR = "jobs";
public static final String TESTGRID_HOME_ENV = "TESTGRID_HOME";
public static final String TESTGRID_HOME_SYSTEM_PROPERTY = "testgrid.home";
Expand Down
Loading

0 comments on commit aec0b1a

Please sign in to comment.