Search in sources :

Example 6 with LinearScanner

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));
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 7 with LinearScanner

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}"));
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) UninstantiatedDescribable(org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable) Matchers.containsString(org.hamcrest.Matchers.containsString) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) InterpolatedSecretsAction(org.jenkinsci.plugins.workflow.cps.view.InterpolatedSecretsAction) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 8 with LinearScanner

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"));
        }
    }
}
Also used : DescriptorMatchPredicate(org.jenkinsci.plugins.workflow.cps.DescriptorMatchPredicate) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) HashMap(java.util.HashMap) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 9 with LinearScanner

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());
        }
    }
}
Also used : NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 10 with LinearScanner

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());
}
Also used : ArgumentsAction(org.jenkinsci.plugins.workflow.actions.ArgumentsAction) NodeStepTypePredicate(org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) ArrayList(java.util.ArrayList) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) LinearScanner(org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Aggregations

FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)11 LinearScanner (org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner)11 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)11 Test (org.junit.Test)11 NodeStepTypePredicate (org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate)10 ArgumentsAction (org.jenkinsci.plugins.workflow.actions.ArgumentsAction)9 Issue (org.jvnet.hudson.test.Issue)6 Matchers.containsString (org.hamcrest.Matchers.containsString)5 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)5 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)5 InterpolatedSecretsAction (org.jenkinsci.plugins.workflow.cps.view.InterpolatedSecretsAction)4 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)3 ParametersAction (hudson.model.ParametersAction)2 ParametersDefinitionProperty (hudson.model.ParametersDefinitionProperty)2 PasswordParameterDefinition (hudson.model.PasswordParameterDefinition)2 PasswordParameterValue (hudson.model.PasswordParameterValue)2 StringParameterDefinition (hudson.model.StringParameterDefinition)2 StringParameterValue (hudson.model.StringParameterValue)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2