From 1f4080c1f3ced04dff961f7d99371134d13e88d1 Mon Sep 17 00:00:00 2001 From: Luis Toledo Date: Mon, 1 Apr 2024 09:52:15 -0300 Subject: [PATCH] add unit test --- .../AnsibleRunnerContextBuilderSpec.groovy | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/test/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerContextBuilderSpec.groovy diff --git a/src/test/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerContextBuilderSpec.groovy b/src/test/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerContextBuilderSpec.groovy new file mode 100644 index 00000000..0b7f9f46 --- /dev/null +++ b/src/test/groovy/com/rundeck/plugins/ansible/ansible/AnsibleRunnerContextBuilderSpec.groovy @@ -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 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 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" + } +}