|
2 | 2 |
|
3 | 3 | import java.nio.charset.StandardCharsets; |
4 | 4 | import org.apache.commons.io.IOUtils; |
| 5 | +import org.jenkinsci.plugins.plaincredentials.FileCredentials; |
| 6 | +import org.jenkinsci.plugins.plaincredentials.StringCredentials; |
| 7 | +import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl; |
| 8 | +import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl; |
5 | 9 | import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; |
6 | 10 | import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
7 | 11 | import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
|
12 | 16 | import com.cloudbees.plugins.credentials.CredentialsProvider; |
13 | 17 | import com.cloudbees.plugins.credentials.CredentialsScope; |
14 | 18 | import com.cloudbees.plugins.credentials.CredentialsStore; |
| 19 | +import com.cloudbees.plugins.credentials.SecretBytes; |
15 | 20 | import com.cloudbees.plugins.credentials.domains.Domain; |
16 | 21 | import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; |
17 | 22 | import hudson.model.Label; |
18 | 23 | import hudson.slaves.DumbSlave; |
| 24 | +import hudson.util.Secret; |
19 | 25 |
|
20 | 26 | import static org.hamcrest.MatcherAssert.assertThat; |
21 | 27 | import static org.hamcrest.Matchers.*; |
@@ -94,11 +100,60 @@ public void testAnsiblePlaybookSshPass() throws Exception { |
94 | 100 | workflowJob.setDefinition(new CpsFlowDefinition(pipeline, true)); |
95 | 101 | WorkflowRun run1 = workflowJob.scheduleBuild2(0).waitForStart(); |
96 | 102 | jenkins.waitForCompletion(run1); |
97 | | - |
98 | | - System.out.println(run1.getLog()); |
99 | 103 | assertThat(run1.getLog(), allOf( |
100 | 104 | containsString("sshpass ******** ansible-playbook playbook.yml -u username -k") |
101 | 105 | )); |
102 | 106 | } |
103 | 107 |
|
| 108 | + @Test |
| 109 | + public void testVaultCredentialsFile() throws Exception { |
| 110 | + |
| 111 | + FileCredentials vaultCredentials = new FileCredentialsImpl(CredentialsScope.GLOBAL, "vaultCredentialsFile", "test username password", "vault-pass.txt", SecretBytes.fromString("text-secret")); |
| 112 | + CredentialsStore store = CredentialsProvider.lookupStores(jenkins.jenkins).iterator().next(); |
| 113 | + store.addCredentials(Domain.global(), vaultCredentials); |
| 114 | + |
| 115 | + String pipeline = IOUtils.toString(PipelineTest.class.getResourceAsStream("/pipelines/vaultCredentialsFile.groovy"), StandardCharsets.UTF_8); |
| 116 | + WorkflowJob workflowJob = jenkins.createProject(WorkflowJob.class); |
| 117 | + workflowJob.setDefinition(new CpsFlowDefinition(pipeline, true)); |
| 118 | + WorkflowRun run1 = workflowJob.scheduleBuild2(0).waitForStart(); |
| 119 | + jenkins.waitForCompletion(run1); |
| 120 | + assertThat(run1.getLog(), allOf( |
| 121 | + containsString("ansible-playbook playbook.yml --vault-password-file ") |
| 122 | + )); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testVaultCredentialsString() throws Exception { |
| 127 | + |
| 128 | + StringCredentials vaultCredentials = new StringCredentialsImpl(CredentialsScope.GLOBAL, "vaultCredentialsString", "test username password", Secret.fromString("test-secret")); |
| 129 | + CredentialsStore store = CredentialsProvider.lookupStores(jenkins.jenkins).iterator().next(); |
| 130 | + store.addCredentials(Domain.global(), vaultCredentials); |
| 131 | + |
| 132 | + String pipeline = IOUtils.toString(PipelineTest.class.getResourceAsStream("/pipelines/vaultCredentialsString.groovy"), StandardCharsets.UTF_8); |
| 133 | + WorkflowJob workflowJob = jenkins.createProject(WorkflowJob.class); |
| 134 | + workflowJob.setDefinition(new CpsFlowDefinition(pipeline, true)); |
| 135 | + WorkflowRun run1 = workflowJob.scheduleBuild2(0).waitForStart(); |
| 136 | + jenkins.waitForCompletion(run1); |
| 137 | + assertThat(run1.getLog(), allOf( |
| 138 | + containsString("ansible-playbook playbook.yml --vault-password-file ") |
| 139 | + )); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testVaultCredentialsFileViaExtras() throws Exception { |
| 144 | + |
| 145 | + FileCredentials vaultCredentials = new FileCredentialsImpl(CredentialsScope.GLOBAL, "vaultCredentialsFileViaExtras", "test username password", "vault-pass.txt", SecretBytes.fromString("text-secret")); |
| 146 | + CredentialsStore store = CredentialsProvider.lookupStores(jenkins.jenkins).iterator().next(); |
| 147 | + store.addCredentials(Domain.global(), vaultCredentials); |
| 148 | + |
| 149 | + String pipeline = IOUtils.toString(PipelineTest.class.getResourceAsStream("/pipelines/vaultCredentialsFileViaExtras.groovy"), StandardCharsets.UTF_8); |
| 150 | + WorkflowJob workflowJob = jenkins.createProject(WorkflowJob.class); |
| 151 | + workflowJob.setDefinition(new CpsFlowDefinition(pipeline, true)); |
| 152 | + WorkflowRun run1 = workflowJob.scheduleBuild2(0).waitForStart(); |
| 153 | + jenkins.waitForCompletion(run1); |
| 154 | + assertThat(run1.getLog(), allOf( |
| 155 | + containsString("ansible-playbook playbook.yml --vault-password-file ") |
| 156 | + )); |
| 157 | + } |
| 158 | + |
104 | 159 | } |
0 commit comments