Skip to content

Instantly share code, notes, and snippets.

@cognifloyd
Created June 22, 2017 21:34
Show Gist options
  • Select an option

  • Save cognifloyd/759a902e0b6f18c19b936a9bc9a7bc2f to your computer and use it in GitHub Desktop.

Select an option

Save cognifloyd/759a902e0b6f18c19b936a9bc9a7bc2f to your computer and use it in GitHub Desktop.
Testing AnsibleBaseRunner
-
name: key_value
test:
- key1=value1
- "key2=value2"
expected:
- "key1=value1 key2=value2"
-
name: at_file
test:
- '@path/to/vars.yaml'
- '@path/to/vars.json'
expected:
- '@path/to/vars.yaml'
- '@path/to/vars.json'
-
name: key_value_and_at_file
test:
- '@path/to/vars.yaml'
- key1=value1
- '@path/to/vars.json'
- 'key2=value2'
- 'key3=value3'
# before ordering
expected:
- '@path/to/vars.yaml'
- '@path/to/vars.json1'
- 'key1=value1 key2=value2 key3=value3'
# after ordering
# expected:
# - '@path/to/vars.yaml'
# - 'key1=value1'
# - '@path/to/vars.json'
# - 'key2=value2 key3=value3'
from __future__ import print_function
import six
import yaml
import shlex
from mock import patch
from st2common.models.system.action import ShellScriptAction
from st2tests.pack_resource import BasePackResourceTestCase
from lib.ansible_base import AnsibleBaseRunner
class TestActionsLibAnsibleBaseRunner(BasePackResourceTestCase):
# from the ActiveDirectory test of a python action
def load_yaml(self, filename):
return yaml.safe_load(self.get_fixture_content(filename))
def setUp(self):
super(TestActionsLibAnsibleBaseRunner, self).setUp()
def test_init(self):
args = ['ansible_base.py', '--arg1', '--arg2', 'value2', '--arg3=value3']
ansible_base_runner = AnsibleBaseRunner(args)
self.assertEqual(ansible_base_runner.args, args[1:])
# AnsibleBaseRunner._parse_extra_vars()
@staticmethod
def generate_arg(st2_arg, value):
dummy_action = ShellScriptAction('', '', '')
# noinspection PyProtectedMember
arg = dummy_action._get_script_arguments(named_args={st2_arg: value})
arg = ' '.join(shlex.split(arg))
return arg
def check_arg_parse(self, arg_name, test_case, expected_ansible_args):
args = ['ansible_base.py', self.generate_arg(arg_name, test_case)]
ansible_base_runner = AnsibleBaseRunner(args)
self.assertItemsEqual(expected_ansible_args, ansible_base_runner.args)
def test_parse_extra_vars_key_value(self):
arg = '--extra_vars'
test = ['key1=value1', 'key2=value2']
expected = ['--extra-vars=' + ' '.join(test)]
self.check_arg_parse(arg, test, expected)
def test_parse_extra_vars_at_file(self):
arg = '--extra_vars'
test = ['@/path/to/vars_file.yaml', '@/other/path/vars_file.json']
expected = ['--extra-vars=' + case for case in test]
self.check_arg_parse(arg, test, expected)
def test_parse_extra_vars_at_file_and_key_value(self):
arg = '--extra_vars'
test = ['@/path/to/vars_file.yaml', 'key1=value1']
expected = ['--extra-vars=' + case for case in test]
self.check_arg_parse(arg, test, expected)
def test_parse_extra_vars_from_yaml_fixtures(self):
arg = '--extra_vars'
test_yaml = self.load_yaml('extra_vars.yaml')
for test in test_yaml:
name = test['name']
case = test['test']
expected = ['--extra-vars=' + e for e in test['expected']]
self.check_arg_parse(arg, case, expected)
# def test_parse_extra_vars_json(self):
# arg = '--extra_vars'
# test = [{'var1': 'value1'}]
# expected = ['--extra-vars=' + six.text_type(case) for case in test]
#
# self.check_arg_parse(arg, test, expected)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment