Skip to content
Permalink
Browse files

[JENKINS-31999] added Pipeline compatibility for test-related builders

- replaced AbstractBuild with Run
- replaced BuildListener with TaskListener
- added new DataBoundConstructor for mandatory parameters
- added DataBoundSetters for optional parameters with defaults
- made custom console logger compatible
  • Loading branch information...
cpoenisch committed May 20, 2016
1 parent af03318 commit c883004ba182c3316c384b5393fd6f612e053bce
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2015 TraceTronic GmbH
* Copyright (c) 2015-2016 TraceTronic GmbH
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@@ -31,11 +31,10 @@

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.InvisibleAction;
import hudson.model.ParameterValue;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.model.StringParameterValue;
import hudson.model.listeners.RunListener;

@@ -59,20 +58,20 @@

private static final Logger LOGGER = Logger.getLogger(TestEnvActionView.class.getName());

private final AbstractBuild<?, ?> build;
private final Run<?, ?> build;
private final transient TaskListener listener;

/**
* Instantiates a new {@link TestEnvActionView}.
*
* @param build
* @param run
* the build
* @param listener
* the listener
*/
public TestEnvActionView(final AbstractBuild<?, ?> build, final TaskListener listener) {
public TestEnvActionView(final Run<?, ?> run, final TaskListener listener) {
super();
this.build = build;
build = run;
this.listener = listener;
}

@@ -90,9 +89,9 @@ public TestEnvActionView(final AbstractBuild<?, ?> build, final TaskListener lis
try {
final EnvVars envVars = build.getEnvironment(listener);
String buildWs = "";
final FilePath buildWsPath = build.getWorkspace();
final String buildWsPath = envVars.get("WORKSPACE");
if (buildWsPath != null) {
buildWs = buildWsPath.getRemote() + File.separator;
buildWs = buildWsPath + File.separator;
}

for (int i = 0; i < testBuilderSize; i++) {
@@ -116,10 +115,10 @@ public TestEnvActionView(final AbstractBuild<?, ?> build, final TaskListener lis
* Listener notifying the build on completion and adding this {@link TestEnvActionView} as a new build action.
*/
@Extension
public static final class RunListenerImpl extends RunListener<AbstractBuild<?, ?>> {
public static final class RunListenerImpl extends RunListener<Run<?, ?>> {

@Override
public void onCompleted(final AbstractBuild<?, ?> run, final TaskListener listener) {
public void onCompleted(final Run<?, ?> run, final TaskListener listener) {
if (run.getAction(TestEnvInvisibleAction.class) != null) {
run.addAction(new TestEnvActionView(run, listener));
}
@@ -32,18 +32,23 @@
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.model.Run;
import hudson.remoting.Callable;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Builder;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Nonnull;

import jenkins.security.MasterToSlaveCallable;
import jenkins.tasks.SimpleBuildStep;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundSetter;

import de.tracetronic.jenkins.plugins.ecutest.env.TestEnvInvisibleAction;
import de.tracetronic.jenkins.plugins.ecutest.log.TTConsoleLogger;
@@ -61,7 +66,7 @@
*
* @author Christian Pönisch <christian.poenisch@tracetronic.de>
*/
public abstract class AbstractTestBuilder extends Builder {
public abstract class AbstractTestBuilder extends Builder implements SimpleBuildStep {

/**
* Defines the default "Packages" directory in the ECU-TEST workspace.
@@ -73,9 +78,23 @@
*/
private static final String DEFAULT_CONFIG_DIR = "Configurations";

@Nonnull
private final String testFile;
private final TestConfig testConfig;
private final ExecutionConfig executionConfig;
@Nonnull
private TestConfig testConfig = TestConfig.newInstance();
@Nonnull
private ExecutionConfig executionConfig = ExecutionConfig.newInstance();

/**
* Instantiates a new {@link AbstractTestBuilder}.
*
* @param testFile
* the test file
*/
public AbstractTestBuilder(final String testFile) {
super();
this.testFile = StringUtils.trimToEmpty(testFile);
}

/**
* Instantiates a new {@link AbstractTestBuilder}.
@@ -86,7 +105,9 @@
* the test configuration
* @param executionConfig
* the execution configuration
* @deprecated since 1.11 use {@link #AbstractTestBuilder(String)}
*/
@Deprecated
public AbstractTestBuilder(final String testFile, final TestConfig testConfig,
final ExecutionConfig executionConfig) {
super();
@@ -112,33 +133,54 @@ public String getDefaultConfigDir() {
/**
* @return the test file path
*/
@Nonnull
public String getTestFile() {
return testFile;
}

/**
* @return the test configuration
*/
@Nonnull
public TestConfig getTestConfig() {
return testConfig;
}

/**
* @return the execution configuration
*/
@Nonnull
public ExecutionConfig getExecutionConfig() {
return executionConfig;
}

/**
* @param testConfig
* the test configuration
*/
@DataBoundSetter
public void setTestConfig(@Nonnull final TestConfig testConfig) {
this.testConfig = testConfig;
}

/**
* @param executionConfig
* the execution configuration
*/
@DataBoundSetter
public void setExecutionConfig(@Nonnull final ExecutionConfig executionConfig) {
this.executionConfig = executionConfig;
}

@Override
public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener)
throws InterruptedException, IOException {
public void perform(final Run<?, ?> run, final FilePath workspace, final Launcher launcher,
final TaskListener listener) throws InterruptedException, IOException {
// Check OS running this build
if (!ProcessUtil.checkOS(launcher, listener)) {
return false;
return;
}

final boolean performed = performTest(build, launcher, listener);
final boolean performed = performTest(run, workspace, launcher, listener);
if (!performed && getExecutionConfig().isStopOnError()) {
final TTConsoleLogger logger = new TTConsoleLogger(listener);
logger.logInfo("- Closing running ECU-TEST and Tool-Server instances...");
@@ -153,14 +195,16 @@ public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher,
logger.logInfo("-> No running Tool-Server instance found.");
}
}
return performed;
return;
}

/**
* Perform a test execution.
*
* @param build
* @param run
* the build
* @param workspace
* the workspace
* @param launcher
* the launcher
* @param listener
@@ -171,8 +215,8 @@ public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher,
* @throws InterruptedException
* if the build gets interrupted
*/
private boolean performTest(final AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener)
throws IOException, InterruptedException {
private boolean performTest(final Run<?, ?> run, final FilePath workspace, final Launcher launcher,
final TaskListener listener) throws IOException, InterruptedException {
final TTConsoleLogger logger = new TTConsoleLogger(listener);

// Check for running ECU-TEST instance
@@ -182,7 +226,7 @@ private boolean performTest(final AbstractBuild<?, ?> build, final Launcher laun
}

// Expand build parameters
final EnvVars buildEnv = build.getEnvironment(listener);
final EnvVars buildEnv = run.getEnvironment(listener);
final String expTestFile = buildEnv.expand(getTestFile());
TestConfig expTestConfig = getTestConfig().expand(buildEnv);
final ExecutionConfig expExecConfig = getExecutionConfig().expand(buildEnv);
@@ -197,7 +241,7 @@ private boolean performTest(final AbstractBuild<?, ?> build, final Launcher laun
final String packageDir = getPackagesDir(launcher, listener);

// Absolutize packages directory, if not absolute assume relative to ECU-TEST workspace
expPkgDir = PathUtil.makeAbsolutePath(packageDir, build.getWorkspace());
expPkgDir = PathUtil.makeAbsolutePath(packageDir, workspace);
}

// Configure test file
@@ -218,7 +262,7 @@ private boolean performTest(final AbstractBuild<?, ?> build, final Launcher laun
final String configDir = getConfigDir(launcher, listener);

// Absolutize configuration directory, if not absolute assume relative to ECU-TEST workspace
final String expConfigDir = PathUtil.makeAbsolutePath(configDir, build.getWorkspace());
final String expConfigDir = PathUtil.makeAbsolutePath(configDir, workspace);
expTbcConfigDir = tbcFile.isAbsolute() ? null : expConfigDir;
expTcfConfigDir = tcfFile.isAbsolute() ? null : expConfigDir;
}
@@ -241,7 +285,7 @@ private boolean performTest(final AbstractBuild<?, ?> build, final Launcher laun
expTestConfig.isLoadOnly(), expTestConfig.getConstants());

// Run tests
return runTest(expTestFilePath, expTestConfig, expExecConfig, build, launcher, listener);
return runTest(expTestFilePath, expTestConfig, expExecConfig, run, launcher, listener);
}

/**
@@ -253,7 +297,7 @@ private boolean performTest(final AbstractBuild<?, ?> build, final Launcher laun
* the expanded test configuration
* @param executionConfig
* the expanded execution configuration
* @param build
* @param run
* the build
* @param launcher
* the launcher
@@ -266,7 +310,7 @@ private boolean performTest(final AbstractBuild<?, ?> build, final Launcher laun
* if the build gets interrupted
*/
protected abstract boolean runTest(String testFile, TestConfig testConfig, ExecutionConfig executionConfig,
AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws IOException,
Run<?, ?> run, Launcher launcher, TaskListener listener) throws IOException,
InterruptedException;

/**
@@ -283,7 +327,7 @@ protected abstract boolean runTest(String testFile, TestConfig testConfig, Execu
* if the current thread is interrupted while waiting for the completion
*/
private boolean checkETInstance(final Launcher launcher, final boolean kill) throws IOException,
InterruptedException {
InterruptedException {
final List<String> foundProcesses = ETClient.checkProcesses(launcher, kill);
return !foundProcesses.isEmpty();
}
@@ -302,8 +346,8 @@ private boolean checkETInstance(final Launcher launcher, final boolean kill) thr
* @throws InterruptedException
* if the current thread is interrupted while waiting for the completion
*/
private boolean closeETInstance(final Launcher launcher, final BuildListener listener) throws IOException,
InterruptedException {
private boolean closeETInstance(final Launcher launcher, final TaskListener listener) throws IOException,
InterruptedException {
final List<String> foundProcesses = ETClient.checkProcesses(launcher, false);
if (foundProcesses.isEmpty()) {
return false;
@@ -325,20 +369,20 @@ private boolean closeETInstance(final Launcher launcher, final BuildListener lis
* if the current thread is interrupted while waiting for the completion
*/
private boolean checkTSInstance(final Launcher launcher, final boolean kill) throws IOException,
InterruptedException {
InterruptedException {
final List<String> foundProcesses = TSClient.checkProcesses(launcher, kill);
return !foundProcesses.isEmpty();
}

/**
* Gets the test identifier by the size of {@link TestEnvInvisibleAction}s already added to the build.
*
* @param build
* @param run
* the build
* @return the test id
*/
protected int getTestId(final AbstractBuild<?, ?> build) {
final List<TestEnvInvisibleAction> testEnvActions = build.getActions(TestEnvInvisibleAction.class);
protected int getTestId(final Run<?, ?> run) {
final List<TestEnvInvisibleAction> testEnvActions = run.getActions(TestEnvInvisibleAction.class);
return testEnvActions.size();
}

@@ -360,7 +404,7 @@ protected int getTestId(final AbstractBuild<?, ?> build) {
* if the build gets interrupted
*/
protected String getTestFilePath(final String testFile, final String pkgDir, final Launcher launcher,
final BuildListener listener) throws IOException, InterruptedException {
final TaskListener listener) throws IOException, InterruptedException {
String testFilePath = null;
final TTConsoleLogger logger = new TTConsoleLogger(listener);
if (testFile.isEmpty()) {
@@ -395,7 +439,7 @@ protected String getTestFilePath(final String testFile, final String pkgDir, fin
* if the build gets interrupted
*/
private String getConfigFilePath(final String configFile, final String configDir, final Launcher launcher,
final BuildListener listener) throws IOException, InterruptedException {
final TaskListener listener) throws IOException, InterruptedException {
String configFilePath = null;
final TTConsoleLogger logger = new TTConsoleLogger(listener);
if (configFile.isEmpty()) {
@@ -423,7 +467,7 @@ private String getConfigFilePath(final String configFile, final String configDir
* @throws InterruptedException
* if the current thread is interrupted while waiting for the completion
*/
protected String getConfigDir(final Launcher launcher, final BuildListener listener) throws InterruptedException {
protected String getConfigDir(final Launcher launcher, final TaskListener listener) throws InterruptedException {
String configDir;
try {
configDir = launcher.getChannel().call(new GetSettingCallable("configPath"));
@@ -446,7 +490,7 @@ protected String getConfigDir(final Launcher launcher, final BuildListener liste
* @throws InterruptedException
* if the current thread is interrupted while waiting for the completion
*/
protected String getPackagesDir(final Launcher launcher, final BuildListener listener) throws InterruptedException {
protected String getPackagesDir(final Launcher launcher, final TaskListener listener) throws InterruptedException {
String packagesDir;
try {
packagesDir = launcher.getChannel().call(new GetSettingCallable("packagePath"));
@@ -492,6 +536,11 @@ public String call() throws IOException {
}
}

@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}

@Override
public AbstractTestDescriptor getDescriptor() {
return (AbstractTestDescriptor) super.getDescriptor();

0 comments on commit c883004

Please sign in to comment.
You can’t perform that action at this time.