mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00: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:
@@ -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)
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user