Skip to content

Commit

Permalink
[JENKINS-31999] added Pipeline compatibility for test-related builders
Browse files Browse the repository at this point in the history
- 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 c883004
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 108 deletions.
@@ -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,
Expand Down Expand Up @@ -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;

Expand All @@ -59,20 +58,20 @@ public class TestEnvActionView extends InvisibleAction {

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;
}

Expand All @@ -90,9 +89,9 @@ public Set<ParameterValue> getEnvVariables() {
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++) {
Expand All @@ -116,10 +115,10 @@ public Set<ParameterValue> getEnvVariables() {
* 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));
}
Expand Down
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -73,9 +78,23 @@ public abstract class AbstractTestBuilder extends Builder {
*/
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}.
Expand All @@ -86,7 +105,9 @@ public abstract class AbstractTestBuilder extends Builder {
* 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();
Expand All @@ -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...");
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -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);
}

/**
Expand All @@ -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
Expand All @@ -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;

/**
Expand All @@ -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();
}
Expand All @@ -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;
Expand All @@ -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();
}

Expand All @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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"));
Expand All @@ -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"));
Expand Down Expand Up @@ -492,6 +536,11 @@ public String call() throws IOException {
}
}

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

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

0 comments on commit c883004

Please sign in to comment.