/*
+----------------------------------------------------------------------+
| Class: CLauncher |
| |
| Developper: Yossi Kimron |
| Eric Gavaldo |
| Version: 1.4 |
+----------------------------------------------------------------------+
*/
package com.xqual.xlauncher.python;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Vector;
import com.xqual.xagent.launcher.CExecutionStep;
import com.xqual.xagent.launcher.CLauncher;
import com.xqual.xagent.launcher.CParamParsingException;
import com.xqual.xagent.launcher.CReturnStatus;
import com.xqual.xagent.launcher.runner.CRunner;
import com.xqual.xagent.launcher.runner.IRunner;
import com.xqual.xcommon.CAttribute;
import com.xqual.xcommon.IConstantsResults;
import com.xqual.xcommon.utils.CFileUtils;
import com.xqual.xcommon.utils.CUtils;
import com.xqual.xlauncher.CTimeoutListener;
/**
* The CLauncherImpl implementation of ILauncher for Python.
* @author Yossi Kimron
*/
public class CLauncherImpl extends CLauncher implements IConstantsResults {
// +==============================================================+
// | Attributes |
// +==============================================================+
static final String TRACE_HEADER = "{python } ";
// parameters impacting executing at run time set by the test operator
private String testRootPath;
private int timeout = 600;
private String pythonInstallPath;
private File workingDir;
private static final String PYTHON_INTERPRETER_EXE = CUtils.getExecutableName("python");
String attribs[][];
private String logfilename = "";
// +==============================================================+
// | Constructors |
// +==============================================================+
public CLauncherImpl() {
super(TRACE_HEADER);
}
// +==============================================================+
// | Methods |
// +==============================================================+
@Override
public CReturnStatus initialize(int sutId, String sutName, String sutVersion) {
setSutDetails(sutId, sutName, sutVersion);
setDefaultTestcaseMustBeCreated(true); // if there is not testcase, let the systems create a default one
// check the configuration sent by the manager
printConfiguration();
Vector executionSteps = new Vector();
try {
// retrieve the parameters we need
testRootPath = getStringParamValue("General", "Test root path");
timeout = getIntegerParamValue("General", "Asynchronous timeout (in seconds)");
pythonInstallPath = getStringParamValue("Python", "Python install path");
} catch (CParamParsingException e) {
traceln(LOG_PRIORITY_SEVERE, "parsing error during initialization");
executionSteps.add(new CExecutionStep(RESULT_FAILURE, "Exception during initialize: " + e.getMessage()));
return new CReturnStatus(RESULT_FAILURE, executionSteps);
}
return new CReturnStatus(RESULT_SUCCESS, executionSteps);
}
@Override
public CReturnStatus preRun(int testId, String testPath, String testName, Vector attributes, String additionalInfo) {
traceln(LOG_PRIORITY_INFO, "preRun testId=" + testId + " testPath=" + testPath + " [" + testName + "]...");
attribs = new String[attributes.size()][2];
for (int i=0; i executionSteps = new Vector();
return new CReturnStatus(RESULT_SUCCESS, executionSteps);
}
@Override
public CReturnStatus run(int testId, String testPath, String testName, int testcaseIndex, String testcaseName, String additionalInfo) {
// Build attributes list
traceln(LOG_PRIORITY_INFO, "building attribute list...");
String attriblist = "";
for (int i=0; i < attribs.length ; i++) {
attriblist = attriblist.concat( " /" + attribs[i][0] + "=\"" + attribs[i][1] + "\"");
}
traceln(LOG_PRIORITY_INFO, "attribute list = " + attriblist );
traceln(LOG_PRIORITY_INFO, "run testId=" + testId + " testPath=" + testRootPath + "/" + testPath + "/" + testName + " testcaseIndex=" + testcaseIndex + attriblist + "...");
Vector executionSteps = new Vector();
String scriptParentFolderPath = testRootPath + "/" + testPath + "/";
workingDir = new File(scriptParentFolderPath);
logfilename = String.format("%s_%s.txt", testName, testcaseIndex );
// +------------------------------------+
// | Interpret the script
// +------------------------------------+
CRunner pythonRunner = new CRunner("[" + testId + "] "+ testPath + ":" + testName + "." + testcaseIndex,
CFileUtils.quoteFilePath(pythonInstallPath + "/" + PYTHON_INTERPRETER_EXE) + " " +
CFileUtils.quoteFilePath(testRootPath + "/" + testPath + "/" + testName + ".py") + " " +
"/debug " +
"/testcaseIndex=" + testcaseIndex + " /log=" + CFileUtils.quoteFilePath(logfilename) + " " + attriblist ,
workingDir);
short result = pythonRunner.requestAction(IRunner.START_PROCESS, IRunner.DO_NOT_WAIT_END_OF_EXECUTION);
if (result == RESULT_FAILURE) {
executionSteps.add(new CExecutionStep(RESULT_FAILURE, "script interpretation failed"));
return new CReturnStatus(RESULT_FAILURE, executionSteps);
}
// to check if the execution completed correctly, we need to check if the "test_completed.txt" has been created
File testCompletedFile = new File(workingDir + "/test_completed.txt");
short resultTimeout = CTimeoutListener.waitForFile(testCompletedFile, timeout);
if (resultTimeout != RESULT_SUCCESS) {
executionSteps.add(new CExecutionStep(RESULT_FAILURE, "timeout of " + timeout + " seconds to execute the test case expired"));
return new CReturnStatus(RESULT_FAILURE, executionSteps);
}
// file exists, remove it to complete asyncronous handshake
if (!testCompletedFile.delete())
executionSteps.add(new CExecutionStep(RESULT_UNKNOWN, "Failed to cleanup test_completed.txt"));
File logFile = new File(workingDir + "/" + logfilename);
if (!logFile.exists()) {
traceln(LOG_PRIORITY_SEVERE, "Result file not found!");
executionSteps.add(new CExecutionStep(RESULT_FAILURE, "run: result file not found!"));
return new CReturnStatus(RESULT_FAILURE, executionSteps);
} else {
executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "run: result file found"));
}
addAttachment(logFile);
return parseResultFile(logFile, executionSteps);
}
@Override
public CReturnStatus postRun(int testId, String testPath, String testName) {
traceln(LOG_PRIORITY_INFO, "postRun testId=" + testId + " testPath=" + testPath + ":" + testName + "...");
Vector executionSteps = new Vector();
executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "postRun: succeeded"));
return new CReturnStatus(RESULT_SUCCESS, executionSteps);
}
@Override
public CReturnStatus terminate() {
Vector executionSteps = new Vector();
executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "Terminate"));
return new CReturnStatus(RESULT_SUCCESS, executionSteps);
}
// +--------------------------+
// | Utilities |
// +--------------------------+
private CReturnStatus parseResultFile(File resultFile, Vector executionSteps) {
String line, message, timestamp;
boolean errorDetected = false;
try {
FileInputStream fileInputStream = new FileInputStream(resultFile);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
//BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
while ((line = bufferedReader.readLine()) != null) {
line = line.trim();
System.out.println(">" + line);
// Requested format: 2010-01-23 19:00:09 [Success] Any message...
if (line.indexOf("[Success]")>=0) {
timestamp = line.substring(0, line.indexOf("[Success]"));
message = line.substring( line.indexOf("[Success]") + 10, line.length());
executionSteps.add(new CExecutionStep( timestamp, RESULT_SUCCESS, message));
} else if (line.indexOf("[Failure]")>=0) {
timestamp = line.substring(0, line.indexOf("[Failure]"));
message = line.substring( line.indexOf("[Failure]") + 10, line.length());
executionSteps.add(new CExecutionStep(timestamp, RESULT_FAILURE, message));
errorDetected = true;
} else if (line.indexOf("[Log]")>=0) {
timestamp = line.substring(0, line.indexOf("[Log]"));
message = line.substring( line.indexOf("[Log]") + 6, line.length());
executionSteps.add(new CExecutionStep( timestamp, RESULT_UNKNOWN, message));
} else {
//traceln(LOG_PRIORITY_SEVERE, "unknown tag!");
}
}
} catch (Exception e) {
traceln(LOG_PRIORITY_SEVERE, "exception whle parsing the result file: " + e);
executionSteps.add(new CExecutionStep(RESULT_FAILURE, "Exception whle parsing the result file: " + e));
errorDetected = true;
}
if (errorDetected) {
return new CReturnStatus(RESULT_FAILURE, executionSteps);
} else {
return new CReturnStatus(RESULT_SUCCESS, executionSteps);
}
}
}