Debian Codegraph @

ansible_collections/netapp/ontap/tests/unit/plugins/modules/test_na_ontap_nfs.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 test template for ONTAP Ansible module '''
5
6 from __future__ import (absolute_import, division, print_function)
7 __metaclass__ = type
8 import copy
9 import pytest
10
11 from ansible_collections.netapp.ontap.tests.unit.compat import unittest
12 from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch, Mock
13 # pylint: disable=unused-import
14 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import\
15 assert_no_warnings, assert_no_warnings_except_zapi, assert_warning_was_raised, call_main, create_and_apply, print_warnings, set_module_args, \
16 AnsibleExitJson, patch_ansible
17 from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import\
18 patch_request_and_invoke, register_responses
19 from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_zapi_response, zapi_responses
20 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
21
22 from ansible_collections.netapp.ontap.plugins.modules.na_ontap_nfs \
23 import NetAppONTAPNFS as nfs_module, main as my_main # module under test
24
25 if not netapp_utils.has_netapp_lib():
26 pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
27
28
29 nfs_info = {
30 "attributes-list": {
31 "nfs-info": {
32 "auth-sys-extended-groups": "false",
33 "cached-cred-harvest-timeout": "86400000",
34 "cached-cred-negative-ttl": "7200000",
35 "cached-cred-positive-ttl": "86400000",
36 "cached-transient-err-ttl": "30000",
37 "chown-mode": "use_export_policy",
38 "enable-ejukebox": "true",
39 "extended-groups-limit": "32",
40 "file-session-io-grouping-count": "5000",
41 "file-session-io-grouping-duration": "120",
42 "ignore-nt-acl-for-root": "false",
43 "is-checksum-enabled-for-replay-cache": "true",
44 "is-mount-rootonly-enabled": "true",
45 "is-netgroup-dns-domain-search": "true",
46 "is-nfs-access-enabled": "false",
47 "is-nfs-rootonly-enabled": "false",
48 "is-nfsv2-enabled": "false",
49 "is-nfsv3-64bit-identifiers-enabled": "false",
50 "is-nfsv3-connection-drop-enabled": "true",
51 "is-nfsv3-enabled": "true",
52 "is-nfsv3-fsid-change-enabled": "true",
53 "is-nfsv4-fsid-change-enabled": "true",
54 "is-nfsv4-numeric-ids-enabled": "true",
55 "is-nfsv40-acl-enabled": "false",
56 "is-nfsv40-enabled": "true",
57 "is-nfsv40-migration-enabled": "false",
58 "is-nfsv40-read-delegation-enabled": "false",
59 "is-nfsv40-referrals-enabled": "false",
60 "is-nfsv40-req-open-confirm-enabled": "false",
61 "is-nfsv40-write-delegation-enabled": "false",
62 "is-nfsv41-acl-enabled": "false",
63 "is-nfsv41-acl-preserve-enabled": "true",
64 "is-nfsv41-enabled": "true",
65 "is-nfsv41-migration-enabled": "false",
66 "is-nfsv41-pnfs-enabled": "true",
67 "is-nfsv41-read-delegation-enabled": "false",
68 "is-nfsv41-referrals-enabled": "false",
69 "is-nfsv41-state-protection-enabled": "true",
70 "is-nfsv41-write-delegation-enabled": "false",
71 "is-qtree-export-enabled": "false",
72 "is-rquota-enabled": "false",
73 "is-tcp-enabled": "false",
74 "is-udp-enabled": "false",
75 "is-v3-ms-dos-client-enabled": "false",
76 "is-validate-qtree-export-enabled": "true",
77 "is-vstorage-enabled": "false",
78 "map-unknown-uid-to-default-windows-user": "true",
79 "mountd-port": "635",
80 "name-service-lookup-protocol": "udp",
81 "netgroup-trust-any-ns-switch-no-match": "false",
82 "nfsv4-acl-max-aces": "400",
83 "nfsv4-grace-seconds": "45",
84 "nfsv4-id-domain": "defaultv4iddomain.com",
85 "nfsv4-lease-seconds": "30",
86 "nfsv41-implementation-id-domain": "netapp.com",
87 "nfsv41-implementation-id-name": "NetApp Release Kalyaniblack__9.4.0",
88 "nfsv41-implementation-id-time": "1541070767",
89 "nfsv4x-session-num-slots": "180",
90 "nfsv4x-session-slot-reply-cache-size": "640",
91 "nlm-port": "4045",
92 "nsm-port": "4046",
93 "ntacl-display-permissive-perms": "false",
94 "ntfs-unix-security-ops": "use_export_policy",
95 "permitted-enc-types": {
96 "string": ["des", "des3", "aes_128", "aes_256"]
97 },
98 "rpcsec-ctx-high": "0",
99 "rpcsec-ctx-idle": "0",
100 "rquotad-port": "4049",
101 "showmount": "true",
102 "showmount-timestamp": "1548372452",
103 "skip-root-owner-write-perm-check": "false",
104 "tcp-max-xfer-size": "1048576",
105 "udp-max-xfer-size": "32768",
106 "v3-search-unconverted-filename": "false",
107 "v4-inherited-acl-preserve": "false",
108 "vserver": "ansible"
109 }
110 },
111 "num-records": "1"
112 }
113
114 nfs_info_no_tcp_max_xfer_size = copy.deepcopy(nfs_info)
115 del nfs_info_no_tcp_max_xfer_size['attributes-list']['nfs-info']['tcp-max-xfer-size']
116
117
118 class MockONTAPConnection(object):
119 ''' mock server connection to ONTAP host '''
120
121 def __init__(self, kind=None, data=None, job_error=None):
122 ''' save arguments '''
123 self.kind = kind
124 self.params = data
125 self.xml_in = None
126 self.xml_out = None
127
128 def invoke_successfully(self, xml, enable_tunneling): # pylint: disable=unused-argument
129 ''' mock invoke_successfully returning xml data '''
130 self.xml_in = xml
131 if self.kind == 'nfs':
132 xml = self.build_nfs_info(self.params)
133 self.xml_out = xml
134 if self.kind == 'nfs_status':
135 xml = self.build_nfs_status_info(self.params)
136 return xml
137
138 @staticmethod
139 def build_nfs_info(nfs_details):
140 ''' build xml data for volume-attributes '''
141 xml = netapp_utils.zapi.NaElement('xml')
142 xml.translate_struct(nfs_info)
143 return xml
144
145 @staticmethod
146 def build_nfs_status_info(nfs_status_details):
147 ''' build xml data for volume-attributes '''
148 xml = netapp_utils.zapi.NaElement('xml')
149 attributes = {
150 'is-enabled': "true"
151 }
152 xml.translate_struct(attributes)
153 return xml
154
155
156 DEFAULT_ARGS = {
157 'vserver': 'nfs_vserver',
158 'hostname': 'test',
159 'username': 'test_user',
160 'password': 'test_pass!',
161 'https': 'false',
162 'use_rest': 'never'
163 }
164
165
166 SRR = zapi_responses({
167 'nfs_info': build_zapi_response(nfs_info),
168 'nfs_info_no_tcp_max_xfer_size': build_zapi_response(nfs_info_no_tcp_max_xfer_size)
169 })
170
171
172 class TestMyModule(unittest.TestCase):
173 ''' a group of related Unit Tests '''
174
175 def setUp(self):
176 self.mock_nfs_group = {
177 'vserver': DEFAULT_ARGS['vserver'],
178 }
179
180 def mock_args(self):
181 return dict(DEFAULT_ARGS)
182
183 def get_nfs_mock_object(self, kind=None):
184 """
185 Helper method to return an na_ontap_volume object
186 :param kind: passes this param to MockONTAPConnection()
187 :return: na_ontap_volume object
188 """
189 nfsy_obj = nfs_module()
190 nfsy_obj.asup_log_for_cserver = Mock(return_value=None)
191 nfsy_obj.cluster = Mock()
192 nfsy_obj.cluster.invoke_successfully = Mock()
193 if kind is None:
194 nfsy_obj.server = MockONTAPConnection()
195 else:
196 nfsy_obj.server = MockONTAPConnection(kind=kind, data=self.mock_nfs_group)
197 return nfsy_obj
198
199 def test_module_fail_when_required_args_missing(self):
200 ''' required arguments are reported as errors '''
201 error = 'missing required arguments'
202 assert error in call_main(my_main, {}, fail=True)['msg']
203
204 def test_get_nonexistent_nfs(self):
205 ''' Test if get_nfs_service returns None for non-existent nfs '''
206 set_module_args(self.mock_args())
207 result = self.get_nfs_mock_object().get_nfs_service()
208 assert result is None
209
210 def test_get_existing_nfs(self):
211 ''' Test if get_policy_group returns details for existing nfs '''
212 set_module_args(self.mock_args())
213 result = self.get_nfs_mock_object('nfs').get_nfs_service()
214 assert result['nfsv3']
215
216 def test_get_nonexistent_nfs_status(self):
217 ''' Test if get__nfs_status returns None for non-existent nfs '''
218 set_module_args(self.mock_args())
219 result = self.get_nfs_mock_object().get_nfs_status()
220 assert result is None
221
222 def test_get_existing_nfs_status(self):
223 ''' Test if get__nfs_status returns details for nfs '''
224 set_module_args(self.mock_args())
225 result = self.get_nfs_mock_object('nfs_status').get_nfs_status()
226 assert result
227
228 def test_modify_nfs(self):
229 ''' Test if modify_nfs runs for existing nfs '''
230 data = self.mock_args()
231 current = {
232 'nfsv3': 'enabled',
233 'nfsv3_fsid_change': 'enabled',
234 'nfsv4': 'enabled',
235 'nfsv41': 'enabled',
236 'vstorage_state': 'enabled',
237 'tcp': 'enabled',
238 'udp': 'enabled',
239 'nfsv4_id_domain': 'nfsv4_id_domain',
240 'nfsv40_acl': 'enabled',
241 'nfsv40_read_delegation': 'enabled',
242 'nfsv40_write_delegation': 'enabled',
243 'nfsv41_acl': 'enabled',
244 'nfsv41_read_delegation': 'enabled',
245 'nfsv41_write_delegation': 'enabled',
246 'showmount': 'enabled',
247 'tcp_max_xfer_size': '1048576',
248 }
249
250 data.update(current)
251 set_module_args(data)
252 self.get_nfs_mock_object('nfs_status').modify_nfs_service(current)
253
254 def test_successfully_modify_nfs(self):
255 ''' Test modify nfs successful for modifying tcp max xfer size. '''
256 data = self.mock_args()
257 data['tcp_max_xfer_size'] = 8192
258 set_module_args(data)
259 with pytest.raises(AnsibleExitJson) as exc:
260 self.get_nfs_mock_object('nfs').apply()
261 assert exc.value.args[0]['changed']
262
263 def test_modify_nfs_idempotency(self):
264 ''' Test modify nfs idempotency '''
265 data = self.mock_args()
266 data['tcp_max_xfer_size'] = '1048576'
267 set_module_args(data)
268 with pytest.raises(AnsibleExitJson) as exc:
269 self.get_nfs_mock_object('nfs').apply()
270 assert not exc.value.args[0]['changed']
271
272 @patch('ansible_collections.netapp.ontap.plugins.modules.na_ontap_nfs.NetAppONTAPNFS.delete_nfs_service')
273 def test_successfully_delete_nfs(self, delete_nfs_service):
274 ''' Test successfully delete nfs '''
275 data = self.mock_args()
276 data['state'] = 'absent'
277 set_module_args(data)
278 obj = self.get_nfs_mock_object('nfs')
279 with pytest.raises(AnsibleExitJson) as exc:
280 obj.apply()
281 assert exc.value.args[0]['changed']
282 delete_nfs_service.assert_called_with()
283
284 @patch('ansible_collections.netapp.ontap.plugins.modules.na_ontap_nfs.NetAppONTAPNFS.get_nfs_service')
285 def test_successfully_enable_nfs(self, get_nfs_service):
286 ''' Test successfully enable nfs on non-existent nfs '''
287 data = self.mock_args()
288 data['state'] = 'present'
289 set_module_args(data)
290 get_nfs_service.side_effect = [
291 None,
292 {}
293 ]
294 obj = self.get_nfs_mock_object('nfs')
295 with pytest.raises(AnsibleExitJson) as exc:
296 obj.apply()
297 assert exc.value.args[0]['changed']
298
299
300 def test_modify_tcp_max_xfer_size():
301 ''' if ZAPI returned a None value, a modify is attempted '''
302 register_responses([
303 # ONTAP 9.4 and later, tcp_max_xfer_size is an INT
304 ('ZAPI', 'nfs-service-get-iter', SRR['nfs_info']),
305 ('ZAPI', 'nfs-status', SRR['success']),
306 ('ZAPI', 'nfs-service-modify', SRR['success']),
307 # ONTAP 9.4 and later, tcp_max_xfer_size is an INT, idempotency
308 ('ZAPI', 'nfs-service-get-iter', SRR['nfs_info']),
309 # ONTAP 9.3 and earlier, tcp_max_xfer_size is not set
310 ('ZAPI', 'nfs-service-get-iter', SRR['nfs_info_no_tcp_max_xfer_size']),
311 ])
312 module_args = {
313 'tcp_max_xfer_size': 4500
314 }
315 assert create_and_apply(nfs_module, DEFAULT_ARGS, module_args)['changed']
316 module_args = {
317 'tcp_max_xfer_size': 1048576
318 }
319 assert not create_and_apply(nfs_module, DEFAULT_ARGS, module_args)['changed']
320 error = 'Error: tcp_max_xfer_size is not supported on ONTAP 9.3 or earlier.'
321 assert create_and_apply(nfs_module, DEFAULT_ARGS, module_args, fail=True)['msg'] == error
322 assert_no_warnings_except_zapi()
323
324
325 def test_warning_on_nfsv41_alias():
326 ''' if ZAPI returned a None value, a modify is attempted '''
327 register_responses([
328 # ONTAP 9.4 and later, tcp_max_xfer_size is an INT
329 ('ZAPI', 'nfs-service-get-iter', SRR['nfs_info']),
330 ('ZAPI', 'nfs-status', SRR['success']),
331 ('ZAPI', 'nfs-service-modify', SRR['success']),
332 ])
333 module_args = {
334 'nfsv4.1': 'disabled'
335 }
336 assert call_main(my_main, DEFAULT_ARGS, module_args)['changed']
337 print_warnings()
338 assert_warning_was_raised('Error: "nfsv4.1" option conflicts with Ansible naming conventions - please use "nfsv41".')