diff options
Diffstat (limited to 'test/units/modules')
-rw-r--r-- | test/units/modules/conftest.py | 21 | ||||
-rw-r--r-- | test/units/modules/test_apt.py | 29 | ||||
-rw-r--r-- | test/units/modules/test_async_wrapper.py | 9 | ||||
-rw-r--r-- | test/units/modules/test_copy.py | 23 | ||||
-rw-r--r-- | test/units/modules/test_hostname.py | 10 | ||||
-rw-r--r-- | test/units/modules/test_iptables.py | 40 | ||||
-rw-r--r-- | test/units/modules/test_known_hosts.py | 2 | ||||
-rw-r--r-- | test/units/modules/test_unarchive.py | 20 | ||||
-rw-r--r-- | test/units/modules/utils.py | 10 |
9 files changed, 52 insertions, 112 deletions
diff --git a/test/units/modules/conftest.py b/test/units/modules/conftest.py index a7d1e04..c60c586 100644 --- a/test/units/modules/conftest.py +++ b/test/units/modules/conftest.py @@ -8,24 +8,15 @@ import json import pytest -from ansible.module_utils.six import string_types -from ansible.module_utils._text import to_bytes -from ansible.module_utils.common._collections_compat import MutableMapping +from ansible.module_utils.common.text.converters import to_bytes @pytest.fixture def patch_ansible_module(request, mocker): - if isinstance(request.param, string_types): - args = request.param - elif isinstance(request.param, MutableMapping): - if 'ANSIBLE_MODULE_ARGS' not in request.param: - request.param = {'ANSIBLE_MODULE_ARGS': request.param} - if '_ansible_remote_tmp' not in request.param['ANSIBLE_MODULE_ARGS']: - request.param['ANSIBLE_MODULE_ARGS']['_ansible_remote_tmp'] = '/tmp' - if '_ansible_keep_remote_files' not in request.param['ANSIBLE_MODULE_ARGS']: - request.param['ANSIBLE_MODULE_ARGS']['_ansible_keep_remote_files'] = False - args = json.dumps(request.param) - else: - raise Exception('Malformed data to the patch_ansible_module pytest fixture') + request.param = {'ANSIBLE_MODULE_ARGS': request.param} + request.param['ANSIBLE_MODULE_ARGS']['_ansible_remote_tmp'] = '/tmp' + request.param['ANSIBLE_MODULE_ARGS']['_ansible_keep_remote_files'] = False + + args = json.dumps(request.param) mocker.patch('ansible.module_utils.basic._ANSIBLE_ARGS', to_bytes(args)) diff --git a/test/units/modules/test_apt.py b/test/units/modules/test_apt.py index 20e056f..a5aa4a9 100644 --- a/test/units/modules/test_apt.py +++ b/test/units/modules/test_apt.py @@ -2,20 +2,13 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import collections -import sys from units.compat.mock import Mock from units.compat import unittest -try: - from ansible.modules.apt import ( - expand_pkgspec_from_fnmatches, - ) -except Exception: - # Need some more module_utils work (porting urls.py) before we can test - # modules. So don't error out in this case. - if sys.version_info[0] >= 3: - pass +from ansible.modules.apt import ( + expand_pkgspec_from_fnmatches, +) class AptExpandPkgspecTestCase(unittest.TestCase): @@ -29,25 +22,25 @@ class AptExpandPkgspecTestCase(unittest.TestCase): ] def test_trivial(self): - foo = ["apt"] + pkg = ["apt"] self.assertEqual( - expand_pkgspec_from_fnmatches(None, foo, self.fake_cache), foo) + expand_pkgspec_from_fnmatches(None, pkg, self.fake_cache), pkg) def test_version_wildcard(self): - foo = ["apt=1.0*"] + pkg = ["apt=1.0*"] self.assertEqual( - expand_pkgspec_from_fnmatches(None, foo, self.fake_cache), foo) + expand_pkgspec_from_fnmatches(None, pkg, self.fake_cache), pkg) def test_pkgname_wildcard_version_wildcard(self): - foo = ["apt*=1.0*"] + pkg = ["apt*=1.0*"] m_mock = Mock() self.assertEqual( - expand_pkgspec_from_fnmatches(m_mock, foo, self.fake_cache), + expand_pkgspec_from_fnmatches(m_mock, pkg, self.fake_cache), ['apt', 'apt-utils']) def test_pkgname_expands(self): - foo = ["apt*"] + pkg = ["apt*"] m_mock = Mock() self.assertEqual( - expand_pkgspec_from_fnmatches(m_mock, foo, self.fake_cache), + expand_pkgspec_from_fnmatches(m_mock, pkg, self.fake_cache), ["apt", "apt-utils"]) diff --git a/test/units/modules/test_async_wrapper.py b/test/units/modules/test_async_wrapper.py index 37b1fda..dbaf683 100644 --- a/test/units/modules/test_async_wrapper.py +++ b/test/units/modules/test_async_wrapper.py @@ -7,26 +7,21 @@ __metaclass__ = type import os import json import shutil +import sys import tempfile -import pytest - -from units.compat.mock import patch, MagicMock from ansible.modules import async_wrapper -from pprint import pprint - class TestAsyncWrapper: def test_run_module(self, monkeypatch): def mock_get_interpreter(module_path): - return ['/usr/bin/python'] + return [sys.executable] module_result = {'rc': 0} module_lines = [ - '#!/usr/bin/python', 'import sys', 'sys.stderr.write("stderr stuff")', "print('%s')" % json.dumps(module_result) diff --git a/test/units/modules/test_copy.py b/test/units/modules/test_copy.py index 20c309b..beeef6d 100644 --- a/test/units/modules/test_copy.py +++ b/test/units/modules/test_copy.py @@ -128,16 +128,19 @@ def test_split_pre_existing_dir_working_dir_exists(directory, expected, mocker): # # Info helpful for making new test cases: # -# base_mode = {'dir no perms': 0o040000, -# 'file no perms': 0o100000, -# 'dir all perms': 0o400000 | 0o777, -# 'file all perms': 0o100000, | 0o777} +# base_mode = { +# 'dir no perms': 0o040000, +# 'file no perms': 0o100000, +# 'dir all perms': 0o040000 | 0o777, +# 'file all perms': 0o100000 | 0o777} # -# perm_bits = {'x': 0b001, +# perm_bits = { +# 'x': 0b001, # 'w': 0b010, # 'r': 0b100} # -# role_shift = {'u': 6, +# role_shift = { +# 'u': 6, # 'g': 3, # 'o': 0} @@ -172,6 +175,10 @@ DATA = ( # Going from no permissions to setting all for user, group, and/or oth # chmod a-X statfile <== removes execute from statfile (0o100777, u'a-X', 0o0666), + # Verify X uses computed not original mode + (0o100777, u'a=,u=rX', 0o0400), + (0o040777, u'a=,u=rX', 0o0500), + # Multiple permissions (0o040000, u'u=rw-x+X,g=r-x+X,o=r-x+X', 0o0755), (0o100000, u'u=rw-x+X,g=r-x+X,o=r-x+X', 0o0644), @@ -185,6 +192,10 @@ UMASK_DATA = ( INVALID_DATA = ( (0o040000, u'a=foo', "bad symbolic permission for mode: a=foo"), (0o040000, u'f=rwx', "bad symbolic permission for mode: f=rwx"), + (0o100777, u'of=r', "bad symbolic permission for mode: of=r"), + + (0o100777, u'ao=r', "bad symbolic permission for mode: ao=r"), + (0o100777, u'oa=r', "bad symbolic permission for mode: oa=r"), ) diff --git a/test/units/modules/test_hostname.py b/test/units/modules/test_hostname.py index 9050fd0..1aa4a57 100644 --- a/test/units/modules/test_hostname.py +++ b/test/units/modules/test_hostname.py @@ -6,7 +6,6 @@ import shutil import tempfile from units.compat.mock import patch, MagicMock, mock_open -from ansible.module_utils import basic from ansible.module_utils.common._utils import get_all_subclasses from ansible.modules import hostname from units.modules.utils import ModuleTestCase, set_module_args @@ -44,12 +43,9 @@ class TestHostname(ModuleTestCase): classname = "%sStrategy" % prefix cls = getattr(hostname, classname, None) - if cls is None: - self.assertFalse( - cls is None, "%s is None, should be a subclass" % classname - ) - else: - self.assertTrue(issubclass(cls, hostname.BaseStrategy)) + assert cls is not None + + self.assertTrue(issubclass(cls, hostname.BaseStrategy)) class TestRedhatStrategy(ModuleTestCase): diff --git a/test/units/modules/test_iptables.py b/test/units/modules/test_iptables.py index 265e770..2459cf7 100644 --- a/test/units/modules/test_iptables.py +++ b/test/units/modules/test_iptables.py @@ -181,7 +181,7 @@ class TestIptables(ModuleTestCase): iptables.main() self.assertTrue(result.exception.args[0]['changed']) - self.assertEqual(run_command.call_count, 2) + self.assertEqual(run_command.call_count, 1) self.assertEqual(run_command.call_args_list[0][0][0], [ '/sbin/iptables', '-t', @@ -208,7 +208,6 @@ class TestIptables(ModuleTestCase): commands_results = [ (1, '', ''), # check_rule_present - (0, '', ''), # check_chain_present (0, '', ''), ] @@ -218,7 +217,7 @@ class TestIptables(ModuleTestCase): iptables.main() self.assertTrue(result.exception.args[0]['changed']) - self.assertEqual(run_command.call_count, 3) + self.assertEqual(run_command.call_count, 2) self.assertEqual(run_command.call_args_list[0][0][0], [ '/sbin/iptables', '-t', @@ -232,7 +231,7 @@ class TestIptables(ModuleTestCase): '-j', 'ACCEPT' ]) - self.assertEqual(run_command.call_args_list[2][0][0], [ + self.assertEqual(run_command.call_args_list[1][0][0], [ '/sbin/iptables', '-t', 'filter', @@ -272,7 +271,7 @@ class TestIptables(ModuleTestCase): iptables.main() self.assertTrue(result.exception.args[0]['changed']) - self.assertEqual(run_command.call_count, 2) + self.assertEqual(run_command.call_count, 1) self.assertEqual(run_command.call_args_list[0][0][0], [ '/sbin/iptables', '-t', @@ -321,7 +320,7 @@ class TestIptables(ModuleTestCase): iptables.main() self.assertTrue(result.exception.args[0]['changed']) - self.assertEqual(run_command.call_count, 3) + self.assertEqual(run_command.call_count, 2) self.assertEqual(run_command.call_args_list[0][0][0], [ '/sbin/iptables', '-t', @@ -343,7 +342,7 @@ class TestIptables(ModuleTestCase): '--to-ports', '8600' ]) - self.assertEqual(run_command.call_args_list[2][0][0], [ + self.assertEqual(run_command.call_args_list[1][0][0], [ '/sbin/iptables', '-t', 'nat', @@ -1019,10 +1018,8 @@ class TestIptables(ModuleTestCase): }) commands_results = [ - (1, '', ''), # check_rule_present (1, '', ''), # check_chain_present (0, '', ''), # create_chain - (0, '', ''), # append_rule ] with patch.object(basic.AnsibleModule, 'run_command') as run_command: @@ -1031,32 +1028,20 @@ class TestIptables(ModuleTestCase): iptables.main() self.assertTrue(result.exception.args[0]['changed']) - self.assertEqual(run_command.call_count, 4) + self.assertEqual(run_command.call_count, 2) self.assertEqual(run_command.call_args_list[0][0][0], [ '/sbin/iptables', '-t', 'filter', - '-C', 'FOOBAR', - ]) - - self.assertEqual(run_command.call_args_list[1][0][0], [ - '/sbin/iptables', - '-t', 'filter', '-L', 'FOOBAR', ]) - self.assertEqual(run_command.call_args_list[2][0][0], [ + self.assertEqual(run_command.call_args_list[1][0][0], [ '/sbin/iptables', '-t', 'filter', '-N', 'FOOBAR', ]) - self.assertEqual(run_command.call_args_list[3][0][0], [ - '/sbin/iptables', - '-t', 'filter', - '-A', 'FOOBAR', - ]) - commands_results = [ (0, '', ''), # check_rule_present ] @@ -1078,7 +1063,6 @@ class TestIptables(ModuleTestCase): commands_results = [ (1, '', ''), # check_rule_present - (1, '', ''), # check_chain_present ] with patch.object(basic.AnsibleModule, 'run_command') as run_command: @@ -1087,17 +1071,11 @@ class TestIptables(ModuleTestCase): iptables.main() self.assertTrue(result.exception.args[0]['changed']) - self.assertEqual(run_command.call_count, 2) + self.assertEqual(run_command.call_count, 1) self.assertEqual(run_command.call_args_list[0][0][0], [ '/sbin/iptables', '-t', 'filter', - '-C', 'FOOBAR', - ]) - - self.assertEqual(run_command.call_args_list[1][0][0], [ - '/sbin/iptables', - '-t', 'filter', '-L', 'FOOBAR', ]) diff --git a/test/units/modules/test_known_hosts.py b/test/units/modules/test_known_hosts.py index 123dd75..667f3e5 100644 --- a/test/units/modules/test_known_hosts.py +++ b/test/units/modules/test_known_hosts.py @@ -6,7 +6,7 @@ import tempfile from ansible.module_utils import basic from units.compat import unittest -from ansible.module_utils._text import to_bytes +from ansible.module_utils.common.text.converters import to_bytes from ansible.module_utils.basic import AnsibleModule from ansible.modules.known_hosts import compute_diff, sanity_check diff --git a/test/units/modules/test_unarchive.py b/test/units/modules/test_unarchive.py index 3e7a58c..935231b 100644 --- a/test/units/modules/test_unarchive.py +++ b/test/units/modules/test_unarchive.py @@ -8,20 +8,6 @@ import pytest from ansible.modules.unarchive import ZipArchive, TgzArchive -class AnsibleModuleExit(Exception): - def __init__(self, *args, **kwargs): - self.args = args - self.kwargs = kwargs - - -class ExitJson(AnsibleModuleExit): - pass - - -class FailJson(AnsibleModuleExit): - pass - - @pytest.fixture def fake_ansible_module(): return FakeAnsibleModule() @@ -32,12 +18,6 @@ class FakeAnsibleModule: self.params = {} self.tmpdir = None - def exit_json(self, *args, **kwargs): - raise ExitJson(*args, **kwargs) - - def fail_json(self, *args, **kwargs): - raise FailJson(*args, **kwargs) - class TestCaseZipArchive: @pytest.mark.parametrize( diff --git a/test/units/modules/utils.py b/test/units/modules/utils.py index 6d169e3..b56229e 100644 --- a/test/units/modules/utils.py +++ b/test/units/modules/utils.py @@ -6,14 +6,12 @@ import json from units.compat import unittest from units.compat.mock import patch from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from ansible.module_utils.common.text.converters import to_bytes def set_module_args(args): - if '_ansible_remote_tmp' not in args: - args['_ansible_remote_tmp'] = '/tmp' - if '_ansible_keep_remote_files' not in args: - args['_ansible_keep_remote_files'] = False + args['_ansible_remote_tmp'] = '/tmp' + args['_ansible_keep_remote_files'] = False args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) basic._ANSIBLE_ARGS = to_bytes(args) @@ -28,8 +26,6 @@ class AnsibleFailJson(Exception): def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False raise AnsibleExitJson(kwargs) |