Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defect #2451038 Octane cannot correctly open test run report #291

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -125,6 +125,37 @@ public static FilePath getWorkspace(Run<?, ?> run) {
logger.error("BuildHandlerUtils.getWorkspace - run is not handled. Run type : " + run.getClass());
return null;
}
public static Set<FilePath> getWorkspaces(Run<?, ?> run) {
if (run.getExecutor() != null && run.getExecutor().getCurrentWorkspace() != null) {
return Collections.singleton(run.getExecutor().getCurrentWorkspace());
}
if (run instanceof AbstractBuild) {
return Collections.singleton(((AbstractBuild) run).getWorkspace());
}
Set<FilePath> workspaces = new HashSet<>();
if (run instanceof WorkflowRun) {
FlowExecution fe = ((WorkflowRun) run).getExecution();
if (fe != null) {
FlowGraphWalker w = new FlowGraphWalker(fe);
for (FlowNode n : w) {
WorkspaceAction action = n.getAction(WorkspaceAction.class);
if (action != null) {
FilePath workspace = action.getWorkspace();
if (workspace == null) {
workspace = handleWorkspaceActionWithoutWorkspace(action);
}
workspaces.add(workspace);
}
}
}
}

if(workspaces.isEmpty()) {
logger.error("BuildHandlerUtils.getWorkspaces - run is not handled. Run type : " + run.getClass());
}

return workspaces;
}

private static FilePath handleWorkspaceActionWithoutWorkspace(WorkspaceAction action) {
logger.error("Found WorkspaceAction without workspace");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,23 @@ private static class GetJUnitTestResults implements FilePath.FileCallable<FilePa
private List<ModuleDetection> moduleDetection;
private long buildStarted;
private FilePath workspace;
private Set<FilePath> allWorkspaces;
private boolean stripPackageAndClass;
private String sharedCheckOutDirectory;
private Pattern testParserRegEx;
private boolean octaneSupportsSteps;

//this class is run on master and JUnitXmlIterator is runnning on slave.
//this object pass some master2slave data
private Object additionalContext;
private String nodeName;
private Object additionalContext;
private final Set<String> nodeNames = new HashSet<>();

public GetJUnitTestResults(Run<?, ?> build, HPRunnerType hpRunnerType, List<FilePath> reports, boolean stripPackageAndClass, String jenkinsRootUrl) throws IOException, InterruptedException {
this.reports = reports;
this.filePath = new FilePath(build.getRootDir()).createTempFile(TEMP_TEST_RESULTS_FILE_NAME_PREFIX, null);
this.buildStarted = build.getStartTimeInMillis();
this.workspace = BuildHandlerUtils.getWorkspace(build);
this.allWorkspaces = BuildHandlerUtils.getWorkspaces(build);
this.stripPackageAndClass = stripPackageAndClass;
this.hpRunnerType = hpRunnerType;
this.jenkinsRootUrl = jenkinsRootUrl;
Expand All @@ -230,22 +232,30 @@ public GetJUnitTestResults(Run<?, ?> build, HPRunnerType hpRunnerType, List<File


if (HPRunnerType.UFT.equals(hpRunnerType) || HPRunnerType.UFT_MBT.equals(hpRunnerType)) {
Node node = JenkinsUtils.getCurrentNode(workspace);
this.nodeName = node != null && !node.getNodeName().isEmpty() ? node.getNodeName() : "";
List<Node> nodes = new ArrayList<>();

allWorkspaces.forEach(workspace-> nodes.add(JenkinsUtils.getCurrentNode(workspace)));
nodes.forEach(node -> this.nodeNames.add(node != null && !node.getNodeName().isEmpty() ? node.getNodeName() : ""));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that there is case when node name is empty as far as I remember it a case when UFT is run on Jenkins machine itself and not on one of it's agents, can you please try to check if your code covers case the UFT test run with not agents?
Secondly instead of define array and fill it up there more convenient way to do it in Java like this
allWorkspaces.stream().filter(workspace-> nodes.add(JenkinsUtils.getCurrentNode(workspace)).map(workspace-> JenkinsUtils.getCurrentNode(workspace))).collect(collectors.toList())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, when running on the Jenkins machine itself, the node name remains empty. I tested the behavior, and the reports URLs are created correctly in this case, with the path to the 'artifacts' folder from the Jenkins machine.

For the second part, I changed the code accordingly. Please let me know if I missed something.

//extract folder names for created tests
String reportFolder = buildRootDir + "/archive/UFTReport" +
(StringUtils.isNotEmpty(this.nodeName) ? "/" + this.nodeName : "");

List<String> reportFolders = new ArrayList<>();
this.nodeNames.forEach(nodeName ->
reportFolders.add(buildRootDir + "/archive/UFTReport" + (StringUtils.isNotEmpty(nodeName) ? "/" + nodeName : "")));

List<String> testFolderNames = new ArrayList<>();
testFolderNames.add(build.getRootDir().getAbsolutePath());
File reportFolderFile = new File(reportFolder);
if (reportFolderFile.exists()) {
File[] children = reportFolderFile.listFiles();
if (children != null) {
for (File child : children) {
testFolderNames.add(child.getName());
reportFolders.forEach(reportFolder ->{
File reportFolderFile = new File(reportFolder);
if (reportFolderFile.exists()) {
File[] children = reportFolderFile.listFiles();
if (children != null) {
for (File child : children) {
testFolderNames.add(child.getParentFile().getName() + "/" + child.getName());
}
}
}
}
});

additionalContext = testFolderNames;
}
if (HPRunnerType.StormRunnerLoad.equals(hpRunnerType)) {
Expand Down Expand Up @@ -292,7 +302,9 @@ public FilePath invoke(File f, VirtualChannel channel) throws IOException, Inter

try {
for (FilePath report : reports) {
JUnitXmlIterator iterator = new JUnitXmlIterator(report.read(), moduleDetection, workspace, sharedCheckOutDirectory, jobName, buildId, buildStarted, stripPackageAndClass, hpRunnerType, jenkinsRootUrl, additionalContext,testParserRegEx, octaneSupportsSteps,nodeName);
JUnitXmlIterator iterator = new JUnitXmlIterator(report.read(), moduleDetection, workspace, sharedCheckOutDirectory, jobName,
buildId, buildStarted, stripPackageAndClass, hpRunnerType, jenkinsRootUrl, additionalContext,
testParserRegEx, octaneSupportsSteps, nodeNames);
while (iterator.hasNext()) {
oos.writeObject(iterator.next());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ public class JUnitXmlIterator extends AbstractXmlIterator<JUnitTestResult> {
private String stepName;
private ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
private Map<String, CodelessResult> testNameToCodelessResultMap = new HashMap<>();
private String nodeName;
private Set<String> nodeNames;

private final int ERROR_MESSAGE_MAX_SIZE = System.getProperty("octane.sdk.tests.error_message_max_size") != null ? Integer.parseInt(System.getProperty("octane.sdk.tests.error_message_max_size")) : 512*512;
private final int ERROR_DETAILS_MAX_SIZE = System.getProperty("octane.sdk.tests.error_details_max_size") != null ? Integer.parseInt(System.getProperty("octane.sdk.tests.error_details_max_size")) : 512*512;


public JUnitXmlIterator(InputStream read, List<ModuleDetection> moduleDetection, FilePath workspace, String sharedCheckOutDirectory, String jobName, String buildId, long buildStarted, boolean stripPackageAndClass, HPRunnerType hpRunnerType, String jenkinsRootUrl, Object additionalContext, Pattern testParserRegEx, boolean octaneSupportsSteps,String nodeName) throws XMLStreamException {
public JUnitXmlIterator(InputStream read, List<ModuleDetection> moduleDetection, FilePath workspace, String sharedCheckOutDirectory, String jobName, String buildId, long buildStarted, boolean stripPackageAndClass, HPRunnerType hpRunnerType, String jenkinsRootUrl, Object additionalContext, Pattern testParserRegEx, boolean octaneSupportsSteps,Set<String> nodeNames) throws XMLStreamException {
super(read);
this.stripPackageAndClass = stripPackageAndClass;
this.moduleDetection = moduleDetection;
Expand All @@ -139,7 +139,7 @@ public JUnitXmlIterator(InputStream read, List<ModuleDetection> moduleDetection,
this.additionalContext = additionalContext;
this.testParserRegEx = testParserRegEx;
this.octaneSupportsSteps = octaneSupportsSteps;
this.nodeName = nodeName;
this.nodeNames = nodeNames;
}

private static long parseTime(String timeString) {
Expand Down Expand Up @@ -261,24 +261,34 @@ private void handleJUnitTest(XMLEvent event) throws XMLStreamException, IOExcept
}

String cleanedTestName = cleanTestName(testName);
String nodeName = "";
if(!nodeNames.isEmpty()){
nodeName = nodeNames.stream().findFirst().get();
}
boolean testReportCreated = true;
if (additionalContext != null && additionalContext instanceof List) {
//test folders are appear in the following format GUITest1[1], while [1] number of test. It possible that tests with the same name executed in the same job
//by adding [1] or [2] we can differentiate between different instances.
//We assume that test folders are sorted so in this section, once we found the test folder, we remove it from collection , in order to find the second instance in next iteration
List<String> createdTests = (List<String>) additionalContext;
String searchFor = cleanedTestName + "[";
Optional<String> optional = createdTests.stream().filter(str -> str.startsWith(searchFor)).findFirst();
Optional<String> optional = createdTests.stream().filter(str -> str.contains(searchFor)).findFirst();
if (optional.isPresent()) {
cleanedTestName = optional.get();
createdTests.remove(cleanedTestName);
String nodeTestString = optional.get();
String node = nodeTestString.split("/")[0];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add check before split that you have "/" exist to prevent unplanned errors

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the check.

if (nodeNames.contains(node)) {
nodeName = node;
}

cleanedTestName = nodeTestString.split("/")[1];
createdTests.remove(nodeTestString);
}
testReportCreated = optional.isPresent();
}

if (testReportCreated) {
final String basePath = ((List<String>) additionalContext).get(0);
String nodeNameSubFolder = StringUtils.isNotEmpty(this.nodeName) ? nodeName +"/" : "";
String nodeNameSubFolder = StringUtils.isNotEmpty(nodeName) ? nodeName +"/" : "";
uftResultFilePath = Paths.get(basePath, "archive", "UFTReport", nodeNameSubFolder, cleanedTestName, "/Result/run_results.xml").toFile().getCanonicalPath();
externalURL = jenkinsRootUrl + "job/" + jobName + "/" + buildId + "/artifact/UFTReport/" + nodeNameSubFolder + cleanedTestName + "/Result/run_results.html";
} else {
Expand Down