Skip to content

Commit

Permalink
Merge pull request #76 from jonesbusy/bugfix/fix-pipeline-extra-vars
Browse files Browse the repository at this point in the history
Regression: Fix extra vars in pipeline and add minimal pipeline tests
  • Loading branch information
jonesbusy authored May 26, 2023
2 parents a008a4b + 5ab5c88 commit 1c78933
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 5 deletions.
28 changes: 27 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<properties>
<changelist>999999-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/ansible-plugin</gitHubRepo>
<jenkins.version>2.387.2</jenkins.version>
<jenkins.version>2.387.3</jenkins.version>
</properties>
<licenses>
<license>
Expand Down Expand Up @@ -81,6 +81,32 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<!-- Test plugins -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-utility-steps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,17 +378,22 @@ private List<ExtraVar> convertExtraVars(Map<String, Object> extraVars) {
if (extraVars == null) {
return null;
}
List<ExtraVar> extraVarList = new ArrayList<ExtraVar>();
List<ExtraVar> extraVarList = new ArrayList<>();
for (Map.Entry<String, Object> entry: extraVars.entrySet()) {
ExtraVar var = new ExtraVar();
var.setKey(entry.getKey());
Object o = entry.getValue();
if (o instanceof Map) {
var.setSecretValue((Secret)((Map)o).get("value"));
var.setSecretValue(Secret.fromString((String)((Map)o).get("value")));
var.setHidden((Boolean)((Map)o).get("hidden"));
} else {
}
else if (o instanceof String) {
var.setSecretValue(Secret.fromString((String)o));
var.setHidden(true);
}
else if (o instanceof Secret) {
var.setSecretValue((Secret)o);
var.setHidden(false);
var.setHidden(true);
}
extraVarList.add(var);
}
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/org/jenkinsci/plugins/ansible/PipelineTest.java
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 ********")
));
}

}
27 changes: 27 additions & 0 deletions src/test/resources/pipelines/extraVarsHiddenString.groovy
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'],
)
}
}
}
}
}
33 changes: 33 additions & 0 deletions src/test/resources/pipelines/extraVarsMap.groovy
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]],
)
}
}
}
}
}
24 changes: 24 additions & 0 deletions src/test/resources/pipelines/minimal.groovy
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')
}
}
}
}
}

0 comments on commit 1c78933

Please sign in to comment.