|
|
@ -1,28 +1,15 @@
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright: Contributors to the Ansible project
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.facts.network import generic_bsd
|
|
|
|
from ansible.module_utils.facts.network import generic_bsd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_bin_path(command, warning=None):
|
|
|
|
def mock_get_bin_path(command, warning=None):
|
|
|
|
cmds = {
|
|
|
|
cmds = {
|
|
|
|
'ifconfig': 'fake/ifconfig',
|
|
|
|
'ifconfig': 'fake/ifconfig',
|
|
|
|
'route': 'fake/route',
|
|
|
|
'route': 'fake/route',
|
|
|
@ -30,7 +17,7 @@ def get_bin_path(command, warning=None):
|
|
|
|
return cmds.get(command, None)
|
|
|
|
return cmds.get(command, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
netbsd_ifconfig_a_out_7_1 = r'''
|
|
|
|
NETBSD_IFCONFIG_A_OUT_7_1 = r'''
|
|
|
|
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33624
|
|
|
|
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33624
|
|
|
|
inet 127.0.0.1 netmask 0xff000000
|
|
|
|
inet 127.0.0.1 netmask 0xff000000
|
|
|
|
inet6 ::1 prefixlen 128
|
|
|
|
inet6 ::1 prefixlen 128
|
|
|
@ -48,7 +35,7 @@ re0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
|
|
|
|
inet6 fe80::5054:ff:fe63:55af%re0 prefixlen 64 scopeid 0x2
|
|
|
|
inet6 fe80::5054:ff:fe63:55af%re0 prefixlen 64 scopeid 0x2
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
netbsd_ifconfig_a_out_post_7_1 = r'''
|
|
|
|
NETBSD_IFCONFIG_A_OUT_POST_7_1 = r'''
|
|
|
|
lo0: flags=0x8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33624
|
|
|
|
lo0: flags=0x8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33624
|
|
|
|
inet 127.0.0.1/8 flags 0x0
|
|
|
|
inet 127.0.0.1/8 flags 0x0
|
|
|
|
inet6 ::1/128 flags 0x20<NODAD>
|
|
|
|
inet6 ::1/128 flags 0x20<NODAD>
|
|
|
@ -101,84 +88,81 @@ NETBSD_EXPECTED = {'all_ipv4_addresses': ['192.168.122.205'],
|
|
|
|
'type': 'ether'}}
|
|
|
|
'type': 'ether'}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_command_old_ifconfig(command):
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
if command == 'fake/route':
|
|
|
|
("test_input", "expected"),
|
|
|
|
return 0, 'Foo', ''
|
|
|
|
[
|
|
|
|
if command == ['fake/ifconfig', '-a']:
|
|
|
|
pytest.param(
|
|
|
|
return 0, netbsd_ifconfig_a_out_7_1, ''
|
|
|
|
{
|
|
|
|
return 1, '', ''
|
|
|
|
"mock_run_command": [
|
|
|
|
|
|
|
|
(0, "Foo", ""),
|
|
|
|
|
|
|
|
(0, "Foo", ""),
|
|
|
|
def run_command_post_7_1_ifconfig(command):
|
|
|
|
(0, NETBSD_IFCONFIG_A_OUT_7_1, ""),
|
|
|
|
if command == 'fake/route':
|
|
|
|
],
|
|
|
|
return 0, 'Foo', ''
|
|
|
|
},
|
|
|
|
if command == ['fake/ifconfig', '-a']:
|
|
|
|
NETBSD_EXPECTED,
|
|
|
|
return 0, netbsd_ifconfig_a_out_post_7_1, ''
|
|
|
|
id="old-ifconfig",
|
|
|
|
return 1, '', ''
|
|
|
|
),
|
|
|
|
|
|
|
|
pytest.param(
|
|
|
|
|
|
|
|
{
|
|
|
|
class TestGenericBsdNetworkNetBSD(unittest.TestCase):
|
|
|
|
"mock_run_command": [
|
|
|
|
gather_subset = ['all']
|
|
|
|
(0, "Foo", ""),
|
|
|
|
|
|
|
|
(0, "Foo", ""),
|
|
|
|
def setUp(self):
|
|
|
|
(0, NETBSD_IFCONFIG_A_OUT_POST_7_1, ""),
|
|
|
|
self.maxDiff = None
|
|
|
|
],
|
|
|
|
self.longMessage = True
|
|
|
|
},
|
|
|
|
|
|
|
|
NETBSD_EXPECTED,
|
|
|
|
# TODO: extract module run_command/get_bin_path usage to methods I can mock without mocking all of run_command
|
|
|
|
id="post-7-1-ifconfig",
|
|
|
|
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
|
|
|
|
def test_generic_bsd_ifconfig(mocker, test_input, expected):
|
|
|
|
|
|
|
|
module = mocker.MagicMock()
|
|
|
|
bsd_net = generic_bsd.GenericBsdIfconfigNetwork(module)
|
|
|
|
mocker.patch.object(module, "get_bin_path", side_effect=mock_get_bin_path)
|
|
|
|
|
|
|
|
mocker.patch.object(
|
|
|
|
res = bsd_net.populate()
|
|
|
|
module, "run_command", side_effect=test_input["mock_run_command"]
|
|
|
|
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)
|
|
|
|
bsd_net = generic_bsd.GenericBsdIfconfigNetwork(module)
|
|
|
|
|
|
|
|
|
|
|
|
res = bsd_net.populate()
|
|
|
|
res = bsd_net.populate()
|
|
|
|
self.assertDictEqual(res, NETBSD_EXPECTED)
|
|
|
|
assert res == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_netbsd_ifconfig_old_and_new(self):
|
|
|
|
|
|
|
|
module_new = self._mock_module()
|
|
|
|
def test_compare_old_new_ifconfig(mocker):
|
|
|
|
module_new.get_bin_path.side_effect = get_bin_path
|
|
|
|
old_ifconfig_module = mocker.MagicMock()
|
|
|
|
module_new.run_command.side_effect = run_command_post_7_1_ifconfig
|
|
|
|
mocker.patch.object(old_ifconfig_module, "get_bin_path", side_effect=mock_get_bin_path)
|
|
|
|
|
|
|
|
mocker.patch.object(
|
|
|
|
bsd_net_new = generic_bsd.GenericBsdIfconfigNetwork(module_new)
|
|
|
|
old_ifconfig_module,
|
|
|
|
res_new = bsd_net_new.populate()
|
|
|
|
"run_command",
|
|
|
|
|
|
|
|
side_effect=[
|
|
|
|
module_old = self._mock_module()
|
|
|
|
(0, "Foo", ""),
|
|
|
|
module_old.get_bin_path.side_effect = get_bin_path
|
|
|
|
(0, "Foo", ""),
|
|
|
|
module_old.run_command.side_effect = run_command_old_ifconfig
|
|
|
|
(0, NETBSD_IFCONFIG_A_OUT_7_1, ""),
|
|
|
|
|
|
|
|
],
|
|
|
|
bsd_net_old = generic_bsd.GenericBsdIfconfigNetwork(module_old)
|
|
|
|
)
|
|
|
|
res_old = bsd_net_old.populate()
|
|
|
|
old_bsd_net = generic_bsd.GenericBsdIfconfigNetwork(old_ifconfig_module)
|
|
|
|
|
|
|
|
old_res = old_bsd_net.populate()
|
|
|
|
self.assertDictEqual(res_old, res_new)
|
|
|
|
|
|
|
|
self.assertDictEqual(res_old, NETBSD_EXPECTED)
|
|
|
|
new_ifconfig_module = mocker.MagicMock()
|
|
|
|
self.assertDictEqual(res_new, NETBSD_EXPECTED)
|
|
|
|
mocker.patch.object(new_ifconfig_module, "get_bin_path", side_effect=mock_get_bin_path)
|
|
|
|
|
|
|
|
mocker.patch.object(
|
|
|
|
def _mock_module(self):
|
|
|
|
new_ifconfig_module,
|
|
|
|
mock_module = Mock()
|
|
|
|
"run_command",
|
|
|
|
mock_module.params = {'gather_subset': self.gather_subset,
|
|
|
|
side_effect=[
|
|
|
|
'gather_timeout': 5,
|
|
|
|
(0, "Foo", ""),
|
|
|
|
'filter': '*'}
|
|
|
|
(0, "Foo", ""),
|
|
|
|
mock_module.get_bin_path = Mock(return_value=None)
|
|
|
|
(0, NETBSD_IFCONFIG_A_OUT_POST_7_1, ""),
|
|
|
|
return mock_module
|
|
|
|
],
|
|
|
|
|
|
|
|
)
|
|
|
|
def test_ensure_correct_netmask_parsing(self):
|
|
|
|
new_bsd_net = generic_bsd.GenericBsdIfconfigNetwork(new_ifconfig_module)
|
|
|
|
n = generic_bsd.GenericBsdIfconfigNetwork(None)
|
|
|
|
new_res = new_bsd_net.populate()
|
|
|
|
lines = [
|
|
|
|
assert old_res == new_res
|
|
|
|
'inet 192.168.7.113 netmask 0xffffff00 broadcast 192.168.7.255',
|
|
|
|
|
|
|
|
'inet 10.109.188.206 --> 10.109.188.206 netmask 0xffffe000',
|
|
|
|
|
|
|
|
]
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
expected = [
|
|
|
|
("test_input", "expected"),
|
|
|
|
|
|
|
|
[
|
|
|
|
|
|
|
|
pytest.param(
|
|
|
|
|
|
|
|
"inet 192.168.7.113 netmask 0xffffff00 broadcast 192.168.7.255",
|
|
|
|
(
|
|
|
|
(
|
|
|
|
{
|
|
|
|
{
|
|
|
|
'ipv4': [
|
|
|
|
'ipv4': [
|
|
|
@ -186,12 +170,16 @@ class TestGenericBsdNetworkNetBSD(unittest.TestCase):
|
|
|
|
'address': '192.168.7.113',
|
|
|
|
'address': '192.168.7.113',
|
|
|
|
'netmask': '255.255.255.0',
|
|
|
|
'netmask': '255.255.255.0',
|
|
|
|
'network': '192.168.7.0',
|
|
|
|
'network': '192.168.7.0',
|
|
|
|
'broadcast': '192.168.7.255'
|
|
|
|
'broadcast': '192.168.7.255',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{'all_ipv4_addresses': ['192.168.7.113']},
|
|
|
|
{'all_ipv4_addresses': ['192.168.7.113']},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
id="ifconfig-output-1",
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
pytest.param(
|
|
|
|
|
|
|
|
"inet 10.109.188.206 --> 10.109.188.206 netmask 0xffffe000",
|
|
|
|
(
|
|
|
|
(
|
|
|
|
{
|
|
|
|
{
|
|
|
|
'ipv4': [
|
|
|
|
'ipv4': [
|
|
|
@ -199,17 +187,21 @@ class TestGenericBsdNetworkNetBSD(unittest.TestCase):
|
|
|
|
'address': '10.109.188.206',
|
|
|
|
'address': '10.109.188.206',
|
|
|
|
'netmask': '255.255.224.0',
|
|
|
|
'netmask': '255.255.224.0',
|
|
|
|
'network': '10.109.160.0',
|
|
|
|
'network': '10.109.160.0',
|
|
|
|
'broadcast': '10.109.191.255'
|
|
|
|
'broadcast': '10.109.191.255',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{'all_ipv4_addresses': ['10.109.188.206']},
|
|
|
|
{'all_ipv4_addresses': ['10.109.188.206']},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
id="ifconfig-output-2",
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
),
|
|
|
|
words = line.split()
|
|
|
|
],
|
|
|
|
current_if = {'ipv4': []}
|
|
|
|
)
|
|
|
|
ips = {'all_ipv4_addresses': []}
|
|
|
|
def test_ensure_correct_netmask_parsing(test_input, expected):
|
|
|
|
|
|
|
|
n = generic_bsd.GenericBsdIfconfigNetwork(None)
|
|
|
|
|
|
|
|
words = test_input.split()
|
|
|
|
|
|
|
|
current_if = {"ipv4": []}
|
|
|
|
|
|
|
|
ips = {"all_ipv4_addresses": []}
|
|
|
|
n.parse_inet_line(words, current_if, ips)
|
|
|
|
n.parse_inet_line(words, current_if, ips)
|
|
|
|
self.assertDictEqual(current_if, expected[i][0])
|
|
|
|
assert current_if == expected[0]
|
|
|
|
self.assertDictEqual(ips, expected[i][1])
|
|
|
|
assert ips == expected[1]
|
|
|
|