Debian Codegraph @

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

raw
1 # Copyright (c) 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_ipaddress.py - REST features '''
5 from __future__ import (absolute_import, division, print_function)
6 __metaclass__ = type
7
8 import pytest
9 import sys
10
11 from ansible.module_utils import basic
12 from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch
13 # pylint: disable=unused-import
14 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import expect_and_capture_ansible_exception, patch_ansible, create_module
15 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
16 from ansible_collections.netapp.ontap.plugins.module_utils import netapp_ipaddress
17
18 if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7):
19 pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available')
20
21
22 class MockONTAPModule(object):
23 def __init__(self):
24 self.module = basic.AnsibleModule(netapp_utils.na_ontap_host_argument_spec())
25
26
27 def create_ontap_module(args=None):
28 if args is None:
29 args = {'hostname': 'xxx'}
30 return create_module(MockONTAPModule, args)
31
32
33 def test_check_ipaddress_is_present():
34 assert netapp_ipaddress._check_ipaddress_is_present(None) is None
35
36
37 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp_ipaddress.HAS_IPADDRESS_LIB', False)
38 def test_module_fail_when_netapp_lib_missing():
39 ''' required lib missing '''
40 error = 'Error: the python ipaddress package is required for this module. Import error: None'
41 assert error in expect_and_capture_ansible_exception(netapp_ipaddress._check_ipaddress_is_present, 'fail', create_ontap_module().module)['msg']
42
43
44 def test_validate_and_compress_ip_address():
45 module = create_ontap_module().module
46 valid_addresses = [
47 # IPv4
48 ['10.11.12.13', '10.11.12.13'],
49 # IPv6
50 ['1111:0123:0012:0001:abcd:0abc:9891:abcd', '1111:123:12:1:abcd:abc:9891:abcd'],
51 ['1111:0000:0000:0000:abcd:0abc:9891:abcd', '1111::abcd:abc:9891:abcd'],
52 ['1111:0000:0000:0012:abcd:0000:0000:abcd', '1111::12:abcd:0:0:abcd'],
53 ['ffff:ffff:0000:0000:0000:0000:0000:0000', 'ffff:ffff::'],
54 ]
55 for before, after in valid_addresses:
56 assert after == netapp_ipaddress.validate_and_compress_ip_address(before, module)
57
58
59 def test_negative_validate_and_compress_ip_address():
60 module = create_ontap_module().module
61 invalid_addresses = [
62 # IPv4
63 ['10.11.12.345', 'Invalid IP address value 10.11.12.345'],
64 # IPv6
65 ['1111:0123:0012:0001:abcd:0abc:9891:abcg', 'Invalid IP address value'],
66 ['1111:0000:0000:0000:abcd:9891:abcd', 'Invalid IP address value'],
67 ['1111:::0012:abcd::abcd', 'Invalid IP address value'],
68 ]
69 for before, error in invalid_addresses:
70 assert error in expect_and_capture_ansible_exception(netapp_ipaddress.validate_and_compress_ip_address, 'fail', before, module)['msg']
71
72
73 def test_netmask_to_len():
74 module = create_ontap_module().module
75 assert netapp_ipaddress.netmask_to_netmask_length('10.10.10.10', '255.255.0.0', module) == 16
76 assert netapp_ipaddress.netmask_to_netmask_length('1111::', 16, module) == 16
77 assert netapp_ipaddress.netmask_to_netmask_length('1111::', '16', module) == 16
78 error = 'Error: only prefix_len is supported for IPv6 addresses, got ffff::'
79 assert error in expect_and_capture_ansible_exception(netapp_ipaddress.netmask_to_netmask_length, 'fail', '1111::', 'ffff::', module)['msg']
80 error = 'Error: Invalid IP network value 10.11.12.13/abc.'
81 assert error in expect_and_capture_ansible_exception(netapp_ipaddress.netmask_to_netmask_length, 'fail', '10.11.12.13', 'abc', module)['msg']
82
83
84 def test_len_to_netmask():
85 module = create_ontap_module().module
86 assert netapp_ipaddress.netmask_length_to_netmask('10.10.10.10', 16, module) == '255.255.0.0'
87 assert netapp_ipaddress.netmask_length_to_netmask('1111::', 16, module) == 'ffff::'
88
89
90 def test_validate_ip_address_is_network_address():
91 module = create_ontap_module().module
92 assert netapp_ipaddress.validate_ip_address_is_network_address('10.11.12.0', module) is None
93 assert netapp_ipaddress.validate_ip_address_is_network_address('10.11.12.0/24', module) is None
94 error = 'Error: Invalid IP network value 10.11.12.0/21'
95 assert error in expect_and_capture_ansible_exception(netapp_ipaddress.validate_ip_address_is_network_address, 'fail', '10.11.12.0/21', module)['msg']