Debian Codegraph @

ansible_collections/netapp/ontap/tests/unit/plugins/modules/test_na_ontap_vscan.py

raw
1 # (c) 2018, NetApp, Inc
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 ''' unit tests for Ansible module: na_ontap_vscan'''
5
6 from __future__ import (absolute_import, division, print_function)
7 __metaclass__ = type
8 import pytest
9
10 from ansible_collections.netapp.ontap.tests.unit.compat import unittest
11 from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch
12 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
13 # pylint: disable=unused-import
14 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import set_module_args, \
15 AnsibleFailJson, AnsibleExitJson, patch_ansible
16
17 from ansible_collections.netapp.ontap.plugins.modules.na_ontap_vscan \
18 import NetAppOntapVscan as vscan_module # module under test
19
20 if not netapp_utils.has_netapp_lib():
21 pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
22 HAS_NETAPP_ZAPI_MSG = "pip install netapp_lib is required"
23
24
25 # REST API canned responses when mocking send_request
26 SRR = {
27 # common responses
28 'is_rest': (200, {}, None),
29 'is_zapi': (400, {}, "Unreachable"),
30 'empty_good': (200, {}, None),
31 'end_of_sequence': (500, None, "Ooops, the UT needs one more SRR response"),
32 'generic_error': (400, None, "Expected error"),
33 # module specific responses
34 'enabled': (200, {'records': [{'enabled': True, 'svm': {'uuid': 'testuuid'}}]}, None),
35 'disabled': (200, {'records': [{'enabled': False, 'svm': {'uuid': 'testuuid'}}]}, None),
36 }
37
38
39 class MockONTAPConnection(object):
40 ''' mock server connection to ONTAP host '''
41
42 def __init__(self, kind=None, data=None):
43 ''' save arguments '''
44 self.kind = kind
45 self.params = data
46 self.xml_in = None
47 self.xml_out = None
48
49 def invoke_successfully(self, xml, enable_tunneling): # pylint: disable=unused-argument
50 ''' mock invoke_successfully returning xml data '''
51 self.xml_in = xml
52 if self.kind == 'enable':
53 xml = self.build_vscan_status_info(self.params)
54 self.xml_out = xml
55 return xml
56
57 @staticmethod
58 def build_vscan_status_info(status):
59 xml = netapp_utils.zapi.NaElement('xml')
60 attributes = {'num-records': 1,
61 'attributes-list': {'vscan-status-info': {'is-vscan-enabled': status}}}
62 xml.translate_struct(attributes)
63 return xml
64
65
66 class TestMyModule(unittest.TestCase):
67 ''' Unit tests for na_ontap_job_schedule '''
68
69 def mock_args(self):
70 return {
71 'enable': False,
72 'vserver': 'vserver',
73 'hostname': 'test',
74 'username': 'test_user',
75 'password': 'test_pass!'
76 }
77
78 def get_vscan_mock_object(self, cx_type='zapi', kind=None, status=None):
79 vscan_obj = vscan_module()
80 if cx_type == 'zapi':
81 if kind is None:
82 vscan_obj.server = MockONTAPConnection()
83 else:
84 vscan_obj.server = MockONTAPConnection(kind=kind, data=status)
85 # For rest, mocking is achieved through side_effect
86 return vscan_obj
87
88 def test_module_fail_when_required_args_missing(self):
89 ''' required arguments are reported as errors '''
90 with pytest.raises(AnsibleFailJson) as exc:
91 set_module_args({})
92 vscan_module()
93 print('Info: %s' % exc.value.args[0]['msg'])
94
95 def test_successfully_enable(self):
96 data = self.mock_args()
97 data['enable'] = True
98 data['use_rest'] = 'never'
99 set_module_args(data)
100 with pytest.raises(AnsibleExitJson) as exc:
101 self.get_vscan_mock_object('zapi', 'enable', 'false').apply()
102 assert exc.value.args[0]['changed']
103
104 def test_idempotently_enable(self):
105 data = self.mock_args()
106 data['enable'] = True
107 data['use_rest'] = 'never'
108 set_module_args(data)
109 with pytest.raises(AnsibleExitJson) as exc:
110 self.get_vscan_mock_object('zapi', 'enable', 'true').apply()
111 assert not exc.value.args[0]['changed']
112
113 def test_successfully_disable(self):
114 data = self.mock_args()
115 data['enable'] = False
116 data['use_rest'] = 'never'
117 set_module_args(data)
118 with pytest.raises(AnsibleExitJson) as exc:
119 self.get_vscan_mock_object('zapi', 'enable', 'true').apply()
120 assert exc.value.args[0]['changed']
121
122 def test_idempotently_disable(self):
123 data = self.mock_args()
124 data['enable'] = False
125 data['use_rest'] = 'never'
126 set_module_args(data)
127 with pytest.raises(AnsibleExitJson) as exc:
128 self.get_vscan_mock_object('zapi', 'enable', 'false').apply()
129 assert not exc.value.args[0]['changed']
130
131 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
132 def test_rest_error(self, mock_request):
133 data = self.mock_args()
134 set_module_args(data)
135 mock_request.side_effect = [
136 SRR['is_rest'],
137 SRR['generic_error'],
138 SRR['end_of_sequence']
139 ]
140 with pytest.raises(AnsibleFailJson) as exc:
141 self.get_vscan_mock_object(cx_type='rest').apply()
142 assert exc.value.args[0]['msg'] == SRR['generic_error'][2]
143
144 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
145 def test_rest_successly_enable(self, mock_request):
146 data = self.mock_args()
147 data['enable'] = True
148 set_module_args(data)
149 mock_request.side_effect = [
150 SRR['is_rest'],
151 SRR['disabled'],
152 SRR['empty_good'],
153 SRR['end_of_sequence']
154 ]
155 with pytest.raises(AnsibleExitJson) as exc:
156 self.get_vscan_mock_object(cx_type='rest').apply()
157 assert exc.value.args[0]['changed']
158
159 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
160 def test_rest_idempotently_enable(self, mock_request):
161 data = self.mock_args()
162 data['enable'] = True
163 set_module_args(data)
164 mock_request.side_effect = [
165 SRR['is_rest'],
166 SRR['enabled'],
167 SRR['end_of_sequence']
168 ]
169 with pytest.raises(AnsibleExitJson) as exc:
170 self.get_vscan_mock_object(cx_type='rest').apply()
171 assert not exc.value.args[0]['changed']
172
173 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
174 def test_rest_successly_disable(self, mock_request):
175 data = self.mock_args()
176 data['enable'] = False
177 set_module_args(data)
178 mock_request.side_effect = [
179 SRR['is_rest'],
180 SRR['enabled'],
181 SRR['empty_good'],
182 SRR['end_of_sequence']
183 ]
184 with pytest.raises(AnsibleExitJson) as exc:
185 self.get_vscan_mock_object(cx_type='rest').apply()
186 assert exc.value.args[0]['changed']
187
188 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
189 def test_rest_idempotently_disable(self, mock_request):
190 data = self.mock_args()
191 data['enable'] = False
192 set_module_args(data)
193 mock_request.side_effect = [
194 SRR['is_rest'],
195 SRR['disabled'],
196 SRR['end_of_sequence']
197 ]
198 with pytest.raises(AnsibleExitJson) as exc:
199 self.get_vscan_mock_object(cx_type='rest').apply()
200 assert not exc.value.args[0]['changed']