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