| 1 |
# (c) 2018-2021, 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 snmp Ansible module ''' |
| 5 |
|
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
import pytest |
| 9 |
import sys |
| 10 |
|
| 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 assert_no_warnings, set_module_args, \ |
| 15 |
AnsibleFailJson, AnsibleExitJson, patch_ansible |
| 16 |
|
| 17 |
|
| 18 |
from ansible_collections.netapp.ontap.plugins.modules.na_ontap_snmp \ |
| 19 |
import NetAppONTAPSnmp as my_module, main as uut_main # module under test |
| 20 |
|
| 21 |
|
| 22 |
if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7): |
| 23 |
pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available') |
| 24 |
|
| 25 |
|
| 26 |
def default_args(): |
| 27 |
args = { |
| 28 |
'state': 'present', |
| 29 |
'hostname': '10.10.10.10', |
| 30 |
'username': 'admin', |
| 31 |
'https': 'true', |
| 32 |
'validate_certs': 'false', |
| 33 |
'password': 'password', |
| 34 |
'use_rest': 'always' |
| 35 |
} |
| 36 |
return args |
| 37 |
|
| 38 |
|
| 39 |
# REST API canned responses when mocking send_request |
| 40 |
SRR = { |
| 41 |
# common responses |
| 42 |
'is_rest': (200, dict(version=dict(generation=9, major=9, minor=0, full='dummy')), None), |
| 43 |
'is_rest_9_6': (200, dict(version=dict(generation=9, major=6, minor=0, full='dummy')), None), |
| 44 |
'is_rest_9_8': (200, dict(version=dict(generation=9, major=8, minor=0, full='dummy')), None), |
| 45 |
'is_zapi': (400, {}, "Unreachable"), |
| 46 |
'empty_good': (200, {}, None), |
| 47 |
'zero_record': (200, dict(records=[], num_records=0), None), |
| 48 |
'one_record_uuid': (200, dict(records=[dict(uuid='a1b2c3')], num_records=1), None), |
| 49 |
'end_of_sequence': (500, None, "Unexpected call to send_request"), |
| 50 |
'server_error': (500, None, "Internal Server error"), |
| 51 |
'generic_error': (400, None, "Expected error"), |
| 52 |
'community_user_record': (200, { |
| 53 |
'records': [{ |
| 54 |
"name": "snmpv3user2", |
| 55 |
"authentication_method": "community", |
| 56 |
'engine_id': "80000315058e02057c0fb8e911bc9f005056bb942e" |
| 57 |
}], |
| 58 |
'num_records': 1 |
| 59 |
}, None), |
| 60 |
'snmp_usm_user_record': (200, { |
| 61 |
'records': [{ |
| 62 |
"name": "snmpv3user3", |
| 63 |
"authentication_method": "usm", |
| 64 |
'engine_id': "80000315058e02057c0fb8e911bc9f005056bb942e", |
| 65 |
'snmpv3': { |
| 66 |
'privacy_protocol': 'aes128', |
| 67 |
'authentication_password': 'humTdumt*@t0nAwa11', |
| 68 |
'authentication_protocol': 'sha', |
| 69 |
'privacy_password': 'p@**GOandCLCt*200' |
| 70 |
} |
| 71 |
}], |
| 72 |
'num_records': 1 |
| 73 |
}, None), |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
def test_module_fail_when_required_args_missing(patch_ansible): |
| 78 |
''' required arguments are reported as errors ''' |
| 79 |
with pytest.raises(AnsibleFailJson) as exc: |
| 80 |
set_module_args(dict(hostname='')) |
| 81 |
my_module() |
| 82 |
print('Info: %s' % exc.value.args[0]['msg']) |
| 83 |
msg = 'missing required arguments: snmp_username' |
| 84 |
assert msg == exc.value.args[0]['msg'] |
| 85 |
|
| 86 |
|
| 87 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 88 |
def test_ensure_get_snmp_community_called(mock_request, patch_ansible): |
| 89 |
''' test get''' |
| 90 |
args = dict(default_args()) |
| 91 |
args['snmp_username'] = 'snmpv3user2' |
| 92 |
set_module_args(args) |
| 93 |
mock_request.side_effect = [ |
| 94 |
SRR['is_rest_9_8'], # get version |
| 95 |
SRR['community_user_record'], # get |
| 96 |
SRR['end_of_sequence'] |
| 97 |
] |
| 98 |
my_obj = my_module() |
| 99 |
with pytest.raises(AnsibleExitJson) as exc: |
| 100 |
my_obj.apply() |
| 101 |
print('Info: %s' % exc.value.args[0]) |
| 102 |
assert exc.value.args[0]['changed'] is False |
| 103 |
assert_no_warnings() |
| 104 |
|
| 105 |
|
| 106 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 107 |
def test_ensure_get_snmp_usm_called(mock_request, patch_ansible): |
| 108 |
''' test get''' |
| 109 |
args = dict(default_args()) |
| 110 |
args['snmp_username'] = 'snmpv3user3' |
| 111 |
set_module_args(args) |
| 112 |
mock_request.side_effect = [ |
| 113 |
SRR['is_rest_9_8'], # get version |
| 114 |
SRR['snmp_usm_user_record'], # get |
| 115 |
SRR['end_of_sequence'] |
| 116 |
] |
| 117 |
my_obj = my_module() |
| 118 |
with pytest.raises(AnsibleExitJson) as exc: |
| 119 |
my_obj.apply() |
| 120 |
print('Info: %s' % exc.value.args[0]) |
| 121 |
assert exc.value.args[0]['changed'] is False |
| 122 |
assert_no_warnings() |
| 123 |
|
| 124 |
|
| 125 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 126 |
def test_ensure_create_snmp_community_called(mock_request, patch_ansible): |
| 127 |
''' test get''' |
| 128 |
args = dict(default_args()) |
| 129 |
args['snmp_username'] = 'snmpv3user2' |
| 130 |
set_module_args(args) |
| 131 |
mock_request.side_effect = [ |
| 132 |
SRR['is_rest_9_8'], # get version |
| 133 |
SRR['zero_record'], # get |
| 134 |
SRR['empty_good'], # create |
| 135 |
SRR['end_of_sequence'] |
| 136 |
] |
| 137 |
my_obj = my_module() |
| 138 |
with pytest.raises(AnsibleExitJson) as exc: |
| 139 |
my_obj.apply() |
| 140 |
print('Info: %s' % exc.value.args[0]) |
| 141 |
assert exc.value.args[0]['changed'] is True |
| 142 |
assert_no_warnings() |
| 143 |
|
| 144 |
|
| 145 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 146 |
def test_ensure_create_snmp_usm_called(mock_request, patch_ansible): |
| 147 |
''' test get''' |
| 148 |
args = dict(default_args()) |
| 149 |
args['snmp_username'] = 'snmpv3user3' |
| 150 |
set_module_args(args) |
| 151 |
mock_request.side_effect = [ |
| 152 |
SRR['is_rest_9_8'], # get version |
| 153 |
SRR['zero_record'], # get |
| 154 |
SRR['empty_good'], # create |
| 155 |
SRR['end_of_sequence'] |
| 156 |
] |
| 157 |
my_obj = my_module() |
| 158 |
with pytest.raises(AnsibleExitJson) as exc: |
| 159 |
my_obj.apply() |
| 160 |
print('Info: %s' % exc.value.args[0]) |
| 161 |
assert exc.value.args[0]['changed'] is True |
| 162 |
assert_no_warnings() |
| 163 |
|
| 164 |
|
| 165 |
def test_fail_to_create_snmp_usm_without_snmpv3_passwords(patch_ansible): |
| 166 |
''' required arguments are reported as errors ''' |
| 167 |
usm_record = { |
| 168 |
'snmp_username': 'usm19', |
| 169 |
'authentication_method': 'usm', |
| 170 |
'snmpv3': { |
| 171 |
'privacy_protocol': 'aes128', |
| 172 |
'authentication_protocol': 'sha' |
| 173 |
} |
| 174 |
} |
| 175 |
with pytest.raises(AnsibleFailJson) as exc: |
| 176 |
set_module_args(usm_record) |
| 177 |
my_module() |
| 178 |
print('Info: %s' % exc.value.args[0]['msg']) |
| 179 |
msg = 'missing required arguments:' |
| 180 |
assert msg in exc.value.args[0]['msg'] |
| 181 |
|
| 182 |
|
| 183 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 184 |
def test_ensure_delete_snmp_community_called(mock_request, patch_ansible): |
| 185 |
''' test get''' |
| 186 |
args = dict(default_args()) |
| 187 |
args['snmp_username'] = 'snmpv3user2' |
| 188 |
args['state'] = 'absent' |
| 189 |
set_module_args(args) |
| 190 |
mock_request.side_effect = [ |
| 191 |
SRR['is_rest_9_8'], # get version |
| 192 |
SRR['community_user_record'], # get |
| 193 |
SRR['community_user_record'], |
| 194 |
SRR['empty_good'], # delete |
| 195 |
SRR['end_of_sequence'] |
| 196 |
] |
| 197 |
my_obj = my_module() |
| 198 |
with pytest.raises(AnsibleExitJson) as exc: |
| 199 |
my_obj.apply() |
| 200 |
print('Info: %s' % exc.value.args[0]) |
| 201 |
assert exc.value.args[0]['changed'] is True |
| 202 |
assert_no_warnings() |
| 203 |
|
| 204 |
|
| 205 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 206 |
def test_ensure_delete_snmp_usm_called(mock_request, patch_ansible): |
| 207 |
''' test get''' |
| 208 |
args = dict(default_args()) |
| 209 |
args['snmp_username'] = 'snmpv3user3' |
| 210 |
args['state'] = 'absent' |
| 211 |
set_module_args(args) |
| 212 |
mock_request.side_effect = [ |
| 213 |
SRR['is_rest_9_8'], # get version |
| 214 |
SRR['snmp_usm_user_record'], # get |
| 215 |
SRR['snmp_usm_user_record'], |
| 216 |
SRR['empty_good'], # delete |
| 217 |
SRR['end_of_sequence'] |
| 218 |
] |
| 219 |
my_obj = my_module() |
| 220 |
with pytest.raises(AnsibleExitJson) as exc: |
| 221 |
my_obj.apply() |
| 222 |
print('Info: %s' % exc.value.args[0]) |
| 223 |
assert exc.value.args[0]['changed'] is True |
| 224 |
assert_no_warnings() |
| 225 |
|
| 226 |
|
| 227 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 228 |
def test_ensure_delete_snmp_community_idempotent(mock_request, patch_ansible): |
| 229 |
''' test get''' |
| 230 |
args = dict(default_args()) |
| 231 |
args['snmp_username'] = 'snmpv3user2' |
| 232 |
args['state'] = 'absent' |
| 233 |
set_module_args(args) |
| 234 |
mock_request.side_effect = [ |
| 235 |
SRR['is_rest_9_8'], # get version |
| 236 |
SRR['zero_record'], # get |
| 237 |
SRR['end_of_sequence'] |
| 238 |
] |
| 239 |
my_obj = my_module() |
| 240 |
with pytest.raises(AnsibleExitJson) as exc: |
| 241 |
my_obj.apply() |
| 242 |
print('Info: %s' % exc.value.args[0]) |
| 243 |
assert exc.value.args[0]['changed'] is False |
| 244 |
assert_no_warnings() |
| 245 |
|
| 246 |
|
| 247 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 248 |
def test_ensure_delete_snmp_usm_idempotent(mock_request, patch_ansible): |
| 249 |
''' test get''' |
| 250 |
args = dict(default_args()) |
| 251 |
args['snmp_username'] = 'snmpv3user3' |
| 252 |
args['state'] = 'absent' |
| 253 |
set_module_args(args) |
| 254 |
mock_request.side_effect = [ |
| 255 |
SRR['is_rest_9_8'], # get version |
| 256 |
SRR['zero_record'], # get |
| 257 |
SRR['end_of_sequence'] |
| 258 |
] |
| 259 |
my_obj = my_module() |
| 260 |
with pytest.raises(AnsibleExitJson) as exc: |
| 261 |
my_obj.apply() |
| 262 |
print('Info: %s' % exc.value.args[0]) |
| 263 |
assert exc.value.args[0]['changed'] is False |
| 264 |
assert_no_warnings() |