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:
Pilou
2017-11-17 18:17:07 +01:00
committed by Matt Clay
parent 71a6dcdf3e
commit a5c9726502
154 changed files with 671 additions and 1113 deletions

View File

@@ -16,11 +16,8 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
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 set_module_args as _set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase
from nose.plugins.skip import SkipTest
try:
@@ -31,27 +28,15 @@ except ImportError:
def set_module_args(args):
set_module_args_custom_auth(args=args, auth={
'api_username': 'csproot',
'api_password': 'csproot',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'api_version': 'v5_0'
})
def set_module_args_custom_auth(args, auth):
args['auth'] = auth
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
if 'auth' not in args:
args['auth'] = {
'api_username': 'csproot',
'api_password': 'csproot',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'api_version': 'v5_0'
}
return _set_module_args(args)
class MockNuageResponse(object):
@@ -66,9 +51,10 @@ class MockNuageConnection(object):
self.response = MockNuageResponse(status_code, reason, errors)
class TestNuageModule(unittest.TestCase):
class TestNuageModule(ModuleTestCase):
def setUp(self):
super(TestNuageModule, self).setUp()
def session_start(self):
self._root_object = vsdk.NUMe()
@@ -79,22 +65,6 @@ class TestNuageModule(unittest.TestCase):
self.session_mock = patch('vspk.v5_0.NUVSDSession.start', new=session_start)
self.session_mock.start()
def fail_json(*args, **kwargs):
kwargs['failed'] = True
raise AnsibleFailJson(kwargs)
self.fail_json_mock = patch('ansible.module_utils.basic.AnsibleModule.fail_json', new=fail_json)
self.fail_json_mock.start()
def exit_json(*args, **kwargs):
if 'changed' not in kwargs:
kwargs['changed'] = False
raise AnsibleExitJson(kwargs)
self.exit_json_mock = patch('ansible.module_utils.basic.AnsibleModule.exit_json', new=exit_json)
self.exit_json_mock.start()
def tearDown(self):
super(TestNuageModule, self).tearDown()
self.session_mock.stop()
self.fail_json_mock.stop()
self.exit_json_mock.stop()

View File

@@ -30,7 +30,8 @@ except ImportError:
raise SkipTest('Nuage Ansible modules requires the vspk and bambou python libraries')
from ansible.compat.tests.mock import patch
from .nuage_module import AnsibleExitJson, AnsibleFailJson, MockNuageConnection, TestNuageModule, set_module_args, set_module_args_custom_auth
from units.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson
from .nuage_module import MockNuageConnection, TestNuageModule
_LOOP_COUNTER = 0
@@ -173,25 +174,25 @@ class TestNuageVSPKModule(TestNuageModule):
def tearDown(self):
super(TestNuageVSPKModule, self).tearDown()
for patch in self.patches:
patch.stop()
for mock in self.patches:
mock.stop()
def test_certificate_auth(self):
set_module_args_custom_auth(
set_module_args(
args={
'type': 'Enterprise',
'state': 'present',
'properties': {
'name': 'test-enterprise'
},
'auth': {
'api_username': 'csproot',
'api_certificate': '/dummy/location/certificate.pem',
'api_key': '/dummy/location/key.pem',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'api_version': 'v5_0'
}
},
auth={
'api_username': 'csproot',
'api_certificate': '/dummy/location/certificate.pem',
'api_key': '/dummy/location/key.pem',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'api_version': 'v5_0'
}
)
@@ -874,16 +875,16 @@ class TestNuageVSPKModule(TestNuageModule):
self.assertEqual(result['msg'], "Job ended in an error")
def test_fail_auth(self):
set_module_args_custom_auth(
set_module_args(
args={
'type': 'Enterprise',
'command': 'find'
},
auth={
'api_username': 'csproot',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'api_version': 'v5_0'
'command': 'find',
'auth': {
'api_username': 'csproot',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'api_version': 'v5_0'
}
}
)
@@ -896,17 +897,17 @@ class TestNuageVSPKModule(TestNuageModule):
self.assertEqual(result['msg'], 'Missing api_password or api_certificate and api_key parameter in auth')
def test_fail_version(self):
set_module_args_custom_auth(
set_module_args(
args={
'type': 'Enterprise',
'command': 'find'
},
auth={
'api_username': 'csproot',
'api_password': 'csproot',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'api_version': 'v1_0'
'command': 'find',
'auth': {
'api_username': 'csproot',
'api_password': 'csproot',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'api_version': 'v1_0'
}
}
)
@@ -1197,16 +1198,16 @@ class TestNuageVSPKModule(TestNuageModule):
self.assertEqual(result['msg'], 'Property fake is not valid for this type of entity')
def test_input_auth_username(self):
set_module_args_custom_auth(
set_module_args(
args={
'type': 'Enterprise',
'command': 'find'
},
auth={
'api_password': 'csproot',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'api_version': 'v5_0'
'command': 'find',
'auth': {
'api_password': 'csproot',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'api_version': 'v5_0'
}
}
)
@@ -1219,16 +1220,16 @@ class TestNuageVSPKModule(TestNuageModule):
self.assertEqual(result['msg'], 'missing required arguments: api_username')
def test_input_auth_enterprise(self):
set_module_args_custom_auth(
set_module_args(
args={
'type': 'Enterprise',
'command': 'find'
},
auth={
'api_username': 'csproot',
'api_password': 'csproot',
'api_url': 'https://localhost:8443',
'api_version': 'v5_0'
'command': 'find',
'auth': {
'api_username': 'csproot',
'api_password': 'csproot',
'api_url': 'https://localhost:8443',
'api_version': 'v5_0'
}
}
)
@@ -1241,16 +1242,16 @@ class TestNuageVSPKModule(TestNuageModule):
self.assertEqual(result['msg'], 'missing required arguments: api_enterprise')
def test_input_auth_url(self):
set_module_args_custom_auth(
set_module_args(
args={
'type': 'Enterprise',
'command': 'find'
},
auth={
'api_username': 'csproot',
'api_password': 'csproot',
'api_enterprise': 'csp',
'api_version': 'v5_0'
'command': 'find',
'auth': {
'api_username': 'csproot',
'api_password': 'csproot',
'api_enterprise': 'csp',
'api_version': 'v5_0'
}
}
)
@@ -1263,16 +1264,16 @@ class TestNuageVSPKModule(TestNuageModule):
self.assertEqual(result['msg'], 'missing required arguments: api_url')
def test_input_auth_version(self):
set_module_args_custom_auth(
set_module_args(
args={
'type': 'Enterprise',
'command': 'find'
},
auth={
'api_username': 'csproot',
'api_password': 'csproot',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
'command': 'find',
'auth': {
'api_username': 'csproot',
'api_password': 'csproot',
'api_enterprise': 'csp',
'api_url': 'https://localhost:8443',
}
}
)