mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-16 01:45:25 -07:00
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'
This commit is contained in:
parent
71a6dcdf3e
commit
a5c9726502
154 changed files with 671 additions and 1113 deletions
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue