diff options
Diffstat (limited to 'test/units/module_utils/facts/network')
4 files changed, 408 insertions, 0 deletions
diff --git a/test/units/module_utils/facts/network/__init__.py b/test/units/module_utils/facts/network/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/units/module_utils/facts/network/__init__.py diff --git a/test/units/module_utils/facts/network/test_fc_wwn.py b/test/units/module_utils/facts/network/test_fc_wwn.py new file mode 100644 index 0000000..32a3a43 --- /dev/null +++ b/test/units/module_utils/facts/network/test_fc_wwn.py @@ -0,0 +1,137 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +from ansible.module_utils.facts.network import fc_wwn +from units.compat.mock import Mock + + +# AIX lsdev +LSDEV_OUTPUT = """ +fcs0 Defined 00-00 8Gb PCI Express Dual Port FC Adapter (df1000f114108a03) +fcs1 Available 04-00 8Gb PCI Express Dual Port FC Adapter (df1000f114108a03) +""" + +# a bit cutted output of lscfg (from Z0 to ZC) +LSCFG_OUTPUT = """ + fcs1 U78CB.001.WZS00ZS-P1-C9-T1 8Gb PCI Express Dual Port FC Adapter (df1000f114108a03) + + Part Number.................00E0806 + Serial Number...............1C4090830F + Manufacturer................001C + EC Level.................... D77161 + Customer Card ID Number.....577D + FRU Number..................00E0806 + Device Specific.(ZM)........3 + Network Address.............10000090FA551508 + ROS Level and ID............027820B7 + Device Specific.(Z0)........31004549 + Device Specific.(ZC)........00000000 + Hardware Location Code......U78CB.001.WZS00ZS-P1-C9-T1 +""" + +# Solaris +FCINFO_OUTPUT = """ +HBA Port WWN: 10000090fa1658de + Port Mode: Initiator + Port ID: 30100 + OS Device Name: /dev/cfg/c13 + Manufacturer: Emulex + Model: LPe12002-S + Firmware Version: LPe12002-S 2.01a12 + FCode/BIOS Version: Boot:5.03a0 Fcode:3.01a1 + Serial Number: 4925381+13090001ER + Driver Name: emlxs + Driver Version: 3.3.00.1 (2018.01.05.16.30) + Type: N-port + State: online + Supported Speeds: 2Gb 4Gb 8Gb + Current Speed: 8Gb + Node WWN: 20000090fa1658de + NPIV Not Supported +""" + +IOSCAN_OUT = """ +Class I H/W Path Driver S/W State H/W Type Description +================================================================== +fc 0 2/0/10/1/0 fcd CLAIMED INTERFACE HP AB379-60101 4Gb Dual Port PCI/PCI-X Fibre Channel Adapter (FC Port 1) + /dev/fcd0 +""" + +FCMSUTIL_OUT = """ + Vendor ID is = 0x1077 + Device ID is = 0x2422 + PCI Sub-system Vendor ID is = 0x103C + PCI Sub-system ID is = 0x12D7 + PCI Mode = PCI-X 133 MHz + ISP Code version = 5.4.0 + ISP Chip version = 3 + Topology = PTTOPT_FABRIC + Link Speed = 4Gb + Local N_Port_id is = 0x010300 + Previous N_Port_id is = None + N_Port Node World Wide Name = 0x50060b00006975ed + N_Port Port World Wide Name = 0x50060b00006975ec + Switch Port World Wide Name = 0x200300051e046c0f + Switch Node World Wide Name = 0x100000051e046c0f + N_Port Symbolic Port Name = server1_fcd0 + N_Port Symbolic Node Name = server1_HP-UX_B.11.31 + Driver state = ONLINE + Hardware Path is = 2/0/10/1/0 + Maximum Frame Size = 2048 + Driver-Firmware Dump Available = NO + Driver-Firmware Dump Timestamp = N/A + TYPE = PFC + NPIV Supported = YES + Driver Version = @(#) fcd B.11.31.1103 Dec 6 2010 +""" + + +def mock_get_bin_path(cmd, required=False, opt_dirs=None): + result = None + if cmd == 'lsdev': + result = '/usr/sbin/lsdev' + elif cmd == 'lscfg': + result = '/usr/sbin/lscfg' + elif cmd == 'fcinfo': + result = '/usr/sbin/fcinfo' + elif cmd == 'ioscan': + result = '/usr/bin/ioscan' + elif cmd == 'fcmsutil': + result = '/opt/fcms/bin/fcmsutil' + return result + + +def mock_run_command(cmd): + rc = 0 + if 'lsdev' in cmd: + result = LSDEV_OUTPUT + elif 'lscfg' in cmd: + result = LSCFG_OUTPUT + elif 'fcinfo' in cmd: + result = FCINFO_OUTPUT + elif 'ioscan' in cmd: + result = IOSCAN_OUT + elif 'fcmsutil' in cmd: + result = FCMSUTIL_OUT + else: + rc = 1 + result = 'Error' + return (rc, result, '') + + +def test_get_fc_wwn_info(mocker): + module = Mock() + inst = fc_wwn.FcWwnInitiatorFactCollector() + + mocker.patch.object(module, 'get_bin_path', side_effect=mock_get_bin_path) + mocker.patch.object(module, 'run_command', side_effect=mock_run_command) + + d = {'aix6': ['10000090FA551508'], 'sunos5': ['10000090fa1658de'], 'hp-ux11': ['0x50060b00006975ec']} + for key, value in d.items(): + mocker.patch('sys.platform', key) + wwn_expected = {"fibre_channel_wwn": value} + assert wwn_expected == inst.collect(module=module) diff --git a/test/units/module_utils/facts/network/test_generic_bsd.py b/test/units/module_utils/facts/network/test_generic_bsd.py new file mode 100644 index 0000000..f061f04 --- /dev/null +++ b/test/units/module_utils/facts/network/test_generic_bsd.py @@ -0,0 +1,217 @@ +# -*- coding: utf-8 -*- +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. +# + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from units.compat.mock import Mock +from units.compat import unittest + +from ansible.module_utils.facts.network import generic_bsd + + +def get_bin_path(command): + if command == 'ifconfig': + return 'fake/ifconfig' + elif command == 'route': + return 'fake/route' + return None + + +netbsd_ifconfig_a_out_7_1 = r''' +lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33624 + inet 127.0.0.1 netmask 0xff000000 + inet6 ::1 prefixlen 128 + inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 +re0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500 + capabilities=3f80<TSO4,IP4CSUM_Rx,IP4CSUM_Tx,TCP4CSUM_Rx,TCP4CSUM_Tx> + capabilities=3f80<UDP4CSUM_Rx,UDP4CSUM_Tx> + enabled=0 + ec_capabilities=3<VLAN_MTU,VLAN_HWTAGGING> + ec_enabled=0 + address: 52:54:00:63:55:af + media: Ethernet autoselect (100baseTX full-duplex) + status: active + inet 192.168.122.205 netmask 0xffffff00 broadcast 192.168.122.255 + inet6 fe80::5054:ff:fe63:55af%re0 prefixlen 64 scopeid 0x2 +''' + +netbsd_ifconfig_a_out_post_7_1 = r''' +lo0: flags=0x8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33624 + inet 127.0.0.1/8 flags 0x0 + inet6 ::1/128 flags 0x20<NODAD> + inet6 fe80::1%lo0/64 flags 0x0 scopeid 0x1 +re0: flags=0x8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500 + capabilities=3f80<TSO4,IP4CSUM_Rx,IP4CSUM_Tx,TCP4CSUM_Rx,TCP4CSUM_Tx> + capabilities=3f80<UDP4CSUM_Rx,UDP4CSUM_Tx> + enabled=0 + ec_capabilities=3<VLAN_MTU,VLAN_HWTAGGING> + ec_enabled=0 + address: 52:54:00:63:55:af + media: Ethernet autoselect (100baseTX full-duplex) + status: active + inet 192.168.122.205/24 broadcast 192.168.122.255 flags 0x0 + inet6 fe80::5054:ff:fe63:55af%re0/64 flags 0x0 scopeid 0x2 +''' + +NETBSD_EXPECTED = {'all_ipv4_addresses': ['192.168.122.205'], + 'all_ipv6_addresses': ['fe80::5054:ff:fe63:55af%re0'], + 'default_ipv4': {}, + 'default_ipv6': {}, + 'interfaces': ['lo0', 're0'], + 'lo0': {'device': 'lo0', + 'flags': ['UP', 'LOOPBACK', 'RUNNING', 'MULTICAST'], + 'ipv4': [{'address': '127.0.0.1', + 'broadcast': '127.255.255.255', + 'netmask': '255.0.0.0', + 'network': '127.0.0.0'}], + 'ipv6': [{'address': '::1', 'prefix': '128'}, + {'address': 'fe80::1%lo0', 'prefix': '64', 'scope': '0x1'}], + 'macaddress': 'unknown', + 'mtu': '33624', + 'type': 'loopback'}, + 're0': {'device': 're0', + 'flags': ['UP', 'BROADCAST', 'RUNNING', 'SIMPLEX', 'MULTICAST'], + 'ipv4': [{'address': '192.168.122.205', + 'broadcast': '192.168.122.255', + 'netmask': '255.255.255.0', + 'network': '192.168.122.0'}], + 'ipv6': [{'address': 'fe80::5054:ff:fe63:55af%re0', + 'prefix': '64', + 'scope': '0x2'}], + 'macaddress': 'unknown', + 'media': 'Ethernet', + 'media_options': [], + 'media_select': 'autoselect', + 'media_type': '100baseTX', + 'mtu': '1500', + 'status': 'active', + 'type': 'ether'}} + + +def run_command_old_ifconfig(command): + if command == 'fake/route': + return 0, 'Foo', '' + if command == ['fake/ifconfig', '-a']: + return 0, netbsd_ifconfig_a_out_7_1, '' + return 1, '', '' + + +def run_command_post_7_1_ifconfig(command): + if command == 'fake/route': + return 0, 'Foo', '' + if command == ['fake/ifconfig', '-a']: + return 0, netbsd_ifconfig_a_out_post_7_1, '' + return 1, '', '' + + +class TestGenericBsdNetworkNetBSD(unittest.TestCase): + gather_subset = ['all'] + + def setUp(self): + self.maxDiff = None + self.longMessage = True + + # TODO: extract module run_command/get_bin_path usage to methods I can mock without mocking all of run_command + def test(self): + module = self._mock_module() + module.get_bin_path.side_effect = get_bin_path + module.run_command.side_effect = run_command_old_ifconfig + + bsd_net = generic_bsd.GenericBsdIfconfigNetwork(module) + + res = bsd_net.populate() + self.assertDictEqual(res, NETBSD_EXPECTED) + + def test_ifconfig_post_7_1(self): + module = self._mock_module() + module.get_bin_path.side_effect = get_bin_path + module.run_command.side_effect = run_command_post_7_1_ifconfig + + bsd_net = generic_bsd.GenericBsdIfconfigNetwork(module) + + res = bsd_net.populate() + self.assertDictEqual(res, NETBSD_EXPECTED) + + def test_netbsd_ifconfig_old_and_new(self): + module_new = self._mock_module() + module_new.get_bin_path.side_effect = get_bin_path + module_new.run_command.side_effect = run_command_post_7_1_ifconfig + + bsd_net_new = generic_bsd.GenericBsdIfconfigNetwork(module_new) + res_new = bsd_net_new.populate() + + module_old = self._mock_module() + module_old.get_bin_path.side_effect = get_bin_path + module_old.run_command.side_effect = run_command_old_ifconfig + + bsd_net_old = generic_bsd.GenericBsdIfconfigNetwork(module_old) + res_old = bsd_net_old.populate() + + self.assertDictEqual(res_old, res_new) + self.assertDictEqual(res_old, NETBSD_EXPECTED) + self.assertDictEqual(res_new, NETBSD_EXPECTED) + + def _mock_module(self): + mock_module = Mock() + mock_module.params = {'gather_subset': self.gather_subset, + 'gather_timeout': 5, + 'filter': '*'} + mock_module.get_bin_path = Mock(return_value=None) + return mock_module + + def test_ensure_correct_netmask_parsing(self): + n = generic_bsd.GenericBsdIfconfigNetwork(None) + lines = [ + 'inet 192.168.7.113 netmask 0xffffff00 broadcast 192.168.7.255', + 'inet 10.109.188.206 --> 10.109.188.206 netmask 0xffffe000', + ] + expected = [ + ( + { + 'ipv4': [ + { + 'address': '192.168.7.113', + 'netmask': '255.255.255.0', + 'network': '192.168.7.0', + 'broadcast': '192.168.7.255' + } + ] + }, + {'all_ipv4_addresses': ['192.168.7.113']}, + ), + ( + { + 'ipv4': [ + { + 'address': '10.109.188.206', + 'netmask': '255.255.224.0', + 'network': '10.109.160.0', + 'broadcast': '10.109.191.255' + } + ] + }, + {'all_ipv4_addresses': ['10.109.188.206']}, + ), + ] + for i, line in enumerate(lines): + words = line.split() + current_if = {'ipv4': []} + ips = {'all_ipv4_addresses': []} + n.parse_inet_line(words, current_if, ips) + self.assertDictEqual(current_if, expected[i][0]) + self.assertDictEqual(ips, expected[i][1]) diff --git a/test/units/module_utils/facts/network/test_iscsi_get_initiator.py b/test/units/module_utils/facts/network/test_iscsi_get_initiator.py new file mode 100644 index 0000000..2048ba2 --- /dev/null +++ b/test/units/module_utils/facts/network/test_iscsi_get_initiator.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +from ansible.module_utils.facts.network import iscsi +from units.compat.mock import Mock + + +# AIX # lsattr -E -l iscsi0 +LSATTR_OUTPUT = """ +disc_filename /etc/iscsi/targets Configuration file False +disc_policy file Discovery Policy True +initiator_name iqn.localhost.hostid.7f000002 iSCSI Initiator Name True +isns_srvnames auto iSNS Servers IP Addresses True +isns_srvports iSNS Servers Port Numbers True +max_targets 16 Maximum Targets Allowed True +num_cmd_elems 200 Maximum number of commands to queue to driver True +""" + +# HP-UX # iscsiutil -l +ISCSIUTIL_OUTPUT = """ +Initiator Name : iqn.2001-04.com.hp.stor:svcio +Initiator Alias : +Authentication Method : None +CHAP Method : CHAP_UNI +Initiator CHAP Name : +CHAP Secret : +NAS Hostname : +NAS Secret : +Radius Server Hostname : +Header Digest : None,CRC32C (default) +Data Digest : None,CRC32C (default) +SLP Scope list for iSLPD : +""" + + +def test_get_iscsi_info(mocker): + module = Mock() + inst = iscsi.IscsiInitiatorNetworkCollector() + + mocker.patch('sys.platform', 'aix6') + mocker.patch('ansible.module_utils.facts.network.iscsi.get_bin_path', return_value='/usr/sbin/lsattr') + mocker.patch.object(module, 'run_command', return_value=(0, LSATTR_OUTPUT, '')) + aix_iscsi_expected = {"iscsi_iqn": "iqn.localhost.hostid.7f000002"} + assert aix_iscsi_expected == inst.collect(module=module) + + mocker.patch('sys.platform', 'hp-ux') + mocker.patch('ansible.module_utils.facts.network.iscsi.get_bin_path', return_value='/opt/iscsi/bin/iscsiutil') + mocker.patch.object(module, 'run_command', return_value=(0, ISCSIUTIL_OUTPUT, '')) + hpux_iscsi_expected = {"iscsi_iqn": " iqn.2001-04.com.hp.stor:svcio"} + assert hpux_iscsi_expected == inst.collect(module=module) |