mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +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:
@@ -20,21 +20,13 @@ from __future__ import (absolute_import, division, print_function)
|
||||
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
|
||||
|
||||
|
||||
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
|
||||
fixture_data = {}
|
||||
|
||||
|
||||
def set_module_args(args):
|
||||
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
|
||||
basic._ANSIBLE_ARGS = to_bytes(args)
|
||||
|
||||
|
||||
def load_fixture(name):
|
||||
path = os.path.join(fixture_path, name)
|
||||
if path not in fixture_data:
|
||||
@@ -80,15 +72,7 @@ def mock_call(calls, url, data=None, headers=None, method=None):
|
||||
return result
|
||||
|
||||
|
||||
class AnsibleExitJson(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class AnsibleFailJson(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class TestNsoModule(unittest.TestCase):
|
||||
class TestNsoModule(ModuleTestCase):
|
||||
|
||||
def execute_module(self, failed=False, changed=False, **kwargs):
|
||||
if failed:
|
||||
@@ -104,27 +88,16 @@ class TestNsoModule(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)
|
||||
|
||||
@@ -22,6 +22,7 @@ import json
|
||||
|
||||
from ansible.compat.tests.mock import patch
|
||||
from ansible.modules.network.nso import nso_config
|
||||
from units.modules.utils import set_module_args, AnsibleFailJson
|
||||
from . import nso_module
|
||||
from .nso_module import MockResponse
|
||||
|
||||
@@ -46,11 +47,11 @@ class TestNsoConfig(nso_module.TestNsoModule):
|
||||
open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
|
||||
|
||||
data = nso_module.load_fixture('config_config.json')
|
||||
nso_module.set_module_args({
|
||||
set_module_args({
|
||||
'username': 'user', 'password': 'password',
|
||||
'url': 'http://localhost:8080/jsonrpc', 'data': data
|
||||
})
|
||||
with self.assertRaises(SystemExit):
|
||||
with self.assertRaises(AnsibleFailJson):
|
||||
self.execute_module(changed=False, changes=[], diffs=[])
|
||||
|
||||
self.assertEqual(0, len(calls))
|
||||
@@ -75,7 +76,7 @@ class TestNsoConfig(nso_module.TestNsoModule):
|
||||
open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
|
||||
|
||||
data = nso_module.load_fixture('config_empty_data.json')
|
||||
nso_module.set_module_args({
|
||||
set_module_args({
|
||||
'username': 'user', 'password': 'password',
|
||||
'url': 'http://localhost:8080/jsonrpc', 'data': data
|
||||
})
|
||||
@@ -118,7 +119,7 @@ class TestNsoConfig(nso_module.TestNsoModule):
|
||||
open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
|
||||
|
||||
data = nso_module.load_fixture('config_config.json')
|
||||
nso_module.set_module_args({
|
||||
set_module_args({
|
||||
'username': 'user', 'password': 'password',
|
||||
'url': 'http://localhost:8080/jsonrpc', 'data': data
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user