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);
}
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"));
}
}
}
Aggregations