diff options
Diffstat (limited to 'test/units/playbook')
-rw-r--r-- | test/units/playbook/role/test_include_role.py | 6 | ||||
-rw-r--r-- | test/units/playbook/role/test_role.py | 77 | ||||
-rw-r--r-- | test/units/playbook/test_base.py | 20 | ||||
-rw-r--r-- | test/units/playbook/test_collectionsearch.py | 1 | ||||
-rw-r--r-- | test/units/playbook/test_helpers.py | 62 | ||||
-rw-r--r-- | test/units/playbook/test_included_file.py | 14 | ||||
-rw-r--r-- | test/units/playbook/test_play_context.py | 2 | ||||
-rw-r--r-- | test/units/playbook/test_taggable.py | 1 | ||||
-rw-r--r-- | test/units/playbook/test_task.py | 2 |
9 files changed, 80 insertions, 105 deletions
diff --git a/test/units/playbook/role/test_include_role.py b/test/units/playbook/role/test_include_role.py index 5e7625b..aa97da1 100644 --- a/test/units/playbook/role/test_include_role.py +++ b/test/units/playbook/role/test_include_role.py @@ -108,8 +108,6 @@ class TestIncludeRole(unittest.TestCase): # skip meta: role_complete continue role = task._role - if not role: - continue yield (role.get_name(), self.var_manager.get_vars(play=play, task=task)) @@ -201,7 +199,7 @@ class TestIncludeRole(unittest.TestCase): self.assertEqual(task_vars.get('l3_variable'), 'l3-main') self.assertEqual(task_vars.get('test_variable'), 'l3-main') else: - self.fail() + self.fail() # pragma: nocover self.assertFalse(expected_roles) @patch('ansible.playbook.role.definition.unfrackpath', @@ -247,5 +245,5 @@ class TestIncludeRole(unittest.TestCase): self.assertEqual(task_vars.get('l3_variable'), 'l3-alt') self.assertEqual(task_vars.get('test_variable'), 'l3-alt') else: - self.fail() + self.fail() # pragma: nocover self.assertFalse(expected_roles) diff --git a/test/units/playbook/role/test_role.py b/test/units/playbook/role/test_role.py index 5d47631..9d6b0ed 100644 --- a/test/units/playbook/role/test_role.py +++ b/test/units/playbook/role/test_role.py @@ -21,10 +21,12 @@ __metaclass__ = type from collections.abc import Container +import pytest + from units.compat import unittest from unittest.mock import patch, MagicMock -from ansible.errors import AnsibleError, AnsibleParserError +from ansible.errors import AnsibleParserError from ansible.playbook.block import Block from units.mock.loader import DictDataLoader @@ -42,12 +44,9 @@ class TestHashParams(unittest.TestCase): self._assert_set(res) self._assert_hashable(res) - def _assert_hashable(self, res): - a_dict = {} - try: - a_dict[res] = res - except TypeError as e: - self.fail('%s is not hashable: %s' % (res, e)) + @staticmethod + def _assert_hashable(res): + hash(res) def _assert_set(self, res): self.assertIsInstance(res, frozenset) @@ -87,36 +86,28 @@ class TestHashParams(unittest.TestCase): def test_generator(self): def my_generator(): - for i in ['a', 1, None, {}]: - yield i + yield params = my_generator() res = hash_params(params) self._assert_hashable(res) + assert list(params) def test_container_but_not_iterable(self): # This is a Container that is not iterable, which is unlikely but... class MyContainer(Container): - def __init__(self, some_thing): - self.data = [] - self.data.append(some_thing) + def __init__(self, _some_thing): + pass def __contains__(self, item): - return item in self.data - - def __hash__(self): - return hash(self.data) - - def __len__(self): - return len(self.data) + """Implementation omitted, since it will never be called.""" - def __call__(self): - return False + params = MyContainer('foo bar') - foo = MyContainer('foo bar') - params = foo + with pytest.raises(TypeError) as ex: + hash_params(params) - self.assertRaises(TypeError, hash_params, params) + assert ex.value.args == ("'MyContainer' object is not iterable",) def test_param_dict_dupe_values(self): params1 = {'foo': False} @@ -151,18 +142,18 @@ class TestHashParams(unittest.TestCase): self.assertNotEqual(hash(res1), hash(res2)) self.assertNotEqual(res1, res2) - foo = {} - foo[res1] = 'params1' - foo[res2] = 'params2' + params_dict = {} + params_dict[res1] = 'params1' + params_dict[res2] = 'params2' - self.assertEqual(len(foo), 2) + self.assertEqual(len(params_dict), 2) - del foo[res2] - self.assertEqual(len(foo), 1) + del params_dict[res2] + self.assertEqual(len(params_dict), 1) - for key in foo: - self.assertTrue(key in foo) - self.assertIn(key, foo) + for key in params_dict: + self.assertTrue(key in params_dict) + self.assertIn(key, params_dict) class TestRole(unittest.TestCase): @@ -177,7 +168,7 @@ class TestRole(unittest.TestCase): }) mock_play = MagicMock() - mock_play.ROLE_CACHE = {} + mock_play.role_cache = {} i = RoleInclude.load('foo_tasks', play=mock_play, loader=fake_loader) r = Role.load(i, play=mock_play) @@ -199,7 +190,7 @@ class TestRole(unittest.TestCase): }) mock_play = MagicMock() - mock_play.ROLE_CACHE = {} + mock_play.role_cache = {} i = RoleInclude.load('foo_tasks', play=mock_play, loader=fake_loader) r = Role.load(i, play=mock_play, from_files=dict(tasks='custom_main')) @@ -217,7 +208,7 @@ class TestRole(unittest.TestCase): }) mock_play = MagicMock() - mock_play.ROLE_CACHE = {} + mock_play.role_cache = {} i = RoleInclude.load('foo_handlers', play=mock_play, loader=fake_loader) r = Role.load(i, play=mock_play) @@ -238,7 +229,7 @@ class TestRole(unittest.TestCase): }) mock_play = MagicMock() - mock_play.ROLE_CACHE = {} + mock_play.role_cache = {} i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader) r = Role.load(i, play=mock_play) @@ -259,7 +250,7 @@ class TestRole(unittest.TestCase): }) mock_play = MagicMock() - mock_play.ROLE_CACHE = {} + mock_play.role_cache = {} i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader) r = Role.load(i, play=mock_play) @@ -280,7 +271,7 @@ class TestRole(unittest.TestCase): }) mock_play = MagicMock() - mock_play.ROLE_CACHE = {} + mock_play.role_cache = {} i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader) r = Role.load(i, play=mock_play) @@ -303,7 +294,7 @@ class TestRole(unittest.TestCase): }) mock_play = MagicMock() - mock_play.ROLE_CACHE = {} + mock_play.role_cache = {} i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader) r = Role.load(i, play=mock_play) @@ -323,7 +314,7 @@ class TestRole(unittest.TestCase): }) mock_play = MagicMock() - mock_play.ROLE_CACHE = {} + mock_play.role_cache = {} i = RoleInclude.load('foo_vars', play=mock_play, loader=fake_loader) r = Role.load(i, play=mock_play) @@ -370,7 +361,7 @@ class TestRole(unittest.TestCase): mock_play = MagicMock() mock_play.collections = None - mock_play.ROLE_CACHE = {} + mock_play.role_cache = {} i = RoleInclude.load('foo_metadata', play=mock_play, loader=fake_loader) r = Role.load(i, play=mock_play) @@ -415,7 +406,7 @@ class TestRole(unittest.TestCase): }) mock_play = MagicMock() - mock_play.ROLE_CACHE = {} + mock_play.role_cache = {} i = RoleInclude.load(dict(role='foo_complex'), play=mock_play, loader=fake_loader) r = Role.load(i, play=mock_play) diff --git a/test/units/playbook/test_base.py b/test/units/playbook/test_base.py index d5810e7..bedd96a 100644 --- a/test/units/playbook/test_base.py +++ b/test/units/playbook/test_base.py @@ -21,13 +21,12 @@ __metaclass__ = type from units.compat import unittest -from ansible.errors import AnsibleParserError +from ansible.errors import AnsibleParserError, AnsibleAssertionError from ansible.module_utils.six import string_types from ansible.playbook.attribute import FieldAttribute, NonInheritableFieldAttribute from ansible.template import Templar from ansible.playbook import base -from ansible.utils.unsafe_proxy import AnsibleUnsafeBytes, AnsibleUnsafeText -from ansible.utils.sentinel import Sentinel +from ansible.utils.unsafe_proxy import AnsibleUnsafeText from units.mock.loader import DictDataLoader @@ -331,12 +330,6 @@ class ExampleSubClass(base.Base): def __init__(self): super(ExampleSubClass, self).__init__() - def get_dep_chain(self): - if self._parent: - return self._parent.get_dep_chain() - else: - return None - class BaseSubClass(base.Base): name = FieldAttribute(isa='string', default='', always_post_validate=True) @@ -588,10 +581,11 @@ class TestBaseSubClass(TestBase): bsc.post_validate, templar) def test_attr_unknown(self): - a_list = ['some string'] - ds = {'test_attr_unknown_isa': a_list} - bsc = self._base_validate(ds) - self.assertEqual(bsc.test_attr_unknown_isa, a_list) + self.assertRaises( + AnsibleAssertionError, + self._base_validate, + {'test_attr_unknown_isa': True} + ) def test_attr_method(self): ds = {'test_attr_method': 'value from the ds'} diff --git a/test/units/playbook/test_collectionsearch.py b/test/units/playbook/test_collectionsearch.py index be40d85..d16541b 100644 --- a/test/units/playbook/test_collectionsearch.py +++ b/test/units/playbook/test_collectionsearch.py @@ -22,7 +22,6 @@ from ansible.errors import AnsibleParserError from ansible.playbook.play import Play from ansible.playbook.task import Task from ansible.playbook.block import Block -from ansible.playbook.collectionsearch import CollectionSearch import pytest diff --git a/test/units/playbook/test_helpers.py b/test/units/playbook/test_helpers.py index a89730c..23385c0 100644 --- a/test/units/playbook/test_helpers.py +++ b/test/units/playbook/test_helpers.py @@ -52,10 +52,6 @@ class MixinForMocks(object): self.mock_inventory = MagicMock(name='MockInventory') self.mock_inventory._hosts_cache = dict() - def _get_host(host_name): - return None - - self.mock_inventory.get_host.side_effect = _get_host # TODO: can we use a real VariableManager? self.mock_variable_manager = MagicMock(name='MockVariableManager') self.mock_variable_manager.get_vars.return_value = dict() @@ -69,11 +65,11 @@ class MixinForMocks(object): self._test_data_path = os.path.dirname(__file__) self.fake_include_loader = DictDataLoader({"/dev/null/includes/test_include.yml": """ - - include: other_test_include.yml + - include_tasks: other_test_include.yml - shell: echo 'hello world' """, "/dev/null/includes/static_test_include.yml": """ - - include: other_test_include.yml + - include_tasks: other_test_include.yml - shell: echo 'hello static world' """, "/dev/null/includes/other_test_include.yml": """ @@ -86,10 +82,6 @@ class TestLoadListOfTasks(unittest.TestCase, MixinForMocks): def setUp(self): self._setup() - def _assert_is_task_list(self, results): - for result in results: - self.assertIsInstance(result, Task) - def _assert_is_task_list_or_blocks(self, results): self.assertIsInstance(results, list) for result in results: @@ -168,57 +160,57 @@ class TestLoadListOfTasks(unittest.TestCase, MixinForMocks): ds, play=self.mock_play, use_handlers=True, variable_manager=self.mock_variable_manager, loader=self.fake_loader) - def test_one_bogus_include(self): - ds = [{'include': 'somefile.yml'}] + def test_one_bogus_include_tasks(self): + ds = [{'include_tasks': 'somefile.yml'}] res = helpers.load_list_of_tasks(ds, play=self.mock_play, variable_manager=self.mock_variable_manager, loader=self.fake_loader) self.assertIsInstance(res, list) - self.assertEqual(len(res), 0) + self.assertEqual(len(res), 1) + self.assertIsInstance(res[0], TaskInclude) - def test_one_bogus_include_use_handlers(self): - ds = [{'include': 'somefile.yml'}] + def test_one_bogus_include_tasks_use_handlers(self): + ds = [{'include_tasks': 'somefile.yml'}] res = helpers.load_list_of_tasks(ds, play=self.mock_play, use_handlers=True, variable_manager=self.mock_variable_manager, loader=self.fake_loader) self.assertIsInstance(res, list) - self.assertEqual(len(res), 0) + self.assertEqual(len(res), 1) + self.assertIsInstance(res[0], TaskInclude) - def test_one_bogus_include_static(self): + def test_one_bogus_import_tasks(self): ds = [{'import_tasks': 'somefile.yml'}] res = helpers.load_list_of_tasks(ds, play=self.mock_play, variable_manager=self.mock_variable_manager, loader=self.fake_loader) self.assertIsInstance(res, list) self.assertEqual(len(res), 0) - def test_one_include(self): - ds = [{'include': '/dev/null/includes/other_test_include.yml'}] + def test_one_include_tasks(self): + ds = [{'include_tasks': '/dev/null/includes/other_test_include.yml'}] res = helpers.load_list_of_tasks(ds, play=self.mock_play, variable_manager=self.mock_variable_manager, loader=self.fake_include_loader) self.assertEqual(len(res), 1) self._assert_is_task_list_or_blocks(res) - def test_one_parent_include(self): - ds = [{'include': '/dev/null/includes/test_include.yml'}] + def test_one_parent_include_tasks(self): + ds = [{'include_tasks': '/dev/null/includes/test_include.yml'}] res = helpers.load_list_of_tasks(ds, play=self.mock_play, variable_manager=self.mock_variable_manager, loader=self.fake_include_loader) self._assert_is_task_list_or_blocks(res) - self.assertIsInstance(res[0], Block) - self.assertIsInstance(res[0]._parent, TaskInclude) + self.assertIsInstance(res[0], TaskInclude) + self.assertIsNone(res[0]._parent) - # TODO/FIXME: do this non deprecated way - def test_one_include_tags(self): - ds = [{'include': '/dev/null/includes/other_test_include.yml', + def test_one_include_tasks_tags(self): + ds = [{'include_tasks': '/dev/null/includes/other_test_include.yml', 'tags': ['test_one_include_tags_tag1', 'and_another_tagB'] }] res = helpers.load_list_of_tasks(ds, play=self.mock_play, variable_manager=self.mock_variable_manager, loader=self.fake_include_loader) self._assert_is_task_list_or_blocks(res) - self.assertIsInstance(res[0], Block) + self.assertIsInstance(res[0], TaskInclude) self.assertIn('test_one_include_tags_tag1', res[0].tags) self.assertIn('and_another_tagB', res[0].tags) - # TODO/FIXME: do this non deprecated way - def test_one_parent_include_tags(self): - ds = [{'include': '/dev/null/includes/test_include.yml', + def test_one_parent_include_tasks_tags(self): + ds = [{'include_tasks': '/dev/null/includes/test_include.yml', # 'vars': {'tags': ['test_one_parent_include_tags_tag1', 'and_another_tag2']} 'tags': ['test_one_parent_include_tags_tag1', 'and_another_tag2'] } @@ -226,20 +218,20 @@ class TestLoadListOfTasks(unittest.TestCase, MixinForMocks): res = helpers.load_list_of_tasks(ds, play=self.mock_play, variable_manager=self.mock_variable_manager, loader=self.fake_include_loader) self._assert_is_task_list_or_blocks(res) - self.assertIsInstance(res[0], Block) + self.assertIsInstance(res[0], TaskInclude) self.assertIn('test_one_parent_include_tags_tag1', res[0].tags) self.assertIn('and_another_tag2', res[0].tags) - def test_one_include_use_handlers(self): - ds = [{'include': '/dev/null/includes/other_test_include.yml'}] + def test_one_include_tasks_use_handlers(self): + ds = [{'include_tasks': '/dev/null/includes/other_test_include.yml'}] res = helpers.load_list_of_tasks(ds, play=self.mock_play, use_handlers=True, variable_manager=self.mock_variable_manager, loader=self.fake_include_loader) self._assert_is_task_list_or_blocks(res) self.assertIsInstance(res[0], Handler) - def test_one_parent_include_use_handlers(self): - ds = [{'include': '/dev/null/includes/test_include.yml'}] + def test_one_parent_include_tasks_use_handlers(self): + ds = [{'include_tasks': '/dev/null/includes/test_include.yml'}] res = helpers.load_list_of_tasks(ds, play=self.mock_play, use_handlers=True, variable_manager=self.mock_variable_manager, loader=self.fake_include_loader) diff --git a/test/units/playbook/test_included_file.py b/test/units/playbook/test_included_file.py index 7341dff..c7a66b0 100644 --- a/test/units/playbook/test_included_file.py +++ b/test/units/playbook/test_included_file.py @@ -105,7 +105,7 @@ def test_included_file_instantiation(): assert inc_file._task is None -def test_process_include_results(mock_iterator, mock_variable_manager): +def test_process_include_tasks_results(mock_iterator, mock_variable_manager): hostname = "testhost1" hostname2 = "testhost2" @@ -113,7 +113,7 @@ def test_process_include_results(mock_iterator, mock_variable_manager): parent_task = Task.load(parent_task_ds) parent_task._play = None - task_ds = {'include': 'include_test.yml'} + task_ds = {'include_tasks': 'include_test.yml'} loaded_task = TaskInclude.load(task_ds, task_include=parent_task) return_data = {'include': 'include_test.yml'} @@ -133,7 +133,7 @@ def test_process_include_results(mock_iterator, mock_variable_manager): assert res[0]._vars == {} -def test_process_include_diff_files(mock_iterator, mock_variable_manager): +def test_process_include_tasks_diff_files(mock_iterator, mock_variable_manager): hostname = "testhost1" hostname2 = "testhost2" @@ -141,11 +141,11 @@ def test_process_include_diff_files(mock_iterator, mock_variable_manager): parent_task = Task.load(parent_task_ds) parent_task._play = None - task_ds = {'include': 'include_test.yml'} + task_ds = {'include_tasks': 'include_test.yml'} loaded_task = TaskInclude.load(task_ds, task_include=parent_task) loaded_task._play = None - child_task_ds = {'include': 'other_include_test.yml'} + child_task_ds = {'include_tasks': 'other_include_test.yml'} loaded_child_task = TaskInclude.load(child_task_ds, task_include=loaded_task) loaded_child_task._play = None @@ -175,7 +175,7 @@ def test_process_include_diff_files(mock_iterator, mock_variable_manager): assert res[1]._vars == {} -def test_process_include_simulate_free(mock_iterator, mock_variable_manager): +def test_process_include_tasks_simulate_free(mock_iterator, mock_variable_manager): hostname = "testhost1" hostname2 = "testhost2" @@ -186,7 +186,7 @@ def test_process_include_simulate_free(mock_iterator, mock_variable_manager): parent_task1._play = None parent_task2._play = None - task_ds = {'include': 'include_test.yml'} + task_ds = {'include_tasks': 'include_test.yml'} loaded_task1 = TaskInclude.load(task_ds, task_include=parent_task1) loaded_task2 = TaskInclude.load(task_ds, task_include=parent_task2) diff --git a/test/units/playbook/test_play_context.py b/test/units/playbook/test_play_context.py index 7c24de5..7461b45 100644 --- a/test/units/playbook/test_play_context.py +++ b/test/units/playbook/test_play_context.py @@ -12,10 +12,8 @@ import pytest from ansible import constants as C from ansible import context from ansible.cli.arguments import option_helpers as opt_help -from ansible.errors import AnsibleError from ansible.playbook.play_context import PlayContext from ansible.playbook.play import Play -from ansible.plugins.loader import become_loader from ansible.utils import context_objects as co diff --git a/test/units/playbook/test_taggable.py b/test/units/playbook/test_taggable.py index 3881e17..c6ce35d 100644 --- a/test/units/playbook/test_taggable.py +++ b/test/units/playbook/test_taggable.py @@ -29,6 +29,7 @@ class TaggableTestObj(Taggable): def __init__(self): self._loader = DictDataLoader({}) self.tags = [] + self._parent = None class TestTaggable(unittest.TestCase): diff --git a/test/units/playbook/test_task.py b/test/units/playbook/test_task.py index 070d7aa..e28d2ec 100644 --- a/test/units/playbook/test_task.py +++ b/test/units/playbook/test_task.py @@ -22,6 +22,7 @@ __metaclass__ = type from units.compat import unittest from unittest.mock import patch from ansible.playbook.task import Task +from ansible.plugins.loader import init_plugin_loader from ansible.parsing.yaml import objects from ansible import errors @@ -74,6 +75,7 @@ class TestTask(unittest.TestCase): @patch.object(errors.AnsibleError, '_get_error_lines_from_file') def test_load_task_kv_form_error_36848(self, mock_get_err_lines): + init_plugin_loader() ds = objects.AnsibleMapping(kv_bad_args_ds) ds.ansible_pos = ('test_task_faux_playbook.yml', 1, 1) mock_get_err_lines.return_value = (kv_bad_args_str, '') |