mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 04:40:22 -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
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue