mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Get tests passing
The largest failure in the tests was due to selinux not being installed. The tests don't require it to be installed, so mock the import.
This commit is contained in:
parent
f9b56a5d7c
commit
49d19e82ab
2 changed files with 78 additions and 58 deletions
|
@ -26,7 +26,7 @@ import errno
|
||||||
from nose.tools import timed
|
from nose.tools import timed
|
||||||
|
|
||||||
from ansible.compat.tests import unittest
|
from ansible.compat.tests import unittest
|
||||||
from ansible.compat.tests.mock import patch, MagicMock, mock_open
|
from ansible.compat.tests.mock import patch, MagicMock, mock_open, Mock
|
||||||
|
|
||||||
class TestModuleUtilsBasic(unittest.TestCase):
|
class TestModuleUtilsBasic(unittest.TestCase):
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ class TestModuleUtilsBasic(unittest.TestCase):
|
||||||
return ("", "", "")
|
return ("", "", "")
|
||||||
|
|
||||||
with patch('platform.linux_distribution', side_effect=_dist):
|
with patch('platform.linux_distribution', side_effect=_dist):
|
||||||
self.assertEqual(get_distribution(), "Amazon")
|
self.assertEqual(get_distribution(), "Amazonfoobar")
|
||||||
|
|
||||||
def _dist(distname='', version='', id='', supported_dists=(), full_distribution_name=1):
|
def _dist(distname='', version='', id='', supported_dists=(), full_distribution_name=1):
|
||||||
if supported_dists != ():
|
if supported_dists != ():
|
||||||
|
@ -80,7 +80,7 @@ class TestModuleUtilsBasic(unittest.TestCase):
|
||||||
return ("", "", "")
|
return ("", "", "")
|
||||||
|
|
||||||
with patch('platform.linux_distribution', side_effect=_dist):
|
with patch('platform.linux_distribution', side_effect=_dist):
|
||||||
self.assertEqual(get_distribution(), "OtherLinux")
|
self.assertEqual(get_distribution(), "Bar")
|
||||||
|
|
||||||
with patch('platform.linux_distribution', side_effect=Exception("boo")):
|
with patch('platform.linux_distribution', side_effect=Exception("boo")):
|
||||||
with patch('platform.dist', return_value=("bar", "2", "Two")):
|
with patch('platform.dist', return_value=("bar", "2", "Two")):
|
||||||
|
@ -356,10 +356,13 @@ class TestModuleUtilsBasic(unittest.TestCase):
|
||||||
self.assertEqual(am.selinux_mls_enabled(), False)
|
self.assertEqual(am.selinux_mls_enabled(), False)
|
||||||
|
|
||||||
basic.HAVE_SELINUX = True
|
basic.HAVE_SELINUX = True
|
||||||
with patch('selinux.is_selinux_mls_enabled', return_value=0):
|
basic.selinux = Mock()
|
||||||
self.assertEqual(am.selinux_mls_enabled(), False)
|
with patch.dict('sys.modules', {'selinux': basic.selinux}):
|
||||||
with patch('selinux.is_selinux_mls_enabled', return_value=1):
|
with patch('selinux.is_selinux_mls_enabled', return_value=0):
|
||||||
self.assertEqual(am.selinux_mls_enabled(), True)
|
self.assertEqual(am.selinux_mls_enabled(), False)
|
||||||
|
with patch('selinux.is_selinux_mls_enabled', return_value=1):
|
||||||
|
self.assertEqual(am.selinux_mls_enabled(), True)
|
||||||
|
delattr(basic, 'selinux')
|
||||||
|
|
||||||
def test_module_utils_basic_ansible_module_selinux_initial_context(self):
|
def test_module_utils_basic_ansible_module_selinux_initial_context(self):
|
||||||
from ansible.module_utils import basic
|
from ansible.module_utils import basic
|
||||||
|
@ -399,10 +402,13 @@ class TestModuleUtilsBasic(unittest.TestCase):
|
||||||
# finally we test the case where the python selinux lib is installed,
|
# finally we test the case where the python selinux lib is installed,
|
||||||
# and both possibilities there (enabled vs. disabled)
|
# and both possibilities there (enabled vs. disabled)
|
||||||
basic.HAVE_SELINUX = True
|
basic.HAVE_SELINUX = True
|
||||||
with patch('selinux.is_selinux_enabled', return_value=0):
|
basic.selinux = Mock()
|
||||||
self.assertEqual(am.selinux_enabled(), False)
|
with patch.dict('sys.modules', {'selinux': basic.selinux}):
|
||||||
with patch('selinux.is_selinux_enabled', return_value=1):
|
with patch('selinux.is_selinux_enabled', return_value=0):
|
||||||
self.assertEqual(am.selinux_enabled(), True)
|
self.assertEqual(am.selinux_enabled(), False)
|
||||||
|
with patch('selinux.is_selinux_enabled', return_value=1):
|
||||||
|
self.assertEqual(am.selinux_enabled(), True)
|
||||||
|
delattr(basic, 'selinux')
|
||||||
|
|
||||||
def test_module_utils_basic_ansible_module_selinux_default_context(self):
|
def test_module_utils_basic_ansible_module_selinux_default_context(self):
|
||||||
from ansible.module_utils import basic
|
from ansible.module_utils import basic
|
||||||
|
@ -422,18 +428,23 @@ class TestModuleUtilsBasic(unittest.TestCase):
|
||||||
# all following tests assume the python selinux bindings are installed
|
# all following tests assume the python selinux bindings are installed
|
||||||
basic.HAVE_SELINUX = True
|
basic.HAVE_SELINUX = True
|
||||||
|
|
||||||
# next, we test with a mocked implementation of selinux.matchpathcon to simulate
|
basic.selinux = Mock()
|
||||||
# an actual context being found
|
|
||||||
with patch('selinux.matchpathcon', return_value=[0, 'unconfined_u:object_r:default_t:s0']):
|
|
||||||
self.assertEqual(am.selinux_default_context(path='/foo/bar'), ['unconfined_u', 'object_r', 'default_t', 's0'])
|
|
||||||
|
|
||||||
# we also test the case where matchpathcon returned a failure
|
with patch.dict('sys.modules', {'selinux': basic.selinux}):
|
||||||
with patch('selinux.matchpathcon', return_value=[-1, '']):
|
# next, we test with a mocked implementation of selinux.matchpathcon to simulate
|
||||||
self.assertEqual(am.selinux_default_context(path='/foo/bar'), [None, None, None, None])
|
# an actual context being found
|
||||||
|
with patch('selinux.matchpathcon', return_value=[0, 'unconfined_u:object_r:default_t:s0']):
|
||||||
|
self.assertEqual(am.selinux_default_context(path='/foo/bar'), ['unconfined_u', 'object_r', 'default_t', 's0'])
|
||||||
|
|
||||||
# finally, we test where an OSError occurred during matchpathcon's call
|
# we also test the case where matchpathcon returned a failure
|
||||||
with patch('selinux.matchpathcon', side_effect=OSError):
|
with patch('selinux.matchpathcon', return_value=[-1, '']):
|
||||||
self.assertEqual(am.selinux_default_context(path='/foo/bar'), [None, None, None, None])
|
self.assertEqual(am.selinux_default_context(path='/foo/bar'), [None, None, None, None])
|
||||||
|
|
||||||
|
# finally, we test where an OSError occurred during matchpathcon's call
|
||||||
|
with patch('selinux.matchpathcon', side_effect=OSError):
|
||||||
|
self.assertEqual(am.selinux_default_context(path='/foo/bar'), [None, None, None, None])
|
||||||
|
|
||||||
|
delattr(basic, 'selinux')
|
||||||
|
|
||||||
def test_module_utils_basic_ansible_module_selinux_context(self):
|
def test_module_utils_basic_ansible_module_selinux_context(self):
|
||||||
from ansible.module_utils import basic
|
from ansible.module_utils import basic
|
||||||
|
@ -453,24 +464,29 @@ class TestModuleUtilsBasic(unittest.TestCase):
|
||||||
# all following tests assume the python selinux bindings are installed
|
# all following tests assume the python selinux bindings are installed
|
||||||
basic.HAVE_SELINUX = True
|
basic.HAVE_SELINUX = True
|
||||||
|
|
||||||
# next, we test with a mocked implementation of selinux.lgetfilecon_raw to simulate
|
basic.selinux = Mock()
|
||||||
# an actual context being found
|
|
||||||
with patch('selinux.lgetfilecon_raw', return_value=[0, 'unconfined_u:object_r:default_t:s0']):
|
|
||||||
self.assertEqual(am.selinux_context(path='/foo/bar'), ['unconfined_u', 'object_r', 'default_t', 's0'])
|
|
||||||
|
|
||||||
# we also test the case where matchpathcon returned a failure
|
with patch.dict('sys.modules', {'selinux': basic.selinux}):
|
||||||
with patch('selinux.lgetfilecon_raw', return_value=[-1, '']):
|
# next, we test with a mocked implementation of selinux.lgetfilecon_raw to simulate
|
||||||
self.assertEqual(am.selinux_context(path='/foo/bar'), [None, None, None, None])
|
# an actual context being found
|
||||||
|
with patch('selinux.lgetfilecon_raw', return_value=[0, 'unconfined_u:object_r:default_t:s0']):
|
||||||
|
self.assertEqual(am.selinux_context(path='/foo/bar'), ['unconfined_u', 'object_r', 'default_t', 's0'])
|
||||||
|
|
||||||
# finally, we test where an OSError occurred during matchpathcon's call
|
# we also test the case where matchpathcon returned a failure
|
||||||
e = OSError()
|
with patch('selinux.lgetfilecon_raw', return_value=[-1, '']):
|
||||||
e.errno = errno.ENOENT
|
self.assertEqual(am.selinux_context(path='/foo/bar'), [None, None, None, None])
|
||||||
with patch('selinux.lgetfilecon_raw', side_effect=e):
|
|
||||||
self.assertRaises(SystemExit, am.selinux_context, path='/foo/bar')
|
|
||||||
|
|
||||||
e = OSError()
|
# finally, we test where an OSError occurred during matchpathcon's call
|
||||||
with patch('selinux.lgetfilecon_raw', side_effect=e):
|
e = OSError()
|
||||||
self.assertRaises(SystemExit, am.selinux_context, path='/foo/bar')
|
e.errno = errno.ENOENT
|
||||||
|
with patch('selinux.lgetfilecon_raw', side_effect=e):
|
||||||
|
self.assertRaises(SystemExit, am.selinux_context, path='/foo/bar')
|
||||||
|
|
||||||
|
e = OSError()
|
||||||
|
with patch('selinux.lgetfilecon_raw', side_effect=e):
|
||||||
|
self.assertRaises(SystemExit, am.selinux_context, path='/foo/bar')
|
||||||
|
|
||||||
|
delattr(basic, 'selinux')
|
||||||
|
|
||||||
def test_module_utils_basic_ansible_module_is_special_selinux_path(self):
|
def test_module_utils_basic_ansible_module_is_special_selinux_path(self):
|
||||||
from ansible.module_utils import basic
|
from ansible.module_utils import basic
|
||||||
|
@ -583,26 +599,30 @@ class TestModuleUtilsBasic(unittest.TestCase):
|
||||||
am.selinux_context = MagicMock(return_value=['bar_u', 'bar_r', None, None])
|
am.selinux_context = MagicMock(return_value=['bar_u', 'bar_r', None, None])
|
||||||
am.is_special_selinux_path = MagicMock(return_value=(False, None))
|
am.is_special_selinux_path = MagicMock(return_value=(False, None))
|
||||||
|
|
||||||
with patch('selinux.lsetfilecon', return_value=0) as m:
|
basic.selinux = Mock()
|
||||||
self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False), True)
|
with patch.dict('sys.modules', {'selinux': basic.selinux}):
|
||||||
m.assert_called_with('/path/to/file', 'foo_u:foo_r:foo_t:s0')
|
with patch('selinux.lsetfilecon', return_value=0) as m:
|
||||||
m.reset_mock()
|
self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False), True)
|
||||||
am.check_mode = True
|
m.assert_called_with('/path/to/file', 'foo_u:foo_r:foo_t:s0')
|
||||||
self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False), True)
|
m.reset_mock()
|
||||||
self.assertEqual(m.called, False)
|
am.check_mode = True
|
||||||
am.check_mode = False
|
self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False), True)
|
||||||
|
self.assertEqual(m.called, False)
|
||||||
|
am.check_mode = False
|
||||||
|
|
||||||
with patch('selinux.lsetfilecon', return_value=1) as m:
|
with patch('selinux.lsetfilecon', return_value=1) as m:
|
||||||
self.assertRaises(SystemExit, am.set_context_if_different, '/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], True)
|
self.assertRaises(SystemExit, am.set_context_if_different, '/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], True)
|
||||||
|
|
||||||
with patch('selinux.lsetfilecon', side_effect=OSError) as m:
|
with patch('selinux.lsetfilecon', side_effect=OSError) as m:
|
||||||
self.assertRaises(SystemExit, am.set_context_if_different, '/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], True)
|
self.assertRaises(SystemExit, am.set_context_if_different, '/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], True)
|
||||||
|
|
||||||
am.is_special_selinux_path = MagicMock(return_value=(True, ['sp_u', 'sp_r', 'sp_t', 's0']))
|
am.is_special_selinux_path = MagicMock(return_value=(True, ['sp_u', 'sp_r', 'sp_t', 's0']))
|
||||||
|
|
||||||
with patch('selinux.lsetfilecon', return_value=0) as m:
|
with patch('selinux.lsetfilecon', return_value=0) as m:
|
||||||
self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False), True)
|
self.assertEqual(am.set_context_if_different('/path/to/file', ['foo_u', 'foo_r', 'foo_t', 's0'], False), True)
|
||||||
m.assert_called_with('/path/to/file', 'sp_u:sp_r:sp_t:s0')
|
m.assert_called_with('/path/to/file', 'sp_u:sp_r:sp_t:s0')
|
||||||
|
|
||||||
|
delattr(basic, 'selinux')
|
||||||
|
|
||||||
def test_module_utils_basic_ansible_module_set_owner_if_different(self):
|
def test_module_utils_basic_ansible_module_set_owner_if_different(self):
|
||||||
from ansible.module_utils import basic
|
from ansible.module_utils import basic
|
||||||
|
|
|
@ -55,15 +55,15 @@ class TestStrategyBase(unittest.TestCase):
|
||||||
|
|
||||||
mock_conn_info = MagicMock()
|
mock_conn_info = MagicMock()
|
||||||
|
|
||||||
mock_tqm._failed_hosts = []
|
mock_tqm._failed_hosts = dict()
|
||||||
mock_tqm._unreachable_hosts = []
|
mock_tqm._unreachable_hosts = dict()
|
||||||
strategy_base = StrategyBase(tqm=mock_tqm)
|
strategy_base = StrategyBase(tqm=mock_tqm)
|
||||||
|
|
||||||
self.assertEqual(strategy_base.run(iterator=mock_iterator, connection_info=mock_conn_info), 0)
|
self.assertEqual(strategy_base.run(iterator=mock_iterator, connection_info=mock_conn_info), 0)
|
||||||
self.assertEqual(strategy_base.run(iterator=mock_iterator, connection_info=mock_conn_info, result=False), 1)
|
self.assertEqual(strategy_base.run(iterator=mock_iterator, connection_info=mock_conn_info, result=False), 1)
|
||||||
mock_tqm._failed_hosts = ["host1"]
|
mock_tqm._failed_hosts = dict(host1=True)
|
||||||
self.assertEqual(strategy_base.run(iterator=mock_iterator, connection_info=mock_conn_info, result=False), 2)
|
self.assertEqual(strategy_base.run(iterator=mock_iterator, connection_info=mock_conn_info, result=False), 2)
|
||||||
mock_tqm._unreachable_hosts = ["host1"]
|
mock_tqm._unreachable_hosts = dict(host1=True)
|
||||||
self.assertEqual(strategy_base.run(iterator=mock_iterator, connection_info=mock_conn_info, result=False), 3)
|
self.assertEqual(strategy_base.run(iterator=mock_iterator, connection_info=mock_conn_info, result=False), 3)
|
||||||
|
|
||||||
def test_strategy_base_get_hosts(self):
|
def test_strategy_base_get_hosts(self):
|
||||||
|
|
Loading…
Reference in a new issue