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

[JENKINS-46124] - support map with EnvStep #105

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package org.jenkinsci.plugins.workflow.steps;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.EnvVars;
import hudson.Extension;
Expand All @@ -34,7 +35,9 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.structs.describable.CustomDescribableModel;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

Expand Down Expand Up @@ -103,7 +106,7 @@ private static final class ExpanderImpl extends EnvironmentExpander {
}
}

@Extension public static class DescriptorImpl extends StepDescriptor {
@Extension public static class DescriptorImpl extends StepDescriptor implements CustomDescribableModel {

@Override public String getFunctionName() {
return "withEnv";
Expand Down Expand Up @@ -150,11 +153,37 @@ private static final class ExpanderImpl extends EnvironmentExpander {
}
}
return b.toString();
} else if (overrides instanceof Map) {
Copy link
Member

Choose a reason for hiding this comment

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

BTW I lobbied for ArgumentsAction[Impl] to persist only the internal, resolved form of arguments, but the design we went with persists the surface form, necessitating extra work like this.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jglick Okay, is there some action to be taken about that now?

Copy link
Member

Choose a reason for hiding this comment

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

Not easily. Just noting some background.

return ((Map<?, ?>) overrides).keySet()
.stream()
.filter(e -> e instanceof String)
.map(Object::toString)
.collect(Collectors.joining(", "));
jglick marked this conversation as resolved.
Show resolved Hide resolved
} else {
return null;
}
}

@NonNull
@Override
public Map<String, Object> customInstantiate(@NonNull Map<String, Object> arguments) {
Map<String, Object> r = new HashMap<>(arguments);
final String key = "overrides";
jglick marked this conversation as resolved.
Show resolved Hide resolved
Object overrides = r.get(key);
if (overrides instanceof Map) {
r.put(key, toKeyValueList((Map<?, ?>) overrides));
}
return r;
}

private static List<String> toKeyValueList(Map<?, ?> map) {
return map.entrySet().stream()
.filter(e -> e.getKey() instanceof String)
jglick marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toMap(e -> (String)e.getKey(), e -> e.getValue() == null ? "" : e.getValue().toString()))
.entrySet().stream()
.map(m -> m.getKey() + "=" + m.getValue())
jglick marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toList());
}
}

}
125 changes: 125 additions & 0 deletions src/test/java/org/jenkinsci/plugins/workflow/steps/EnvStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ public class EnvStepTest {
});
}

@Test public void mapOverriding() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"env.CUSTOM = 'initial'\n" +
"env.FOOPATH = node {isUnix() ? '/opt/foos' : 'C:\\\\foos'}\n" +
jglick marked this conversation as resolved.
Show resolved Hide resolved
"env.NULLED = 'outside'\n" +
"node {\n" +
" withEnv([CUSTOM: 'override', NOVEL: 'val', BUILD_TAG: 'custom', NULLED: null, 'FOOPATH+BALL': isUnix() ? '/opt/ball' : 'C:\\\\ball']) {\n" +
" isUnix() ? sh('echo inside CUSTOM=$CUSTOM NOVEL=$NOVEL BUILD_TAG=$BUILD_TAG NULLED=$NULLED FOOPATH=$FOOPATH:') : bat('echo inside CUSTOM=%CUSTOM% NOVEL=%NOVEL% BUILD_TAG=%BUILD_TAG% NULLED=%NULLED% FOOPATH=%FOOPATH%;')\n" +
" echo \"groovy NULLED=${env.NULLED}\"\n" +
" }\n" +
" isUnix() ? sh('echo outside CUSTOM=$CUSTOM NOVEL=$NOVEL NULLED=outside') : bat('echo outside CUSTOM=%CUSTOM% NOVEL=%NOVEL% NULLED=outside')\n" +
"}", true));
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains(Functions.isWindows() ? "inside CUSTOM=override NOVEL=val BUILD_TAG=custom NULLED= FOOPATH=C:\\ball;C:\\foos;" : "inside CUSTOM=override NOVEL=val BUILD_TAG=custom NULLED= FOOPATH=/opt/ball:/opt/foos:", b);
story.j.assertLogContains("groovy NULLED=null", b);
story.j.assertLogContains("outside CUSTOM=initial NOVEL= NULLED=outside", b);
List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(
b.getExecution(),
Predicates.and(
new NodeStepTypePredicate("withEnv"),
n -> n instanceof StepStartNode && !((StepStartNode) n).isBody()));
assertThat(coreStepNodes, Matchers.hasSize(1));
assertEquals("CUSTOM, NOVEL, BUILD_TAG, NULLED, FOOPATH+BALL", ArgumentsAction.getStepArgumentsAsString(coreStepNodes.get(0)));
jetersen marked this conversation as resolved.
Show resolved Hide resolved
}
});
}

@Test public void parallel() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
Expand All @@ -105,6 +135,28 @@ public class EnvStepTest {
});
}

@Test public void mapParallel() {
jglick marked this conversation as resolved.
Show resolved Hide resolved
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"parallel a: {\n" +
" node {withEnv([TOOL: 'aloc']) {semaphore 'a'; isUnix() ? sh('echo a TOOL=$TOOL') : bat('echo a TOOL=%TOOL%')}}\n" +
"}, b: {\n" +
" node {withEnv([TOOL: 'bloc']) {semaphore 'b'; isUnix() ? sh('echo b TOOL=$TOOL') : bat('echo b TOOL=%TOOL%')}}\n" +
"}", true));
WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get();
SemaphoreStep.waitForStart("a/1", b);
SemaphoreStep.waitForStart("b/1", b);
SemaphoreStep.success("a/1", null);
SemaphoreStep.success("b/1", null);
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
story.j.assertLogContains("a TOOL=aloc", b);
story.j.assertLogContains("b TOOL=bloc", b);
}
});
}

@Test public void restarting() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
Expand Down Expand Up @@ -140,6 +192,41 @@ public class EnvStepTest {
});
}

@Test public void mapRestarting() {
jglick marked this conversation as resolved.
Show resolved Hide resolved
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"def show(which) {\n" +
" echo \"groovy ${which} ${env.TESTVAR}:\"\n" +
" isUnix() ? sh(\"echo shell ${which} \\$TESTVAR:\") : bat(\"echo shell ${which} %TESTVAR%:\")\n" +
"}\n" +
"node {\n" +
" withEnv([TESTVAR: 'val']) {\n" +
" show 'before'\n" +
" semaphore 'restarting'\n" +
" show 'after'\n" +
" }\n" +
" show 'outside'\n" +
"}", true));
WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get();
SemaphoreStep.waitForStart("restarting/1", b);
}
});
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
SemaphoreStep.success("restarting/1", null);
WorkflowRun b = story.j.assertBuildStatusSuccess(story.j.waitForCompletion(story.j.jenkins.getItemByFullName("p", WorkflowJob.class).getLastBuild()));
story.j.assertLogContains("groovy before val:", b);
story.j.assertLogContains("shell before val:", b);
story.j.assertLogContains("groovy after val:", b);
story.j.assertLogContains("shell after val:", b);
story.j.assertLogContains("groovy outside null:", b);
story.j.assertLogContains("shell outside :", b);
}
});
}

@Test public void nested() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
Expand All @@ -158,6 +245,44 @@ public class EnvStepTest {
});
}

@Test public void mapNumbersNested() {
jglick marked this conversation as resolved.
Show resolved Hide resolved
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" withEnv([A: 1]) {\n" +
" withEnv([B: 2]) {\n" +
" withEnv([C: true]) {\n" +
" isUnix() ? sh('echo A=$A B=$B C=$C') : bat('echo A=%A% B=%B% C=%C%')\n" +
" }\n" +
" }\n" +
" }\n" +
"}", true));
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("A=1 B=2 C=true", b);
}
});
}

@Test public void mapNested() {
jglick marked this conversation as resolved.
Show resolved Hide resolved
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" withEnv([A: 'one']) {\n" +
" withEnv([B: 'two']) {\n" +
" isUnix() ? sh('echo A=$A B=$B') : bat('echo A=%A% B=%B%')\n" +
" }\n" +
" }\n" +
"}", true));
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("A=one B=two", b);
}
});
}

@Test public void configRoundTrip() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
Expand Down