diff options
author | Pilou <pierre-louis@libregerbil.fr> | 2017-11-17 18:17:07 +0100 |
---|---|---|
committer | Matt Clay <matt@mystile.com> | 2017-11-17 09:17:07 -0800 |
commit | a5c9726502a0f15f03da9f8b38141a6de81631ec (patch) | |
tree | 1c8208edc5c53cba018582394a132b2efc7f510b /test | |
parent | 71a6dcdf3e528bd088b1399b9dcf3cfb62ae0f36 (diff) | |
download | ansible-a5c9726502a0f15f03da9f8b38141a6de81631ec.tar.gz |
Unit tests: share common code (#31456)
* move set_module_args to units.modules.utils
* unit tests: reuse set_module_args
* unit tests: mock exit/fail_json in module.utils.ModuleTestCase
* unit tests: use module.utils.ModuleTestCase
* unit tests: fix 'import shadowed by loop variable'
Diffstat (limited to 'test')
154 files changed, 671 insertions, 1113 deletions
diff --git a/test/units/modules/cloud/amazon/test_api_gateway.py b/test/units/modules/cloud/amazon/test_api_gateway.py index f9a678ef70..bf94852ac3 100644 --- a/test/units/modules/cloud/amazon/test_api_gateway.py +++ b/test/units/modules/cloud/amazon/test_api_gateway.py @@ -23,10 +23,8 @@ from __future__ import (absolute_import, division, print_function) from nose.plugins.skip import SkipTest import pytest import sys -import json -from ansible.module_utils._text import to_bytes -from ansible.module_utils import basic from ansible.module_utils.ec2 import HAS_BOTO3 +from units.modules.utils import set_module_args if not HAS_BOTO3: raise SkipTest("test_api_gateway.py requires the `boto3` and `botocore` modules") @@ -34,11 +32,6 @@ if not HAS_BOTO3: import ansible.modules.cloud.amazon.aws_api_gateway as agw -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - exit_return_dict = {} diff --git a/test/units/modules/cloud/amazon/test_lambda.py b/test/units/modules/cloud/amazon/test_lambda.py index 567a7bce12..c890c977a3 100644 --- a/test/units/modules/cloud/amazon/test_lambda.py +++ b/test/units/modules/cloud/amazon/test_lambda.py @@ -20,12 +20,11 @@ from __future__ import (absolute_import, division, print_function) import copy -import json import pytest from ansible.compat.tests.mock import MagicMock, Mock, patch from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import set_module_args boto3 = pytest.importorskip("boto3") @@ -35,11 +34,6 @@ _temp = __import__("ansible.modules.cloud.amazon.lambda") lda = getattr(_temp.modules.cloud.amazon, "lambda") -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - base_lambda_config = { 'FunctionName': 'lambda_name', 'Role': 'arn:aws:iam::987654321012:role/lambda_basic_execution', diff --git a/test/units/modules/cloud/amazon/test_lambda_policy.py b/test/units/modules/cloud/amazon/test_lambda_policy.py index 550e0405b7..b2e61f3a28 100644 --- a/test/units/modules/cloud/amazon/test_lambda_policy.py +++ b/test/units/modules/cloud/amazon/test_lambda_policy.py @@ -19,13 +19,12 @@ # Make coding more python3-ish from __future__ import (absolute_import, division, print_function) +import copy + from nose.plugins.skip import SkipTest from ansible.module_utils.aws.core import HAS_BOTO3 -from ansible.module_utils import basic -from ansible.module_utils.basic import to_bytes from ansible.compat.tests.mock import MagicMock -import json -import copy +from units.modules.utils import set_module_args if not HAS_BOTO3: raise SkipTest("test_api_gateway.py requires the `boto3` and `botocore` modules") @@ -45,11 +44,6 @@ base_module_args = { } -def set_module_args(mod_args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': mod_args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def test_module_is_created_sensibly(): set_module_args(base_module_args) module = setup_module_object() diff --git a/test/units/modules/monitoring/test_circonus_annotation.py b/test/units/modules/monitoring/test_circonus_annotation.py index 652f73f16e..5073b2ae1b 100644 --- a/test/units/modules/monitoring/test_circonus_annotation.py +++ b/test/units/modules/monitoring/test_circonus_annotation.py @@ -6,48 +6,21 @@ import re import uuid from urllib3.response import HTTPResponse -from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch from ansible.module_utils import basic from ansible.module_utils._text import to_bytes from ansible.modules.monitoring import circonus_annotation +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - -def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - -class TestCirconusAnnotation(unittest.TestCase): +class TestCirconusAnnotation(ModuleTestCase): def setUp(self): + super(TestCirconusAnnotation, self).setUp() self.module = circonus_annotation - self.mock_exit_fail = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) - self.mock_exit_fail.start() - self.addCleanup(self.mock_exit_fail.stop) - def tearDown(self): - pass + super(TestCirconusAnnotation, self).tearDown() def test_without_required_parameters(self): """Failure must occurs when all parameters are missing""" diff --git a/test/units/modules/network/aireos/aireos_module.py b/test/units/modules/network/aireos/aireos_module.py index 3483447a6d..5fc6c93578 100644 --- a/test/units/modules/network/aireos/aireos_module.py +++ b/test/units/modules/network/aireos/aireos_module.py @@ -22,15 +22,7 @@ __metaclass__ = type import os import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes - - -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -54,15 +46,7 @@ def load_fixture(name): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestCiscoWlcModule(unittest.TestCase): +class TestCiscoWlcModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False): @@ -84,27 +68,16 @@ class TestCiscoWlcModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/aireos/test_aireos_command.py b/test/units/modules/network/aireos/test_aireos_command.py index e4403c171e..14d7f58e5a 100644 --- a/test/units/modules/network/aireos/test_aireos_command.py +++ b/test/units/modules/network/aireos/test_aireos_command.py @@ -23,7 +23,8 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.aireos import aireos_command -from .aireos_module import TestCiscoWlcModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .aireos_module import TestCiscoWlcModule, load_fixture class TestCiscoWlcCommandModule(TestCiscoWlcModule): @@ -31,10 +32,12 @@ class TestCiscoWlcCommandModule(TestCiscoWlcModule): module = aireos_command def setUp(self): + super(TestCiscoWlcCommandModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.aireos.aireos_command.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestCiscoWlcCommandModule, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/aireos/test_aireos_config.py b/test/units/modules/network/aireos/test_aireos_config.py index 311d7398e3..7f15c87937 100644 --- a/test/units/modules/network/aireos/test_aireos_config.py +++ b/test/units/modules/network/aireos/test_aireos_config.py @@ -20,11 +20,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.aireos import aireos_config -from .aireos_module import TestCiscoWlcModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .aireos_module import TestCiscoWlcModule, load_fixture class TestCiscoWlcConfigModule(TestCiscoWlcModule): @@ -32,6 +31,8 @@ class TestCiscoWlcConfigModule(TestCiscoWlcModule): module = aireos_config def setUp(self): + super(TestCiscoWlcConfigModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.aireos.aireos_config.get_config') self.get_config = self.mock_get_config.start() @@ -42,6 +43,7 @@ class TestCiscoWlcConfigModule(TestCiscoWlcModule): self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestCiscoWlcConfigModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() self.mock_run_commands.stop() diff --git a/test/units/modules/network/aruba/aruba_module.py b/test/units/modules/network/aruba/aruba_module.py index d8883e8958..d11d7d4393 100644 --- a/test/units/modules/network/aruba/aruba_module.py +++ b/test/units/modules/network/aruba/aruba_module.py @@ -22,16 +22,9 @@ __metaclass__ = type import os import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -54,15 +47,7 @@ def load_fixture(name): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestArubaModule(unittest.TestCase): +class TestArubaModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False): @@ -84,27 +69,16 @@ class TestArubaModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/aruba/test_aruba_command.py b/test/units/modules/network/aruba/test_aruba_command.py index ca268f59b9..3d31aa426a 100644 --- a/test/units/modules/network/aruba/test_aruba_command.py +++ b/test/units/modules/network/aruba/test_aruba_command.py @@ -23,7 +23,8 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.aruba import aruba_command -from .aruba_module import TestArubaModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .aruba_module import TestArubaModule, load_fixture class TestArubaCommandModule(TestArubaModule): @@ -31,10 +32,14 @@ class TestArubaCommandModule(TestArubaModule): module = aruba_command def setUp(self): + super(TestArubaCommandModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.aruba.aruba_command.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestArubaCommandModule, self).tearDown() + self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/aruba/test_aruba_config.py b/test/units/modules/network/aruba/test_aruba_config.py index 34f36881f7..7510b10398 100644 --- a/test/units/modules/network/aruba/test_aruba_config.py +++ b/test/units/modules/network/aruba/test_aruba_config.py @@ -20,11 +20,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.aruba import aruba_config -from .aruba_module import TestArubaModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .aruba_module import TestArubaModule, load_fixture class TestArubaConfigModule(TestArubaModule): @@ -32,6 +31,8 @@ class TestArubaConfigModule(TestArubaModule): module = aruba_config def setUp(self): + super(TestArubaConfigModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.aruba.aruba_config.get_config') self.get_config = self.mock_get_config.start() @@ -42,6 +43,8 @@ class TestArubaConfigModule(TestArubaModule): self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestArubaConfigModule, self).tearDown() + self.mock_get_config.stop() self.mock_load_config.stop() self.mock_run_commands.stop() diff --git a/test/units/modules/network/enos/enos_module.py b/test/units/modules/network/enos/enos_module.py index d9395f5745..5728657c61 100644 --- a/test/units/modules/network/enos/enos_module.py +++ b/test/units/modules/network/enos/enos_module.py @@ -22,16 +22,9 @@ __metaclass__ = type import os import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -54,15 +47,7 @@ def load_fixture(name): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestEnosModule(unittest.TestCase): +class TestEnosModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False): @@ -84,27 +69,16 @@ class TestEnosModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/enos/test_enos_command.py b/test/units/modules/network/enos/test_enos_command.py index 99e7403d2b..8d9fc2c35e 100644 --- a/test/units/modules/network/enos/test_enos_command.py +++ b/test/units/modules/network/enos/test_enos_command.py @@ -23,7 +23,8 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.enos import enos_command -from .enos_module import TestEnosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .enos_module import TestEnosModule, load_fixture class TestEnosCommandModule(TestEnosModule): @@ -31,10 +32,12 @@ class TestEnosCommandModule(TestEnosModule): module = enos_command def setUp(self): + super(TestEnosCommandModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.enos.enos_command.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestEnosCommandModule, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/enos/test_enos_facts.py b/test/units/modules/network/enos/test_enos_facts.py index 4724ab19a6..b0c4ea03c4 100644 --- a/test/units/modules/network/enos/test_enos_facts.py +++ b/test/units/modules/network/enos/test_enos_facts.py @@ -22,8 +22,9 @@ __metaclass__ = type import json from ansible.compat.tests.mock import patch -from .enos_module import TestEnosModule, load_fixture, set_module_args +from .enos_module import TestEnosModule, load_fixture from ansible.modules.network.enos import enos_facts +from units.modules.utils import set_module_args class TestEnosFacts(TestEnosModule): @@ -31,11 +32,13 @@ class TestEnosFacts(TestEnosModule): module = enos_facts def setUp(self): + super(TestEnosFacts, self).setUp() self.mock_run_commands = patch( 'ansible.modules.network.enos.enos_facts.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestEnosFacts, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/eos/eos_module.py b/test/units/modules/network/eos/eos_module.py index 3db2d1f089..d9d5d44fa2 100644 --- a/test/units/modules/network/eos/eos_module.py +++ b/test/units/modules/network/eos/eos_module.py @@ -22,16 +22,9 @@ __metaclass__ = type import json import os -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -54,15 +47,7 @@ def load_fixture(name): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestEosModule(unittest.TestCase): +class TestEosModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, commands=None, inputs=None, sort=True, defaults=False, transport='cli'): @@ -98,27 +83,16 @@ class TestEosModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/eos/test_eos_banner.py b/test/units/modules/network/eos/test_eos_banner.py index 3f501bb680..7684dc22c9 100644 --- a/test/units/modules/network/eos/test_eos_banner.py +++ b/test/units/modules/network/eos/test_eos_banner.py @@ -17,11 +17,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.eos import eos_banner -from .eos_module import TestEosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .eos_module import TestEosModule, load_fixture class TestEosBannerModule(TestEosModule): @@ -29,6 +28,8 @@ class TestEosBannerModule(TestEosModule): module = eos_banner def setUp(self): + super(TestEosBannerModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.eos.eos_banner.run_commands') self.run_commands = self.mock_run_commands.start() @@ -36,6 +37,8 @@ class TestEosBannerModule(TestEosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestEosBannerModule, self).tearDown() + self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/eos/test_eos_command.py b/test/units/modules/network/eos/test_eos_command.py index 02865d2f7e..0734147f43 100644 --- a/test/units/modules/network/eos/test_eos_command.py +++ b/test/units/modules/network/eos/test_eos_command.py @@ -23,7 +23,8 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.eos import eos_command -from .eos_module import TestEosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .eos_module import TestEosModule, load_fixture class TestEosCommandModule(TestEosModule): @@ -31,10 +32,12 @@ class TestEosCommandModule(TestEosModule): module = eos_command def setUp(self): + super(TestEosCommandModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.eos.eos_command.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestEosCommandModule, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None, transport='cli'): diff --git a/test/units/modules/network/eos/test_eos_config.py b/test/units/modules/network/eos/test_eos_config.py index 54033318c2..4a244b91f2 100644 --- a/test/units/modules/network/eos/test_eos_config.py +++ b/test/units/modules/network/eos/test_eos_config.py @@ -19,11 +19,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.eos import eos_config -from .eos_module import TestEosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .eos_module import TestEosModule, load_fixture class TestEosConfigModule(TestEosModule): @@ -31,6 +30,7 @@ class TestEosConfigModule(TestEosModule): module = eos_config def setUp(self): + super(TestEosConfigModule, self).setUp() self.mock_get_config = patch('ansible.modules.network.eos.eos_config.get_config') self.get_config = self.mock_get_config.start() @@ -38,6 +38,7 @@ class TestEosConfigModule(TestEosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestEosConfigModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/eos/test_eos_eapi.py b/test/units/modules/network/eos/test_eos_eapi.py index dfb4e83f4a..dd16ea60cc 100644 --- a/test/units/modules/network/eos/test_eos_eapi.py +++ b/test/units/modules/network/eos/test_eos_eapi.py @@ -19,11 +19,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.eos import eos_eapi -from .eos_module import TestEosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .eos_module import TestEosModule, load_fixture class TestEosEapiModule(TestEosModule): @@ -31,6 +30,8 @@ class TestEosEapiModule(TestEosModule): module = eos_eapi def setUp(self): + super(TestEosEapiModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.eos.eos_eapi.run_commands') self.run_commands = self.mock_run_commands.start() @@ -43,6 +44,8 @@ class TestEosEapiModule(TestEosModule): self.command_fixtures = {} def tearDown(self): + super(TestEosEapiModule, self).tearDown() + self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/eos/test_eos_system.py b/test/units/modules/network/eos/test_eos_system.py index 8cbf0335b1..f02687694a 100644 --- a/test/units/modules/network/eos/test_eos_system.py +++ b/test/units/modules/network/eos/test_eos_system.py @@ -19,11 +19,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.eos import eos_system -from .eos_module import TestEosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .eos_module import TestEosModule, load_fixture class TestEosSystemModule(TestEosModule): @@ -31,6 +30,8 @@ class TestEosSystemModule(TestEosModule): module = eos_system def setUp(self): + super(TestEosSystemModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.eos.eos_system.get_config') self.get_config = self.mock_get_config.start() @@ -38,6 +39,8 @@ class TestEosSystemModule(TestEosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestEosSystemModule, self).tearDown() + self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/eos/test_eos_user.py b/test/units/modules/network/eos/test_eos_user.py index 74cfb7bc04..ac08a2158c 100644 --- a/test/units/modules/network/eos/test_eos_user.py +++ b/test/units/modules/network/eos/test_eos_user.py @@ -17,11 +17,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.eos import eos_user -from .eos_module import TestEosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .eos_module import TestEosModule, load_fixture class TestEosUserModule(TestEosModule): @@ -29,6 +28,8 @@ class TestEosUserModule(TestEosModule): module = eos_user def setUp(self): + super(TestEosUserModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.eos.eos_user.get_config') self.get_config = self.mock_get_config.start() @@ -36,6 +37,8 @@ class TestEosUserModule(TestEosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestEosUserModule, self).tearDown() + self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/f5/test_bigip_asm_policy.py b/test/units/modules/network/f5/test_bigip_asm_policy.py index 226eabde04..72cac746a5 100644 --- a/test/units/modules/network/f5/test_bigip_asm_policy.py +++ b/test/units/modules/network/f5/test_bigip_asm_policy.py @@ -17,10 +17,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock, PropertyMock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_asm_policy import V1Parameters @@ -46,11 +45,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) with open(path) as f: diff --git a/test/units/modules/network/f5/test_bigip_command.py b/test/units/modules/network/f5/test_bigip_command.py index f4ff740195..39d4c2333d 100644 --- a/test/units/modules/network/f5/test_bigip_command.py +++ b/test/units/modules/network/f5/test_bigip_command.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_command import Parameters @@ -38,11 +37,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) with open(path) as f: diff --git a/test/units/modules/network/f5/test_bigip_config.py b/test/units/modules/network/f5/test_bigip_config.py index 228937d7bb..9cb02c7260 100644 --- a/test/units/modules/network/f5/test_bigip_config.py +++ b/test/units/modules/network/f5/test_bigip_config.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_config import Parameters @@ -38,11 +37,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) with open(path) as f: diff --git a/test/units/modules/network/f5/test_bigip_configsync_action.py b/test/units/modules/network/f5/test_bigip_configsync_action.py index 509eefaf8d..4ffc8cd6ea 100644 --- a/test/units/modules/network/f5/test_bigip_configsync_action.py +++ b/test/units/modules/network/f5/test_bigip_configsync_action.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_configsync_actions import Parameters @@ -38,11 +37,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_configsync_actions.py b/test/units/modules/network/f5/test_bigip_configsync_actions.py index 8729b40ade..b7269f7a53 100644 --- a/test/units/modules/network/f5/test_bigip_configsync_actions.py +++ b/test/units/modules/network/f5/test_bigip_configsync_actions.py @@ -30,9 +30,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_configsync_actions import Parameters @@ -50,11 +49,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_device_dns.py b/test/units/modules/network/f5/test_bigip_device_dns.py index 59679c1675..4cc44223a7 100644 --- a/test/units/modules/network/f5/test_bigip_device_dns.py +++ b/test/units/modules/network/f5/test_bigip_device_dns.py @@ -17,10 +17,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_device_dns import Parameters @@ -40,11 +39,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_device_trust.py b/test/units/modules/network/f5/test_bigip_device_trust.py index 10d1139e1e..b440753051 100644 --- a/test/units/modules/network/f5/test_bigip_device_trust.py +++ b/test/units/modules/network/f5/test_bigip_device_trust.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_device_trust import Parameters @@ -46,11 +45,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_gtm_facts.py b/test/units/modules/network/f5/test_bigip_gtm_facts.py index e4d2b89117..05a1c27e9d 100644 --- a/test/units/modules/network/f5/test_bigip_gtm_facts.py +++ b/test/units/modules/network/f5/test_bigip_gtm_facts.py @@ -16,10 +16,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.six import iteritems +from units.modules.utils import set_module_args try: from library.bigip_gtm_facts import Parameters @@ -63,11 +62,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_gtm_pool.py b/test/units/modules/network/f5/test_bigip_gtm_pool.py index 1bfdf4aa93..be2d4bb704 100644 --- a/test/units/modules/network/f5/test_bigip_gtm_pool.py +++ b/test/units/modules/network/f5/test_bigip_gtm_pool.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_gtm_pool import Parameters @@ -42,11 +41,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_gtm_wide_ip.py b/test/units/modules/network/f5/test_bigip_gtm_wide_ip.py index 46de8c03ba..908c0d12e3 100644 --- a/test/units/modules/network/f5/test_bigip_gtm_wide_ip.py +++ b/test/units/modules/network/f5/test_bigip_gtm_wide_ip.py @@ -17,10 +17,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_gtm_wide_ip import Parameters @@ -44,11 +43,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_hostname.py b/test/units/modules/network/f5/test_bigip_hostname.py index 49db8c341a..f29790c9db 100644 --- a/test/units/modules/network/f5/test_bigip_hostname.py +++ b/test/units/modules/network/f5/test_bigip_hostname.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_hostname import Parameters @@ -38,11 +37,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_iapp_service.py b/test/units/modules/network/f5/test_bigip_iapp_service.py index 8f7fd425c4..592123b83e 100644 --- a/test/units/modules/network/f5/test_bigip_iapp_service.py +++ b/test/units/modules/network/f5/test_bigip_iapp_service.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_iapp_service import Parameters @@ -38,11 +37,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_iapp_template.py b/test/units/modules/network/f5/test_bigip_iapp_template.py index 5378bd94f0..9d01243860 100644 --- a/test/units/modules/network/f5/test_bigip_iapp_template.py +++ b/test/units/modules/network/f5/test_bigip_iapp_template.py @@ -15,10 +15,9 @@ if sys.version_info < (2, 7): raise SkipTest("F5 Ansible modules require Python >= 2.7") from ansible.compat.tests import unittest -from ansible.module_utils import basic from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_iapp_template import Parameters @@ -38,11 +37,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_iapplx_package.py b/test/units/modules/network/f5/test_bigip_iapplx_package.py index 3ddc252cf7..4d44c7b6c0 100644 --- a/test/units/modules/network/f5/test_bigip_iapplx_package.py +++ b/test/units/modules/network/f5/test_bigip_iapplx_package.py @@ -15,10 +15,9 @@ if sys.version_info < (2, 7): raise SkipTest("F5 Ansible modules require Python >= 2.7") from ansible.compat.tests import unittest -from ansible.module_utils import basic from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_iapp_template import Parameters @@ -38,11 +37,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_irule.py b/test/units/modules/network/f5/test_bigip_irule.py index eb2974adb8..a6a26299b2 100644 --- a/test/units/modules/network/f5/test_bigip_irule.py +++ b/test/units/modules/network/f5/test_bigip_irule.py @@ -16,10 +16,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, mock_open, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.six import PY3 +from units.modules.utils import set_module_args try: from library.bigip_irule import Parameters @@ -43,11 +42,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_monitor_http.py b/test/units/modules/network/f5/test_bigip_monitor_http.py index 4ce0b28812..fc171d25ad 100644 --- a/test/units/modules/network/f5/test_bigip_monitor_http.py +++ b/test/units/modules/network/f5/test_bigip_monitor_http.py @@ -31,10 +31,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_monitor_http import Parameters @@ -54,11 +53,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_monitor_https.py b/test/units/modules/network/f5/test_bigip_monitor_https.py index 68144dae68..2b5f3bc53a 100644 --- a/test/units/modules/network/f5/test_bigip_monitor_https.py +++ b/test/units/modules/network/f5/test_bigip_monitor_https.py @@ -31,10 +31,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_monitor_https import Parameters @@ -54,11 +53,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_monitor_tcp.py b/test/units/modules/network/f5/test_bigip_monitor_tcp.py index 412f5e4736..eeac55ea56 100644 --- a/test/units/modules/network/f5/test_bigip_monitor_tcp.py +++ b/test/units/modules/network/f5/test_bigip_monitor_tcp.py @@ -31,10 +31,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_monitor_tcp import ParametersTcp @@ -64,11 +63,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_monitor_tcp_echo.py b/test/units/modules/network/f5/test_bigip_monitor_tcp_echo.py index 36baab4066..b3633c9a61 100644 --- a/test/units/modules/network/f5/test_bigip_monitor_tcp_echo.py +++ b/test/units/modules/network/f5/test_bigip_monitor_tcp_echo.py @@ -31,10 +31,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_monitor_tcp_echo import Parameters @@ -53,11 +52,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_monitor_tcp_half_open.py b/test/units/modules/network/f5/test_bigip_monitor_tcp_half_open.py index f2a4a228ac..6f67ddfdc2 100644 --- a/test/units/modules/network/f5/test_bigip_monitor_tcp_half_open.py +++ b/test/units/modules/network/f5/test_bigip_monitor_tcp_half_open.py @@ -31,10 +31,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_monitor_tcp_half_open import Parameters @@ -53,11 +52,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_partition.py b/test/units/modules/network/f5/test_bigip_partition.py index cd2ea1b019..23e9c8f0e8 100644 --- a/test/units/modules/network/f5/test_bigip_partition.py +++ b/test/units/modules/network/f5/test_bigip_partition.py @@ -30,9 +30,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_partition import Parameters @@ -52,11 +51,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_policy.py b/test/units/modules/network/f5/test_bigip_policy.py index 88b323a9c2..5f769ad4d5 100644 --- a/test/units/modules/network/f5/test_bigip_policy.py +++ b/test/units/modules/network/f5/test_bigip_policy.py @@ -30,9 +30,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_policy import Parameters @@ -56,11 +55,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_pool.py b/test/units/modules/network/f5/test_bigip_pool.py index a6b4277ba6..c34959127b 100644 --- a/test/units/modules/network/f5/test_bigip_pool.py +++ b/test/units/modules/network/f5/test_bigip_pool.py @@ -17,10 +17,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_pool import Parameters @@ -40,11 +39,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_provision.py b/test/units/modules/network/f5/test_bigip_provision.py index 35dba41099..9b575638b8 100644 --- a/test/units/modules/network/f5/test_bigip_provision.py +++ b/test/units/modules/network/f5/test_bigip_provision.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_provision import Parameters @@ -38,11 +37,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_qkview.py b/test/units/modules/network/f5/test_bigip_qkview.py index 9a7830aa53..55cd5e06a8 100644 --- a/test/units/modules/network/f5/test_bigip_qkview.py +++ b/test/units/modules/network/f5/test_bigip_qkview.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_qkview import Parameters @@ -42,11 +41,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_remote_syslog.py b/test/units/modules/network/f5/test_bigip_remote_syslog.py index f78c099fc0..51c0c894e1 100644 --- a/test/units/modules/network/f5/test_bigip_remote_syslog.py +++ b/test/units/modules/network/f5/test_bigip_remote_syslog.py @@ -19,6 +19,7 @@ from ansible.compat.tests.mock import patch, Mock from ansible.module_utils import basic from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_remote_syslog import Parameters @@ -46,11 +47,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_selfip.py b/test/units/modules/network/f5/test_bigip_selfip.py index 48266745e0..eb57109c84 100644 --- a/test/units/modules/network/f5/test_bigip_selfip.py +++ b/test/units/modules/network/f5/test_bigip_selfip.py @@ -31,10 +31,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_selfip import Parameters @@ -56,11 +55,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_snmp.py b/test/units/modules/network/f5/test_bigip_snmp.py index 7c86f7d62c..05fdda8a50 100644 --- a/test/units/modules/network/f5/test_bigip_snmp.py +++ b/test/units/modules/network/f5/test_bigip_snmp.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_snmp import Parameters @@ -38,11 +37,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_snmp_trap.py b/test/units/modules/network/f5/test_bigip_snmp_trap.py index 1251718471..9b7a011ac2 100644 --- a/test/units/modules/network/f5/test_bigip_snmp_trap.py +++ b/test/units/modules/network/f5/test_bigip_snmp_trap.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, DEFAULT, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_snmp_trap import NetworkedParameters @@ -44,11 +43,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_ssl_certificate.py b/test/units/modules/network/f5/test_bigip_ssl_certificate.py index 2eb33d9ea6..bdacfa2c99 100644 --- a/test/units/modules/network/f5/test_bigip_ssl_certificate.py +++ b/test/units/modules/network/f5/test_bigip_ssl_certificate.py @@ -15,10 +15,9 @@ if sys.version_info < (2, 7): raise SkipTest("F5 Ansible modules require Python >= 2.7") from ansible.compat.tests import unittest -from ansible.module_utils import basic from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_ssl_certificate import ArgumentSpec @@ -44,11 +43,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_ssl_key.py b/test/units/modules/network/f5/test_bigip_ssl_key.py index 477ca11e37..ee575868a3 100644 --- a/test/units/modules/network/f5/test_bigip_ssl_key.py +++ b/test/units/modules/network/f5/test_bigip_ssl_key.py @@ -15,10 +15,9 @@ if sys.version_info < (2, 7): raise SkipTest("F5 Ansible modules require Python >= 2.7") from ansible.compat.tests import unittest -from ansible.module_utils import basic from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_ssl_key import ArgumentSpec @@ -40,11 +39,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_ucs.py b/test/units/modules/network/f5/test_bigip_ucs.py index e4e69cc604..e819ca28c2 100644 --- a/test/units/modules/network/f5/test_bigip_ucs.py +++ b/test/units/modules/network/f5/test_bigip_ucs.py @@ -17,10 +17,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_ucs import Parameters @@ -44,11 +43,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_user.py b/test/units/modules/network/f5/test_bigip_user.py index ffbde0afa8..028c96252b 100644 --- a/test/units/modules/network/f5/test_bigip_user.py +++ b/test/units/modules/network/f5/test_bigip_user.py @@ -17,10 +17,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_user import Parameters @@ -44,11 +43,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_virtual_address.py b/test/units/modules/network/f5/test_bigip_virtual_address.py index eb75a7fb26..556878d9d2 100644 --- a/test/units/modules/network/f5/test_bigip_virtual_address.py +++ b/test/units/modules/network/f5/test_bigip_virtual_address.py @@ -16,9 +16,8 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client +from units.modules.utils import set_module_args try: from library.bigip_virtual_address import Parameters @@ -38,11 +37,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/f5/test_bigip_wait.py b/test/units/modules/network/f5/test_bigip_wait.py index 4bac673157..7fe4d06d05 100644 --- a/test/units/modules/network/f5/test_bigip_wait.py +++ b/test/units/modules/network/f5/test_bigip_wait.py @@ -31,10 +31,9 @@ if sys.version_info < (2, 7): from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, Mock -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import F5ModuleError +from units.modules.utils import set_module_args try: from library.bigip_wait import Parameters @@ -56,11 +55,6 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) diff --git a/test/units/modules/network/ios/ios_module.py b/test/units/modules/network/ios/ios_module.py index 35d6658725..f450c4ad26 100644 --- a/test/units/modules/network/ios/ios_module.py +++ b/test/units/modules/network/ios/ios_module.py @@ -22,16 +22,9 @@ __metaclass__ = type import os import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -54,15 +47,7 @@ def load_fixture(name): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestIosModule(unittest.TestCase): +class TestIosModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False): @@ -84,27 +69,16 @@ class TestIosModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/ios/test_ios_banner.py b/test/units/modules/network/ios/test_ios_banner.py index e464e5f74e..e4bd98109d 100644 --- a/test/units/modules/network/ios/test_ios_banner.py +++ b/test/units/modules/network/ios/test_ios_banner.py @@ -17,11 +17,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.ios import ios_banner -from .ios_module import TestIosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ios_module import TestIosModule, load_fixture class TestIosBannerModule(TestIosModule): @@ -29,6 +28,8 @@ class TestIosBannerModule(TestIosModule): module = ios_banner def setUp(self): + super(TestIosBannerModule, self).setUp() + self.mock_exec_command = patch('ansible.modules.network.ios.ios_banner.exec_command') self.exec_command = self.mock_exec_command.start() @@ -36,6 +37,7 @@ class TestIosBannerModule(TestIosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestIosBannerModule, self).tearDown() self.mock_exec_command.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/ios/test_ios_command.py b/test/units/modules/network/ios/test_ios_command.py index d415680801..45a8ddabdd 100644 --- a/test/units/modules/network/ios/test_ios_command.py +++ b/test/units/modules/network/ios/test_ios_command.py @@ -23,7 +23,8 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.ios import ios_command -from .ios_module import TestIosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ios_module import TestIosModule, load_fixture class TestIosCommandModule(TestIosModule): @@ -31,10 +32,13 @@ class TestIosCommandModule(TestIosModule): module = ios_command def setUp(self): + super(TestIosCommandModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.ios.ios_command.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestIosCommandModule, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/ios/test_ios_config.py b/test/units/modules/network/ios/test_ios_config.py index c6a5bcef46..2294bce3fe 100644 --- a/test/units/modules/network/ios/test_ios_config.py +++ b/test/units/modules/network/ios/test_ios_config.py @@ -20,11 +20,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.ios import ios_config -from .ios_module import TestIosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ios_module import TestIosModule, load_fixture class TestIosConfigModule(TestIosModule): @@ -32,6 +31,8 @@ class TestIosConfigModule(TestIosModule): module = ios_config def setUp(self): + super(TestIosConfigModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.ios.ios_config.get_config') self.get_config = self.mock_get_config.start() @@ -42,6 +43,7 @@ class TestIosConfigModule(TestIosModule): self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestIosConfigModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() self.mock_run_commands.stop() diff --git a/test/units/modules/network/ios/test_ios_logging.py b/test/units/modules/network/ios/test_ios_logging.py index 7138a0cd8d..91c0006798 100644 --- a/test/units/modules/network/ios/test_ios_logging.py +++ b/test/units/modules/network/ios/test_ios_logging.py @@ -25,7 +25,8 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.ios import ios_logging -from .ios_module import TestIosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ios_module import TestIosModule, load_fixture class TestIosLoggingModule(TestIosModule): @@ -33,6 +34,8 @@ class TestIosLoggingModule(TestIosModule): module = ios_logging def setUp(self): + super(TestIosLoggingModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.ios.ios_logging.get_config') self.get_config = self.mock_get_config.start() @@ -40,6 +43,8 @@ class TestIosLoggingModule(TestIosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestIosLoggingModule, self).tearDown() + self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/ios/test_ios_ping.py b/test/units/modules/network/ios/test_ios_ping.py index a045fcc626..8d1b2a1aee 100644 --- a/test/units/modules/network/ios/test_ios_ping.py +++ b/test/units/modules/network/ios/test_ios_ping.py @@ -22,7 +22,8 @@ __metaclass__ = type from ansible.compat.tests.mock import patch from ansible.modules.network.ios import ios_ping -from .ios_module import TestIosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ios_module import TestIosModule, load_fixture class TestIosPingModule(TestIosModule): @@ -30,10 +31,12 @@ class TestIosPingModule(TestIosModule): module = ios_ping def setUp(self): + super(TestIosPingModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.ios.ios_ping.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestIosPingModule, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/ios/test_ios_system.py b/test/units/modules/network/ios/test_ios_system.py index 4f2fa93a4a..3e37d8b3a1 100644 --- a/test/units/modules/network/ios/test_ios_system.py +++ b/test/units/modules/network/ios/test_ios_system.py @@ -20,11 +20,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.ios import ios_system -from .ios_module import TestIosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ios_module import TestIosModule, load_fixture class TestIosSystemModule(TestIosModule): @@ -32,6 +31,8 @@ class TestIosSystemModule(TestIosModule): module = ios_system def setUp(self): + super(TestIosSystemModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.ios.ios_system.get_config') self.get_config = self.mock_get_config.start() @@ -39,6 +40,7 @@ class TestIosSystemModule(TestIosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestIosSystemModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/ios/test_ios_user.py b/test/units/modules/network/ios/test_ios_user.py index e39c3e73a7..ee185eb7db 100644 --- a/test/units/modules/network/ios/test_ios_user.py +++ b/test/units/modules/network/ios/test_ios_user.py @@ -23,7 +23,8 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.ios import ios_user -from .ios_module import TestIosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ios_module import TestIosModule, load_fixture class TestIosUserModule(TestIosModule): @@ -31,6 +32,8 @@ class TestIosUserModule(TestIosModule): module = ios_user def setUp(self): + super(TestIosUserModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.ios.ios_user.get_config') self.get_config = self.mock_get_config.start() @@ -38,6 +41,7 @@ class TestIosUserModule(TestIosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestIosUserModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/ios/test_ios_vrf.py b/test/units/modules/network/ios/test_ios_vrf.py index 53f634b483..c7c3bc4122 100644 --- a/test/units/modules/network/ios/test_ios_vrf.py +++ b/test/units/modules/network/ios/test_ios_vrf.py @@ -20,11 +20,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.ios import ios_vrf -from .ios_module import TestIosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ios_module import TestIosModule, load_fixture class TestIosVrfModule(TestIosModule): @@ -32,6 +31,8 @@ class TestIosVrfModule(TestIosModule): module = ios_vrf def setUp(self): + super(TestIosVrfModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.ios.ios_vrf.get_config') self.get_config = self.mock_get_config.start() @@ -42,6 +43,7 @@ class TestIosVrfModule(TestIosModule): self.exec_command = self.mock_exec_command.start() def tearDown(self): + super(TestIosVrfModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() self.mock_exec_command.stop() diff --git a/test/units/modules/network/iosxr/iosxr_module.py b/test/units/modules/network/iosxr/iosxr_module.py index e4ecf5884a..a8229ca3df 100644 --- a/test/units/modules/network/iosxr/iosxr_module.py +++ b/test/units/modules/network/iosxr/iosxr_module.py @@ -22,16 +22,9 @@ __metaclass__ = type import os import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -54,15 +47,7 @@ def load_fixture(name): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestIosxrModule(unittest.TestCase): +class TestIosxrModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False): @@ -84,27 +69,16 @@ class TestIosxrModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/iosxr/test_iosxr_command.py b/test/units/modules/network/iosxr/test_iosxr_command.py index fcf1efd9d3..3f1a0e6aa4 100644 --- a/test/units/modules/network/iosxr/test_iosxr_command.py +++ b/test/units/modules/network/iosxr/test_iosxr_command.py @@ -19,11 +19,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.iosxr import iosxr_command -from .iosxr_module import TestIosxrModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .iosxr_module import TestIosxrModule, load_fixture class TestIosxrCommandModule(TestIosxrModule): @@ -31,10 +30,14 @@ class TestIosxrCommandModule(TestIosxrModule): module = iosxr_command def setUp(self): + super(TestIosxrCommandModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.iosxr.iosxr_command.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestIosxrCommandModule, self).tearDown() + self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/iosxr/test_iosxr_config.py b/test/units/modules/network/iosxr/test_iosxr_config.py index 5e2f696e3a..5d62639929 100644 --- a/test/units/modules/network/iosxr/test_iosxr_config.py +++ b/test/units/modules/network/iosxr/test_iosxr_config.py @@ -20,11 +20,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.iosxr import iosxr_config -from .iosxr_module import TestIosxrModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .iosxr_module import TestIosxrModule, load_fixture class TestIosxrConfigModule(TestIosxrModule): @@ -32,12 +31,16 @@ class TestIosxrConfigModule(TestIosxrModule): module = iosxr_config def setUp(self): + super(TestIosxrConfigModule, self).setUp() + self.patcher_get_config = patch('ansible.modules.network.iosxr.iosxr_config.get_config') self.mock_get_config = self.patcher_get_config.start() self.patcher_exec_command = patch('ansible.modules.network.iosxr.iosxr_config.load_config') self.mock_exec_command = self.patcher_exec_command.start() def tearDown(self): + super(TestIosxrConfigModule, self).tearDown() + self.patcher_get_config.stop() self.patcher_exec_command.stop() diff --git a/test/units/modules/network/iosxr/test_iosxr_facts.py b/test/units/modules/network/iosxr/test_iosxr_facts.py index c1433b3592..717f403009 100644 --- a/test/units/modules/network/iosxr/test_iosxr_facts.py +++ b/test/units/modules/network/iosxr/test_iosxr_facts.py @@ -22,7 +22,8 @@ __metaclass__ = type import json from ansible.compat.tests.mock import patch -from .iosxr_module import TestIosxrModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .iosxr_module import TestIosxrModule, load_fixture from ansible.modules.network.iosxr import iosxr_facts @@ -31,11 +32,15 @@ class TestIosxrFacts(TestIosxrModule): module = iosxr_facts def setUp(self): + super(TestIosxrFacts, self).setUp() + self.mock_run_commands = patch( 'ansible.modules.network.iosxr.iosxr_facts.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestIosxrFacts, self).tearDown() + self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/iosxr/test_iosxr_netconf.py b/test/units/modules/network/iosxr/test_iosxr_netconf.py index 544059d01c..7cde357718 100644 --- a/test/units/modules/network/iosxr/test_iosxr_netconf.py +++ b/test/units/modules/network/iosxr/test_iosxr_netconf.py @@ -21,7 +21,8 @@ __metaclass__ = type from ansible.compat.tests.mock import patch from ansible.modules.network.iosxr import iosxr_netconf -from .iosxr_module import TestIosxrModule, set_module_args +from units.modules.utils import set_module_args +from .iosxr_module import TestIosxrModule class TestIosxrNetconfModule(TestIosxrModule): @@ -29,6 +30,8 @@ class TestIosxrNetconfModule(TestIosxrModule): module = iosxr_netconf def setUp(self): + super(TestIosxrNetconfModule, self).setUp() + self.mock_exec_command = patch('ansible.modules.network.iosxr.iosxr_netconf.exec_command') self.exec_command = self.mock_exec_command.start() @@ -39,6 +42,7 @@ class TestIosxrNetconfModule(TestIosxrModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestIosxrNetconfModule, self).tearDown() self.mock_exec_command.stop() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/iosxr/test_iosxr_system.py b/test/units/modules/network/iosxr/test_iosxr_system.py index 83d928ffbe..de5029eb8f 100644 --- a/test/units/modules/network/iosxr/test_iosxr_system.py +++ b/test/units/modules/network/iosxr/test_iosxr_system.py @@ -19,10 +19,9 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch -from .iosxr_module import TestIosxrModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .iosxr_module import TestIosxrModule, load_fixture from ansible.modules.network.iosxr import iosxr_system @@ -31,6 +30,8 @@ class TestIosxrSystemModule(TestIosxrModule): module = iosxr_system def setUp(self): + super(TestIosxrSystemModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.iosxr.iosxr_system.get_config') self.get_config = self.mock_get_config.start() @@ -38,6 +39,8 @@ class TestIosxrSystemModule(TestIosxrModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestIosxrSystemModule, self).tearDown() + self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/iosxr/test_iosxr_user.py b/test/units/modules/network/iosxr/test_iosxr_user.py index 7f392e7a1c..2254249498 100644 --- a/test/units/modules/network/iosxr/test_iosxr_user.py +++ b/test/units/modules/network/iosxr/test_iosxr_user.py @@ -19,11 +19,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.iosxr import iosxr_user -from .iosxr_module import TestIosxrModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .iosxr_module import TestIosxrModule, load_fixture class TestIosxrUserModule(TestIosxrModule): @@ -31,6 +30,8 @@ class TestIosxrUserModule(TestIosxrModule): module = iosxr_user def setUp(self): + super(TestIosxrUserModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.iosxr.iosxr_user.get_config') self.get_config = self.mock_get_config.start() @@ -38,6 +39,8 @@ class TestIosxrUserModule(TestIosxrModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestIosxrUserModule, self).tearDown() + self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/ironware/ironware_module.py b/test/units/modules/network/ironware/ironware_module.py index 4a68cbbc54..a66a7754b4 100644 --- a/test/units/modules/network/ironware/ironware_module.py +++ b/test/units/modules/network/ironware/ironware_module.py @@ -22,16 +22,10 @@ __metaclass__ = type import os import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -54,15 +48,7 @@ def load_fixture(name): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestIronwareModule(unittest.TestCase): +class TestIronwareModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False): @@ -84,27 +70,16 @@ class TestIronwareModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/ironware/test_ironware_command.py b/test/units/modules/network/ironware/test_ironware_command.py index 9cb4c22acd..f7d53e0e04 100644 --- a/test/units/modules/network/ironware/test_ironware_command.py +++ b/test/units/modules/network/ironware/test_ironware_command.py @@ -19,11 +19,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.ironware import ironware_command -from .ironware_module import TestIronwareModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ironware_module import TestIronwareModule, load_fixture class TestIronwareCommandModule(TestIronwareModule): @@ -31,10 +30,12 @@ class TestIronwareCommandModule(TestIronwareModule): module = ironware_command def setUp(self): + super(TestIronwareCommandModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.ironware.ironware_command.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestIronwareCommandModule, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/ironware/test_ironware_config.py b/test/units/modules/network/ironware/test_ironware_config.py index c23c1a8b81..ad228db05a 100644 --- a/test/units/modules/network/ironware/test_ironware_config.py +++ b/test/units/modules/network/ironware/test_ironware_config.py @@ -24,7 +24,8 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.ironware import ironware_config -from .ironware_module import TestIronwareModule, load_fixture, set_module_args +from .ironware_module import TestIronwareModule, load_fixture +from units.modules.utils import set_module_args class TestIronwareConfigModule(TestIronwareModule): @@ -32,6 +33,8 @@ class TestIronwareConfigModule(TestIronwareModule): module = ironware_config def setUp(self): + super(TestIronwareConfigModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.ironware.ironware_config.get_config') self.get_config = self.mock_get_config.start() @@ -42,6 +45,7 @@ class TestIronwareConfigModule(TestIronwareModule): self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestIronwareConfigModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() self.mock_run_commands.stop() diff --git a/test/units/modules/network/ironware/test_ironware_facts.py b/test/units/modules/network/ironware/test_ironware_facts.py index f1e66f7bec..44792706d8 100644 --- a/test/units/modules/network/ironware/test_ironware_facts.py +++ b/test/units/modules/network/ironware/test_ironware_facts.py @@ -22,8 +22,9 @@ __metaclass__ = type import json from ansible.compat.tests.mock import patch -from .ironware_module import TestIronwareModule, load_fixture, set_module_args +from .ironware_module import TestIronwareModule, load_fixture from ansible.modules.network.ironware import ironware_facts +from units.modules.utils import set_module_args class TestIronwareFacts(TestIronwareModule): @@ -31,11 +32,13 @@ class TestIronwareFacts(TestIronwareModule): module = ironware_facts def setUp(self): + super(TestIronwareFacts, self).setUp() self.mock_run_commands = patch( 'ansible.modules.network.ironware.ironware_facts.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestIronwareFacts, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/junos/junos_module.py b/test/units/modules/network/junos/junos_module.py index 30fb4c7fe0..b0701b1fe2 100644 --- a/test/units/modules/network/junos/junos_module.py +++ b/test/units/modules/network/junos/junos_module.py @@ -27,16 +27,9 @@ try: except ImportError: from xml.etree.ElementTree import parse -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -63,15 +56,7 @@ def load_fixture(name, content='xml'): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestJunosModule(unittest.TestCase): +class TestJunosModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False, format='text'): @@ -87,27 +72,16 @@ class TestJunosModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/junos/test_junos_command.py b/test/units/modules/network/junos/test_junos_command.py index a647d669b0..fe8cc2f1ae 100644 --- a/test/units/modules/network/junos/test_junos_command.py +++ b/test/units/modules/network/junos/test_junos_command.py @@ -21,7 +21,8 @@ __metaclass__ = type from ansible.compat.tests.mock import patch from ansible.modules.network.junos import junos_command -from .junos_module import TestJunosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .junos_module import TestJunosModule, load_fixture RPC_CLI_MAP = { 'get-software-information': 'show version' @@ -33,10 +34,14 @@ class TestJunosCommandModule(TestJunosModule): module = junos_command def setUp(self): + super(TestJunosCommandModule, self).setUp() + self.mock_send_request = patch('ansible.modules.network.junos.junos_command.send_request') self.send_request = self.mock_send_request.start() def tearDown(self): + super(TestJunosCommandModule, self).tearDown() + self.mock_send_request.stop() def load_fixtures(self, commands=None, format='text', changed=False): diff --git a/test/units/modules/network/junos/test_junos_config.py b/test/units/modules/network/junos/test_junos_config.py index ece27ec1d8..17dcf0fe6a 100644 --- a/test/units/modules/network/junos/test_junos_config.py +++ b/test/units/modules/network/junos/test_junos_config.py @@ -22,7 +22,8 @@ __metaclass__ = type from ansible.compat.tests.mock import patch from ansible.modules.network.junos import junos_config -from .junos_module import TestJunosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .junos_module import TestJunosModule, load_fixture class TestJunosConfigModule(TestJunosModule): @@ -30,6 +31,8 @@ class TestJunosConfigModule(TestJunosModule): module = junos_config def setUp(self): + super(TestJunosConfigModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.junos.junos_config.get_configuration') self.get_config = self.mock_get_config.start() @@ -55,6 +58,7 @@ class TestJunosConfigModule(TestJunosModule): self.send_request = self.mock_send_request.start() def tearDown(self): + super(TestJunosConfigModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() self.mock_lock_configuration.stop() diff --git a/test/units/modules/network/junos/test_junos_facts.py b/test/units/modules/network/junos/test_junos_facts.py index 7e7d866d1d..c1032bd9dd 100644 --- a/test/units/modules/network/junos/test_junos_facts.py +++ b/test/units/modules/network/junos/test_junos_facts.py @@ -21,7 +21,8 @@ __metaclass__ = type from ansible.compat.tests.mock import patch from ansible.modules.network.junos import junos_facts -from .junos_module import TestJunosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .junos_module import TestJunosModule, load_fixture RPC_CLI_MAP = { 'get-software-information': 'show version', @@ -38,6 +39,8 @@ class TestJunosCommandModule(TestJunosModule): module = junos_facts def setUp(self): + super(TestJunosCommandModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.junos.junos_facts.get_configuration') self.get_config = self.mock_get_config.start() @@ -45,6 +48,7 @@ class TestJunosCommandModule(TestJunosModule): self.send_request = self.mock_send_request.start() def tearDown(self): + super(TestJunosCommandModule, self).tearDown() self.mock_send_request.stop() def load_fixtures(self, commands=None, format='text', changed=False): diff --git a/test/units/modules/network/junos/test_junos_netconf.py b/test/units/modules/network/junos/test_junos_netconf.py index 1bf7947f00..6d7abcd68f 100644 --- a/test/units/modules/network/junos/test_junos_netconf.py +++ b/test/units/modules/network/junos/test_junos_netconf.py @@ -21,7 +21,8 @@ __metaclass__ = type from ansible.compat.tests.mock import patch from ansible.modules.network.junos import junos_netconf -from .junos_module import TestJunosModule, set_module_args +from units.modules.utils import set_module_args +from .junos_module import TestJunosModule class TestJunosCommandModule(TestJunosModule): @@ -29,6 +30,8 @@ class TestJunosCommandModule(TestJunosModule): module = junos_netconf def setUp(self): + super(TestJunosCommandModule, self).setUp() + self.mock_exec_command = patch('ansible.modules.network.junos.junos_netconf.exec_command') self.exec_command = self.mock_exec_command.start() @@ -42,6 +45,7 @@ class TestJunosCommandModule(TestJunosModule): self.commit_configuration = self.mock_commit_configuration.start() def tearDown(self): + super(TestJunosCommandModule, self).tearDown() self.mock_exec_command.stop() self.mock_lock_configuration.stop() self.mock_unlock_configuration.stop() diff --git a/test/units/modules/network/junos/test_junos_package.py b/test/units/modules/network/junos/test_junos_package.py index 8bfc0ab197..31dd778f75 100644 --- a/test/units/modules/network/junos/test_junos_package.py +++ b/test/units/modules/network/junos/test_junos_package.py @@ -20,7 +20,8 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type from ansible.compat.tests.mock import patch, MagicMock -from .junos_module import TestJunosModule, set_module_args +from units.modules.utils import set_module_args +from .junos_module import TestJunosModule jnpr_mock = MagicMock() modules = { @@ -41,10 +42,10 @@ class TestJunosCommandModule(TestJunosModule): module = junos_package def setUp(self): - pass + super(TestJunosCommandModule, self).setUp() def tearDown(self): - pass + super(TestJunosCommandModule, self).tearDown() def test_junos_package_src(self): set_module_args(dict(src='junos-vsrx-12.1X46-D10.2-domestic.tgz')) diff --git a/test/units/modules/network/junos/test_junos_rpc.py b/test/units/modules/network/junos/test_junos_rpc.py index 5cd431e5e2..7ac8f89a0e 100644 --- a/test/units/modules/network/junos/test_junos_rpc.py +++ b/test/units/modules/network/junos/test_junos_rpc.py @@ -26,7 +26,8 @@ except ImportError: from ansible.compat.tests.mock import patch from ansible.modules.network.junos import junos_rpc -from .junos_module import TestJunosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .junos_module import TestJunosModule, load_fixture RPC_CLI_MAP = { @@ -43,10 +44,12 @@ class TestJunosCommandModule(TestJunosModule): module = junos_rpc def setUp(self): + super(TestJunosCommandModule, self).setUp() self.mock_send_request = patch('ansible.modules.network.junos.junos_rpc.send_request') self.send_request = self.mock_send_request.start() def tearDown(self): + super(TestJunosCommandModule, self).tearDown() self.mock_send_request.stop() def load_fixtures(self, commands=None, format='text', changed=False): diff --git a/test/units/modules/network/netscaler/netscaler_module.py b/test/units/modules/network/netscaler/netscaler_module.py index 9b2b2e83e1..f3715883de 100644 --- a/test/units/modules/network/netscaler/netscaler_module.py +++ b/test/units/modules/network/netscaler/netscaler_module.py @@ -1,10 +1,7 @@ import sys from ansible.compat.tests.mock import patch, Mock -from ansible.compat.tests import unittest -from ansible.module_utils import basic -import json -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase base_modules_mock = Mock() nitro_service_mock = Mock() @@ -30,40 +27,18 @@ base_modules_to_mock = { nitro_base_patcher = patch.dict(sys.modules, base_modules_to_mock) -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestModule(unittest.TestCase): +class TestModule(ModuleTestCase): def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def exited(self, changed=False): - def exit_json(*args, **kwargs): - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] return result diff --git a/test/units/modules/network/netscaler/test_netscaler_cs_action.py b/test/units/modules/network/netscaler/test_netscaler_cs_action.py index bea60ca940..cf30cf43a5 100644 --- a/test/units/modules/network/netscaler/test_netscaler_cs_action.py +++ b/test/units/modules/network/netscaler/test_netscaler_cs_action.py @@ -18,7 +18,8 @@ # from ansible.compat.tests.mock import patch, Mock, MagicMock, call -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher import sys @@ -53,12 +54,16 @@ class TestNetscalerCSActionModule(TestModule): cls.nitro_specific_patcher.stop() def setUp(self): + super(TestNetscalerCSActionModule, self).setUp() + self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerCSActionModule, self).tearDown() + self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_cs_policy.py b/test/units/modules/network/netscaler/test_netscaler_cs_policy.py index 42d5f2f8de..6bee0f0e1d 100644 --- a/test/units/modules/network/netscaler/test_netscaler_cs_policy.py +++ b/test/units/modules/network/netscaler/test_netscaler_cs_policy.py @@ -25,7 +25,8 @@ if sys.version_info[:2] != (2, 6): import requests -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher class TestNetscalerCSPolicyModule(TestModule): @@ -58,10 +59,14 @@ class TestNetscalerCSPolicyModule(TestModule): )) def setUp(self): + super(TestNetscalerCSPolicyModule, self).setUp() + self.nitro_base_patcher.start() self.nitro_specific_patcher.start() def tearDown(self): + super(TestNetscalerCSPolicyModule, self).tearDown() + self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_cs_vserver.py b/test/units/modules/network/netscaler/test_netscaler_cs_vserver.py index 45f1ac53d8..6c7927850c 100644 --- a/test/units/modules/network/netscaler/test_netscaler_cs_vserver.py +++ b/test/units/modules/network/netscaler/test_netscaler_cs_vserver.py @@ -18,7 +18,8 @@ # from ansible.compat.tests.mock import patch, Mock, MagicMock, call -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher import sys @@ -58,12 +59,16 @@ class TestNetscalerCSVserverModule(TestModule): cls.nitro_specific_patcher.stop() def setUp(self): + super(TestNetscalerCSVserverModule, self).setUp() + self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerCSVserverModule, self).tearDown() + self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_gslb_service.py b/test/units/modules/network/netscaler/test_netscaler_gslb_service.py index 0f57ae7fed..8b5eaa9892 100644 --- a/test/units/modules/network/netscaler/test_netscaler_gslb_service.py +++ b/test/units/modules/network/netscaler/test_netscaler_gslb_service.py @@ -18,7 +18,8 @@ # from ansible.compat.tests.mock import patch, Mock, MagicMock, call -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher import sys @@ -61,12 +62,16 @@ class TestNetscalerGSLBSiteModule(TestModule): cls.nitro_specific_patcher.stop() def setUp(self): + super(TestNetscalerGSLBSiteModule, self).setUp() + self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerGSLBSiteModule, self).tearDown() + self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_gslb_site.py b/test/units/modules/network/netscaler/test_netscaler_gslb_site.py index ba7413d879..0cec388acd 100644 --- a/test/units/modules/network/netscaler/test_netscaler_gslb_site.py +++ b/test/units/modules/network/netscaler/test_netscaler_gslb_site.py @@ -18,7 +18,8 @@ # from ansible.compat.tests.mock import patch, Mock, MagicMock, call -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher import sys @@ -51,12 +52,16 @@ class TestNetscalerGSLBSiteModule(TestModule): cls.nitro_specific_patcher.stop() def setUp(self): + super(TestNetscalerGSLBSiteModule, self).setUp() + self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerGSLBSiteModule, self).tearDown() + self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_gslb_vserver.py b/test/units/modules/network/netscaler/test_netscaler_gslb_vserver.py index 5087eb7dea..fa7ca41bfc 100644 --- a/test/units/modules/network/netscaler/test_netscaler_gslb_vserver.py +++ b/test/units/modules/network/netscaler/test_netscaler_gslb_vserver.py @@ -18,7 +18,8 @@ # from ansible.compat.tests.mock import patch, Mock, MagicMock, call -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher import sys @@ -55,12 +56,16 @@ class TestNetscalerGSLBVserverModule(TestModule): cls.nitro_specific_patcher.stop() def setUp(self): + super(TestNetscalerGSLBVserverModule, self).setUp() + self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerGSLBVserverModule, self).tearDown() + self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_lb_monitor.py b/test/units/modules/network/netscaler/test_netscaler_lb_monitor.py index d45ffdf7cc..e889106785 100644 --- a/test/units/modules/network/netscaler/test_netscaler_lb_monitor.py +++ b/test/units/modules/network/netscaler/test_netscaler_lb_monitor.py @@ -18,7 +18,8 @@ # from ansible.compat.tests.mock import patch, Mock, MagicMock, call -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher import sys @@ -51,12 +52,16 @@ class TestNetscalerLBVServerModule(TestModule): cls.nitro_specific_patcher.stop() def setUp(self): + super(TestNetscalerLBVServerModule, self).setUp() + self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerLBVServerModule, self).tearDown() + self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_lb_vserver.py b/test/units/modules/network/netscaler/test_netscaler_lb_vserver.py index 6becb5fb1f..f943bb89b3 100644 --- a/test/units/modules/network/netscaler/test_netscaler_lb_vserver.py +++ b/test/units/modules/network/netscaler/test_netscaler_lb_vserver.py @@ -18,7 +18,8 @@ # from ansible.compat.tests.mock import patch, Mock, MagicMock, call -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher import sys @@ -60,12 +61,15 @@ class TestNetscalerLBVServerModule(TestModule): cls.nitro_specific_patcher.stop() def setUp(self): + super(TestNetscalerLBVServerModule, self).setUp() self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerLBVServerModule, self).tearDown() + self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_save_config.py b/test/units/modules/network/netscaler/test_netscaler_save_config.py index 98bed1a7eb..d752dc8344 100644 --- a/test/units/modules/network/netscaler/test_netscaler_save_config.py +++ b/test/units/modules/network/netscaler/test_netscaler_save_config.py @@ -18,7 +18,8 @@ # from ansible.compat.tests.mock import patch, Mock, MagicMock, call -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher import sys @@ -42,9 +43,11 @@ class TestNetscalerSaveConfigModule(TestModule): cls.nitro_base_patcher.stop() def setUp(self): + super(TestNetscalerSaveConfigModule, self).setUp() self.nitro_base_patcher.start() def tearDown(self): + super(TestNetscalerSaveConfigModule, self).tearDown() self.nitro_base_patcher.stop() def test_graceful_nitro_error_on_login(self): diff --git a/test/units/modules/network/netscaler/test_netscaler_server.py b/test/units/modules/network/netscaler/test_netscaler_server.py index 7bd026ec11..32aafb4072 100644 --- a/test/units/modules/network/netscaler/test_netscaler_server.py +++ b/test/units/modules/network/netscaler/test_netscaler_server.py @@ -18,7 +18,8 @@ # from ansible.compat.tests.mock import patch, Mock, MagicMock, call -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher import sys @@ -53,12 +54,14 @@ class TestNetscalerServerModule(TestModule): cls.nitro_specific_patcher.stop() def setUp(self): + super(TestNetscalerServerModule, self).setUp() self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerServerModule, self).tearDown() self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_service.py b/test/units/modules/network/netscaler/test_netscaler_service.py index 2921d69ca4..2804583bd6 100644 --- a/test/units/modules/network/netscaler/test_netscaler_service.py +++ b/test/units/modules/network/netscaler/test_netscaler_service.py @@ -25,7 +25,8 @@ if sys.version_info[:2] != (2, 6): import requests -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher class TestNetscalerServiceModule(TestModule): @@ -65,12 +66,14 @@ class TestNetscalerServiceModule(TestModule): )) def setUp(self): + super(TestNetscalerServiceModule, self).setUp() self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerServiceModule, self).tearDown() self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_servicegroup.py b/test/units/modules/network/netscaler/test_netscaler_servicegroup.py index 05c56acb73..4f5c9f794a 100644 --- a/test/units/modules/network/netscaler/test_netscaler_servicegroup.py +++ b/test/units/modules/network/netscaler/test_netscaler_servicegroup.py @@ -25,7 +25,8 @@ if sys.version_info[:2] != (2, 6): import requests -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher class TestNetscalerServicegroupModule(TestModule): @@ -69,12 +70,14 @@ class TestNetscalerServicegroupModule(TestModule): )) def setUp(self): + super(TestNetscalerServicegroupModule, self).setUp() self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerServicegroupModule, self).tearDown() self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/netscaler/test_netscaler_ssl_certkey.py b/test/units/modules/network/netscaler/test_netscaler_ssl_certkey.py index 311a15aa81..3fe1e045fd 100644 --- a/test/units/modules/network/netscaler/test_netscaler_ssl_certkey.py +++ b/test/units/modules/network/netscaler/test_netscaler_ssl_certkey.py @@ -18,7 +18,8 @@ # from ansible.compat.tests.mock import patch, Mock, MagicMock, call -from .netscaler_module import TestModule, nitro_base_patcher, set_module_args +from units.modules.utils import set_module_args +from .netscaler_module import TestModule, nitro_base_patcher import sys @@ -53,12 +54,14 @@ class TestNetscalerSSLCertkeyModule(TestModule): cls.nitro_specific_patcher.stop() def setUp(self): + super(TestNetscalerSSLCertkeyModule, self).setUp() self.nitro_base_patcher.start() self.nitro_specific_patcher.start() # Setup minimal required arguments to pass AnsibleModule argument parsing def tearDown(self): + super(TestNetscalerSSLCertkeyModule, self).tearDown() self.nitro_base_patcher.stop() self.nitro_specific_patcher.stop() diff --git a/test/units/modules/network/nso/nso_module.py b/test/units/modules/network/nso/nso_module.py index 44ce5d5049..4397490310 100644 --- a/test/units/modules/network/nso/nso_module.py +++ b/test/units/modules/network/nso/nso_module.py @@ -20,21 +20,13 @@ from __future__ import (absolute_import, division, print_function) import os import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - def load_fixture(name): path = os.path.join(fixture_path, name) if path not in fixture_data: @@ -80,15 +72,7 @@ def mock_call(calls, url, data=None, headers=None, method=None): return result -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestNsoModule(unittest.TestCase): +class TestNsoModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, **kwargs): if failed: @@ -104,27 +88,16 @@ class TestNsoModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/nso/test_nso_config.py b/test/units/modules/network/nso/test_nso_config.py index 546688a0ce..b0eb6d66f9 100644 --- a/test/units/modules/network/nso/test_nso_config.py +++ b/test/units/modules/network/nso/test_nso_config.py @@ -22,6 +22,7 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.nso import nso_config +from units.modules.utils import set_module_args, AnsibleFailJson from . import nso_module from .nso_module import MockResponse @@ -46,11 +47,11 @@ class TestNsoConfig(nso_module.TestNsoModule): open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs) data = nso_module.load_fixture('config_config.json') - nso_module.set_module_args({ + set_module_args({ 'username': 'user', 'password': 'password', 'url': 'http://localhost:8080/jsonrpc', 'data': data }) - with self.assertRaises(SystemExit): + with self.assertRaises(AnsibleFailJson): self.execute_module(changed=False, changes=[], diffs=[]) self.assertEqual(0, len(calls)) @@ -75,7 +76,7 @@ class TestNsoConfig(nso_module.TestNsoModule): open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs) data = nso_module.load_fixture('config_empty_data.json') - nso_module.set_module_args({ + set_module_args({ 'username': 'user', 'password': 'password', 'url': 'http://localhost:8080/jsonrpc', 'data': data }) @@ -118,7 +119,7 @@ class TestNsoConfig(nso_module.TestNsoModule): open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs) data = nso_module.load_fixture('config_config.json') - nso_module.set_module_args({ + set_module_args({ 'username': 'user', 'password': 'password', 'url': 'http://localhost:8080/jsonrpc', 'data': data }) diff --git a/test/units/modules/network/nuage/nuage_module.py b/test/units/modules/network/nuage/nuage_module.py index 2a38ca3981..c27b5d3e8f 100644 --- a/test/units/modules/network/nuage/nuage_module.py +++ b/test/units/modules/network/nuage/nuage_module.py @@ -16,11 +16,8 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. -import json -from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import set_module_args as _set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase from nose.plugins.skip import SkipTest try: @@ -31,27 +28,15 @@ except ImportError: def set_module_args(args): - set_module_args_custom_auth(args=args, auth={ - 'api_username': 'csproot', - 'api_password': 'csproot', - 'api_enterprise': 'csp', - 'api_url': 'https://localhost:8443', - 'api_version': 'v5_0' - }) - - -def set_module_args_custom_auth(args, auth): - args['auth'] = auth - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass + if 'auth' not in args: + args['auth'] = { + 'api_username': 'csproot', + 'api_password': 'csproot', + 'api_enterprise': 'csp', + 'api_url': 'https://localhost:8443', + 'api_version': 'v5_0' + } + return _set_module_args(args) class MockNuageResponse(object): @@ -66,9 +51,10 @@ class MockNuageConnection(object): self.response = MockNuageResponse(status_code, reason, errors) -class TestNuageModule(unittest.TestCase): +class TestNuageModule(ModuleTestCase): def setUp(self): + super(TestNuageModule, self).setUp() def session_start(self): self._root_object = vsdk.NUMe() @@ -79,22 +65,6 @@ class TestNuageModule(unittest.TestCase): self.session_mock = patch('vspk.v5_0.NUVSDSession.start', new=session_start) self.session_mock.start() - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - self.fail_json_mock = patch('ansible.module_utils.basic.AnsibleModule.fail_json', new=fail_json) - self.fail_json_mock.start() - - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - self.exit_json_mock = patch('ansible.module_utils.basic.AnsibleModule.exit_json', new=exit_json) - self.exit_json_mock.start() - def tearDown(self): + super(TestNuageModule, self).tearDown() self.session_mock.stop() - self.fail_json_mock.stop() - self.exit_json_mock.stop() diff --git a/test/units/modules/network/nuage/test_nuage_vspk.py b/test/units/modules/network/nuage/test_nuage_vspk.py index f83f611b13..29abe49e84 100644 --- a/test/units/modules/network/nuage/test_nuage_vspk.py +++ b/test/units/modules/network/nuage/test_nuage_vspk.py @@ -30,7 +30,8 @@ except ImportError: raise SkipTest('Nuage Ansible modules requires the vspk and bambou python libraries') from ansible.compat.tests.mock import patch -from .nuage_module import AnsibleExitJson, AnsibleFailJson, MockNuageConnection, TestNuageModule, set_module_args, set_module_args_custom_auth +from units.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson +from .nuage_module import MockNuageConnection, TestNuageModule _LOOP_COUNTER = 0 @@ -173,25 +174,25 @@ class TestNuageVSPKModule(TestNuageModule): def tearDown(self): super(TestNuageVSPKModule, self).tearDown() - for patch in self.patches: - patch.stop() + for mock in self.patches: + mock.stop() def test_certificate_auth(self): - set_module_args_custom_auth( + set_module_args( args={ 'type': 'Enterprise', 'state': 'present', 'properties': { 'name': 'test-enterprise' + }, + 'auth': { + 'api_username': 'csproot', + 'api_certificate': '/dummy/location/certificate.pem', + 'api_key': '/dummy/location/key.pem', + 'api_enterprise': 'csp', + 'api_url': 'https://localhost:8443', + 'api_version': 'v5_0' } - }, - auth={ - 'api_username': 'csproot', - 'api_certificate': '/dummy/location/certificate.pem', - 'api_key': '/dummy/location/key.pem', - 'api_enterprise': 'csp', - 'api_url': 'https://localhost:8443', - 'api_version': 'v5_0' } ) @@ -874,16 +875,16 @@ class TestNuageVSPKModule(TestNuageModule): self.assertEqual(result['msg'], "Job ended in an error") def test_fail_auth(self): - set_module_args_custom_auth( + set_module_args( args={ 'type': 'Enterprise', - 'command': 'find' - }, - auth={ - 'api_username': 'csproot', - 'api_enterprise': 'csp', - 'api_url': 'https://localhost:8443', - 'api_version': 'v5_0' + 'command': 'find', + 'auth': { + 'api_username': 'csproot', + 'api_enterprise': 'csp', + 'api_url': 'https://localhost:8443', + 'api_version': 'v5_0' + } } ) @@ -896,17 +897,17 @@ class TestNuageVSPKModule(TestNuageModule): self.assertEqual(result['msg'], 'Missing api_password or api_certificate and api_key parameter in auth') def test_fail_version(self): - set_module_args_custom_auth( + set_module_args( args={ 'type': 'Enterprise', - 'command': 'find' - }, - auth={ - 'api_username': 'csproot', - 'api_password': 'csproot', - 'api_enterprise': 'csp', - 'api_url': 'https://localhost:8443', - 'api_version': 'v1_0' + 'command': 'find', + 'auth': { + 'api_username': 'csproot', + 'api_password': 'csproot', + 'api_enterprise': 'csp', + 'api_url': 'https://localhost:8443', + 'api_version': 'v1_0' + } } ) @@ -1197,16 +1198,16 @@ class TestNuageVSPKModule(TestNuageModule): self.assertEqual(result['msg'], 'Property fake is not valid for this type of entity') def test_input_auth_username(self): - set_module_args_custom_auth( + set_module_args( args={ 'type': 'Enterprise', - 'command': 'find' - }, - auth={ - 'api_password': 'csproot', - 'api_enterprise': 'csp', - 'api_url': 'https://localhost:8443', - 'api_version': 'v5_0' + 'command': 'find', + 'auth': { + 'api_password': 'csproot', + 'api_enterprise': 'csp', + 'api_url': 'https://localhost:8443', + 'api_version': 'v5_0' + } } ) @@ -1219,16 +1220,16 @@ class TestNuageVSPKModule(TestNuageModule): self.assertEqual(result['msg'], 'missing required arguments: api_username') def test_input_auth_enterprise(self): - set_module_args_custom_auth( + set_module_args( args={ 'type': 'Enterprise', - 'command': 'find' - }, - auth={ - 'api_username': 'csproot', - 'api_password': 'csproot', - 'api_url': 'https://localhost:8443', - 'api_version': 'v5_0' + 'command': 'find', + 'auth': { + 'api_username': 'csproot', + 'api_password': 'csproot', + 'api_url': 'https://localhost:8443', + 'api_version': 'v5_0' + } } ) @@ -1241,16 +1242,16 @@ class TestNuageVSPKModule(TestNuageModule): self.assertEqual(result['msg'], 'missing required arguments: api_enterprise') def test_input_auth_url(self): - set_module_args_custom_auth( + set_module_args( args={ 'type': 'Enterprise', - 'command': 'find' - }, - auth={ - 'api_username': 'csproot', - 'api_password': 'csproot', - 'api_enterprise': 'csp', - 'api_version': 'v5_0' + 'command': 'find', + 'auth': { + 'api_username': 'csproot', + 'api_password': 'csproot', + 'api_enterprise': 'csp', + 'api_version': 'v5_0' + } } ) @@ -1263,16 +1264,16 @@ class TestNuageVSPKModule(TestNuageModule): self.assertEqual(result['msg'], 'missing required arguments: api_url') def test_input_auth_version(self): - set_module_args_custom_auth( + set_module_args( args={ 'type': 'Enterprise', - 'command': 'find' - }, - auth={ - 'api_username': 'csproot', - 'api_password': 'csproot', - 'api_enterprise': 'csp', - 'api_url': 'https://localhost:8443', + 'command': 'find', + 'auth': { + 'api_username': 'csproot', + 'api_password': 'csproot', + 'api_enterprise': 'csp', + 'api_url': 'https://localhost:8443', + } } ) diff --git a/test/units/modules/network/nxos/nxos_module.py b/test/units/modules/network/nxos/nxos_module.py index a9546f8448..1948cfc0aa 100644 --- a/test/units/modules/network/nxos/nxos_module.py +++ b/test/units/modules/network/nxos/nxos_module.py @@ -22,18 +22,15 @@ __metaclass__ = type import os import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase +from units.modules.utils import set_module_args as _set_module_args def set_module_args(args): if 'provider' not in args: args['provider'] = {'transport': args.get('transport') or 'cli'} - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) + return _set_module_args(args) fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -59,15 +56,7 @@ def load_fixture(module_name, name, device=''): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestNxosModule(unittest.TestCase): +class TestNxosModule(ModuleTestCase): def execute_module_devices(self, failed=False, changed=False, commands=None, sort=True, defaults=False): module_name = self.module.__name__.rsplit('.', 1)[1] @@ -107,27 +96,16 @@ class TestNxosModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/nxos/test_nxos_acl.py b/test/units/modules/network/nxos/test_nxos_acl.py index 6c2163fa05..fce8207ef3 100644 --- a/test/units/modules/network/nxos/test_nxos_acl.py +++ b/test/units/modules/network/nxos/test_nxos_acl.py @@ -31,6 +31,8 @@ class TestNxosAclModule(TestNxosModule): module = nxos_acl def setUp(self): + super(TestNxosAclModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_acl.run_commands') self.run_commands = self.mock_run_commands.start() @@ -38,6 +40,7 @@ class TestNxosAclModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosAclModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_acl_interface.py b/test/units/modules/network/nxos/test_nxos_acl_interface.py index c4dc3591d6..8e71fa8c86 100644 --- a/test/units/modules/network/nxos/test_nxos_acl_interface.py +++ b/test/units/modules/network/nxos/test_nxos_acl_interface.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metacl_interfaceass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_acl_interface from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosAclInterfaceModule(TestNxosModule): module = nxos_acl_interface def setUp(self): + super(TestNxosAclInterfaceModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_acl_interface.run_commands') self.run_commands = self.mock_run_commands.start() @@ -38,6 +38,7 @@ class TestNxosAclInterfaceModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosAclInterfaceModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_banner.py b/test/units/modules/network/nxos/test_nxos_banner.py index 145b4bd57e..7c0644a38e 100644 --- a/test/units/modules/network/nxos/test_nxos_banner.py +++ b/test/units/modules/network/nxos/test_nxos_banner.py @@ -17,11 +17,9 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_banner -from .nxos_module import TestNxosModule, load_fixture, set_module_args +from .nxos_module import TestNxosModule, set_module_args class TestNxosBannerModule(TestNxosModule): @@ -29,6 +27,7 @@ class TestNxosBannerModule(TestNxosModule): module = nxos_banner def setUp(self): + super(TestNxosBannerModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_banner.run_commands') self.run_commands = self.mock_run_commands.start() @@ -36,6 +35,7 @@ class TestNxosBannerModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosBannerModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_bgp.py b/test/units/modules/network/nxos/test_nxos_bgp.py index 275b3bdbb2..b39508be0b 100644 --- a/test/units/modules/network/nxos/test_nxos_bgp.py +++ b/test/units/modules/network/nxos/test_nxos_bgp.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_bgp from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosBgpModule(TestNxosModule): module = nxos_bgp def setUp(self): + super(TestNxosBgpModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_bgp.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosBgpModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosBgpModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_bgp_af.py b/test/units/modules/network/nxos/test_nxos_bgp_af.py index b3c8bb616f..2708d59660 100644 --- a/test/units/modules/network/nxos/test_nxos_bgp_af.py +++ b/test/units/modules/network/nxos/test_nxos_bgp_af.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_bgp_af from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosBgpAfModule(TestNxosModule): module = nxos_bgp_af def setUp(self): + super(TestNxosBgpAfModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_bgp_af.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosBgpAfModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosBgpAfModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_bgp_neighbor.py b/test/units/modules/network/nxos/test_nxos_bgp_neighbor.py index 60b151f9f8..ffbd7b0a39 100644 --- a/test/units/modules/network/nxos/test_nxos_bgp_neighbor.py +++ b/test/units/modules/network/nxos/test_nxos_bgp_neighbor.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_bgp_neighbor from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosBgpNeighborModule(TestNxosModule): module = nxos_bgp_neighbor def setUp(self): + super(TestNxosBgpNeighborModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_bgp_neighbor.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosBgpNeighborModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosBgpNeighborModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_bgp_neighbor_af.py b/test/units/modules/network/nxos/test_nxos_bgp_neighbor_af.py index bbaf1e4f14..7a0b1219ce 100644 --- a/test/units/modules/network/nxos/test_nxos_bgp_neighbor_af.py +++ b/test/units/modules/network/nxos/test_nxos_bgp_neighbor_af.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_bgp_neighbor_af from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosBgpNeighborAfModule(TestNxosModule): module = nxos_bgp_neighbor_af def setUp(self): + super(TestNxosBgpNeighborAfModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_bgp_neighbor_af.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosBgpNeighborAfModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosBgpNeighborAfModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_command.py b/test/units/modules/network/nxos/test_nxos_command.py index ee073ac003..6f3e0e14c3 100644 --- a/test/units/modules/network/nxos/test_nxos_command.py +++ b/test/units/modules/network/nxos/test_nxos_command.py @@ -31,10 +31,13 @@ class TestNxosCommandModule(TestNxosModule): module = nxos_command def setUp(self): + super(TestNxosCommandModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_command.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestNxosCommandModule, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None, device=''): diff --git a/test/units/modules/network/nxos/test_nxos_config.py b/test/units/modules/network/nxos/test_nxos_config.py index 42e178ab94..e403eced05 100644 --- a/test/units/modules/network/nxos/test_nxos_config.py +++ b/test/units/modules/network/nxos/test_nxos_config.py @@ -21,8 +21,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_config from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -33,6 +31,8 @@ class TestNxosConfigModule(TestNxosModule): module = nxos_config def setUp(self): + super(TestNxosConfigModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.nxos.nxos_config.get_config') self.get_config = self.mock_get_config.start() @@ -40,6 +40,7 @@ class TestNxosConfigModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosConfigModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_evpn_global.py b/test/units/modules/network/nxos/test_nxos_evpn_global.py index 1adce68b82..48db78b5b9 100644 --- a/test/units/modules/network/nxos/test_nxos_evpn_global.py +++ b/test/units/modules/network/nxos/test_nxos_evpn_global.py @@ -19,7 +19,6 @@ # Make coding more python3-ish from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_evpn_global @@ -31,6 +30,7 @@ class TestNxosEvpnGlobalModule(TestNxosModule): module = nxos_evpn_global def setUp(self): + super(TestNxosEvpnGlobalModule, self).setUp() self.mock_get_config = patch('ansible.modules.network.nxos.nxos_evpn_global.get_config') self.get_config = self.mock_get_config.start() @@ -38,6 +38,7 @@ class TestNxosEvpnGlobalModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosEvpnGlobalModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_evpn_vni.py b/test/units/modules/network/nxos/test_nxos_evpn_vni.py index 8ee0aad3f5..28707fcbfc 100644 --- a/test/units/modules/network/nxos/test_nxos_evpn_vni.py +++ b/test/units/modules/network/nxos/test_nxos_evpn_vni.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_evpn_vni from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosEvpnVniModule(TestNxosModule): module = nxos_evpn_vni def setUp(self): + super(TestNxosEvpnVniModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_evpn_vni.run_commands') self.run_commands = self.mock_run_commands.start() @@ -41,6 +41,7 @@ class TestNxosEvpnVniModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosEvpnVniModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_feature.py b/test/units/modules/network/nxos/test_nxos_feature.py index 60fd4aab1f..e4194bae42 100644 --- a/test/units/modules/network/nxos/test_nxos_feature.py +++ b/test/units/modules/network/nxos/test_nxos_feature.py @@ -31,6 +31,7 @@ class TestNxosFeatureModule(TestNxosModule): module = nxos_feature def setUp(self): + super(TestNxosFeatureModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_feature.run_commands') self.run_commands = self.mock_run_commands.start() @@ -38,6 +39,7 @@ class TestNxosFeatureModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosFeatureModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_hsrp.py b/test/units/modules/network/nxos/test_nxos_hsrp.py index d60d5e26e4..5812cdfdb9 100644 --- a/test/units/modules/network/nxos/test_nxos_hsrp.py +++ b/test/units/modules/network/nxos/test_nxos_hsrp.py @@ -19,11 +19,9 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_hsrp -from .nxos_module import TestNxosModule, load_fixture, set_module_args +from .nxos_module import TestNxosModule, set_module_args class TestNxosHsrpModule(TestNxosModule): @@ -31,6 +29,7 @@ class TestNxosHsrpModule(TestNxosModule): module = nxos_hsrp def setUp(self): + super(TestNxosHsrpModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_hsrp.run_commands') self.run_commands = self.mock_run_commands.start() @@ -38,6 +37,7 @@ class TestNxosHsrpModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosHsrpModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_interface.py b/test/units/modules/network/nxos/test_nxos_interface.py index b9e14f1abb..f0dc10b239 100644 --- a/test/units/modules/network/nxos/test_nxos_interface.py +++ b/test/units/modules/network/nxos/test_nxos_interface.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_interface from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,7 @@ class TestNxosInterfaceModule(TestNxosModule): module = nxos_interface def setUp(self): + super(TestNxosInterfaceModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_interface.run_commands') self.run_commands = self.mock_run_commands.start() @@ -38,6 +37,7 @@ class TestNxosInterfaceModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosInterfaceModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_interface_ospf.py b/test/units/modules/network/nxos/test_nxos_interface_ospf.py index f47d2f9cf2..78b2b41195 100644 --- a/test/units/modules/network/nxos/test_nxos_interface_ospf.py +++ b/test/units/modules/network/nxos/test_nxos_interface_ospf.py @@ -29,6 +29,8 @@ class TestNxosInterfaceOspfModule(TestNxosModule): module = nxos_interface_ospf def setUp(self): + super(TestNxosInterfaceOspfModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.nxos.nxos_interface_ospf.get_config') self.get_config = self.mock_get_config.start() @@ -36,6 +38,7 @@ class TestNxosInterfaceOspfModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosInterfaceOspfModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_ip_interface.py b/test/units/modules/network/nxos/test_nxos_ip_interface.py index 96d95c34d4..2e1e5e1b12 100644 --- a/test/units/modules/network/nxos/test_nxos_ip_interface.py +++ b/test/units/modules/network/nxos/test_nxos_ip_interface.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_ip_interface from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosIPInterfaceModule(TestNxosModule): module = nxos_ip_interface def setUp(self): + super(TestNxosIPInterfaceModule, self).setUp() + self.mock_get_interface_mode = patch( 'ansible.modules.network.nxos.nxos_ip_interface.get_interface_mode') self.get_interface_mode = self.mock_get_interface_mode.start() @@ -43,6 +43,7 @@ class TestNxosIPInterfaceModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosIPInterfaceModule, self).tearDown() self.mock_get_interface_mode.stop() self.mock_send_show_command.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_nxapi.py b/test/units/modules/network/nxos/test_nxos_nxapi.py index 9b4e089a32..36754dc85e 100644 --- a/test/units/modules/network/nxos/test_nxos_nxapi.py +++ b/test/units/modules/network/nxos/test_nxos_nxapi.py @@ -19,9 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json -import os - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_nxapi from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -32,6 +29,8 @@ class TestNxosNxapiModule(TestNxosModule): module = nxos_nxapi def setUp(self): + super(TestNxosNxapiModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_nxapi.run_commands') self.run_commands = self.mock_run_commands.start() @@ -39,6 +38,7 @@ class TestNxosNxapiModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosNxapiModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_ospf.py b/test/units/modules/network/nxos/test_nxos_ospf.py index c2652e4b30..38374e8e70 100644 --- a/test/units/modules/network/nxos/test_nxos_ospf.py +++ b/test/units/modules/network/nxos/test_nxos_ospf.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_ospf from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosOspfModule(TestNxosModule): module = nxos_ospf def setUp(self): + super(TestNxosOspfModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_ospf.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosOspfModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosOspfModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_ospf_vrf.py b/test/units/modules/network/nxos/test_nxos_ospf_vrf.py index 9fac7471ca..2258d9c67d 100644 --- a/test/units/modules/network/nxos/test_nxos_ospf_vrf.py +++ b/test/units/modules/network/nxos/test_nxos_ospf_vrf.py @@ -19,11 +19,9 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_ospf_vrf -from .nxos_module import TestNxosModule, load_fixture, set_module_args +from .nxos_module import TestNxosModule, set_module_args class TestNxosOspfVrfModule(TestNxosModule): @@ -31,6 +29,8 @@ class TestNxosOspfVrfModule(TestNxosModule): module = nxos_ospf_vrf def setUp(self): + super(TestNxosOspfVrfModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_ospf_vrf.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosOspfVrfModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosOspfVrfModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_overlay_global.py b/test/units/modules/network/nxos/test_nxos_overlay_global.py index 2f05077c09..c4ef81d74c 100644 --- a/test/units/modules/network/nxos/test_nxos_overlay_global.py +++ b/test/units/modules/network/nxos/test_nxos_overlay_global.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_overlay_global from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,7 @@ class TestNxosOverlayGlobalModule(TestNxosModule): module = nxos_overlay_global def setUp(self): + super(TestNxosOverlayGlobalModule, self).setUp() self.mock_load_config = patch('ansible.modules.network.nxos.nxos_overlay_global.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +37,7 @@ class TestNxosOverlayGlobalModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosOverlayGlobalModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_pim.py b/test/units/modules/network/nxos/test_nxos_pim.py index c1326bd7db..52bf64c7d8 100644 --- a/test/units/modules/network/nxos/test_nxos_pim.py +++ b/test/units/modules/network/nxos/test_nxos_pim.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import os - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_pim from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosPimModule(TestNxosModule): module = nxos_pim def setUp(self): + super(TestNxosPimModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.nxos.nxos_pim.get_config') self.get_config = self.mock_get_config.start() @@ -38,6 +38,7 @@ class TestNxosPimModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosPimModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_pim_interface.py b/test/units/modules/network/nxos/test_nxos_pim_interface.py index b75c270366..0de3ea3d5d 100644 --- a/test/units/modules/network/nxos/test_nxos_pim_interface.py +++ b/test/units/modules/network/nxos/test_nxos_pim_interface.py @@ -29,6 +29,8 @@ class TestNxosIPInterfaceModule(TestNxosModule): module = nxos_pim_interface def setUp(self): + super(TestNxosIPInterfaceModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.nxos.nxos_pim_interface.get_config') self.get_config = self.mock_get_config.start() @@ -39,6 +41,7 @@ class TestNxosIPInterfaceModule(TestNxosModule): self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestNxosIPInterfaceModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() self.mock_run_commands.stop() diff --git a/test/units/modules/network/nxos/test_nxos_pim_rp_address.py b/test/units/modules/network/nxos/test_nxos_pim_rp_address.py index 4af8865494..608bf06ef3 100644 --- a/test/units/modules/network/nxos/test_nxos_pim_rp_address.py +++ b/test/units/modules/network/nxos/test_nxos_pim_rp_address.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_pim_rp_address from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosPimRpAddressModule(TestNxosModule): module = nxos_pim_rp_address def setUp(self): + super(TestNxosPimRpAddressModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_pim_rp_address.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosPimRpAddressModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosPimRpAddressModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_portchannel.py b/test/units/modules/network/nxos/test_nxos_portchannel.py index db4defade8..32b3a04ed6 100644 --- a/test/units/modules/network/nxos/test_nxos_portchannel.py +++ b/test/units/modules/network/nxos/test_nxos_portchannel.py @@ -19,11 +19,9 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_portchannel -from .nxos_module import TestNxosModule, load_fixture, set_module_args +from .nxos_module import TestNxosModule, set_module_args class TestNxosPortchannelModule(TestNxosModule): @@ -31,6 +29,8 @@ class TestNxosPortchannelModule(TestNxosModule): module = nxos_portchannel def setUp(self): + super(TestNxosPortchannelModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_portchannel.run_commands') self.run_commands = self.mock_run_commands.start() @@ -41,6 +41,7 @@ class TestNxosPortchannelModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosPortchannelModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_static_route.py b/test/units/modules/network/nxos/test_nxos_static_route.py index 00abfa4db2..8d126d806c 100644 --- a/test/units/modules/network/nxos/test_nxos_static_route.py +++ b/test/units/modules/network/nxos/test_nxos_static_route.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_static_route from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosStaticRouteModule(TestNxosModule): module = nxos_static_route def setUp(self): + super(TestNxosStaticRouteModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_static_route.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosStaticRouteModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosStaticRouteModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_switchport.py b/test/units/modules/network/nxos/test_nxos_switchport.py index de302f3ff4..ef05f7b6e0 100644 --- a/test/units/modules/network/nxos/test_nxos_switchport.py +++ b/test/units/modules/network/nxos/test_nxos_switchport.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import os - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_switchport from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosSwitchportModule(TestNxosModule): module = nxos_switchport def setUp(self): + super(TestNxosSwitchportModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_switchport.run_commands') self.run_commands = self.mock_run_commands.start() @@ -38,6 +38,7 @@ class TestNxosSwitchportModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosSwitchportModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_system.py b/test/units/modules/network/nxos/test_nxos_system.py index 18e2042b3c..44a1b20f50 100644 --- a/test/units/modules/network/nxos/test_nxos_system.py +++ b/test/units/modules/network/nxos/test_nxos_system.py @@ -19,7 +19,6 @@ # Make coding more python3-ish from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_system @@ -31,6 +30,8 @@ class TestNxosSystemModule(TestNxosModule): module = nxos_system def setUp(self): + super(TestNxosSystemModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.nxos.nxos_system.get_config') self.get_config = self.mock_get_config.start() @@ -38,6 +39,7 @@ class TestNxosSystemModule(TestNxosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestNxosSystemModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_vlan.py b/test/units/modules/network/nxos/test_nxos_vlan.py index ab93983033..16806407f9 100644 --- a/test/units/modules/network/nxos/test_nxos_vlan.py +++ b/test/units/modules/network/nxos/test_nxos_vlan.py @@ -31,6 +31,8 @@ class TestNxosVlanModule(TestNxosModule): module = nxos_vlan def setUp(self): + super(TestNxosVlanModule, self).setUp() + self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_vlan.run_commands') self.run_commands = self.mock_run_commands.start() @@ -41,6 +43,7 @@ class TestNxosVlanModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosVlanModule, self).tearDown() self.mock_run_commands.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_vpc.py b/test/units/modules/network/nxos/test_nxos_vpc.py index 4e658a22a9..e524859190 100644 --- a/test/units/modules/network/nxos/test_nxos_vpc.py +++ b/test/units/modules/network/nxos/test_nxos_vpc.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import os - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_vpc from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosVpcModule(TestNxosModule): module = nxos_vpc def setUp(self): + super(TestNxosVpcModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_vpc.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosVpcModule(TestNxosModule): self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestNxosVpcModule, self).tearDown() self.mock_load_config.stop() self.mock_run_commands.stop() diff --git a/test/units/modules/network/nxos/test_nxos_vpc_interface.py b/test/units/modules/network/nxos/test_nxos_vpc_interface.py index 3e25ada534..5c85e01909 100644 --- a/test/units/modules/network/nxos/test_nxos_vpc_interface.py +++ b/test/units/modules/network/nxos/test_nxos_vpc_interface.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import os - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_vpc_interface from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosVpcModule(TestNxosModule): module = nxos_vpc_interface def setUp(self): + super(TestNxosVpcModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_vpc_interface.load_config') self.load_config = self.mock_load_config.start() @@ -41,6 +41,7 @@ class TestNxosVpcModule(TestNxosModule): self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestNxosVpcModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() self.mock_run_commands.stop() diff --git a/test/units/modules/network/nxos/test_nxos_vrf.py b/test/units/modules/network/nxos/test_nxos_vrf.py index 71b072476e..036309e33e 100644 --- a/test/units/modules/network/nxos/test_nxos_vrf.py +++ b/test/units/modules/network/nxos/test_nxos_vrf.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import os - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_vrf from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosVrfModule(TestNxosModule): module = nxos_vrf def setUp(self): + super(TestNxosVrfModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_vrf.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosVrfModule(TestNxosModule): self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestNxosVrfModule, self).tearDown() self.mock_load_config.stop() self.mock_run_commands.stop() diff --git a/test/units/modules/network/nxos/test_nxos_vrf_af.py b/test/units/modules/network/nxos/test_nxos_vrf_af.py index 42f985bdbf..834f75c0f3 100644 --- a/test/units/modules/network/nxos/test_nxos_vrf_af.py +++ b/test/units/modules/network/nxos/test_nxos_vrf_af.py @@ -19,11 +19,9 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_vrf_af -from .nxos_module import TestNxosModule, load_fixture, set_module_args +from .nxos_module import TestNxosModule, set_module_args class TestNxosVrfafModule(TestNxosModule): @@ -31,6 +29,8 @@ class TestNxosVrfafModule(TestNxosModule): module = nxos_vrf_af def setUp(self): + super(TestNxosVrfafModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_vrf_af.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosVrfafModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosVrfafModule, self).tearDown() self.mock_load_config.stop() self.mock_get_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_vxlan_vtep.py b/test/units/modules/network/nxos/test_nxos_vxlan_vtep.py index db6fa11d78..7b925c4653 100644 --- a/test/units/modules/network/nxos/test_nxos_vxlan_vtep.py +++ b/test/units/modules/network/nxos/test_nxos_vxlan_vtep.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_vxlan_vtep from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosVxlanVtepVniModule(TestNxosModule): module = nxos_vxlan_vtep def setUp(self): + super(TestNxosVxlanVtepVniModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_vxlan_vtep.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosVxlanVtepVniModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosVxlanVtepVniModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/nxos/test_nxos_vxlan_vtep_vni.py b/test/units/modules/network/nxos/test_nxos_vxlan_vtep_vni.py index c96e436dc9..ca445e40b1 100644 --- a/test/units/modules/network/nxos/test_nxos_vxlan_vtep_vni.py +++ b/test/units/modules/network/nxos/test_nxos_vxlan_vtep_vni.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.nxos import nxos_vxlan_vtep_vni from .nxos_module import TestNxosModule, load_fixture, set_module_args @@ -31,6 +29,8 @@ class TestNxosVxlanVtepVniModule(TestNxosModule): module = nxos_vxlan_vtep_vni def setUp(self): + super(TestNxosVxlanVtepVniModule, self).setUp() + self.mock_load_config = patch('ansible.modules.network.nxos.nxos_vxlan_vtep_vni.load_config') self.load_config = self.mock_load_config.start() @@ -38,6 +38,7 @@ class TestNxosVxlanVtepVniModule(TestNxosModule): self.get_config = self.mock_get_config.start() def tearDown(self): + super(TestNxosVxlanVtepVniModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/ovs/ovs_module.py b/test/units/modules/network/ovs/ovs_module.py index 5360e7a864..fe49809d50 100644 --- a/test/units/modules/network/ovs/ovs_module.py +++ b/test/units/modules/network/ovs/ovs_module.py @@ -22,16 +22,9 @@ __metaclass__ = type import os import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -54,15 +47,7 @@ def load_fixture(name): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestOpenVSwitchModule(unittest.TestCase): +class TestOpenVSwitchModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, commands=None, test_name=None): @@ -82,27 +67,16 @@ class TestOpenVSwitchModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/network/ovs/test_openvswitch_bridge.py b/test/units/modules/network/ovs/test_openvswitch_bridge.py index 9bef615591..0e39572bc6 100644 --- a/test/units/modules/network/ovs/test_openvswitch_bridge.py +++ b/test/units/modules/network/ovs/test_openvswitch_bridge.py @@ -20,11 +20,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.ovs import openvswitch_bridge -from .ovs_module import TestOpenVSwitchModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ovs_module import TestOpenVSwitchModule, load_fixture test_name_side_effect_matrix = { 'test_openvswitch_bridge_absent_idempotent': [ @@ -86,6 +85,8 @@ class TestOpenVSwitchBridgeModule(TestOpenVSwitchModule): module = openvswitch_bridge def setUp(self): + super(TestOpenVSwitchBridgeModule, self).setUp() + self.mock_run_command = ( patch('ansible.module_utils.basic.AnsibleModule.run_command')) self.run_command = self.mock_run_command.start() @@ -94,6 +95,8 @@ class TestOpenVSwitchBridgeModule(TestOpenVSwitchModule): self.get_bin_path = self.mock_get_bin_path.start() def tearDown(self): + super(TestOpenVSwitchBridgeModule, self).tearDown() + self.mock_run_command.stop() self.mock_get_bin_path.stop() diff --git a/test/units/modules/network/ovs/test_openvswitch_db.py b/test/units/modules/network/ovs/test_openvswitch_db.py index eb55c0cceb..b99924c4b2 100644 --- a/test/units/modules/network/ovs/test_openvswitch_db.py +++ b/test/units/modules/network/ovs/test_openvswitch_db.py @@ -20,11 +20,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.ovs import openvswitch_db -from .ovs_module import TestOpenVSwitchModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ovs_module import TestOpenVSwitchModule, load_fixture test_name_side_effect_matrix = { 'test_openvswitch_db_absent_idempotent': [ @@ -50,6 +49,8 @@ class TestOpenVSwitchDBModule(TestOpenVSwitchModule): module = openvswitch_db def setUp(self): + super(TestOpenVSwitchDBModule, self).setUp() + self.mock_run_command = ( patch('ansible.module_utils.basic.AnsibleModule.run_command')) self.run_command = self.mock_run_command.start() @@ -58,6 +59,8 @@ class TestOpenVSwitchDBModule(TestOpenVSwitchModule): self.get_bin_path = self.mock_get_bin_path.start() def tearDown(self): + super(TestOpenVSwitchDBModule, self).tearDown() + self.mock_run_command.stop() self.mock_get_bin_path.stop() diff --git a/test/units/modules/network/ovs/test_openvswitch_port.py b/test/units/modules/network/ovs/test_openvswitch_port.py index d56e480bbc..d9fc55e691 100644 --- a/test/units/modules/network/ovs/test_openvswitch_port.py +++ b/test/units/modules/network/ovs/test_openvswitch_port.py @@ -20,11 +20,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.ovs import openvswitch_port -from .ovs_module import TestOpenVSwitchModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .ovs_module import TestOpenVSwitchModule, load_fixture test_name_side_effect_matrix = { 'test_openvswitch_port_absent_idempotent': [ @@ -75,6 +74,8 @@ class TestOpenVSwitchPortModule(TestOpenVSwitchModule): module = openvswitch_port def setUp(self): + super(TestOpenVSwitchPortModule, self).setUp() + self.mock_run_command = ( patch('ansible.module_utils.basic.AnsibleModule.run_command')) self.run_command = self.mock_run_command.start() @@ -83,6 +84,8 @@ class TestOpenVSwitchPortModule(TestOpenVSwitchModule): self.get_bin_path = self.mock_get_bin_path.start() def tearDown(self): + super(TestOpenVSwitchPortModule, self).tearDown() + self.mock_run_command.stop() self.mock_get_bin_path.stop() diff --git a/test/units/modules/network/vyos/test_vyos_banner.py b/test/units/modules/network/vyos/test_vyos_banner.py index f7de5cc944..57c6f66122 100644 --- a/test/units/modules/network/vyos/test_vyos_banner.py +++ b/test/units/modules/network/vyos/test_vyos_banner.py @@ -17,11 +17,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.vyos import vyos_banner -from .vyos_module import TestVyosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .vyos_module import TestVyosModule, load_fixture class TestVyosBannerModule(TestVyosModule): @@ -29,6 +28,8 @@ class TestVyosBannerModule(TestVyosModule): module = vyos_banner def setUp(self): + super(TestVyosBannerModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.vyos.vyos_banner.get_config') self.get_config = self.mock_get_config.start() @@ -36,6 +37,7 @@ class TestVyosBannerModule(TestVyosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestVyosBannerModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/vyos/test_vyos_command.py b/test/units/modules/network/vyos/test_vyos_command.py index d3b90f847d..c64423c45b 100644 --- a/test/units/modules/network/vyos/test_vyos_command.py +++ b/test/units/modules/network/vyos/test_vyos_command.py @@ -23,7 +23,8 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.vyos import vyos_command -from .vyos_module import TestVyosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .vyos_module import TestVyosModule, load_fixture class TestVyosCommandModule(TestVyosModule): @@ -31,10 +32,12 @@ class TestVyosCommandModule(TestVyosModule): module = vyos_command def setUp(self): + super(TestVyosCommandModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.vyos.vyos_command.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestVyosCommandModule, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/vyos/test_vyos_config.py b/test/units/modules/network/vyos/test_vyos_config.py index 09dc7a0d2f..b1bdeab6eb 100644 --- a/test/units/modules/network/vyos/test_vyos_config.py +++ b/test/units/modules/network/vyos/test_vyos_config.py @@ -20,11 +20,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.vyos import vyos_config -from .vyos_module import TestVyosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .vyos_module import TestVyosModule, load_fixture class TestVyosConfigModule(TestVyosModule): @@ -32,6 +31,8 @@ class TestVyosConfigModule(TestVyosModule): module = vyos_config def setUp(self): + super(TestVyosConfigModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.vyos.vyos_config.get_config') self.get_config = self.mock_get_config.start() @@ -42,6 +43,8 @@ class TestVyosConfigModule(TestVyosModule): self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestVyosConfigModule, self).tearDown() + self.mock_get_config.stop() self.mock_load_config.stop() self.mock_run_commands.stop() diff --git a/test/units/modules/network/vyos/test_vyos_facts.py b/test/units/modules/network/vyos/test_vyos_facts.py index b9c2aa098e..82a71fba31 100644 --- a/test/units/modules/network/vyos/test_vyos_facts.py +++ b/test/units/modules/network/vyos/test_vyos_facts.py @@ -23,7 +23,8 @@ import json from ansible.compat.tests.mock import patch from ansible.modules.network.vyos import vyos_facts -from .vyos_module import TestVyosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .vyos_module import TestVyosModule, load_fixture class TestVyosFactsModule(TestVyosModule): @@ -31,10 +32,12 @@ class TestVyosFactsModule(TestVyosModule): module = vyos_facts def setUp(self): + super(TestVyosFactsModule, self).setUp() self.mock_run_commands = patch('ansible.modules.network.vyos.vyos_facts.run_commands') self.run_commands = self.mock_run_commands.start() def tearDown(self): + super(TestVyosFactsModule, self).tearDown() self.mock_run_commands.stop() def load_fixtures(self, commands=None): diff --git a/test/units/modules/network/vyos/test_vyos_static_route.py b/test/units/modules/network/vyos/test_vyos_static_route.py index 2162818ce2..85a47ec8ab 100644 --- a/test/units/modules/network/vyos/test_vyos_static_route.py +++ b/test/units/modules/network/vyos/test_vyos_static_route.py @@ -21,7 +21,8 @@ __metaclass__ = type from ansible.compat.tests.mock import patch from ansible.modules.network.vyos import vyos_static_route -from .vyos_module import TestVyosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .vyos_module import TestVyosModule, load_fixture class TestVyosStaticRouteModule(TestVyosModule): @@ -29,6 +30,8 @@ class TestVyosStaticRouteModule(TestVyosModule): module = vyos_static_route def setUp(self): + super(TestVyosStaticRouteModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.vyos.vyos_static_route.get_config') self.get_config = self.mock_get_config.start() @@ -36,6 +39,8 @@ class TestVyosStaticRouteModule(TestVyosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestVyosStaticRouteModule, self).tearDown() + self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/vyos/test_vyos_system.py b/test/units/modules/network/vyos/test_vyos_system.py index 2e2c2f2a7b..389538552e 100644 --- a/test/units/modules/network/vyos/test_vyos_system.py +++ b/test/units/modules/network/vyos/test_vyos_system.py @@ -19,11 +19,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.vyos import vyos_system -from .vyos_module import TestVyosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .vyos_module import TestVyosModule, load_fixture class TestVyosSystemModule(TestVyosModule): @@ -31,6 +30,8 @@ class TestVyosSystemModule(TestVyosModule): module = vyos_system def setUp(self): + super(TestVyosSystemModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.vyos.vyos_system.get_config') self.get_config = self.mock_get_config.start() @@ -38,6 +39,8 @@ class TestVyosSystemModule(TestVyosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestVyosSystemModule, self).tearDown() + self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/vyos/test_vyos_user.py b/test/units/modules/network/vyos/test_vyos_user.py index fe2e2ed83f..bdc73d7f01 100644 --- a/test/units/modules/network/vyos/test_vyos_user.py +++ b/test/units/modules/network/vyos/test_vyos_user.py @@ -19,11 +19,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.compat.tests.mock import patch from ansible.modules.network.vyos import vyos_user -from .vyos_module import TestVyosModule, load_fixture, set_module_args +from units.modules.utils import set_module_args +from .vyos_module import TestVyosModule, load_fixture class TestVyosUserModule(TestVyosModule): @@ -31,6 +30,8 @@ class TestVyosUserModule(TestVyosModule): module = vyos_user def setUp(self): + super(TestVyosUserModule, self).setUp() + self.mock_get_config = patch('ansible.modules.network.vyos.vyos_user.get_config') self.get_config = self.mock_get_config.start() @@ -38,6 +39,7 @@ class TestVyosUserModule(TestVyosModule): self.load_config = self.mock_load_config.start() def tearDown(self): + super(TestVyosUserModule, self).tearDown() self.mock_get_config.stop() self.mock_load_config.stop() diff --git a/test/units/modules/network/vyos/vyos_module.py b/test/units/modules/network/vyos/vyos_module.py index 012f7e08f7..264cd5c66e 100644 --- a/test/units/modules/network/vyos/vyos_module.py +++ b/test/units/modules/network/vyos/vyos_module.py @@ -22,16 +22,9 @@ __metaclass__ = type import os import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixture_data = {} @@ -54,15 +47,7 @@ def load_fixture(name): return data -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -class TestVyosModule(unittest.TestCase): +class TestVyosModule(ModuleTestCase): def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False): self.load_fixtures(commands) @@ -83,27 +68,16 @@ class TestVyosModule(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/packaging/language/test_pip.py b/test/units/modules/packaging/language/test_pip.py index 3a4146ab62..92e3661ae5 100644 --- a/test/units/modules/packaging/language/test_pip.py +++ b/test/units/modules/packaging/language/test_pip.py @@ -6,15 +6,12 @@ from ansible.module_utils import basic from ansible.modules.packaging.language import pip -from ..utils import (set_module_args, AnsibleFailJson, exit_json, fail_json) +from units.modules.utils import set_module_args, AnsibleFailJson, ModuleTestCase -class TestPip(unittest.TestCase): - +class TestPip(ModuleTestCase): def setUp(self): - self.mock_ansible_module = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) - self.mock_ansible_module.start() - self.addCleanup(self.mock_ansible_module.stop) + super(TestPip, self).setUp() @patch.object(basic.AnsibleModule, 'get_bin_path') def test_failure_when_pip_absent(self, mock_get_bin_path): diff --git a/test/units/modules/packaging/os/test_rhn_channel.py b/test/units/modules/packaging/os/test_rhn_channel.py index c89eb39ca4..84820f21a9 100644 --- a/test/units/modules/packaging/os/test_rhn_channel.py +++ b/test/units/modules/packaging/os/test_rhn_channel.py @@ -1,29 +1,19 @@ import json -from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch -from ansible.module_utils import basic -from ansible.module_utils.six.moves import xmlrpc_client -from ansible.module_utils._text import to_bytes from ansible.modules.packaging.os import rhn_channel -from ..utils import (set_module_args, AnsibleExitJson, AnsibleFailJson, - exit_json, fail_json, get_method_name, mock_request) +from units.modules.packaging.utils import mock_request +from units.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase -class TestRhnChannel(unittest.TestCase): +class TestRhnChannel(ModuleTestCase): def setUp(self): + super(TestRhnChannel, self).setUp() + self.module = rhn_channel self.module.HAS_UP2DATE_CLIENT = True - self.mock_exit_fail = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) - self.mock_exit_fail.start() - self.addCleanup(self.mock_exit_fail.stop) - - def tearDown(self): - pass - def test_without_required_parameters(self): """Failure must occurs when all parameters are missing""" with self.assertRaises(AnsibleFailJson): diff --git a/test/units/modules/packaging/os/test_rhn_register.py b/test/units/modules/packaging/os/test_rhn_register.py index 647dd68f57..f746906f8b 100644 --- a/test/units/modules/packaging/os/test_rhn_register.py +++ b/test/units/modules/packaging/os/test_rhn_register.py @@ -7,8 +7,8 @@ from ansible.module_utils._text import to_native from ansible.module_utils.six.moves import xmlrpc_client from ansible.modules.packaging.os import rhn_register -from ..utils import (set_module_args, AnsibleExitJson, AnsibleFailJson, - exit_json, fail_json, get_method_name, mock_request) +from units.modules.packaging.utils import mock_request +from units.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase SYSTEMID = """<?xml version="1.0"?> @@ -37,9 +37,11 @@ def skipWhenAllModulesMissing(modules): return unittest.skip("{0}: none are available".format(', '.join(modules))) -class TestRhnRegister(unittest.TestCase): +class TestRhnRegister(ModuleTestCase): def setUp(self): + super(TestRhnRegister, self).setUp() + self.module = rhn_register self.module.HAS_UP2DATE_CLIENT = True @@ -51,17 +53,10 @@ class TestRhnRegister(unittest.TestCase): self.mock_load_config.start() self.addCleanup(self.mock_load_config.stop) - self.mock_exit_fail = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) - self.mock_exit_fail.start() - self.addCleanup(self.mock_exit_fail.stop) - enable_patcher = patch.object(rhn_register.Rhn, 'enable') self.mock_enable = enable_patcher.start() self.addCleanup(enable_patcher.stop) - def tearDown(self): - pass - # This one fails, module needs to be fixed. # @patch('os.path.isfile') # def test_systemid_requirements_missing(self, mock_isfile): diff --git a/test/units/modules/packaging/utils.py b/test/units/modules/packaging/utils.py index ebc2cd215e..ddda19d9b0 100644 --- a/test/units/modules/packaging/utils.py +++ b/test/units/modules/packaging/utils.py @@ -6,30 +6,6 @@ from ansible.module_utils.six.moves import xmlrpc_client from ansible.module_utils._text import to_bytes -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - -def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - def get_method_name(request_body): return xmlrpc_client.loads(request_body)[1] diff --git a/test/units/modules/system/test_iptables.py b/test/units/modules/system/test_iptables.py index 60e000dc6a..f573a60ff9 100644 --- a/test/units/modules/system/test_iptables.py +++ b/test/units/modules/system/test_iptables.py @@ -1,52 +1,21 @@ -import json - from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch from ansible.module_utils import basic from ansible.modules.system import iptables -from ansible.module_utils._text import to_bytes - - -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - -def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args def get_bin_path(*args, **kwargs): return "/sbin/iptables" -class TestIptables(unittest.TestCase): +class TestIptables(ModuleTestCase): def setUp(self): - self.mock_basic = patch.multiple(basic.AnsibleModule, - exit_json=exit_json, - fail_json=fail_json, - get_bin_path=get_bin_path) - self.mock_basic.start() - self.addCleanup(self.mock_basic.stop) - - def tearDown(self): - pass + super(TestIptables, self).setUp() + self.mock_get_bin_path = patch.object(basic.AnsibleModule, 'get_bin_path', get_bin_path) + self.mock_get_bin_path.start() + self.addCleanup(self.mock_get_bin_path.stop) # ensure that the patching is 'undone' def test_without_required_parameters(self): """Failure must occurs when all parameters are missing""" diff --git a/test/units/modules/system/test_parted.py b/test/units/modules/system/test_parted.py index ad74cd598a..95735aabd1 100644 --- a/test/units/modules/system/test_parted.py +++ b/test/units/modules/system/test_parted.py @@ -16,13 +16,10 @@ # along with Ansible. If not, see <http://www.gnu.org/licenses/>. __metaclass__ = type -import json -from ansible.compat.tests import unittest from ansible.compat.tests.mock import patch, call from ansible.modules.system import parted as parted_module -from ansible.modules.system.parted import parse_partition_info, parted -from ansible.module_utils import basic -from ansible.module_utils._text import to_bytes +from ansible.modules.system.parted import parse_partition_info +from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args # Example of output : parted -s -m /dev/sdb -- unit 'MB' print parted_output1 = """ @@ -92,27 +89,10 @@ parted_dict2 = { } -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - -def set_module_args(args): - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - -class TestParted(unittest.TestCase): +class TestParted(ModuleTestCase): def setUp(self): + super(TestParted, self).setUp() + self.module = parted_module self.mock_check_parted_label = (patch('ansible.modules.system.parted.check_parted_label', return_value=False)) self.check_parted_label = self.mock_check_parted_label.start() @@ -127,6 +107,7 @@ class TestParted(unittest.TestCase): self.get_bin_path = self.mock_get_bin_path.start() def tearDown(self): + super(TestParted, self).tearDown() self.mock_run_command.stop() self.mock_get_bin_path.stop() self.mock_parted.stop() @@ -146,22 +127,16 @@ class TestParted(unittest.TestCase): return result def failed(self): - def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - with patch.object(basic.AnsibleModule, 'fail_json', fail_json): - with self.assertRaises(AnsibleFailJson) as exc: - self.module.main() + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertTrue(result['failed'], result) return result def changed(self, changed=False): - with patch.object(basic.AnsibleModule, 'exit_json', exit_json): - with self.assertRaises(AnsibleExitJson) as exc: - self.module.main() + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], changed, result) diff --git a/test/units/modules/utils.py b/test/units/modules/utils.py new file mode 100644 index 0000000000..a0e9351b12 --- /dev/null +++ b/test/units/modules/utils.py @@ -0,0 +1,38 @@ +import json + +from ansible.compat.tests import unittest +from ansible.compat.tests.mock import patch +from ansible.module_utils import basic +from ansible.module_utils._text import to_bytes + + +def set_module_args(args): + args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) + basic._ANSIBLE_ARGS = to_bytes(args) + + +class AnsibleExitJson(Exception): + pass + + +class AnsibleFailJson(Exception): + pass + + +def exit_json(*args, **kwargs): + if 'changed' not in kwargs: + kwargs['changed'] = False + raise AnsibleExitJson(kwargs) + + +def fail_json(*args, **kwargs): + kwargs['failed'] = True + raise AnsibleFailJson(kwargs) + + +class ModuleTestCase(unittest.TestCase): + + def setUp(self): + self.mock_module = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) + self.mock_module.start() + self.addCleanup(self.mock_module.stop) |