diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-14 20:03:01 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-14 20:03:01 +0000 |
commit | a453ac31f3428614cceb99027f8efbdb9258a40b (patch) | |
tree | f61f87408f32a8511cbd91799f9cececb53e0374 /collections-debian-merged/ansible_collections/netapp/aws/tests | |
parent | Initial commit. (diff) | |
download | ansible-upstream.tar.xz ansible-upstream.zip |
Adding upstream version 2.10.7+merged+base+2.10.8+dfsg.upstream/2.10.7+merged+base+2.10.8+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'collections-debian-merged/ansible_collections/netapp/aws/tests')
9 files changed, 844 insertions, 0 deletions
diff --git a/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/__init__.py b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/__init__.py diff --git a/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/builtins.py b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/builtins.py new file mode 100644 index 00000000..f60ee678 --- /dev/null +++ b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/builtins.py @@ -0,0 +1,33 @@ +# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com> +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +# +# Compat for python2.7 +# + +# One unittest needs to import builtins via __import__() so we need to have +# the string that represents it +try: + import __builtin__ +except ImportError: + BUILTINS = 'builtins' +else: + BUILTINS = '__builtin__' diff --git a/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/mock.py b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/mock.py new file mode 100644 index 00000000..0972cd2e --- /dev/null +++ b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/mock.py @@ -0,0 +1,122 @@ +# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com> +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +''' +Compat module for Python3.x's unittest.mock module +''' +import sys + +# Python 2.7 + +# Note: Could use the pypi mock library on python3.x as well as python2.x. It +# is the same as the python3 stdlib mock library + +try: + # Allow wildcard import because we really do want to import all of mock's + # symbols into this compat shim + # pylint: disable=wildcard-import,unused-wildcard-import + from unittest.mock import * +except ImportError: + # Python 2 + # pylint: disable=wildcard-import,unused-wildcard-import + try: + from mock import * + except ImportError: + print('You need the mock library installed on python2.x to run tests') + + +# Prior to 3.4.4, mock_open cannot handle binary read_data +if sys.version_info >= (3,) and sys.version_info < (3, 4, 4): + file_spec = None + + def _iterate_read_data(read_data): + # Helper for mock_open: + # Retrieve lines from read_data via a generator so that separate calls to + # readline, read, and readlines are properly interleaved + sep = b'\n' if isinstance(read_data, bytes) else '\n' + data_as_list = [l + sep for l in read_data.split(sep)] + + if data_as_list[-1] == sep: + # If the last line ended in a newline, the list comprehension will have an + # extra entry that's just a newline. Remove this. + data_as_list = data_as_list[:-1] + else: + # If there wasn't an extra newline by itself, then the file being + # emulated doesn't have a newline to end the last line remove the + # newline that our naive format() added + data_as_list[-1] = data_as_list[-1][:-1] + + for line in data_as_list: + yield line + + def mock_open(mock=None, read_data=''): + """ + A helper function to create a mock to replace the use of `open`. It works + for `open` called directly or used as a context manager. + + The `mock` argument is the mock object to configure. If `None` (the + default) then a `MagicMock` will be created for you, with the API limited + to methods or attributes available on standard file handles. + + `read_data` is a string for the `read` methoddline`, and `readlines` of the + file handle to return. This is an empty string by default. + """ + def _readlines_side_effect(*args, **kwargs): + if handle.readlines.return_value is not None: + return handle.readlines.return_value + return list(_data) + + def _read_side_effect(*args, **kwargs): + if handle.read.return_value is not None: + return handle.read.return_value + return type(read_data)().join(_data) + + def _readline_side_effect(): + if handle.readline.return_value is not None: + while True: + yield handle.readline.return_value + for line in _data: + yield line + + global file_spec + if file_spec is None: + import _io + file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) + + if mock is None: + mock = MagicMock(name='open', spec=open) + + handle = MagicMock(spec=file_spec) + handle.__enter__.return_value = handle + + _data = _iterate_read_data(read_data) + + handle.write.return_value = None + handle.read.return_value = None + handle.readline.return_value = None + handle.readlines.return_value = None + + handle.read.side_effect = _read_side_effect + handle.readline.side_effect = _readline_side_effect() + handle.readlines.side_effect = _readlines_side_effect + + mock.return_value = handle + return mock diff --git a/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/unittest.py b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/unittest.py new file mode 100644 index 00000000..98f08ad6 --- /dev/null +++ b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/compat/unittest.py @@ -0,0 +1,38 @@ +# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com> +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +''' +Compat module for Python2.7's unittest module +''' + +import sys + +# Allow wildcard import because we really do want to import all of +# unittests's symbols into this compat shim +# pylint: disable=wildcard-import,unused-wildcard-import +if sys.version_info < (2, 7): + try: + # Need unittest2 on python2.6 + from unittest2 import * + except ImportError: + print('You need unittest2 installed on python2.6.x to run tests') +else: + from unittest import * diff --git a/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_active_directory.py b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_active_directory.py new file mode 100644 index 00000000..abcc3fbd --- /dev/null +++ b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_active_directory.py @@ -0,0 +1,111 @@ +# (c) 2019, NetApp, Inc +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +''' unit tests ONTAP Ansible module: ''' + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type +import json +import pytest + +from ansible.module_utils import basic +from ansible.module_utils._text import to_bytes +from ansible_collections.netapp.aws.tests.unit.compat import unittest +from ansible_collections.netapp.aws.tests.unit.compat.mock import patch + +from ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_active_directory \ + import AwsCvsNetappActiveDir as ad_module + + +def set_module_args(args): + """prepare arguments so that they will be picked up during module creation""" + args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) + basic._ANSIBLE_ARGS = to_bytes(args) # pylint: disable=protected-access + + +class AnsibleExitJson(Exception): + """Exception class to be raised by module.exit_json and caught by the test case""" + + +class AnsibleFailJson(Exception): + """Exception class to be raised by module.fail_json and caught by the test case""" + + +def exit_json(*args, **kwargs): # pylint: disable=unused-argument + """function to patch over exit_json; package return data into an exception""" + if 'changed' not in kwargs: + kwargs['changed'] = False + raise AnsibleExitJson(kwargs) + + +def fail_json(*args, **kwargs): # pylint: disable=unused-argument + """function to patch over fail_json; package return data into an exception""" + kwargs['failed'] = True + raise AnsibleFailJson(kwargs) + + +class TestMyModule(unittest.TestCase): + ''' a group of related Unit Tests ''' + + def setUp(self): + self.mock_module_helper = patch.multiple(basic.AnsibleModule, + exit_json=exit_json, + fail_json=fail_json) + self.mock_module_helper.start() + self.addCleanup(self.mock_module_helper.stop) + + def set_default_args_fail_check(self): + return dict({ + 'state': 'present', + 'DNS': '101.102.103.123', + 'domain': 'mydomain.com', + 'password': 'netapp1!', + 'username': 'myuser', + 'api_url': 'myapiurl.com', + 'secret_key': 'mysecretkey', + 'api_key': 'myapikey' + }) + + def set_default_args_pass_check(self): + return dict({ + 'state': 'present', + 'DNS': '101.102.103.123', + 'domain': 'mydomain.com', + 'password': 'netapp1!', + 'region': 'us-east-1', + 'netBIOS': 'testing', + 'username': 'myuser', + 'api_url': 'myapiurl.com', + 'secret_key': 'mysecretkey', + 'api_key': 'myapikey' + }) + + def test_module_fail_when_required_args_missing(self): + ''' required arguments are reported as errors ''' + with pytest.raises(AnsibleFailJson) as exc: + set_module_args(self.set_default_args_fail_check()) + ad_module() + print('Info: %s' % exc.value.args[0]['msg']) + + def test_module_fail_when_required_args_present(self): + ''' required arguments are reported as errors ''' + with pytest.raises(AnsibleExitJson) as exc: + set_module_args(self.set_default_args_pass_check()) + ad_module() + exit_json(changed=True, msg="TestCase Fail when required ars are present") + assert exc.value.args[0]['changed'] + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_active_directory.AwsCvsNetappActiveDir.get_activedirectory_id') + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_active_directory.AwsCvsNetappActiveDir.get_activedirectory') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.post') + def test_create_aws_netapp_cvs_activedir(self, get_post_api, get_aws_api, get_ad_id): + set_module_args(self.set_default_args_pass_check()) + my_obj = ad_module() + + get_aws_api.return_value = None + get_post_api.return_value = None, None + get_ad_id.return_value = "123" + with pytest.raises(AnsibleExitJson) as exc: + my_obj.apply() + print('Info: test_create_aws_netapp_cvs_active_directory: %s' % repr(exc.value)) + assert exc.value.args[0]['changed'] diff --git a/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_filesystems.py b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_filesystems.py new file mode 100644 index 00000000..9712db10 --- /dev/null +++ b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_filesystems.py @@ -0,0 +1,148 @@ +# (c) 2019, NetApp, Inc +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +''' unit tests AWS CVS FileSystems Ansible module: aws_netapp_cvs_filesystems''' + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import json +import pytest + +from ansible.module_utils import basic +from ansible.module_utils._text import to_bytes +from ansible_collections.netapp.aws.tests.unit.compat import unittest +from ansible_collections.netapp.aws.tests.unit.compat.mock import patch +from ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_filesystems \ + import AwsCvsNetappFileSystem as fileSystem_module + + +def set_module_args(args): + """prepare arguments so that they will be picked up during module creation""" + args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) + basic._ANSIBLE_ARGS = to_bytes(args) # pylint: disable=protected-access + + +class AnsibleExitJson(Exception): + """Exception class to be raised by module.exit_json and caught by the test case""" + + +class AnsibleFailJson(Exception): + """Exception class to be raised by module.fail_json and caught by the test case""" + + +def exit_json(*args, **kwargs): # pylint: disable=unused-argument + """function to patch over exit_json; package return data into an exception""" + if 'changed' not in kwargs: + kwargs['changed'] = False + raise AnsibleExitJson(kwargs) + + +def fail_json(*args, **kwargs): # pylint: disable=unused-argument + """function to patch over fail_json; package return data into an exception""" + kwargs['failed'] = True + raise AnsibleFailJson(kwargs) + + +class TestMyModule(unittest.TestCase): + ''' a group of related Unit Tests ''' + + def setUp(self): + self.mock_module_helper = patch.multiple(basic.AnsibleModule, + exit_json=exit_json, + fail_json=fail_json) + self.mock_module_helper.start() + self.addCleanup(self.mock_module_helper.stop) + + def set_default_args_fail_check(self): + return dict({ + 'creationToken': 'TestFilesystem', + 'region': 'us-east-1', + 'quotaInBytes': 3424, + 'serviceLevel': 'standard', + 'api_url': 'hostname.com', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def set_default_args_pass_check(self): + return dict({ + 'state': 'present', + 'creationToken': 'TestFilesystem', + 'region': 'us-east-1', + 'quotaInBytes': 3424, + 'serviceLevel': 'standard', + 'api_url': 'hostname.com', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def set_args_create_aws_netapp_cvs_filesystems(self): + return dict({ + 'state': 'present', + 'creationToken': 'TestFilesystem', + 'region': 'us-east-1', + 'quotaInBytes': 3424, + 'serviceLevel': 'standard', + 'api_url': 'hostname.com', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def set_args_delete_aws_netapp_cvs_filesystems(self): + return dict({ + 'state': 'absent', + 'creationToken': 'TestFilesystem', + 'region': 'us-east-1', + 'api_url': 'hostname.com', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def test_module_fail_when_required_args_missing(self): + ''' required arguments are reported as errors ''' + with pytest.raises(AnsibleFailJson) as exc: + set_module_args(self.set_default_args_fail_check()) + fileSystem_module() + print('Info: test_module_fail_when_required_args_missing: %s' % exc.value.args[0]['msg']) + + def test_module_fail_when_required_args_present(self): + ''' required arguments are reported as errors ''' + with pytest.raises(AnsibleExitJson) as exc: + set_module_args(self.set_default_args_pass_check()) + fileSystem_module() + exit_json(changed=True, msg="Induced arguments check") + print('Info: test_module_fail_when_required_args_present: %s' % exc.value.args[0]['msg']) + assert exc.value.args[0]['changed'] + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_filesystems.AwsCvsNetappFileSystem.get_filesystem_id') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.get_state') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.post') + def test_create_aws_netapp_cvs_snapshots_pass(self, get_post_api, get_state_api, get_filesystem_id): + set_module_args(self.set_args_create_aws_netapp_cvs_filesystems()) + my_obj = fileSystem_module() + get_filesystem_id.return_value = None + get_state_api.return_value = 'done' + response = {'jobs': [{'jobId': 'dummy'}]} + get_post_api.return_value = response, None + with pytest.raises(AnsibleExitJson) as exc: + my_obj.apply() + print('Info: test_create_aws_netapp_cvs_filesystem_pass: %s' % repr(exc.value.args[0])) + assert exc.value.args[0]['changed'] + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_filesystems.AwsCvsNetappFileSystem.get_filesystem_id') + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_filesystems.AwsCvsNetappFileSystem.get_filesystem') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.get_state') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.delete') + def test_delete_aws_netapp_cvs_snapshots_pass(self, get_post_api, get_state_api, get_filesystem, get_filesystem_id): + set_module_args(self.set_args_delete_aws_netapp_cvs_filesystems()) + my_obj = fileSystem_module() + get_filesystem_id.return_value = '432-432-532423-4232' + get_filesystem.return_value = 'dummy' + get_state_api.return_value = 'done' + response = {'jobs': [{'jobId': 'dummy'}]} + get_post_api.return_value = response, None + with pytest.raises(AnsibleExitJson) as exc: + my_obj.apply() + print('Info: test_create_aws_netapp_cvs_filesyste_pass: %s' % repr(exc.value.args[0])) + assert exc.value.args[0]['changed'] diff --git a/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_pool.py b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_pool.py new file mode 100644 index 00000000..68d038db --- /dev/null +++ b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_pool.py @@ -0,0 +1,251 @@ +# (c) 2019, NetApp, Inc +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +''' Unit tests for AWS Cloud Volumes Services - Manage Pools ''' + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type +import json +import pytest + +from ansible.module_utils import basic +from ansible.module_utils._text import to_bytes +from ansible_collections.netapp.aws.tests.unit.compat import unittest +from ansible_collections.netapp.aws.tests.unit.compat.mock import patch +from ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_pool \ + import NetAppAWSCVS as pool_module + + +def set_module_args(args): + """prepare arguments so that they will be picked up during module creation""" + args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) + basic._ANSIBLE_ARGS = to_bytes(args) # pylint: disable=protected-access + + +class AnsibleExitJson(Exception): + """Exception class to be raised by module.exit_json and caught by the test case""" + + +class AnsibleFailJson(Exception): + """Exception class to be raised by module.fail_json and caught by the test case""" + + +def exit_json(*args, **kwargs): # pylint: disable=unused-argument + """function to patch over exit_json; package return data into an exception""" + if 'changed' not in kwargs: + kwargs['changed'] = False + raise AnsibleExitJson(kwargs) + + +def fail_json(*args, **kwargs): # pylint: disable=unused-argument + """function to patch over fail_json; package return data into an exception""" + kwargs['failed'] = True + raise AnsibleFailJson(kwargs) + + +class TestMyModule(unittest.TestCase): + ''' a group of related Unit Tests ''' + + def setUp(self): + self.mock_module_helper = patch.multiple(basic.AnsibleModule, + exit_json=exit_json, + fail_json=fail_json) + self.mock_module_helper.start() + self.addCleanup(self.mock_module_helper.stop) + + def set_default_args_fail_check(self): + return dict({ + 'from_name': 'TestPoolAA', + 'name': 'TestPoolAA_new', + 'serviceLevel': 'standard', + 'sizeInBytes': 4000000000000, + 'vendorID': 'ansiblePoolTestVendorA', + 'region': 'us-east-1', + 'api_url': 'hostname.invalid', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def set_default_args_pass_check(self): + return dict({ + 'state': 'present', + 'from_name': 'TestPoolAA', + 'name': 'TestPoolAA_new', + 'serviceLevel': 'standard', + 'sizeInBytes': 4000000000000, + 'vendorID': 'ansiblePoolTestVendorA', + 'region': 'us-east-1', + 'api_url': 'hostname.invalid', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def set_args_create_aws_netapp_cvs_pool(self): + return dict({ + 'state': 'present', + 'name': 'TestPoolAA', + 'serviceLevel': 'standard', + 'sizeInBytes': 4000000000000, + 'vendorID': 'ansiblePoolTestVendorA', + 'region': 'us-east-1', + 'api_url': 'hostname.invalid', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def set_args_update_aws_netapp_cvs_pool(self): + return dict({ + 'state': 'present', + 'from_name': 'TestPoolAA', + 'name': 'TestPoolAA_new', + 'serviceLevel': 'standard', + 'sizeInBytes': 4000000000000, + 'vendorID': 'ansiblePoolTestVendorA', + 'region': 'us-east-1', + 'api_url': 'hostname.invalid', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def set_args_delete_aws_netapp_cvs_pool(self): + return dict({ + 'state': 'absent', + 'name': 'TestPoolAA', + 'region': 'us-east-1', + 'api_url': 'hostname.invalid', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def test_module_fail_when_required_args_missing(self): + ''' required arguments are reported as errors ''' + with pytest.raises(AnsibleFailJson) as exc: + set_module_args(self.set_default_args_fail_check()) + pool_module() + print('Info: test_module_fail_when_required_args_missing: %s' % exc.value.args[0]['msg']) + + def test_module_pass_when_required_args_present(self): + ''' required arguments are present ''' + with pytest.raises(AnsibleExitJson) as exc: + set_module_args(self.set_default_args_pass_check()) + pool_module() + exit_json(changed=True, msg="Induced arguments check") + print('Info: test_module_pass_when_required_args_present: %s' % exc.value.args[0]['msg']) + assert exc.value.args[0]['changed'] + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_pool.NetAppAWSCVS.get_aws_netapp_cvs_pool') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.put') + def test_update_aws_netapp_cvs_pool_pass(self, get_put_api, get_aws_api): + set_module_args(self.set_args_update_aws_netapp_cvs_pool()) + my_obj = pool_module() + my_pool = { + "name": "Dummyname", + "poolId": "1f63b3d0-4fd4-b4fe-1ed6-c62f5f20d975", + "region": "us-east-1", + "serviceLevel": "extreme", + "sizeInBytes": 40000000000000000, + "state": "available", + "vendorID": "Dummy" + } + get_aws_api.return_value = my_pool + get_put_api.return_value = my_pool, None + with pytest.raises(AnsibleExitJson) as exc: + my_obj.apply() + print('Info: test_update_aws_netapp_cvs_pool_pass: %s' % repr(exc.value)) + assert exc.value.args[0]['changed'] + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_pool.NetAppAWSCVS.get_aws_netapp_cvs_pool') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.put') + def test_update_aws_netapp_cvs_pool_fail(self, get_put_api, get_aws_api): + set_module_args(self.set_args_update_aws_netapp_cvs_pool()) + my_obj = pool_module() + my_pool = { + "name": "Dummyname", + "poolId": "1f63b3d0-4fd4-b4fe-1ed6-c62f5f20d975", + "region": "us-east-1", + "serviceLevel": "extreme", + "sizeInBytes": 40000000000000000, + "state": "available", + "vendorID": "Dummy" + } + get_put_api.return_value = my_pool, "Error" + get_aws_api.return_value = my_pool + with pytest.raises(AnsibleFailJson) as exc: + my_obj.apply() + print('Info: test_update_aws_netapp_cvs_pool_fail: %s' % repr(exc.value)) + assert exc.value.args[0]['msg'] is not None + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_pool.NetAppAWSCVS.get_aws_netapp_cvs_pool') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.post') + def test_create_aws_netapp_cvs_pool_pass(self, get_post_api, get_aws_api): + set_module_args(self.set_args_create_aws_netapp_cvs_pool()) + my_obj = pool_module() + get_aws_api.return_value = None + get_post_api.return_value = None, None + with pytest.raises(AnsibleExitJson) as exc: + my_obj.apply() + print('Info: test_create_aws_netapp_cvs_pool_pass: %s' % repr(exc.value)) + assert exc.value.args[0]['changed'] + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_pool.NetAppAWSCVS.get_aws_netapp_cvs_pool') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.post') + def test_create_aws_netapp_cvs_pool_fail(self, get_post_api, get_aws_api): + set_module_args(self.set_args_create_aws_netapp_cvs_pool()) + my_obj = pool_module() + my_pool = { + "name": "Dummyname", + "poolId": "1f63b3d0-4fd4-b4fe-1ed6-c62f5f20d975", + "region": "us-east-1", + "serviceLevel": "extreme", + "sizeInBytes": 40000000000000000, + "state": "available", + "vendorID": "Dummy" + } + get_post_api.return_value = my_pool, "Error" + get_aws_api.return_value = None + with pytest.raises(AnsibleFailJson) as exc: + my_obj.apply() + print('Info: test_create_aws_netapp_cvs_pool_fail: %s' % repr(exc.value)) + assert exc.value.args[0]['msg'] is not None + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_pool.NetAppAWSCVS.get_aws_netapp_cvs_pool') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.delete') + def test_delete_aws_netapp_cvs_pool_pass(self, get_delete_api, get_aws_api): + set_module_args(self.set_args_delete_aws_netapp_cvs_pool()) + my_obj = pool_module() + my_pool = { + "name": "Dummyname", + "poolId": "1f63b3d0-4fd4-b4fe-1ed6-c62f5f20d975", + "region": "us-east-1", + "serviceLevel": "extreme", + "sizeInBytes": 40000000000000000, + "state": "available", + "vendorID": "Dummy" + } + get_aws_api.return_value = my_pool + get_delete_api.return_value = None, None + with pytest.raises(AnsibleExitJson) as exc: + my_obj.apply() + print('Info: test_delete_aws_netapp_cvs_pool_pass: %s' % repr(exc.value)) + assert exc.value.args[0]['changed'] + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_pool.NetAppAWSCVS.get_aws_netapp_cvs_pool') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.delete') + def test_delete_aws_netapp_cvs_pool_fail(self, get_delete_api, get_aws_api): + set_module_args(self.set_args_delete_aws_netapp_cvs_pool()) + my_obj = pool_module() + my_pool = { + "name": "Dummyname", + "poolId": "1f63b3d0-4fd4-b4fe-1ed6-c62f5f20d975", + "region": "us-east-1", + "serviceLevel": "extreme", + "sizeInBytes": 40000000000000000, + "state": "available", + "vendorID": "Dummy" + } + get_delete_api.return_value = my_pool, "Error" + get_aws_api.return_value = my_pool + with pytest.raises(AnsibleFailJson) as exc: + my_obj.apply() + print('Info: test_delete_aws_netapp_cvs_pool_fail: %s' % repr(exc.value)) + assert exc.value.args[0]['msg'] is not None diff --git a/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_snapshots.py b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_snapshots.py new file mode 100644 index 00000000..1f4c4bbe --- /dev/null +++ b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/plugins/modules/test_aws_netapp_cvs_snapshots.py @@ -0,0 +1,140 @@ +# (c) 2019, NetApp, Inc +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +''' unit tests NetApp AWS CVS Snapshots Ansible module: aws_netapp_cvs_snapshots''' + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type +import json +import pytest + +from ansible.module_utils import basic +from ansible.module_utils._text import to_bytes +from ansible_collections.netapp.aws.tests.unit.compat import unittest +from ansible_collections.netapp.aws.tests.unit.compat.mock import patch +from ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_snapshots \ + import AwsCvsNetappSnapshot as snapshot_module + + +def set_module_args(args): + """prepare arguments so that they will be picked up during module creation""" + args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) + basic._ANSIBLE_ARGS = to_bytes(args) # pylint: disable=protected-access + + +class AnsibleExitJson(Exception): + """Exception class to be raised by module.exit_json and caught by the test case""" + + +class AnsibleFailJson(Exception): + """Exception class to be raised by module.fail_json and caught by the test case""" + + +def exit_json(*args, **kwargs): # pylint: disable=unused-argument + """function to patch over exit_json; package return data into an exception""" + if 'changed' not in kwargs: + kwargs['changed'] = False + raise AnsibleExitJson(kwargs) + + +def fail_json(*args, **kwargs): # pylint: disable=unused-argument + """function to patch over fail_json; package return data into an exception""" + kwargs['failed'] = True + raise AnsibleFailJson(kwargs) + + +class TestMyModule(unittest.TestCase): + ''' a group of related Unit Tests ''' + + def setUp(self): + self.mock_module_helper = patch.multiple(basic.AnsibleModule, + exit_json=exit_json, + fail_json=fail_json) + self.mock_module_helper.start() + self.addCleanup(self.mock_module_helper.stop) + + def set_default_args_fail_check(self): + return dict({ + 'name': 'TestFilesystem', + 'fileSystemId': 'standard', + 'from_name': 'from_TestFilesystem', + 'region': 'us-east-1', + 'api_url': 'hostname.com', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def set_default_args_pass_check(self): + return dict({ + 'state': 'present', + 'name': 'testSnapshot', + 'fileSystemId': 'standard', + 'from_name': 'from_TestFilesystem', + 'region': 'us-east-1', + 'api_url': 'hostname.com', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def set_args_create_aws_netapp_cvs_snapshots(self): + return dict({ + 'state': 'present', + 'name': 'testSnapshot', + 'fileSystemId': '123-4213-432-432', + 'region': 'us-east-1', + 'api_url': 'hostname.com', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def set_args_delete_aws_netapp_cvs_snapshots(self): + return dict({ + 'state': 'absent', + 'name': 'testSnapshot', + 'region': 'us-east-1', + 'api_url': 'hostname.com', + 'api_key': 'myapikey', + 'secret_key': 'mysecretkey' + }) + + def test_module_fail_when_required_args_missing(self): + ''' required arguments are reported as errors ''' + with pytest.raises(AnsibleFailJson) as exc: + set_module_args(self.set_default_args_fail_check()) + snapshot_module() + print('Info: test_module_fail_when_required_args_missing: %s' % exc.value.args[0]['msg']) + + def test_module_fail_when_required_args_present(self): + ''' required arguments are reported as errors ''' + with pytest.raises(AnsibleExitJson) as exc: + set_module_args(self.set_default_args_pass_check()) + snapshot_module() + exit_json(changed=True, msg="Induced arguments check") + print('Info: test_module_fail_when_required_args_present: %s' % exc.value.args[0]['msg']) + assert exc.value.args[0]['changed'] + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_snapshots.AwsCvsNetappSnapshot.get_snapshot_id') + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_snapshots.AwsCvsNetappSnapshot.get_filesystem_id') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.post') + def test_create_aws_netapp_cvs_snapshots_pass(self, get_post_api, get_filesystem_id, get_snapshot_id): + set_module_args(self.set_args_create_aws_netapp_cvs_snapshots()) + my_obj = snapshot_module() + get_filesystem_id.return_value = 'fiesystemName' + get_snapshot_id.return_value = None + get_post_api.return_value = None, None + with pytest.raises(AnsibleExitJson) as exc: + my_obj.apply() + print('Info: test_create_aws_netapp_cvs_snapshots_pass: %s' % repr(exc.value.args[0])) + assert exc.value.args[0]['changed'] + + @patch('ansible_collections.netapp.aws.plugins.modules.aws_netapp_cvs_snapshots.AwsCvsNetappSnapshot.get_snapshot_id') + @patch('ansible_collections.netapp.aws.plugins.module_utils.netapp.AwsCvsRestAPI.delete') + def test_delete_aws_netapp_cvs_snapshots_pass(self, get_post_api, get_snapshot_id): + set_module_args(self.set_args_delete_aws_netapp_cvs_snapshots()) + my_obj = snapshot_module() + get_snapshot_id.return_value = "1f63b3d0-4fd4-b4fe-1ed6-c62f5f20d975" + get_post_api.return_value = None, None + with pytest.raises(AnsibleExitJson) as exc: + my_obj.apply() + print('Info: test_create_aws_netapp_cvs_snapshots_pass: %s' % repr(exc.value.args[0])) + assert exc.value.args[0]['changed'] diff --git a/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/requirements.txt b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/requirements.txt new file mode 100644 index 00000000..46fbfa46 --- /dev/null +++ b/collections-debian-merged/ansible_collections/netapp/aws/tests/unit/requirements.txt @@ -0,0 +1 @@ +unittest2 ; python_version < '2.7' |