use of org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner in project workflow-cps-plugin by jenkinsci.
the class DSLTest method monomorphic.
/**
* Tests the ability to execute a step with an unnamed monomorphic describable argument.
*/
@Issue("JENKINS-29711")
@Test
public void monomorphic() throws Exception {
p.setDefinition(new CpsFlowDefinition("monomorphStep([firstArg:'one', secondArg:'two'])", true));
r.assertLogContains("First arg: one, second arg: two", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
WorkflowRun run = p.getLastBuild();
LinearScanner scanner = new LinearScanner();
FlowNode node = scanner.findFirstMatch(run.getExecution().getCurrentHeads(), new NodeStepTypePredicate("monomorphStep"));
ArgumentsAction argumentsAction = node.getPersistentAction(ArgumentsAction.class);
Assert.assertNotNull(argumentsAction);
Assert.assertEquals("one,two", ArgumentsAction.getStepArgumentsAsString(node));
}
use of org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner in project workflow-cps-plugin by jenkinsci.
the class DSLTest method sensitiveVariableInterpolationWithNestedDescribable.
@Issue("JENKINS-63254")
@Test
public void sensitiveVariableInterpolationWithNestedDescribable() throws Exception {
final String credentialsId = "creds-sensitiveVariableInterpolationWithNestedDescribable";
final String username = "bob";
final String password = "secr3t";
UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "sample", username, password);
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
p.setDefinition(new CpsFlowDefinition("" + "node {\n" + "withCredentials([usernamePassword(credentialsId: '" + credentialsId + "', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\n" + "monomorphWithSymbolStep(monomorphSymbol([firstArg:\"${PASSWORD}\", secondArg:'two']))" + "}\n" + "}", true));
WorkflowRun run = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
r.assertLogContains("First arg: ****, second arg: two", run);
r.assertLogContains("Warning: A secret was passed to \"monomorphWithSymbolStep\"", run);
r.assertLogContains("Affected argument(s) used the following variable(s): [PASSWORD]", run);
InterpolatedSecretsAction reportAction = run.getAction(InterpolatedSecretsAction.class);
Assert.assertNotNull(reportAction);
List<InterpolatedSecretsAction.InterpolatedWarnings> warnings = reportAction.getWarnings();
MatcherAssert.assertThat(warnings.size(), is(1));
InterpolatedSecretsAction.InterpolatedWarnings stepWarning = warnings.get(0);
MatcherAssert.assertThat(stepWarning.getStepName(), is("monomorphWithSymbolStep"));
MatcherAssert.assertThat(stepWarning.getInterpolatedVariables(), is(Arrays.asList("PASSWORD")));
LinearScanner scan = new LinearScanner();
FlowNode node = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("monomorphWithSymbolStep"));
ArgumentsAction argAction = node.getPersistentAction(ArgumentsAction.class);
Assert.assertFalse(argAction.isUnmodifiedArguments());
Object var = argAction.getArguments().values().iterator().next();
MatcherAssert.assertThat(var, instanceOf(UninstantiatedDescribable.class));
MatcherAssert.assertThat(((UninstantiatedDescribable) var).getArguments().toString(), is("{firstArg=${PASSWORD}, secondArg=two}"));
}
use of org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner in project workflow-cps-plugin by jenkinsci.
the class ArgumentsActionImplTest method testSpecialMetastepCases.
/**
* Handling of Metasteps with nested parameter -- we unwrap the step if there's just a single parameter given
* Otherwise we leave it as-is.
*/
@Test
public void testSpecialMetastepCases() throws Exception {
// First we test a metastep with a state argument
WorkflowJob job = r.createProject(WorkflowJob.class);
job.setDefinition(new CpsFlowDefinition(// Need to do some customization to load me
"state(moderate: true, state:[$class: 'Oregon']) \n", false));
WorkflowRun run = r.buildAndAssertSuccess(job);
LinearScanner scan = new LinearScanner();
FlowNode node = scan.findFirstMatch(run.getExecution(), new DescriptorMatchPredicate(StateMetaStep.DescriptorImpl.class));
Assert.assertNotNull(node);
Map<String, Object> args = ArgumentsAction.getArguments(node);
Assert.assertEquals(2, args.size());
Assert.assertEquals(true, args.get("moderate"));
Map<String, Object> stateArgs = (Map<String, Object>) args.get("state");
Assert.assertTrue("Nested state Describable should only include a class argument or none at all", stateArgs.size() <= 1 && Sets.difference(stateArgs.keySet(), new HashSet<>(Arrays.asList("$class"))).size() == 0);
// Same metastep but only one arg supplied, shouldn't auto-unwrap the internal step because can take 2 args
job = r.createProject(WorkflowJob.class);
job.setDefinition(new CpsFlowDefinition(// Need to do some customization to load me
"state(state:[$class: 'Oregon']) \n" + "state(new org.jenkinsci.plugins.workflow.testMetaStep.Oregon()) \n", false));
run = r.buildAndAssertSuccess(job);
List<FlowNode> nodes = scan.filteredNodes(run.getExecution(), new DescriptorMatchPredicate(StateMetaStep.DescriptorImpl.class));
for (FlowNode n : nodes) {
Assert.assertNotNull(n);
args = ArgumentsAction.getArguments(n);
Assert.assertEquals(1, args.size());
Map<String, Object> argsMap = (Map) args;
Object stateValue = argsMap.get("state");
if (stateValue instanceof Map) {
Assert.assertEquals("Oregon", ((Map<String, Object>) stateValue).get("$class"));
}
}
}
use of org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner in project workflow-cps-plugin by jenkinsci.
the class ArgumentsActionImplTest method testMissingDescriptionInsideStage.
@Test
@Issue("JENKINS-48644")
public void testMissingDescriptionInsideStage() throws Exception {
// No need for windows-specific testing
Assume.assumeTrue(r.jenkins.getComputer("").isUnix());
WorkflowJob j = r.createProject(WorkflowJob.class);
j.setDefinition(new CpsFlowDefinition("node{\n" + " stage ('Build') {\n" + " sh \"echo 'Building'\"\n" + " }\n" + " stage ('Test') {\n" + " sh \"echo 'testing'\"\n" + " }\n" + " stage ('Deploy') {\n" + " sh \"echo 'deploy'\"\n" + " }\n" + "}\n", true));
WorkflowRun run = r.buildAndAssertSuccess(j);
List<FlowNode> nodes = new LinearScanner().filteredNodes(run.getExecution(), new NodeStepTypePredicate("sh"));
for (FlowNode f : nodes) {
if (ArgumentsAction.getStepArgumentsAsString(f) == null) {
Assert.fail("No arguments action for node: " + f.toString());
}
}
}
use of org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner in project workflow-cps-plugin by jenkinsci.
the class ArgumentsActionImplTest method testUnusualStepInstantiations.
@Test
public void testUnusualStepInstantiations() throws Exception {
WorkflowJob job = r.createProject(WorkflowJob.class);
job.setDefinition(new CpsFlowDefinition(" node('" + r.jenkins.getSelfLabel().getName() + "') { \n" + " writeFile text: 'hello world', file: 'msg.out'\n" + " step([$class: 'ArtifactArchiver', artifacts: 'msg.out', fingerprint: false])\n " + // Symbol-based, because withEnv is a metastep; TODO huh? no it is not
" withEnv(['CUSTOM=val']) {\n" + " echo env.CUSTOM\n" + " }\n" + "}", false));
WorkflowRun run = r.buildAndAssertSuccess(job);
LinearScanner scan = new LinearScanner();
FlowNode testNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("writeFile"));
ArgumentsAction act = testNode.getPersistentAction(ArgumentsAction.class);
Assert.assertNotNull(act);
Assert.assertEquals("hello world", act.getArgumentValue("text"));
Assert.assertEquals("msg.out", act.getArgumentValue("file"));
testNode = scan.findFirstMatch(run.getExecution().getCurrentHeads().get(0), new NodeStepTypePredicate("step"));
act = testNode.getPersistentAction(ArgumentsAction.class);
Assert.assertNotNull(act);
Map<String, Object> delegateMap = ((Map<String, Object>) act.getArgumentValue("delegate"));
Assert.assertEquals("msg.out", delegateMap.get("artifacts"));
Assert.assertEquals(Boolean.FALSE, delegateMap.get("fingerprint"));
// Start node for EnvAction
testNode = run.getExecution().getNode("7");
act = testNode.getPersistentAction(ArgumentsAction.class);
Assert.assertNotNull(act);
Assert.assertEquals(1, act.getArguments().size());
Object ob = act.getArguments().get("overrides");
Assert.assertEquals("CUSTOM=val", (String) ((ArrayList) ob).get(0));
testDeserialize(run.getExecution());
}
Aggregations