Skip to content

Commit a8b005f

Browse files
authored
Merge pull request #86 from jonesbusy/feature/add-vault-pipeline-tests
Add vault pipelines tests
2 parents ad0cb80 + f24f790 commit a8b005f

4 files changed

Lines changed: 140 additions & 2 deletions

File tree

src/test/java/org/jenkinsci/plugins/ansible/PipelineTest.java

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import java.nio.charset.StandardCharsets;
44
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;
59
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
610
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
711
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
@@ -12,10 +16,12 @@
1216
import com.cloudbees.plugins.credentials.CredentialsProvider;
1317
import com.cloudbees.plugins.credentials.CredentialsScope;
1418
import com.cloudbees.plugins.credentials.CredentialsStore;
19+
import com.cloudbees.plugins.credentials.SecretBytes;
1520
import com.cloudbees.plugins.credentials.domains.Domain;
1621
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
1722
import hudson.model.Label;
1823
import hudson.slaves.DumbSlave;
24+
import hudson.util.Secret;
1925

2026
import static org.hamcrest.MatcherAssert.assertThat;
2127
import static org.hamcrest.Matchers.*;
@@ -94,11 +100,60 @@ public void testAnsiblePlaybookSshPass() throws Exception {
94100
workflowJob.setDefinition(new CpsFlowDefinition(pipeline, true));
95101
WorkflowRun run1 = workflowJob.scheduleBuild2(0).waitForStart();
96102
jenkins.waitForCompletion(run1);
97-
98-
System.out.println(run1.getLog());
99103
assertThat(run1.getLog(), allOf(
100104
containsString("sshpass ******** ansible-playbook playbook.yml -u username -k")
101105
));
102106
}
103107

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+
104159
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
pipeline {
2+
agent {
3+
label('test-agent')
4+
}
5+
stages {
6+
stage('Create playbook') {
7+
steps {
8+
writeFile(encoding: 'UTF-8', file: 'playbook.yml', text: '''- hosts: localhost
9+
connection: local
10+
gather_facts: no
11+
tasks:
12+
- debug: msg=test
13+
''')
14+
}
15+
}
16+
stage('Ansible playbook') {
17+
steps {
18+
warnError(message: 'ansible command not found?') {
19+
ansiblePlaybook(
20+
playbook: 'playbook.yml',
21+
vaultCredentialsId: 'vaultCredentialsFile',
22+
)
23+
}
24+
}
25+
}
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pipeline {
2+
agent {
3+
label('test-agent')
4+
}
5+
stages {
6+
stage('Create playbook') {
7+
steps {
8+
writeFile(encoding: 'UTF-8', file: 'playbook.yml', text: '''- hosts: localhost
9+
connection: local
10+
gather_facts: no
11+
tasks:
12+
- debug: msg=test
13+
''')
14+
}
15+
}
16+
stage('Ansible playbook') {
17+
steps {
18+
warnError(message: 'ansible command not found?') {
19+
withCredentials([file(credentialsId: 'vaultCredentialsFileViaExtras', variable: 'VAULT_FILE')]) {
20+
ansiblePlaybook(
21+
playbook: 'playbook.yml',
22+
extras: '--vault-password-file $VAULT_FILE',
23+
)
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
pipeline {
2+
agent {
3+
label('test-agent')
4+
}
5+
stages {
6+
stage('Create playbook') {
7+
steps {
8+
writeFile(encoding: 'UTF-8', file: 'playbook.yml', text: '''- hosts: localhost
9+
connection: local
10+
gather_facts: no
11+
tasks:
12+
- debug: msg=test
13+
''')
14+
}
15+
}
16+
stage('Ansible playbook') {
17+
steps {
18+
warnError(message: 'ansible command not found?') {
19+
ansiblePlaybook(
20+
playbook: 'playbook.yml',
21+
vaultCredentialsId: 'vaultCredentialsString',
22+
)
23+
}
24+
}
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)