From dce38a52817f825cb6c2e00e4d2620e08b3a059c Mon Sep 17 00:00:00 2001 From: Wilco Greven Date: Wed, 30 Dec 2015 14:46:52 +0100 Subject: [PATCH 1/2] Add buildRemote workflow step. --- pom.xml | 7 +- .../BuildInfoExporterAction.java | 5 +- .../BuildRemoteStep.java | 205 ++++++++++++++++++ .../RemoteBuildConfiguration.java | 86 ++++---- .../BuildRemoteStep/config.jelly | 24 ++ .../BuildRemoteStep/help-job.html | 3 + .../BuildRemoteStep/help-parameters.html | 3 + .../BuildRemoteStep/help-propagate.html | 3 + .../BuildRemoteStep/help-token.html | 3 + .../BuildRemoteStep/help-wait.html | 3 + 10 files changed, 300 insertions(+), 42 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep.java create mode 100644 src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/config.jelly create mode 100644 src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-job.html create mode 100644 src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-parameters.html create mode 100644 src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-propagate.html create mode 100644 src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-token.html create mode 100644 src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-wait.html diff --git a/pom.xml b/pom.xml index edcbbe43..d7f07b98 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.jenkins-ci.plugins plugin - 1.580 + 1.609.1 Parameterized-Remote-Trigger @@ -76,6 +76,11 @@ token-macro 1.9 + + org.jenkins-ci.plugins.workflow + workflow-step-api + 1.12 + diff --git a/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildInfoExporterAction.java b/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildInfoExporterAction.java index b000c271..fca9f618 100644 --- a/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildInfoExporterAction.java +++ b/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildInfoExporterAction.java @@ -4,6 +4,7 @@ import hudson.model.EnvironmentContributingAction; import hudson.model.Result; import hudson.model.AbstractBuild; +import hudson.model.Run; import java.util.ArrayList; import java.util.HashSet; @@ -22,14 +23,14 @@ class BuildInfoExporterAction implements EnvironmentContributingAction { private List builds; - public BuildInfoExporterAction(AbstractBuild parentBuild, BuildReference buildRef) { + public BuildInfoExporterAction(Run parentBuild, BuildReference buildRef) { super(); this.builds = new ArrayList(); this.builds.add(buildRef); } - static BuildInfoExporterAction addBuildInfoExporterAction(AbstractBuild parentBuild, String triggeredProject, int buildNumber, Result buildResult) { + static BuildInfoExporterAction addBuildInfoExporterAction(Run parentBuild, String triggeredProject, int buildNumber, Result buildResult) { BuildReference reference = new BuildReference(triggeredProject, buildNumber, buildResult); BuildInfoExporterAction action = parentBuild.getAction(BuildInfoExporterAction.class); diff --git a/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep.java b/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep.java new file mode 100644 index 00000000..ed4799e8 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep.java @@ -0,0 +1,205 @@ +package org.jenkinsci.plugins.ParameterizedRemoteTrigger; + +import com.google.inject.Inject; +import hudson.Extension; +import hudson.Util; +import hudson.model.Run; +import hudson.model.TaskListener; +import hudson.util.ListBoxModel; +import net.sf.json.JSONObject; +import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl; +import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl; +import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution; +import org.jenkinsci.plugins.workflow.steps.Step; +import org.jenkinsci.plugins.workflow.steps.StepContextParameter; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; +import org.kohsuke.stapler.StaplerRequest; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import java.io.IOException; +import java.io.StringReader; +import java.util.Enumeration; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; + +/** + * Jenkins workflow step for triggering a Jenkins job on a remote server. + */ +public class BuildRemoteStep extends AbstractStepImpl { + private final String remoteJenkinsName; + private final String job; + private String token = ""; + private boolean wait = DescriptorImpl.defaultWait; + private boolean propagate = DescriptorImpl.defaultPropagate; + private Map parameters = new LinkedHashMap(); + + @DataBoundConstructor + public BuildRemoteStep(String remoteJenkinsName, String job) { + this.remoteJenkinsName = remoteJenkinsName; + this.job = job.trim(); + } + + public String getRemoteJenkinsName() { + return remoteJenkinsName; + } + + public String getJob() { + return job; + } + + public String getToken() { + return token; + } + + @DataBoundSetter + public void setToken(String token) { + this.token = Util.fixNull(token); + } + + public Map getParameters() { + return parameters; + } + + @DataBoundSetter + public void setParameters(Map parameters) { + this.parameters = parameters; + } + + public boolean getWait() { + return wait; + } + + @DataBoundSetter + public void setWait(boolean wait) { + this.wait = wait; + } + + public boolean getPropagate() { + return propagate; + } + + @DataBoundSetter + public void setPropagate(boolean propagate) { + this.propagate = propagate; + } + + public static class Execution extends AbstractSynchronousNonBlockingStepExecution { + private static final long serialVersionUID = 1L; + + @Inject + private transient BuildRemoteStep step; + @StepContextParameter + private transient Run run; + @StepContextParameter + private transient TaskListener listener; + + @Override + protected Void run() throws Exception { + RemoteBuildConfiguration remoteBuildConfiguration = new RemoteBuildConfiguration( + step.getRemoteJenkinsName(), !step.getPropagate(), step.getJob(), step.getToken(), + parametersToString(step.getParameters()), false, null, null, false, step.getPropagate(), 10); + remoteBuildConfiguration.perform(run, null, null, listener); + return null; + } + + private String parametersToString(Map parameters) { + StringBuilder parametersBuilder = new StringBuilder(); + if (parameters != null) { + for (Map.Entry parameter : parameters.entrySet()) { + parametersBuilder.append(parameter.getKey()).append('=').append(parameter.getValue()).append('\n'); + } + } + return parametersBuilder.toString().trim(); + } + } + + @Extension + public static class DescriptorImpl extends AbstractStepDescriptorImpl { + public static boolean defaultWait = true; + public static boolean defaultPropagate = true; + + @Inject + private transient RemoteBuildConfiguration.DescriptorImpl remoteBuildConfigurationDescriptor; + + public DescriptorImpl() { + super(Execution.class); + load(); + } + + @Override + public String getFunctionName() { + return "buildRemote"; + } + + @Override + public String getDisplayName() { + return "Trigger a remote parameterized job"; + } + + @Override + public Step newInstance(Map arguments) throws Exception { + Map parameters = null; + if (arguments.containsKey("parameters")) { + parameters = (Map) arguments.get("parameters"); + arguments.remove("parameters"); + } + + BuildRemoteStep step = (BuildRemoteStep)super.newInstance(arguments); + + if (parameters != null) { + step.setParameters(parameters); + } + + return step; + } + + @Override + public Step newInstance(@CheckForNull StaplerRequest req, @Nonnull JSONObject formData) throws FormException { + String parameters = formData.getString("parameters"); + formData.remove("parameters"); + + Map parsedParameters; + try { + parsedParameters = parseParameters(parameters); + } catch (IOException e) { + throw new FormException(e, "parameters"); + } + + BuildRemoteStep step = (BuildRemoteStep)super.newInstance(req, formData); + step.setParameters(parsedParameters); + return step; + } + + public ListBoxModel doFillRemoteJenkinsNameItems() { + ListBoxModel model = new ListBoxModel(); + + for (RemoteJenkinsServer site : remoteBuildConfigurationDescriptor.getRemoteSites()) { + model.add(site.getDisplayName()); + } + + return model; + } + + /** + * Reads the parameters from the given string as if it where a property file. + * @param parameters a string containing a list of properties in property file format + * @return a map containing the parameter names and values + * @throws IOException when the parameters could not be read + */ + private Map parseParameters(String parameters) throws IOException { + Properties parameterProperties = new Properties(); + parameterProperties.load(new StringReader(parameters)); + + Map parsedParameters = new LinkedHashMap(); + Enumeration parameterNames = parameterProperties.propertyNames(); + while (parameterNames.hasMoreElements()) { + String parameterName = (String)parameterNames.nextElement(); + parsedParameters.put(parameterName, parameterProperties.getProperty(parameterName)); + } + return parsedParameters; + } + } +} diff --git a/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteBuildConfiguration.java b/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteBuildConfiguration.java index fa62789e..46ec9369 100644 --- a/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteBuildConfiguration.java +++ b/src/main/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/RemoteBuildConfiguration.java @@ -5,14 +5,16 @@ import hudson.EnvVars; import hudson.Launcher; import hudson.Extension; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.util.CopyOnWriteList; import hudson.util.ListBoxModel; import hudson.model.AbstractBuild; -import hudson.model.BuildListener; import hudson.model.Result; import hudson.model.AbstractProject; import hudson.tasks.Builder; import hudson.tasks.BuildStepDescriptor; +import jenkins.tasks.SimpleBuildStep; import net.sf.json.JSONObject; import net.sf.json.JSONArray; import net.sf.json.JSONSerializer; @@ -45,12 +47,14 @@ import org.apache.commons.codec.binary.Base64; +import javax.annotation.Nonnull; + /** * * @author Maurice W. * */ -public class RemoteBuildConfiguration extends Builder { +public class RemoteBuildConfiguration extends Builder implements SimpleBuildStep { private final String token; private final String remoteJenkinsName; @@ -157,11 +161,11 @@ public RemoteBuildConfiguration(String remoteJenkinsName, boolean shouldNotFailB * ```getCleanedParameters``` before returning. * * @param build + * @param workspace * @return List of build parameters */ - private List loadExternalParameterFile(AbstractBuild build) { + private List loadExternalParameterFile(Run build, FilePath workspace) { - FilePath workspace = build.getWorkspace(); BufferedReader br = null; List ParameterList = new ArrayList(); try { @@ -237,7 +241,7 @@ private List getCleanedParameters(List parameters) { * List of params to be tokenized/replaced * @return List of resolved variables/tokens */ - private List replaceTokens(AbstractBuild build, BuildListener listener, List params) { + private List replaceTokens(Run build, TaskListener listener, List params) { List tokenizedParams = new ArrayList(); for (int i = 0; i < params.size(); i++) { @@ -257,9 +261,11 @@ private List replaceTokens(AbstractBuild build, BuildListener list * String to be tokenized/replaced * @return String with resolved Environment variables */ - private String replaceToken(AbstractBuild build, BuildListener listener, String input) { + private String replaceToken(Run build, TaskListener listener, String input) { try { - return TokenMacro.expandAll(build, listener, input); + if (build instanceof AbstractBuild) { + return TokenMacro.expandAll((AbstractBuild)build, listener, input); + } } catch (Exception e) { listener.getLogger().println( String.format("Failed to resolve parameters in string %s due to following error:\n%s", input, @@ -443,10 +449,10 @@ private String buildGetUrl(String job, String securityToken) { * @param e * Exception that caused the build to fail * @param listener - * Build Listener + * Task Listener * @throws IOException */ - private void failBuild(Exception e, BuildListener listener) throws IOException { + private void failBuild(Exception e, TaskListener listener) throws IOException { System.out.print(e.getStackTrace()); if (this.getShouldNotFailBuild()) { listener.error("Remote build failed for the following reason, but the build will continue:"); @@ -458,9 +464,7 @@ private void failBuild(Exception e, BuildListener listener) throws IOException { } @Override - public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, - IOException, IllegalArgumentException { - + public void perform(@Nonnull Run build, FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName()); // Stores the status of the remote build @@ -468,13 +472,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis if (remoteServer == null) { this.failBuild(new Exception("No remote host is defined for this job."), listener); - return true; + return; } String remoteServerURL = remoteServer.getAddress().toString(); List cleanedParams = null; - if (this.getLoadParamsFromFile()) { - cleanedParams = loadExternalParameterFile(build); + if (this.getLoadParamsFromFile() && workspace != null) { + cleanedParams = loadExternalParameterFile(build, workspace); } else { // tokenize all variables and encode all variables, then build the fully-qualified trigger URL cleanedParams = getCleanedParameters(); @@ -668,7 +672,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis listener.getLogger().println("Not blocking local job until remote job completes - fire and forget."); } - return true; + return; } private String findParameter(String parameter, List parameters) { @@ -680,7 +684,7 @@ private String findParameter(String parameter, List parameters) { return null; } - private boolean compareParameters(BuildListener listener, JSONArray parameters, List expectedParams) { + private boolean compareParameters(TaskListener listener, JSONArray parameters, List expectedParams) { for (int j = 0; j < parameters.size(); j++) { JSONObject parameter = parameters.getJSONObject(j); String name = parameter.getString("name"); @@ -704,7 +708,7 @@ private boolean compareParameters(BuildListener listener, JSONArray parameters, return true; } - public String getBuildStatus(String buildUrlString, AbstractBuild build, BuildListener listener) throws IOException { + public String getBuildStatus(String buildUrlString, Run build, TaskListener listener) throws IOException { String buildStatus = "UNKNOWN"; RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName()); @@ -743,7 +747,7 @@ public String getBuildStatus(String buildUrlString, AbstractBuild build, BuildLi return buildStatus; } - public String getBuildUrl(String buildUrlString, AbstractBuild build, BuildListener listener) throws IOException { + public String getBuildUrl(String buildUrlString, Run build, TaskListener listener) throws IOException { String buildUrl = ""; RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName()); @@ -776,7 +780,7 @@ public String getBuildUrl(String buildUrlString, AbstractBuild build, BuildListe return buildUrl; } - public String getConsoleOutput(String urlString, String requestType, AbstractBuild build, BuildListener listener) + public String getConsoleOutput(String urlString, String requestType, Run build, TaskListener listener) throws IOException { return getConsoleOutput( urlString, requestType, build, listener, 1 ); @@ -789,17 +793,17 @@ public String getConsoleOutput(String urlString, String requestType, AbstractBui * @param urlString the URL that needs to be called * @param requestType the type of request (GET, POST, etc) * @param build the build that is being triggered - * @param listener build listener + * @param listener task listener * @return a valid JSON object, or null * @throws IOException */ - public JSONObject sendHTTPCall(String urlString, String requestType, AbstractBuild build, BuildListener listener) + public JSONObject sendHTTPCall(String urlString, String requestType, Run build, TaskListener listener) throws IOException { return sendHTTPCall( urlString, requestType, build, listener, 1 ); } - public String getConsoleOutput(String urlString, String requestType, AbstractBuild build, BuildListener listener, int numberOfAttempts) + public String getConsoleOutput(String urlString, String requestType, Run build, TaskListener listener, int numberOfAttempts) throws IOException { RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName()); int retryLimit = this.getConnectionRetryLimit(); @@ -828,12 +832,14 @@ public String getConsoleOutput(String urlString, String requestType, AbstractBui if (!usernameTokenConcat.equals(":")) { // token-macro replacment - try { - usernameTokenConcat = TokenMacro.expandAll(build, listener, usernameTokenConcat); - } catch (MacroEvaluationException e) { - this.failBuild(e, listener); - } catch (InterruptedException e) { - this.failBuild(e, listener); + if (build instanceof AbstractBuild) { + try { + usernameTokenConcat = TokenMacro.expandAll((AbstractBuild)build, listener, usernameTokenConcat); + } catch (MacroEvaluationException e) { + this.failBuild(e, listener); + } catch (InterruptedException e) { + this.failBuild(e, listener); + } } byte[] encodedAuthKey = Base64.encodeBase64(usernameTokenConcat.getBytes()); @@ -920,7 +926,7 @@ public String getConsoleOutput(String urlString, String requestType, AbstractBui * @return * @throws IOException */ - public JSONObject sendHTTPCall(String urlString, String requestType, AbstractBuild build, BuildListener listener, int numberOfAttempts) + public JSONObject sendHTTPCall(String urlString, String requestType, Run build, TaskListener listener, int numberOfAttempts) throws IOException { RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName()); int retryLimit = this.getConnectionRetryLimit(); @@ -949,12 +955,14 @@ public JSONObject sendHTTPCall(String urlString, String requestType, AbstractBui if (!usernameTokenConcat.equals(":")) { // token-macro replacment - try { - usernameTokenConcat = TokenMacro.expandAll(build, listener, usernameTokenConcat); - } catch (MacroEvaluationException e) { - this.failBuild(e, listener); - } catch (InterruptedException e) { - this.failBuild(e, listener); + if (build instanceof AbstractBuild) { + try { + usernameTokenConcat = TokenMacro.expandAll((AbstractBuild)build, listener, usernameTokenConcat); + } catch (MacroEvaluationException e) { + this.failBuild(e, listener); + } catch (InterruptedException e) { + this.failBuild(e, listener); + } } byte[] encodedAuthKey = Base64.encodeBase64(usernameTokenConcat.getBytes()); @@ -1148,11 +1156,11 @@ private String getBuildTypeUrl(boolean isRemoteJobParameterized) { * Pokes the remote server to see if it has default parameters defined or not. * * @param jobName Name of the remote job to test - * @param build Build object - * @param listener listner object + * @param build Run object + * @param listener listener object * @return true if the remote job has default parameters set, otherwise false */ - private boolean isRemoteJobParameterized(String jobName, AbstractBuild build, BuildListener listener) { + private boolean isRemoteJobParameterized(String jobName, Run build, TaskListener listener) { boolean isParameterized = false; //build the proper URL to inspect the remote job diff --git a/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/config.jelly b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/config.jelly new file mode 100644 index 00000000..fafdce2b --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/config.jelly @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-job.html b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-job.html new file mode 100644 index 00000000..f2ee8ed7 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-job.html @@ -0,0 +1,3 @@ +

+ The job on the remote Jenkins host which you would like to trigger. +

\ No newline at end of file diff --git a/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-parameters.html b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-parameters.html new file mode 100644 index 00000000..415996e9 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-parameters.html @@ -0,0 +1,3 @@ +

+ Parameters which will be used when triggering the remote job. If no parameters are needed, then just leave this blank. +

\ No newline at end of file diff --git a/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-propagate.html b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-propagate.html new file mode 100644 index 00000000..fef433a5 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-propagate.html @@ -0,0 +1,3 @@ +

+ If this option is disabled, the build will not fail even if the remote build fails. +

\ No newline at end of file diff --git a/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-token.html b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-token.html new file mode 100644 index 00000000..8d24a7c7 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-token.html @@ -0,0 +1,3 @@ +

+ Security token which is defined on the job of the remote Jenkins host. If no job token is needed to trigger this job, then just leave it blank. +

\ No newline at end of file diff --git a/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-wait.html b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-wait.html new file mode 100644 index 00000000..e1c70cc7 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStep/help-wait.html @@ -0,0 +1,3 @@ +

+ You may ask that this workflow build wait for completion of the downstream build. +

\ No newline at end of file From 35f847259efaa460f6be7dae97953e44da133e73 Mon Sep 17 00:00:00 2001 From: Wilco Greven Date: Mon, 4 Jan 2016 14:08:51 +0100 Subject: [PATCH 2/2] Add workflow step configuration test. --- pom.xml | 7 ++++ .../BuildRemoteStepTest.java | 42 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/test/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStepTest.java diff --git a/pom.xml b/pom.xml index d7f07b98..4908ff28 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,13 @@ workflow-step-api 1.12 + + org.jenkins-ci.plugins.workflow + workflow-step-api + 1.12 + test-jar + test + diff --git a/src/test/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStepTest.java b/src/test/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStepTest.java new file mode 100644 index 00000000..dbd76d90 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/ParameterizedRemoteTrigger/BuildRemoteStepTest.java @@ -0,0 +1,42 @@ +package org.jenkinsci.plugins.ParameterizedRemoteTrigger; + +import net.sf.json.JSONObject; +import org.jenkinsci.plugins.workflow.steps.StepConfigTester; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +import java.util.HashMap; +import java.util.Map; + +public class BuildRemoteStepTest { + @Rule + public JenkinsRule jenkinsRule = new JenkinsRule(); + + @Test + public void configRoundTrip() throws Exception { + JSONObject authenticationMode = new JSONObject(); + authenticationMode.put("value", "none"); + JSONObject auth = new JSONObject(); + auth.put("authenticationMode", authenticationMode); + + String remoteUrl = jenkinsRule.getURL().toString(); + RemoteJenkinsServer remoteJenkinsServer = + new RemoteJenkinsServer(remoteUrl, "JENKINS", false, auth); + RemoteBuildConfiguration.DescriptorImpl descriptor = + jenkinsRule.jenkins.getDescriptorByType(RemoteBuildConfiguration.DescriptorImpl.class); + descriptor.setRemoteSites(remoteJenkinsServer); + + Map parameters = new HashMap(); + parameters.put("name", "value"); + + BuildRemoteStep before = new BuildRemoteStep("JENKINS", "jobName"); + before.setToken("TOKEN"); + before.setPropagate(false); + before.setWait(false); + before.setParameters(parameters); + + BuildRemoteStep after = new StepConfigTester(jenkinsRule).configRoundTrip(before); + jenkinsRule.assertEqualDataBoundBeans(before, after); + } +}