Search in sources :

Example 1 with DescriptorMatchPredicate

use of org.jenkinsci.plugins.workflow.cps.DescriptorMatchPredicate in project workflow-cps-plugin by jenkinsci.

the class ArgumentsActionImplTest method testBasicCredentials.

@Test
public void testBasicCredentials() throws Exception {
    String username = "bob";
    String password = "s3cr3t";
    UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "test", "sample", username, password);
    c.setUsernameSecret(true);
    CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
    WorkflowJob job = r.createProject(WorkflowJob.class);
    job.setDefinition(new CpsFlowDefinition("node{ withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'test',\n" + "                usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {\n" + "    //available as an env variable, but will be masked if you try to print it out any which way\n" + "    echo \"$PASSWORD'\" \n" + "    echo \"${env.USERNAME}\"\n" + "    echo \"bob\"\n" + "} }\n" + "withCredentials([usernamePassword(credentialsId: 'test', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {\n" + "  echo \"${env.USERNAME} ${env.PASSWORD}\"\n" + "}", false));
    WorkflowRun run = job.scheduleBuild2(0).getStartCondition().get();
    r.waitForCompletion(run);
    FlowExecution exec = run.getExecution();
    String log = r.getLog(run);
    ForkScanner scanner = new ForkScanner();
    List<FlowNode> filtered = scanner.filteredNodes(exec, new DescriptorMatchPredicate(BindingStep.DescriptorImpl.class));
    // Check the binding step is OK
    Assert.assertEquals(8, filtered.size());
    FlowNode node = Collections2.filter(filtered, FlowScanningUtils.hasActionPredicate(ArgumentsActionImpl.class)).iterator().next();
    ArgumentsActionImpl act = node.getPersistentAction(ArgumentsActionImpl.class);
    Assert.assertNotNull(act.getArgumentValue("bindings"));
    Assert.assertNotNull(act.getArguments().get("bindings"));
    // Test that masking really does mask bound credentials appropriately
    filtered = scanner.filteredNodes(exec, new DescriptorMatchPredicate(EchoStep.DescriptorImpl.class));
    for (FlowNode f : filtered) {
        act = f.getPersistentAction(ArgumentsActionImpl.class);
        MatcherAssert.assertThat((String) act.getArguments().get("message"), allOf(not(containsString("bob")), not(containsString("s3cr3t"))));
    }
    List<FlowNode> allStepped = scanner.filteredNodes(run.getExecution().getCurrentHeads(), FlowScanningUtils.hasActionPredicate(ArgumentsActionImpl.class));
    // One ArgumentsActionImpl per block or atomic step
    Assert.assertEquals(6, allStepped.size());
    testDeserialize(exec);
}
Also used : DescriptorMatchPredicate(org.jenkinsci.plugins.workflow.cps.DescriptorMatchPredicate) EchoStep(org.jenkinsci.plugins.workflow.steps.EchoStep) ForkScanner(org.jenkinsci.plugins.workflow.graphanalysis.ForkScanner) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 2 with DescriptorMatchPredicate

use of org.jenkinsci.plugins.workflow.cps.DescriptorMatchPredicate 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)

Aggregations

CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)2 DescriptorMatchPredicate (org.jenkinsci.plugins.workflow.cps.DescriptorMatchPredicate)2 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)2 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)2 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)2 Test (org.junit.Test)2 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)1 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)1 ForkScanner (org.jenkinsci.plugins.workflow.graphanalysis.ForkScanner)1 LinearScanner (org.jenkinsci.plugins.workflow.graphanalysis.LinearScanner)1 EchoStep (org.jenkinsci.plugins.workflow.steps.EchoStep)1