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