Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ltamaster committed Apr 1, 2024
1 parent 8a73d5a commit 1f4080c
Showing 1 changed file with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.rundeck.plugins.ansible.ansible

import com.dtolabs.rundeck.core.common.Framework
import com.dtolabs.rundeck.core.common.INodeSet
import com.dtolabs.rundeck.core.execution.ExecutionContext
import com.dtolabs.rundeck.plugins.step.PluginStepContext
import com.rundeck.plugins.ansible.plugin.AnsiblePluginGroup
import spock.lang.Specification
import com.dtolabs.rundeck.core.execution.ExecutionLogger

class AnsibleRunnerContextBuilderSpec extends Specification {

def "test plugin group"(){
given:

PluginStepContext context = Mock(PluginStepContext){
getDataContext() >> ['job': ['loglevel':'INFO']]
getExecutionContext() >> Mock(ExecutionContext){
getDataContext() >> [:]
getExecutionLogger() >> Mock(ExecutionLogger)
}
getFramework() >> Mock(Framework)
getNodes() >> Mock(INodeSet){
getNodes() >> []
}
}

Map<String, Object> configuration = [
'ansible-playbook' : 'path/to/playbook'
]

AnsiblePluginGroup pluginGroup = new AnsiblePluginGroup()
pluginGroup.setAnsibleConfigFilePath("/etc/ansible/ansible.cfg")
pluginGroup.setEncryptExtraVars(true)
pluginGroup.setAnsibleBinariesDirPath("/usr/local/lib")

when:
AnsibleRunnerContextBuilder contextBuilder = new AnsibleRunnerContextBuilder(context.getExecutionContext(),
context.getFramework(),
context.getNodes(),
configuration,
pluginGroup)

then:
contextBuilder.getConfigFile() == "/etc/ansible/ansible.cfg"
contextBuilder.getBinariesFilePath() == "/usr/local/lib"
contextBuilder.encryptExtraVars()
contextBuilder.getPlaybookPath() == "path/to/playbook"
}

def "test plugin group not set"(){
given:

PluginStepContext context = Mock(PluginStepContext){
getDataContext() >> ['job': ['loglevel':'INFO']]
getExecutionContext() >> Mock(ExecutionContext){
getDataContext() >> [:]
}
getFramework() >> Mock(Framework)
getNodes() >> Mock(INodeSet){
getNodes() >> []
}
}

Map<String, Object> configuration = [
'ansible-playbook' : 'path/to/playbook',
'ansible-config-file-path': '/etc/ansible/ansible.cfg'
]

when:
AnsibleRunnerContextBuilder contextBuilder = new AnsibleRunnerContextBuilder(context.getExecutionContext(),
context.getFramework(),
context.getNodes(),
configuration,
null)

then:
contextBuilder.getConfigFile() == "/etc/ansible/ansible.cfg"
contextBuilder.getBinariesFilePath() == null
!contextBuilder.encryptExtraVars()
contextBuilder.getPlaybookPath() == "path/to/playbook"
}
}

0 comments on commit 1f4080c

Please sign in to comment.