Debian Codegraph @

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

raw
1 # Copyright (c) 2018-2022 NetApp
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 ''' unit tests for module_utils netapp.py - general features '''
5 from __future__ import (absolute_import, division, print_function)
6 __metaclass__ = type
7
8 import sys
9 import pytest
10 from ansible.module_utils import basic
11 # pylint: disable=unused-import
12 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import \
13 patch_ansible, create_module, expect_and_capture_ansible_exception
14 from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import patch_request_and_invoke
15 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
16
17 if sys.version_info < (3, 11):
18 pytestmark = pytest.mark.skip("Skipping Unit Tests on 3.11")
19
20
21 DEFAULT_ARGS = {
22 'hostname': 'test',
23 'username': 'test_user',
24 'password': 'test_pass!',
25 'cert_filepath': None,
26 'key_filepath': None,
27 }
28
29 CERT_ARGS = {
30 'hostname': 'test',
31 'cert_filepath': 'test_pem.pem',
32 'key_filepath': 'test_key.key'
33 }
34
35
36 class MockONTAPModule:
37 def __init__(self):
38 self.module = basic.AnsibleModule(netapp_utils.na_ontap_host_argument_spec())
39
40
41 def create_ontap_module(default_args=None, module_args=None):
42 return create_module(MockONTAPModule, default_args, module_args).module
43
44
45 def test_has_feature_success_default():
46 ''' existing feature_flag with default '''
47 flag = 'deprecation_warning'
48 module = create_ontap_module(DEFAULT_ARGS)
49 assert netapp_utils.has_feature(module, flag)
50
51
52 def test_has_feature_success_user_true():
53 ''' existing feature_flag with value set to True '''
54 flag = 'user_deprecation_warning'
55 module_args = {'feature_flags': {flag: True}}
56 module = create_ontap_module(DEFAULT_ARGS, module_args)
57 assert netapp_utils.has_feature(module, flag)
58
59
60 def test_has_feature_success_user_false():
61 ''' existing feature_flag with value set to False '''
62 flag = 'user_deprecation_warning'
63 module_args = {'feature_flags': {flag: False}}
64 module = create_ontap_module(DEFAULT_ARGS, module_args)
65 assert not netapp_utils.has_feature(module, flag)
66
67
68 def test_has_feature_invalid_key():
69 ''' existing feature_flag with unknown key '''
70 flag = 'deprecation_warning_bad_key'
71 module = create_ontap_module(DEFAULT_ARGS)
72 msg = 'Internal error: unexpected feature flag: %s' % flag
73 assert expect_and_capture_ansible_exception(netapp_utils.has_feature, 'fail', module, flag)['msg'] == msg
74
75
76 def test_has_feature_invalid_bool():
77 ''' existing feature_flag with non boolean value '''
78 flag = 'user_deprecation_warning'
79 module_args = {'feature_flags': {flag: 'non bool'}}
80 module = create_ontap_module(DEFAULT_ARGS, module_args)
81 msg = 'Error: expected bool type for feature flag: %s' % flag
82 assert expect_and_capture_ansible_exception(netapp_utils.has_feature, 'fail', module, flag)['msg'] == msg
83
84
85 def test_get_na_ontap_host_argument_spec_peer():
86 ''' validate spec does not have default key and feature_flags option '''
87 spec = netapp_utils.na_ontap_host_argument_spec_peer()
88 for key in ('username', 'https'):
89 assert key in spec
90 assert 'feature_flags' not in spec
91 for entry in spec.values():
92 assert 'type' in entry
93 assert 'default' not in entry
94
95
96 def test_setup_host_options_from_module_params_from_empty():
97 ''' make sure module.params options are reflected in host_options '''
98 module = create_ontap_module(DEFAULT_ARGS)
99 host_options = {}
100 keys = ('hostname', 'username')
101 netapp_utils.setup_host_options_from_module_params(host_options, module, keys)
102 # we gave 2 keys
103 assert len(host_options) == 2
104 for key in keys:
105 assert host_options[key] == DEFAULT_ARGS[key]
106
107
108 def test_setup_host_options_from_module_params_username_not_set_when_cert_present():
109 ''' make sure module.params options are reflected in host_options '''
110 module = create_ontap_module(DEFAULT_ARGS)
111 host_options = dict(cert_filepath='some_path')
112 unchanged_keys = tuple(host_options.keys())
113 copied_over_keys = ('hostname',)
114 ignored_keys = ('username',)
115 keys = unchanged_keys + copied_over_keys + ignored_keys
116 netapp_utils.setup_host_options_from_module_params(host_options, module, keys)
117 # we gave 2 keys
118 assert len(host_options) == 2
119 for key in ignored_keys:
120 assert key not in host_options
121 for key in copied_over_keys:
122 assert host_options[key] == DEFAULT_ARGS[key]
123 print(host_options)
124 for key in unchanged_keys:
125 assert host_options[key] != DEFAULT_ARGS[key]
126
127
128 def test_setup_host_options_from_module_params_not_none_fields_are_preserved():
129 ''' make sure module.params options are reflected in host_options '''
130 args = dict(DEFAULT_ARGS)
131 args['cert_filepath'] = 'some_path'
132 module = create_ontap_module(args)
133 host_options = dict(cert_filepath='some_other_path')
134 unchanged_keys = tuple(host_options.keys())
135 copied_over_keys = ('hostname',)
136 ignored_keys = ('username',)
137 keys = unchanged_keys + copied_over_keys + ignored_keys
138 netapp_utils.setup_host_options_from_module_params(host_options, module, keys)
139 # we gave 2 keys
140 assert len(host_options) == 2
141 for key in ignored_keys:
142 assert key not in host_options
143 for key in copied_over_keys:
144 assert host_options[key] == args[key]
145 print(host_options)
146 for key in unchanged_keys:
147 assert host_options[key] != args[key]
148
149
150 def test_setup_host_options_from_module_params_cert_not_set_when_username_present():
151 ''' make sure module.params options are reflected in host_options '''
152 args = dict(DEFAULT_ARGS)
153 args['cert_filepath'] = 'some_path'
154 module = create_ontap_module(args)
155 host_options = dict(username='some_name')
156 unchanged_keys = tuple(host_options.keys())
157 copied_over_keys = ('hostname',)
158 ignored_keys = ('cert_filepath',)
159 keys = unchanged_keys + copied_over_keys + ignored_keys
160 netapp_utils.setup_host_options_from_module_params(host_options, module, keys)
161 # we gave 2 keys
162 assert len(host_options) == 2
163 for key in ignored_keys:
164 assert key not in host_options
165 for key in copied_over_keys:
166 assert host_options[key] == args[key]
167 print(host_options)
168 for key in unchanged_keys:
169 assert host_options[key] != args[key]
170
171
172 def test_setup_host_options_from_module_params_conflict():
173 ''' make sure module.params options are reflected in host_options '''
174 module = create_ontap_module(DEFAULT_ARGS)
175 host_options = dict(username='some_name', key_filepath='not allowed')
176 msg = 'Error: host cannot have both basic authentication (username/password) and certificate authentication (cert/key files).'
177 assert expect_and_capture_ansible_exception(netapp_utils.setup_host_options_from_module_params,
178 'fail', host_options, module, host_options.keys())['msg'] == msg
179
180
181 def test_set_auth_method():
182 args = {'hostname': ''}
183 # neither password nor cert
184 error = expect_and_capture_ansible_exception(netapp_utils.set_auth_method, 'fail', create_ontap_module(args), None, None, None, None)['msg']
185 assert 'Error: ONTAP module requires username/password or SSL certificate file(s)' in error
186 # keyfile but no cert
187 error = expect_and_capture_ansible_exception(netapp_utils.set_auth_method, 'fail', create_ontap_module(args), None, None, None, 'keyfile')['msg']
188 assert 'Error: cannot have a key file without a cert file' in error