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

Pipeline support (update from PR #25) #34

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ inject-tests/
test-output
*~
*.iml
.idea
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@
<artifactId>junit</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.1</version>
</dependency>
</dependencies>

<repositories>
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/hudson/plugins/testlink/AbstractTestLinkBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public class AbstractTestLinkBuilder extends Builder {
* The name of the Build.
*/
protected String buildName;
/**
* The Build custom fields.
*/
protected String buildCustomFields;
/**
* The platform name.
*/
Expand Down Expand Up @@ -133,7 +137,7 @@ public class AbstractTestLinkBuilder extends Builder {
* Create a AbstractTestLinkBuilder.
*/
public AbstractTestLinkBuilder(String testLinkName, String testProjectName, String testPlanName,
String platformName, String buildName, String customFields, String testPlanCustomFields, List<BuildStep> singleBuildSteps,
String platformName, String buildName, String buildCustomFields, String customFields, String testPlanCustomFields, List<BuildStep> singleBuildSteps,
List<BuildStep> beforeIteratingAllTestCasesBuildSteps, List<BuildStep> iterativeBuildSteps,
List<BuildStep> afterIteratingAllTestCasesBuildSteps, Boolean transactional,
Boolean failedTestsMarkBuildAsFailure, Boolean failIfNoResults, Boolean failOnNotRun,
Expand All @@ -144,6 +148,7 @@ public AbstractTestLinkBuilder(String testLinkName, String testProjectName, Stri
this.testPlanName = testPlanName;
this.platformName = platformName;
this.buildName = buildName;
this.buildCustomFields = buildCustomFields;
this.customFields = customFields;
this.testPlanCustomFields = testPlanCustomFields;
this.singleBuildSteps = singleBuildSteps;
Expand All @@ -162,12 +167,12 @@ public AbstractTestLinkBuilder(String testLinkName, String testProjectName, Stri
* @deprecated to add test plan custom fields
*/
public AbstractTestLinkBuilder(String testLinkName, String testProjectName, String testPlanName,
String platformName, String buildName, String customFields, List<BuildStep> singleBuildSteps,
String platformName, String buildName, String buildCustomFields, String customFields, List<BuildStep> singleBuildSteps,
List<BuildStep> beforeIteratingAllTestCasesBuildSteps, List<BuildStep> iterativeBuildSteps,
List<BuildStep> afterIteratingAllTestCasesBuildSteps, Boolean transactional,
Boolean failedTestsMarkBuildAsFailure, Boolean failIfNoResults, Boolean failOnNotRun,
List<ResultSeeker> resultSeekers) {
this(testLinkName, testProjectName, testPlanName, platformName, buildName, customFields,
this(testLinkName, testProjectName, testPlanName, platformName, buildName, buildCustomFields, customFields,
/*testPlanCustomFields*/ null, singleBuildSteps, beforeIteratingAllTestCasesBuildSteps,
iterativeBuildSteps, afterIteratingAllTestCasesBuildSteps, transactional, failedTestsMarkBuildAsFailure,
failIfNoResults, failOnNotRun, resultSeekers);
Expand All @@ -181,6 +186,7 @@ public AbstractTestLinkBuilder(String testLinkName, String testProjectName, Stri
* @param testPlanName TestLink Test Plan name.
* @param platformName TestLink Platform name.
* @param buildName TestLink Build name.
* @param buildCustomFields TestLink Build custom fields.
* @param customFields TestLink comma-separated list of Custom Fields.
* @param singleBuildSteps List of build steps to execute once for all automated test cases.
* @param beforeIteratingAllTestCasesBuildSteps Command executed before iterating all test cases.
Expand All @@ -193,13 +199,13 @@ public AbstractTestLinkBuilder(String testLinkName, String testProjectName, Stri
* @deprecated
*/
public AbstractTestLinkBuilder(String testLinkName, String testProjectName, String testPlanName,
String platformName, String buildName, String customFields, Boolean executionStatusNotRun,
String platformName, String buildName, String buildCustomFields, String customFields, Boolean executionStatusNotRun,
Boolean executionStatusPassed, Boolean executionStatusFailed, Boolean executionStatusBlocked,
List<BuildStep> singleBuildSteps, List<BuildStep> beforeIteratingAllTestCasesBuildSteps,
List<BuildStep> iterativeBuildSteps, List<BuildStep> afterIteratingAllTestCasesBuildSteps,
Boolean transactional, Boolean failedTestsMarkBuildAsFailure, Boolean failIfNoResults,
Boolean failOnNotRun, List<ResultSeeker> resultSeekers) {
this(testLinkName, testProjectName, testPlanName, platformName, buildName, customFields,
this(testLinkName, testProjectName, testPlanName, platformName, buildName, buildCustomFields, customFields,
/*testPlanCustomFields*/ null, singleBuildSteps, beforeIteratingAllTestCasesBuildSteps,
iterativeBuildSteps, afterIteratingAllTestCasesBuildSteps, transactional, failedTestsMarkBuildAsFailure,
failIfNoResults, failOnNotRun, resultSeekers);
Expand All @@ -225,6 +231,8 @@ public String getBuildName() {
return this.buildName;
}

public String getBuildCustomFields() { return this.buildCustomFields; }

public String getCustomFields() {
return this.customFields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package hudson.plugins.testlink;

import hudson.model.Action;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.testlink.util.TestLinkHelper;
Expand Down
39 changes: 30 additions & 9 deletions src/main/java/hudson/plugins/testlink/TestLinkBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -90,7 +91,7 @@ public TestLinkBuilder(String testLinkName, String testProjectName,
List<BuildStep> afterIteratingAllTestCasesBuildSteps,
Boolean transactional, Boolean failedTestsMarkBuildAsFailure,
Boolean failIfNoResults, List<ResultSeeker> resultSeekers) {
this(testLinkName, testProjectName, testPlanName, buildName,
this(testLinkName, testProjectName, testPlanName, buildName, null,
null, customFields, executionStatusNotRun, executionStatusPassed,
executionStatusFailed, executionStatusBlocked, singleBuildSteps,
beforeIteratingAllTestCasesBuildSteps, iterativeBuildSteps,
Expand All @@ -112,7 +113,7 @@ public TestLinkBuilder(String testLinkName, String testProjectName,
List<BuildStep> afterIteratingAllTestCasesBuildSteps,
Boolean transactional, Boolean failedTestsMarkBuildAsFailure,
Boolean failIfNoResults, Boolean failOnNotRun, List<ResultSeeker> resultSeekers) {
super(testLinkName, testProjectName, testPlanName, buildName, null,
super(testLinkName, testProjectName, testPlanName, buildName, null, null,
customFields, executionStatusNotRun, executionStatusPassed,
executionStatusFailed, executionStatusBlocked, singleBuildSteps,
beforeIteratingAllTestCasesBuildSteps, iterativeBuildSteps,
Expand All @@ -125,7 +126,7 @@ public TestLinkBuilder(String testLinkName, String testProjectName,
* @deprecated to add test plan custom fields
*/
public TestLinkBuilder(String testLinkName, String testProjectName,
String testPlanName, String platformName, String buildName, String customFields,
String testPlanName, String platformName, String buildName, String buildCustomFields, String customFields,
Boolean executionStatusNotRun, Boolean executionStatusPassed,
Boolean executionStatusFailed, Boolean executionStatusBlocked,
List<BuildStep> singleBuildSteps,
Expand All @@ -134,15 +135,16 @@ public TestLinkBuilder(String testLinkName, String testProjectName,
List<BuildStep> afterIteratingAllTestCasesBuildSteps,
Boolean transactional, Boolean failedTestsMarkBuildAsFailure,
Boolean failIfNoResults, Boolean failOnNotRun, List<ResultSeeker> resultSeekers) {
super(testLinkName, testProjectName, testPlanName, platformName, buildName,
super(testLinkName, testProjectName, testPlanName, platformName, buildName, buildCustomFields,
customFields, singleBuildSteps, beforeIteratingAllTestCasesBuildSteps, iterativeBuildSteps,
afterIteratingAllTestCasesBuildSteps, transactional, failedTestsMarkBuildAsFailure,
failIfNoResults, failOnNotRun, resultSeekers);
}

@DataBoundConstructor
public TestLinkBuilder(String testLinkName, String testProjectName,
String testPlanName, String platformName, String buildName, String customFields, String testPlanCustomFields,
String testPlanName, String platformName, String buildName,
String buildCustomFields, String customFields, String testPlanCustomFields,
Boolean executionStatusNotRun, Boolean executionStatusPassed,
Boolean executionStatusFailed, Boolean executionStatusBlocked,
List<BuildStep> singleBuildSteps,
Expand All @@ -151,7 +153,7 @@ public TestLinkBuilder(String testLinkName, String testProjectName,
List<BuildStep> afterIteratingAllTestCasesBuildSteps,
Boolean transactional, Boolean failedTestsMarkBuildAsFailure,
Boolean failIfNoResults, Boolean failOnNotRun, List<ResultSeeker> resultSeekers) {
super(testLinkName, testProjectName, testPlanName, platformName, buildName,
super(testLinkName, testProjectName, testPlanName, platformName, buildName, buildCustomFields,
customFields, testPlanCustomFields, singleBuildSteps, beforeIteratingAllTestCasesBuildSteps, iterativeBuildSteps,
afterIteratingAllTestCasesBuildSteps, transactional, failedTestsMarkBuildAsFailure,
failIfNoResults, failOnNotRun, resultSeekers);
Expand Down Expand Up @@ -194,16 +196,19 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
build.getEnvironment(listener), getPlatformName());
final String buildName = TestLinkHelper.expandVariable(build.getBuildVariableResolver(),
build.getEnvironment(listener), getBuildName());
final String buildCustomFields = TestLinkHelper.expandVariable(build.getBuildVariableResolver(),
build.getEnvironment(listener), getBuildCustomFields());
final String buildNotes = Messages.TestLinkBuilder_Build_Notes();
if(LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "TestLink project name: ["+testProjectName+"]");
LOGGER.log(Level.FINE, "TestLink plan name: ["+testPlanName+"]");
LOGGER.log(Level.FINE, "TestLink platform name: ["+platformName+"]");
LOGGER.log(Level.FINE, "TestLink build name: ["+buildName+"]");
LOGGER.log(Level.FINE, "TestLink build notes: ["+buildNotes+"]");
LOGGER.log(Level.FINE, "TestLink build Custom Fields: ["+buildCustomFields+"]");
}
// TestLink Site object
testLinkSite = this.getTestLinkSite(testLinkUrl, testLinkDevKey, testProjectName, testPlanName, platformName, buildName, buildNotes);
testLinkSite = this.getTestLinkSite(testLinkUrl, testLinkDevKey, testProjectName, testPlanName, platformName, buildName, buildCustomFields, buildNotes);

if (StringUtils.isNotBlank(platformName) && testLinkSite.getPlatform() == null)
listener.getLogger().println(Messages.TestLinkBuilder_PlatformNotFound(platformName));
Expand Down Expand Up @@ -257,7 +262,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
if(getResultSeekers() != null) {
for (ResultSeeker resultSeeker : getResultSeekers()) {
LOGGER.log(Level.INFO, "Seeking test results. Using: " + resultSeeker.getDescriptor().getDisplayName());
resultSeeker.seek(automatedTestCases, build, launcher, listener, testLinkSite);
resultSeeker.seek(automatedTestCases, build, build.getWorkspace(), launcher, listener, testLinkSite);
}
}
} catch (ResultSeekerException trse) {
Expand Down Expand Up @@ -325,7 +330,7 @@ private TestCaseWrapper[] transform(TestCase[] testCases) {
*/
public TestLinkSite getTestLinkSite(String testLinkUrl, String testLinkDevKey,
String testProjectName, String testPlanName, String platformName,
String buildName, String buildNotes) throws MalformedURLException {
String buildName, String buildCustomFields, String buildNotes) throws MalformedURLException {
final TestLinkAPI api;
final URL url = new URL(testLinkUrl);
api = new TestLinkAPI(url, testLinkDevKey);
Expand All @@ -344,7 +349,23 @@ public TestLinkSite getTestLinkSite(String testLinkUrl, String testLinkDevKey,
}
}

/* Extract custom fields and values */
java.util.Map<String, String> cfs = new HashMap<String, String>();
if(buildCustomFields != null && !(buildCustomFields.isEmpty())) {
for (String part: buildCustomFields.split(",")) {
String[] cf = part.split(":");
String name = cf[0].replaceAll("\\s+", "");
String value = cf[1].replaceAll("\\s+", "");
cfs.put(name, value);
}
}

final Build build = api.createBuild(testPlan.getId(), buildName, buildNotes);
try {
api.updateBuildCustomFields(build.getId(), testProject.getId(), testPlan.getId(), cfs);
}catch(TestLinkAPIException e) {
LOGGER.log(Level.INFO, "TestLink builder failed ro update build custom fields. " + e);
}
return new TestLinkSite(api, testProject, testPlan, platform, build);
}

Expand Down
151 changes: 151 additions & 0 deletions src/main/java/hudson/plugins/testlink/TestLinkJunitWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* The MIT License
*
* Copyright (c) <2011> <Bruno P. Kinoshita>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.testlink;

import hudson.tasks.junit.JUnitParser;
import hudson.tasks.junit.TestResult;
import hudson.model.Run;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.TaskListener;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Map;
import java.util.HashMap;

import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.DirectoryScanner;
import hudson.Util;
import jenkins.MasterToSlaveFileCallable;
import hudson.remoting.VirtualChannel;
import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.Element;
import java.util.List;
import java.util.Iterator;
import java.util.logging.Logger;

/**
* Created by azikha01 on 29/07/2016.
*/
public class TestLinkJunitWrapper extends JUnitParser {
private Map<String, Map<String, String>> customFields = null;
private PrintStream logger = null;
private static final Logger LOGGER = Logger.getLogger("hudson.plugins.testlink");

public TestLinkJunitWrapper(boolean keepLongStdio, boolean allowEmptyResults) {
super(keepLongStdio, allowEmptyResults);
}

public TestResult parseResult(String testResultLocations, Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {

logger = listener.getLogger();
TestResult r = super.parseResult(testResultLocations, build, workspace, launcher, listener);

/* Second parse of files to find Test Case custom field values */
this.customFields = (Map<String, Map<String, String>>)workspace.act(new TestLinkJunitWrapper.ParseResultCallable(testResultLocations));
Iterator it = customFields.entrySet().iterator();
while (it.hasNext()){
Map.Entry pair = (Map.Entry)it.next();
logger.println("Test Case " + pair.getKey());
Map<String, String> cfs = (Map<String, String>)pair.getValue();
Iterator itt = cfs.entrySet().iterator();
while (itt.hasNext()) {
Map.Entry pairr = (Map.Entry)itt.next();
logger.println("\tCustom field = " + pairr.getKey() + " value = " + pairr.getValue());
}
}
return r;
}

private static class ParseResultCallable extends MasterToSlaveFileCallable<Map<String, Map<String, String>>> {
private final String testResults;

private ParseResultCallable(String testResults) {
this.testResults = testResults;
}

public Map<String, Map<String, String>> invoke(File ws, VirtualChannel channel) throws IOException {
FileSet fs = Util.createFileSet(ws, this.testResults);
DirectoryScanner ds = fs.getDirectoryScanner();
Map<String, Map<String, String>> customFields = new HashMap<String, Map<String, String>>();
String[] files = ds.getIncludedFiles();
if(files.length > 0) {
String[] reportFiles = ds.getIncludedFiles();
File baseDir = ds.getBasedir();

int len$ = reportFiles.length;

for(int f = 0; f < len$; ++f) {
String value = reportFiles[f];
File reportFile = new File(baseDir, value);

try {
this.parseCustomFields(reportFile, customFields);
} catch (org.dom4j.DocumentException e) {
throw new IOException(e);
}
}

}

return customFields;
}

private void parseCustomFields (File reportFile, Map<String, Map<String, String>> customFields) throws org.dom4j.DocumentException {
String xmlReport = reportFile.getName();
SAXReader saxReader = new SAXReader();
Document result = saxReader.read(reportFile);
Element root = result.getRootElement();
List testCases = root.elements("testcase");

for(Iterator stdout = testCases.iterator(); stdout.hasNext();) {
Element tc = (Element)stdout.next();
String m = tc.attributeValue("classname");
Map<String, String> cfs = new HashMap<String, String>();
// Get other attributes and extract custom fields
List children = tc.elements();
for (Iterator child = children.iterator(); child.hasNext();){
// get tag and text
Element childe = (Element)child.next();
// exclude Junit defined names like error, failure, stdin, stdout
if (childe.getName().equals("skipped") ||
childe.getName().equals("error") ||
childe.getName().equals("failure") ||
childe.getName().equals("system-out") ||
childe.getName().equals("system-err")
) {
continue;
}
cfs.put(childe.getName(), childe.getText());
// what should we do with these custom fields
customFields.put(m, cfs);
}
}
}
}

public Map<String, Map<String, String>> getCustomFields (){ return customFields;}
}
Loading