Improve coverage of role unit tests (#80695)

pull/80703/head
Matt Clay 2 years ago committed by GitHub
parent be6f2f43b6
commit 23e2ca91c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -108,8 +108,6 @@ class TestIncludeRole(unittest.TestCase):
# skip meta: role_complete # skip meta: role_complete
continue continue
role = task._role role = task._role
if not role:
continue
yield (role.get_name(), yield (role.get_name(),
self.var_manager.get_vars(play=play, task=task)) 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('l3_variable'), 'l3-main')
self.assertEqual(task_vars.get('test_variable'), 'l3-main') self.assertEqual(task_vars.get('test_variable'), 'l3-main')
else: else:
self.fail() self.fail() # pragma: nocover
self.assertFalse(expected_roles) self.assertFalse(expected_roles)
@patch('ansible.playbook.role.definition.unfrackpath', @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('l3_variable'), 'l3-alt')
self.assertEqual(task_vars.get('test_variable'), 'l3-alt') self.assertEqual(task_vars.get('test_variable'), 'l3-alt')
else: else:
self.fail() self.fail() # pragma: nocover
self.assertFalse(expected_roles) self.assertFalse(expected_roles)

@ -21,6 +21,8 @@ __metaclass__ = type
from collections.abc import Container from collections.abc import Container
import pytest
from units.compat import unittest from units.compat import unittest
from unittest.mock import patch, MagicMock from unittest.mock import patch, MagicMock
@ -42,12 +44,9 @@ class TestHashParams(unittest.TestCase):
self._assert_set(res) self._assert_set(res)
self._assert_hashable(res) self._assert_hashable(res)
def _assert_hashable(self, res): @staticmethod
a_dict = {} def _assert_hashable(res):
try: hash(res)
a_dict[res] = res
except TypeError as e:
self.fail('%s is not hashable: %s' % (res, e))
def _assert_set(self, res): def _assert_set(self, res):
self.assertIsInstance(res, frozenset) self.assertIsInstance(res, frozenset)
@ -87,36 +86,28 @@ class TestHashParams(unittest.TestCase):
def test_generator(self): def test_generator(self):
def my_generator(): def my_generator():
for i in ['a', 1, None, {}]: yield
yield i
params = my_generator() params = my_generator()
res = hash_params(params) res = hash_params(params)
self._assert_hashable(res) self._assert_hashable(res)
assert list(params)
def test_container_but_not_iterable(self): def test_container_but_not_iterable(self):
# This is a Container that is not iterable, which is unlikely but... # This is a Container that is not iterable, which is unlikely but...
class MyContainer(Container): class MyContainer(Container):
def __init__(self, some_thing): def __init__(self, _some_thing):
self.data = [] pass
self.data.append(some_thing)
def __contains__(self, item): def __contains__(self, item):
return item in self.data """Implementation omitted, since it will never be called."""
def __hash__(self):
return hash(self.data)
def __len__(self):
return len(self.data)
def __call__(self): params = MyContainer('foo bar')
return False
foo = MyContainer('foo bar') with pytest.raises(TypeError) as ex:
params = foo 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): def test_param_dict_dupe_values(self):
params1 = {'foo': False} params1 = {'foo': False}

Loading…
Cancel
Save