Debian Codegraph @

ansible_collections/netapp/ontap/tests/unit/plugins/module_utils/test_rest_vserver.py

raw
1 # This code is part of Ansible, but is an independent component.
2 # This particular file snippet, and this file snippet only, is BSD licensed.
3 # Modules you write using this snippet, which is embedded dynamically by Ansible
4 # still belong to the author of the module, and may assign their own license
5 # to the complete work.
6 #
7 # Copyright (c) 2022, Laurent Nicolas <[email protected]>
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without modification,
11 # are permitted provided that the following conditions are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above copyright notice,
16 # this list of conditions and the following disclaimer in the documentation
17 # and/or other materials provided with the distribution.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 """ unit tests for module_utils rest_vserver.py
30
31 Provides wrappers for svm/svms REST APIs
32 """
33
34 from __future__ import (absolute_import, division, print_function)
35 __metaclass__ = type
36
37 import pytest
38 import sys
39
40 from ansible.module_utils import basic
41 # pylint: disable=unused-import
42 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import create_module, expect_and_capture_ansible_exception, patch_ansible
43 from ansible_collections.netapp.ontap.tests.unit.framework.rest_factory import rest_error_message, rest_responses
44 from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import patch_request_and_invoke, register_responses
45 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
46
47 from ansible_collections.netapp.ontap.plugins.module_utils import rest_vserver
48
49
50 if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7):
51 pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available')
52
53
54 # REST API canned responses when mocking send_request
55 SRR = rest_responses({
56 'svm_uuid': (200, {"records": [{"uuid": "test_uuid"}], "num_records": 1}, None),
57 })
58
59
60 DEFAULT_ARGS = {
61 'hostname': 'test',
62 'username': 'test_user',
63 'password': 'test_pass!',
64 'cert_filepath': None,
65 'key_filepath': None,
66 }
67
68
69 class MockONTAPModule:
70 def __init__(self):
71 self.module = basic.AnsibleModule(netapp_utils.na_ontap_host_argument_spec())
72
73
74 def create_restapi_object(default_args, module_args=None):
75 module = create_module(MockONTAPModule, default_args, module_args)
76 return netapp_utils.OntapRestAPI(module.module)
77
78
79 def test_successfully_get_vserver():
80 register_responses([
81 ('GET', 'svm/svms', SRR['svm_uuid']),
82 ('GET', 'svm/svms', SRR['zero_records']),
83 ])
84 rest_api = create_restapi_object(DEFAULT_ARGS)
85 assert rest_vserver.get_vserver(rest_api, 'svm_name') == ({'uuid': 'test_uuid'}, None)
86 assert rest_vserver.get_vserver(rest_api, 'svm_name') == (None, None)
87
88
89 def test_negative_get_vserver():
90 register_responses([
91 ('GET', 'svm/svms', SRR['generic_error']),
92 ])
93 rest_api = create_restapi_object(DEFAULT_ARGS)
94 assert rest_vserver.get_vserver(rest_api, 'svm_name') == (None, rest_error_message('', 'svm/svms'))
95
96
97 def test_successfully_get_vserver_uuid():
98 register_responses([
99 ('GET', 'svm/svms', SRR['svm_uuid']),
100 ('GET', 'svm/svms', SRR['zero_records']),
101 ])
102 rest_api = create_restapi_object(DEFAULT_ARGS)
103 assert rest_vserver.get_vserver_uuid(rest_api, 'svm_name') == ('test_uuid', None)
104 assert rest_vserver.get_vserver_uuid(rest_api, 'svm_name') == (None, None)
105
106
107 def test_negative_get_vserver_uuid():
108 register_responses([
109 ('GET', 'svm/svms', SRR['generic_error']),
110 ('GET', 'svm/svms', SRR['generic_error']),
111 ('GET', 'svm/svms', SRR['zero_records']),
112 ('GET', 'svm/svms', SRR['zero_records']),
113 ])
114 rest_api = create_restapi_object(DEFAULT_ARGS)
115 assert rest_vserver.get_vserver_uuid(rest_api, 'svm_name') == (None, rest_error_message('', 'svm/svms'))
116 assert expect_and_capture_ansible_exception(rest_vserver.get_vserver_uuid, 'fail', rest_api, 'svm_name', rest_api.module)['msg'] ==\
117 rest_error_message('Error fetching vserver svm_name', 'svm/svms')
118 assert rest_vserver.get_vserver_uuid(rest_api, 'svm_name', error_on_none=True) == (None, 'vserver svm_name does not exist or is not a data vserver.')
119 assert expect_and_capture_ansible_exception(rest_vserver.get_vserver_uuid, 'fail', rest_api, 'svm_name', rest_api.module, error_on_none=True)['msg'] ==\
120 'Error vserver svm_name does not exist or is not a data vserver.'