use of org.jenkinsci.plugins.workflow.graphanalysis.ForkScanner 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);
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
WorkflowJob job = r.jenkins.createProject(WorkflowJob.class, "credentialed");
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" + "}"));
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);
Assert.assertEquals(ArgumentsAction.NotStoredReason.MASKED_VALUE, act.getArguments().get("message"));
Assert.assertNull(ArgumentsAction.getStepArgumentsAsString(f));
}
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);
}
Aggregations