-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from jonesbusy/bugfix/fix-pipeline-extra-vars
Regression: Fix extra vars in pipeline and add minimal pipeline tests
- Loading branch information
Showing
6 changed files
with
188 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/java/org/jenkinsci/plugins/ansible/PipelineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.jenkinsci.plugins.ansible; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import org.apache.commons.io.IOUtils; | ||
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowRun; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import hudson.model.Label; | ||
import hudson.slaves.DumbSlave; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
public class PipelineTest { | ||
|
||
@ClassRule | ||
public static JenkinsRule jenkins = new JenkinsRule(); | ||
|
||
private static DumbSlave agent; | ||
|
||
@BeforeClass | ||
public static void startAgent() throws Exception { | ||
agent = jenkins.createSlave(Label.get("test-agent")); | ||
} | ||
|
||
@Test | ||
public void testMinimalPipeline() throws Exception { | ||
String pipeline = IOUtils.toString(PipelineTest.class.getResourceAsStream("/pipelines/minimal.groovy"), StandardCharsets.UTF_8); | ||
WorkflowJob workflowJob = jenkins.createProject(WorkflowJob.class); | ||
workflowJob.setDefinition(new CpsFlowDefinition(pipeline, true)); | ||
WorkflowRun run1 = workflowJob.scheduleBuild2(0).waitForStart(); | ||
jenkins.waitForCompletion(run1); | ||
assertThat(run1.getLog(), allOf( | ||
containsString("ansible-playbook playbook.yml") | ||
)); | ||
} | ||
|
||
@Test | ||
public void testExtraVarsHiddenString() throws Exception { | ||
String pipeline = IOUtils.toString(PipelineTest.class.getResourceAsStream("/pipelines/extraVarsHiddenString.groovy"), StandardCharsets.UTF_8); | ||
WorkflowJob workflowJob = jenkins.createProject(WorkflowJob.class); | ||
workflowJob.setDefinition(new CpsFlowDefinition(pipeline, true)); | ||
WorkflowRun run1 = workflowJob.scheduleBuild2(0).waitForStart(); | ||
jenkins.waitForCompletion(run1); | ||
assertThat(run1.getLog(), allOf( | ||
containsString("ansible-playbook playbook.yml -e ********") | ||
)); | ||
} | ||
|
||
@Test | ||
public void testExtraVarsMap() throws Exception { | ||
String pipeline = IOUtils.toString(PipelineTest.class.getResourceAsStream("/pipelines/extraVarsMap.groovy"), StandardCharsets.UTF_8); | ||
WorkflowJob workflowJob = jenkins.createProject(WorkflowJob.class); | ||
workflowJob.setDefinition(new CpsFlowDefinition(pipeline, true)); | ||
WorkflowRun run1 = workflowJob.scheduleBuild2(0).waitForStart(); | ||
jenkins.waitForCompletion(run1); | ||
assertThat(run1.getLog(), allOf( | ||
containsString("ansible-playbook playbook.yml -e foo1=bar1"), | ||
containsString("ansible-playbook playbook.yml -e ********") | ||
)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
pipeline { | ||
agent { | ||
label('test-agent') | ||
} | ||
stages { | ||
stage('Create playbook') { | ||
steps { | ||
writeFile(encoding: 'UTF-8', file: 'playbook.yml', text: '''- hosts: localhost | ||
connection: local | ||
gather_facts: no | ||
tasks: | ||
- debug: msg=test | ||
''') | ||
} | ||
} | ||
stage('Ansible playbook') { | ||
steps { | ||
warnError(message: 'ansible command not found?') { | ||
ansiblePlaybook( | ||
playbook: 'playbook.yml', | ||
extraVars: [foo: 'bar'], | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
pipeline { | ||
agent { | ||
label('test-agent') | ||
} | ||
stages { | ||
stage('Create playbook') { | ||
steps { | ||
writeFile(encoding: 'UTF-8', file: 'playbook.yml', text: '''- hosts: localhost | ||
connection: local | ||
gather_facts: no | ||
tasks: | ||
- debug: msg=test | ||
''') | ||
} | ||
} | ||
stage('Ansible playbook') { | ||
steps { | ||
warnError(message: 'ansible command not found?') { | ||
ansiblePlaybook( | ||
playbook: 'playbook.yml', | ||
extraVars: [foo1: [value: 'bar1', hidden: false]], | ||
) | ||
} | ||
warnError(message: 'ansible command not found?') { | ||
ansiblePlaybook( | ||
playbook: 'playbook.yml', | ||
extraVars: [foo2: [value: 'bar2', hidden: true]], | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pipeline { | ||
agent { | ||
label('test-agent') | ||
} | ||
stages { | ||
stage('Create playbook') { | ||
steps { | ||
writeFile(encoding: 'UTF-8', file: 'playbook.yml', text: '''- hosts: localhost | ||
connection: local | ||
gather_facts: no | ||
tasks: | ||
- debug: msg=test | ||
''') | ||
} | ||
} | ||
stage('Ansible playbook') { | ||
steps { | ||
warnError(message: 'ansible command not found?') { | ||
ansiblePlaybook(playbook: 'playbook.yml') | ||
} | ||
} | ||
} | ||
} | ||
} |