| 1 |
# (c) 2018-2022, 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 for ONTAP Command Ansible module ''' |
| 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.mock import patch |
| 11 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 12 |
# pylint: disable=unused-import |
| 13 |
from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import\ |
| 14 |
patch_request_and_invoke, register_responses |
| 15 |
from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_zapi_response, zapi_error_message, zapi_responses |
| 16 |
from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import\ |
| 17 |
call_main, create_module, expect_and_capture_ansible_exception, patch_ansible |
| 18 |
|
| 19 |
|
| 20 |
from ansible_collections.netapp.ontap.plugins.modules.na_ontap_command import NetAppONTAPCommand as my_module, main as my_main # module under test |
| 21 |
|
| 22 |
if not netapp_utils.has_netapp_lib(): |
| 23 |
pytestmark = pytest.mark.skip('skipping as missing required netapp_lib') |
| 24 |
|
| 25 |
|
| 26 |
DEFAULT_ARGS = { |
| 27 |
'hostname': 'hostname', |
| 28 |
'username': 'username', |
| 29 |
'password': 'password', |
| 30 |
'use_rest': 'use_rest', |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
def cli_output(priv, result, translate=True): |
| 35 |
prefix = 'NetApp Release' |
| 36 |
print('HERE', 'start') |
| 37 |
if priv == 'advanced': |
| 38 |
prefix = '\n' + prefix |
| 39 |
if result == "u'77'": |
| 40 |
result = u'77' |
| 41 |
elif result == "b'77'": |
| 42 |
print('HERE', b'77') |
| 43 |
result = b'77' |
| 44 |
elif result is None: |
| 45 |
result = b'7' |
| 46 |
return { |
| 47 |
'cli-output': prefix, |
| 48 |
'cli-result-value': result |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
def build_zapi_response_raw(contents): |
| 53 |
""" when testing special encodings, we cannot use build_zapi_response as translate_struct converts to text |
| 54 |
""" |
| 55 |
if netapp_utils.has_netapp_lib(): |
| 56 |
xml = netapp_utils.zapi.NaElement('results') |
| 57 |
xml.add_attr('status', 'status_ok') |
| 58 |
xml.add_new_child('cli-output', contents['cli-output']) |
| 59 |
xml.add_new_child('cli-result-value', contents['cli-result-value']) |
| 60 |
# print('XML ut:', xml.to_string()) |
| 61 |
xml.add_attr('status', 'passed') |
| 62 |
return (xml, 'valid') |
| 63 |
return ('netapp-lib is required', 'invalid') |
| 64 |
|
| 65 |
|
| 66 |
ZRR = zapi_responses({ |
| 67 |
'cli_version': build_zapi_response_raw(cli_output(None, None)), |
| 68 |
'cli_version_advanced': build_zapi_response_raw(cli_output('advanced', None)), |
| 69 |
'cli_version_77': build_zapi_response(cli_output(None, '77')), |
| 70 |
'cli_version_b77': build_zapi_response_raw(cli_output(None, "b'77'")), |
| 71 |
'cli_version_u77': build_zapi_response_raw(cli_output(None, "u'77'")), |
| 72 |
}) |
| 73 |
|
| 74 |
|
| 75 |
def test_module_fail_when_required_args_missing(): |
| 76 |
''' required arguments are reported as errors ''' |
| 77 |
register_responses([ |
| 78 |
]) |
| 79 |
module_args = { |
| 80 |
} |
| 81 |
error = 'missing required arguments: command' |
| 82 |
assert error in call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 83 |
|
| 84 |
|
| 85 |
def test_default_priv(): |
| 86 |
''' make sure privilege is not required ''' |
| 87 |
register_responses([ |
| 88 |
('ZAPI', 'system-cli', ZRR['cli_version']), |
| 89 |
]) |
| 90 |
module_args = { |
| 91 |
'command': 'version', |
| 92 |
} |
| 93 |
msg = call_main(my_main, DEFAULT_ARGS, module_args)['msg'] |
| 94 |
needle = b'<cli-output>NetApp Release' |
| 95 |
assert needle in msg |
| 96 |
print('Version (raw): %s' % msg) |
| 97 |
|
| 98 |
|
| 99 |
def test_admin_priv(): |
| 100 |
''' make sure admin is accepted ''' |
| 101 |
register_responses([ |
| 102 |
('ZAPI', 'system-cli', ZRR['cli_version']), |
| 103 |
]) |
| 104 |
module_args = { |
| 105 |
'command': 'version', |
| 106 |
'privilege': 'admin', |
| 107 |
} |
| 108 |
msg = call_main(my_main, DEFAULT_ARGS, module_args)['msg'] |
| 109 |
needle = b'<cli-output>NetApp Release' |
| 110 |
assert needle in msg |
| 111 |
print('Version (raw): %s' % msg) |
| 112 |
|
| 113 |
|
| 114 |
def test_advanced_priv(): |
| 115 |
''' make sure advanced is not required ''' |
| 116 |
register_responses([ |
| 117 |
('ZAPI', 'system-cli', ZRR['cli_version_advanced']), |
| 118 |
]) |
| 119 |
module_args = { |
| 120 |
'command': 'version', |
| 121 |
'privilege': 'advanced', |
| 122 |
} |
| 123 |
msg = call_main(my_main, DEFAULT_ARGS, module_args)['msg'] |
| 124 |
# Interestingly, the ZAPI returns a slightly different response |
| 125 |
needle = b'<cli-output>\nNetApp Release' |
| 126 |
assert needle in msg |
| 127 |
print('Version (raw): %s' % msg) |
| 128 |
|
| 129 |
|
| 130 |
def get_dict_output(extra_args=None): |
| 131 |
''' get result value after calling command module ''' |
| 132 |
module_args = { |
| 133 |
'command': 'version', |
| 134 |
'return_dict': 'true', |
| 135 |
} |
| 136 |
if extra_args: |
| 137 |
module_args.update(extra_args) |
| 138 |
dict_output = call_main(my_main, DEFAULT_ARGS, module_args)['msg'] |
| 139 |
print('dict_output: %s' % repr(dict_output)) |
| 140 |
return dict_output |
| 141 |
|
| 142 |
|
| 143 |
def test_dict_output_77(): |
| 144 |
''' make sure correct value is returned ''' |
| 145 |
register_responses([ |
| 146 |
('ZAPI', 'system-cli', ZRR['cli_version_77']), |
| 147 |
]) |
| 148 |
result = '77' |
| 149 |
assert get_dict_output()['result_value'] == int(result) |
| 150 |
|
| 151 |
|
| 152 |
def test_dict_output_b77(): |
| 153 |
''' make sure correct value is returned ''' |
| 154 |
register_responses([ |
| 155 |
('ZAPI', 'system-cli', ZRR['cli_version_b77']), |
| 156 |
]) |
| 157 |
result = b'77' |
| 158 |
assert get_dict_output()['result_value'] == int(result) |
| 159 |
|
| 160 |
|
| 161 |
def test_dict_output_u77(): |
| 162 |
''' make sure correct value is returned ''' |
| 163 |
register_responses([ |
| 164 |
('ZAPI', 'system-cli', ZRR['cli_version_u77']), |
| 165 |
]) |
| 166 |
result = "u'77'" |
| 167 |
assert get_dict_output()['result_value'] == int(eval(result)) |
| 168 |
|
| 169 |
|
| 170 |
def test_dict_output_exclude(): |
| 171 |
''' make sure correct value is returned ''' |
| 172 |
register_responses([ |
| 173 |
('ZAPI', 'system-cli', ZRR['cli_version']), |
| 174 |
('ZAPI', 'system-cli', ZRR['cli_version']), |
| 175 |
]) |
| 176 |
dict_output = get_dict_output({'exclude_lines': 'NetApp Release'}) |
| 177 |
assert len(dict_output['stdout_lines']) == 1 |
| 178 |
assert len(dict_output['stdout_lines_filter']) == 0 |
| 179 |
dict_output = get_dict_output({'exclude_lines': 'whatever'}) |
| 180 |
assert len(dict_output['stdout_lines']) == 1 |
| 181 |
assert len(dict_output['stdout_lines_filter']) == 1 |
| 182 |
|
| 183 |
|
| 184 |
def test_dict_output_include(): |
| 185 |
''' make sure correct value is returned ''' |
| 186 |
register_responses([ |
| 187 |
('ZAPI', 'system-cli', ZRR['cli_version']), |
| 188 |
('ZAPI', 'system-cli', ZRR['cli_version']), |
| 189 |
]) |
| 190 |
dict_output = get_dict_output({'include_lines': 'NetApp Release'}) |
| 191 |
assert len(dict_output['stdout_lines']) == 1 |
| 192 |
assert len(dict_output['stdout_lines_filter']) == 1 |
| 193 |
dict_output = get_dict_output({'include_lines': 'whatever'}) |
| 194 |
assert len(dict_output['stdout_lines']) == 1 |
| 195 |
assert len(dict_output['stdout_lines_filter']) == 0 |
| 196 |
|
| 197 |
|
| 198 |
def test_check_mode(): |
| 199 |
''' make sure nothing is done ''' |
| 200 |
register_responses([ |
| 201 |
]) |
| 202 |
module_args = { |
| 203 |
'command': 'version', |
| 204 |
} |
| 205 |
my_obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 206 |
my_obj.module.check_mode = True |
| 207 |
msg = expect_and_capture_ansible_exception(my_obj.apply, 'exit')['msg'] |
| 208 |
needle = "Would run command: '['version']'" |
| 209 |
assert needle in msg |
| 210 |
print('Version (raw): %s' % msg) |
| 211 |
|
| 212 |
|
| 213 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.has_netapp_lib') |
| 214 |
def test_missing_netapp_lib(mock_has_netapp_lib): |
| 215 |
module_args = { |
| 216 |
'command': 'version', |
| 217 |
} |
| 218 |
mock_has_netapp_lib.return_value = False |
| 219 |
msg = 'Error: the python NetApp-Lib module is required. Import error: None' |
| 220 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 221 |
|
| 222 |
|
| 223 |
def test_zapi_errors(): |
| 224 |
''' make sure nothing is done ''' |
| 225 |
register_responses([ |
| 226 |
('ZAPI', 'system-cli', ZRR['error']), |
| 227 |
('ZAPI', 'system-cli', ZRR['cli_version']), |
| 228 |
('ZAPI', 'system-cli', ZRR['cli_version']), |
| 229 |
('ZAPI', 'system-cli', ZRR['cli_version']), |
| 230 |
|
| 231 |
]) |
| 232 |
module_args = { |
| 233 |
'command': 'version', |
| 234 |
} |
| 235 |
error = zapi_error_message("Error running command ['version']") |
| 236 |
assert error in call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 237 |
# EMS error is ignored |
| 238 |
assert b'NetApp Release' in call_main(my_main, DEFAULT_ARGS, module_args, fail=False)['msg'] |
| 239 |
# EMS cserver error is ignored |
| 240 |
assert b'NetApp Release' in call_main(my_main, DEFAULT_ARGS, module_args, fail=False)['msg'] |
| 241 |
# EMS vserver error is ignored |
| 242 |
module_args = { |
| 243 |
'command': 'version', |
| 244 |
'vserver': 'svm' |
| 245 |
} |
| 246 |
assert b'NetApp Release' in call_main(my_main, DEFAULT_ARGS, module_args, fail=False)['msg'] |