/* +----------------------------------------------------------------------+ | Class: CLauncher | | | | Developper: Eric Gavaldo (eric.gavaldo@xqual.com) | | Version: 1.2 | +----------------------------------------------------------------------+ */ package com.xqual.xlauncher.pyunit; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; 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; /** * The CLauncherImpl implementation of ILauncher for PyUnit. * @author egavaldo */ public class CLauncherImpl extends CLauncher implements IConstantsResults { // +==============================================================+ // | Attributes | // +==============================================================+ static final String TRACE_HEADER = "{pyunit } "; static final String TRACE_FILE_NAME = "python_traces.txt"; // parameters impacting executing at run time set by the test operator private String testRootPath; private String pythonInstallPath; private String pyunitInterpreter; private File workingDir; private static final String PYTHON_INTERPRETER_EXE = CUtils.getExecutableName("python"); // +==============================================================+ // | 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"); // i.e. Y:/Externals/pyunit-1.4.1/examples pythonInstallPath = getStringParamValue("PyUnit", "Python install path"); // i.e. C:/Python26 pyunitInterpreter = CFileUtils.quoteFilePath(pythonInstallPath + "/" + PYTHON_INTERPRETER_EXE) + " "; } 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 + "]..."); // +------------------------------------+ // | Set the module path // +------------------------------------+ // This is the equivalent of a class path but in python you cannot pass it in argument :( // // SET PYTHONPATH=.; // // i.e. PYTHONPATH=.;Y:\Externals\pyunit-1.4.1 // // Unfortunately, this cannot be done programatically so THIS MUST BE DONE MANUALLY (ONLY ONCE ON THE HOST) workingDir = new File(testRootPath + (testPath.length() > 0 ? "/" + testPath : "")); // parent folder of all the test classes Vector 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) { traceln(LOG_PRIORITY_INFO, "run testId=" + testId + " testPath=" + testRootPath + "/" + testPath + "/" + testName + " testcaseIndex=" + testcaseIndex + "..."); Vector executionSteps = new Vector(); // +------------------------------------+ // | Interpret the script // +------------------------------------+ File pyUnitTraceFile = new File(TRACE_FILE_NAME); if (pyUnitTraceFile.exists()) pyUnitTraceFile.delete(); try { pyUnitTraceFile.createNewFile(); } catch (IOException e) { traceln(LOG_PRIORITY_SEVERE, "Could not create file \"" + TRACE_FILE_NAME + "\"..."); } // +------------------------------------+ // | Run the PyUnit test class // +------------------------------------+ CRunner pyUnitRunner = new CRunner("[" + testId + "] "+ testPath + "." + testName, pyunitInterpreter.toString() + " " + testName + ".py", workingDir); // here we redirect the output to a file for later parsing short result = pyUnitRunner.requestAction(IRunner.START_PROCESS, IRunner.WAIT_END_OF_EXECUTION, pyUnitTraceFile); // +------------------------------------+ // | Add the output console to the test // +------------------------------------+ addAttachment(pyUnitTraceFile); if (result == RESULT_FAILURE) { executionSteps.add(new CExecutionStep(RESULT_FAILURE, "execution process failed")); parseResultFile(executionSteps); // to have the details anyway return new CReturnStatus(RESULT_FAILURE, executionSteps); } else { executionSteps.add(new CExecutionStep(RESULT_SUCCESS, "execution process succeeded")); return parseResultFile(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(Vector executionSteps) { // parse the result file to get the result and the execution steps File resultFile = new File(TRACE_FILE_NAME); if (!resultFile.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")); } String line; boolean errorDetected = false; boolean testHaveBeenRun = false; try { FileInputStream fileInputStream = new FileInputStream(resultFile); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); int nbLines = 0; while ((line = bufferedReader.readLine()) != null) { nbLines++; line = line.trim(); System.out.println(">" + line); if (line.startsWith("FAIL:")) { executionSteps.add(new CExecutionStep(RESULT_FAILURE, line)); errorDetected = true; } else if (line.startsWith("Ran ")) { if (line.startsWith("Ran 0 tests")) { executionSteps.add(new CExecutionStep(RESULT_FAILURE, line)); errorDetected = true; } else { executionSteps.add(new CExecutionStep(RESULT_UNKNOWN, line)); } testHaveBeenRun = true; } else if (line.startsWith("FAILED (")) { executionSteps.add(new CExecutionStep(RESULT_FAILURE, line)); errorDetected = true; } else { //traceln(LOG_PRIORITY_SEVERE, "unknown tag!"); } } if (nbLines == 0) { executionSteps.add(new CExecutionStep(RESULT_FAILURE, "empty trace")); errorDetected = true; } } catch (Exception e) { traceln(LOG_PRIORITY_SEVERE, "exception whle parsing the result file: " + e); executionSteps.add(new CExecutionStep(RESULT_FAILURE, "Exception while parsing the result file: " + e)); errorDetected = true; } if (errorDetected || !testHaveBeenRun) { return new CReturnStatus(RESULT_FAILURE, executionSteps); } else { return new CReturnStatus(RESULT_SUCCESS, executionSteps); } } }