Skip to content

Commit 58aa1c0

Browse files
committed
Added skip empty extra vars unit test
1 parent 2be0cac commit 58aa1c0

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/test/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerSpec.groovy

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,54 @@ class AnsibleRunnerSpec extends Specification{
264264

265265
}
266266

267+
def "test skip empty extra vars"() {
268+
given:
269+
String playbook = "test"
270+
String privateKey = "privateKey"
271+
String extraVars = "test: 123\nemptyVar: " // emptyVar is empty → should be skipped
272+
273+
def runnerBuilder = AnsibleRunner.playbookInline(playbook)
274+
runnerBuilder.encryptExtraVars(true)
275+
runnerBuilder.sshPrivateKey(privateKey)
276+
runnerBuilder.extraVars(extraVars)
277+
278+
def process = Mock(Process) {
279+
waitFor() >> 0
280+
getInputStream() >> new ByteArrayInputStream("".getBytes())
281+
getOutputStream() >> new ByteArrayOutputStream()
282+
getErrorStream() >> new ByteArrayInputStream("".getBytes())
283+
}
284+
285+
def processExecutor = Mock(ProcessExecutor) {
286+
run() >> process
287+
}
288+
289+
def processBuilder = Mock(ProcessExecutor.ProcessExecutorBuilder) {
290+
build() >> processExecutor
291+
}
292+
293+
def ansibleVault = Mock(AnsibleVault) {
294+
checkAnsibleVault() >> true
295+
getVaultPasswordScriptFile() >> new File("vault-script-client.py")
296+
}
297+
298+
runnerBuilder.processExecutorBuilder(processBuilder)
299+
runnerBuilder.ansibleVault(ansibleVault)
300+
runnerBuilder.customTmpDirPath("/tmp")
301+
302+
when:
303+
AnsibleRunner runner = runnerBuilder.build()
304+
def result = runner.run()
305+
306+
then:
307+
// Only 1 encryptVariable call — "test" — "emptyVar" is skipped
308+
1 * ansibleVault.encryptVariable(_,_) >> "!vault | value"
309+
result == 0
310+
!runner.getTempPlaybook().exists()
311+
!runner.getTempVarsFile().exists()
312+
}
313+
314+
267315
def "test password authentication with encrypted extra vars "(){
268316
given:
269317

0 commit comments

Comments
 (0)