Drop dependency on netifaces

The netifaces library was abandoned and archived. Replace it by psutil
which is already part of the requirements.

Closes-Bug: #2071596
Change-Id: Ibca206ec2af1374199d0c0cfad897dded1298733
This commit is contained in:
Takashi Kajinami 2024-10-06 22:08:13 +09:00
parent fe98640fa3
commit 42ea1dbd1a
3 changed files with 211 additions and 90 deletions

View File

@ -20,10 +20,10 @@ import socket
import struct import struct
import sys import sys
import netifaces
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log as logging from oslo_log import log as logging
from oslo_utils import netutils from oslo_utils import netutils
import psutil
from ironic_python_agent import utils from ironic_python_agent import utils
@ -214,31 +214,29 @@ def _get_lldp_info(interfaces):
return lldp_info return lldp_info
def get_default_ip_addr(type, interface_id): def get_default_ip_addr(family, interface_id):
"""Retrieve default IPv4 or IPv6 address.""" """Retrieve default IPv4, IPv6 or mac address."""
try: try:
addrs = netifaces.ifaddresses(interface_id) addrs = psutil.net_if_addrs()[interface_id]
return addrs[type][0]['addr'] for addr in addrs:
except (ValueError, IndexError, KeyError): if addr.family == family:
return addr.address
except KeyError:
# No default IP address found # No default IP address found
pass
return None return None
def get_ipv4_addr(interface_id): def get_ipv4_addr(interface_id):
return get_default_ip_addr(netifaces.AF_INET, interface_id) return get_default_ip_addr(socket.AF_INET, interface_id)
def get_ipv6_addr(interface_id): def get_ipv6_addr(interface_id):
return get_default_ip_addr(netifaces.AF_INET6, interface_id) return get_default_ip_addr(socket.AF_INET6, interface_id)
def get_mac_addr(interface_id): def get_mac_addr(interface_id):
try: return get_default_ip_addr(socket.AF_PACKET, interface_id)
addrs = netifaces.ifaddresses(interface_id)
return addrs[netifaces.AF_LINK][0]['addr']
except (ValueError, IndexError, KeyError):
# No mac address found
return None
# Other options... # Other options...

View File

@ -13,18 +13,19 @@
# limitations under the License. # limitations under the License.
import binascii import binascii
from collections import namedtuple
import glob import glob
import json import json
import logging import logging
import os import os
import re import re
import shutil import shutil
import socket
import stat import stat
import time import time
from unittest import mock from unittest import mock
from ironic_lib import utils as il_utils from ironic_lib import utils as il_utils
import netifaces
from oslo_concurrency import processutils from oslo_concurrency import processutils
from oslo_config import cfg from oslo_config import cfg
from oslo_utils import units from oslo_utils import units
@ -6123,30 +6124,32 @@ class TestCollectSystemLogs(base.IronicAgentTest):
self.assertGreaterEqual(len(io_dict), len(expected)) self.assertGreaterEqual(len(io_dict), len(expected))
FakeAddr = namedtuple('FakeAddr', ('family', 'address'))
@mock.patch.object(hardware.GenericHardwareManager, '_get_system_lshw_dict', @mock.patch.object(hardware.GenericHardwareManager, '_get_system_lshw_dict',
autospec=True, return_value={'id': 'host'}) autospec=True, return_value={'id': 'host'})
@mock.patch.object(hardware, 'get_managers', autospec=True, @mock.patch.object(hardware, 'get_managers', autospec=True,
return_value=[hardware.GenericHardwareManager()]) return_value=[hardware.GenericHardwareManager()])
@mock.patch('netifaces.ifaddresses', autospec=True) @mock.patch('psutil.net_if_addrs', autospec=True)
@mock.patch('os.listdir', autospec=True) @mock.patch('os.listdir', autospec=True)
@mock.patch('os.path.exists', autospec=True) @mock.patch('os.path.exists', autospec=True)
@mock.patch('builtins.open', autospec=True) @mock.patch('builtins.open', autospec=True)
@mock.patch.object(il_utils, 'execute', autospec=True) @mock.patch.object(il_utils, 'execute', autospec=True)
@mock.patch.object(netutils, 'get_mac_addr', autospec=True)
@mock.patch.object(netutils, 'interface_has_carrier', autospec=True) @mock.patch.object(netutils, 'interface_has_carrier', autospec=True)
class TestListNetworkInterfaces(base.IronicAgentTest): class TestListNetworkInterfaces(base.IronicAgentTest):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.hardware = hardware.GenericHardwareManager() self.hardware = hardware.GenericHardwareManager()
def test_list_network_interfaces(self, def test_list_network_interfaces(self,
mock_has_carrier, mock_has_carrier,
mock_get_mac,
mocked_execute, mocked_execute,
mocked_open, mocked_open,
mocked_exists, mocked_exists,
mocked_listdir, mocked_listdir,
mocked_ifaddresses, mocked_net_if_addrs,
mockedget_managers, mockedget_managers,
mocked_lshw): mocked_lshw):
mocked_lshw.return_value = json.loads(hws.LSHW_JSON_OUTPUT_V2[0]) mocked_lshw.return_value = json.loads(hws.LSHW_JSON_OUTPUT_V2[0])
@ -6156,16 +6159,24 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
mocked_open.return_value.__exit__ = mock.Mock() mocked_open.return_value.__exit__ = mock.Mock()
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
read_mock.side_effect = ['1'] read_mock.side_effect = ['1']
mocked_ifaddresses.return_value = { mocked_net_if_addrs.return_value = {
netifaces.AF_INET: [{'addr': '192.168.1.2'}], 'lo': [
netifaces.AF_INET6: [{'addr': 'fd00::101'}] FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'eth0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
],
'foobar': [
FakeAddr(socket.AF_INET, '192.168.2.2'),
FakeAddr(socket.AF_INET6, 'fd00:1000::101')
]
} }
mocked_execute.return_value = ('em0\n', '') mocked_execute.return_value = ('em0\n', '')
mock_has_carrier.return_value = True mock_has_carrier.return_value = True
mock_get_mac.side_effect = [
'00:0c:29:8c:11:b1',
None,
]
interfaces = self.hardware.list_network_interfaces() interfaces = self.hardware.list_network_interfaces()
self.assertEqual(1, len(interfaces)) self.assertEqual(1, len(interfaces))
self.assertEqual('eth0', interfaces[0].name) self.assertEqual('eth0', interfaces[0].name)
@ -6179,12 +6190,11 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
def test_list_network_interfaces_with_biosdevname(self, def test_list_network_interfaces_with_biosdevname(self,
mock_has_carrier, mock_has_carrier,
mock_get_mac,
mocked_execute, mocked_execute,
mocked_open, mocked_open,
mocked_exists, mocked_exists,
mocked_listdir, mocked_listdir,
mocked_ifaddresses, mocked_net_if_addrs,
mockedget_managers, mockedget_managers,
mocked_lshw): mocked_lshw):
mocked_listdir.return_value = ['lo', 'eth0'] mocked_listdir.return_value = ['lo', 'eth0']
@ -6193,12 +6203,19 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
mocked_open.return_value.__exit__ = mock.Mock() mocked_open.return_value.__exit__ = mock.Mock()
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
read_mock.side_effect = ['1'] read_mock.side_effect = ['1']
mocked_ifaddresses.return_value = { mocked_net_if_addrs.return_value = {
netifaces.AF_INET: [{'addr': '192.168.1.2'}], 'lo': [
netifaces.AF_INET6: [{'addr': 'fd00::101'}] FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'eth0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
]
} }
mocked_execute.return_value = ('em0\n', '') mocked_execute.return_value = ('em0\n', '')
mock_get_mac.return_value = '00:0c:29:8c:11:b1'
mock_has_carrier.return_value = True mock_has_carrier.return_value = True
interfaces = self.hardware.list_network_interfaces() interfaces = self.hardware.list_network_interfaces()
self.assertEqual(1, len(interfaces)) self.assertEqual(1, len(interfaces))
@ -6215,12 +6232,11 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
def test_list_network_interfaces_with_lldp(self, def test_list_network_interfaces_with_lldp(self,
mocked_lldp_info, mocked_lldp_info,
mock_has_carrier, mock_has_carrier,
mock_get_mac,
mocked_execute, mocked_execute,
mocked_open, mocked_open,
mocked_exists, mocked_exists,
mocked_listdir, mocked_listdir,
mocked_ifaddresses, mocked_net_if_addrs,
mockedget_managers, mockedget_managers,
mocked_lshw): mocked_lshw):
CONF.set_override('collect_lldp', True) CONF.set_override('collect_lldp', True)
@ -6230,9 +6246,17 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
mocked_open.return_value.__exit__ = mock.Mock() mocked_open.return_value.__exit__ = mock.Mock()
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
read_mock.side_effect = ['1'] read_mock.side_effect = ['1']
mocked_ifaddresses.return_value = { mocked_net_if_addrs.return_value = {
netifaces.AF_INET: [{'addr': '192.168.1.2'}], 'lo': [
netifaces.AF_INET6: [{'addr': 'fd00::101'}] FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'eth0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
]
} }
mocked_lldp_info.return_value = {'eth0': [ mocked_lldp_info.return_value = {'eth0': [
(0, b''), (0, b''),
@ -6241,7 +6265,6 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
(3, b'\x00x')] (3, b'\x00x')]
} }
mock_has_carrier.return_value = True mock_has_carrier.return_value = True
mock_get_mac.return_value = '00:0c:29:8c:11:b1'
mocked_execute.return_value = ('em0\n', '') mocked_execute.return_value = ('em0\n', '')
interfaces = self.hardware.list_network_interfaces() interfaces = self.hardware.list_network_interfaces()
self.assertEqual(1, len(interfaces)) self.assertEqual(1, len(interfaces))
@ -6260,10 +6283,16 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
self.assertEqual('em0', interfaces[0].biosdevname) self.assertEqual('em0', interfaces[0].biosdevname)
@mock.patch.object(netutils, 'get_lldp_info', autospec=True) @mock.patch.object(netutils, 'get_lldp_info', autospec=True)
def test_list_network_interfaces_with_lldp_error( def test_list_network_interfaces_with_lldp_error(self,
self, mocked_lldp_info, mock_has_carrier, mock_get_mac, mocked_lldp_info,
mocked_execute, mocked_open, mocked_exists, mocked_listdir, mock_has_carrier,
mocked_ifaddresses, mockedget_managers, mocked_lshw): mocked_execute,
mocked_open,
mocked_exists,
mocked_listdir,
mocked_net_if_addrs,
mockedget_managers,
mocked_lshw):
CONF.set_override('collect_lldp', True) CONF.set_override('collect_lldp', True)
mocked_listdir.return_value = ['lo', 'eth0'] mocked_listdir.return_value = ['lo', 'eth0']
mocked_exists.side_effect = [False, False, True] mocked_exists.side_effect = [False, False, True]
@ -6271,14 +6300,21 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
mocked_open.return_value.__exit__ = mock.Mock() mocked_open.return_value.__exit__ = mock.Mock()
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
read_mock.side_effect = ['1'] read_mock.side_effect = ['1']
mocked_ifaddresses.return_value = { mocked_net_if_addrs.return_value = {
netifaces.AF_INET: [{'addr': '192.168.1.2'}], 'lo': [
netifaces.AF_INET6: [{'addr': 'fd00::101'}] FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'eth0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
]
} }
mocked_lldp_info.side_effect = Exception('Boom!') mocked_lldp_info.side_effect = Exception('Boom!')
mocked_execute.return_value = ('em0\n', '') mocked_execute.return_value = ('em0\n', '')
mock_has_carrier.return_value = True mock_has_carrier.return_value = True
mock_get_mac.return_value = '00:0c:29:8c:11:b1'
interfaces = self.hardware.list_network_interfaces() interfaces = self.hardware.list_network_interfaces()
self.assertEqual(1, len(interfaces)) self.assertEqual(1, len(interfaces))
self.assertEqual('eth0', interfaces[0].name) self.assertEqual('eth0', interfaces[0].name)
@ -6291,12 +6327,11 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
def test_list_network_interfaces_no_carrier(self, def test_list_network_interfaces_no_carrier(self,
mock_has_carrier, mock_has_carrier,
mock_get_mac,
mocked_execute, mocked_execute,
mocked_open, mocked_open,
mocked_exists, mocked_exists,
mocked_listdir, mocked_listdir,
mocked_ifaddresses, mocked_net_if_addrs,
mockedget_managers, mockedget_managers,
mocked_lshw): mocked_lshw):
@ -6307,13 +6342,20 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
mocked_open.return_value.__exit__ = mock.Mock() mocked_open.return_value.__exit__ = mock.Mock()
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
read_mock.side_effect = [OSError('boom')] read_mock.side_effect = [OSError('boom')]
mocked_ifaddresses.return_value = { mocked_net_if_addrs.return_value = {
netifaces.AF_INET: [{'addr': '192.168.1.2'}], 'lo': [
netifaces.AF_INET6: [{'addr': 'fd00::101'}] FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'eth0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
]
} }
mocked_execute.return_value = ('em0\n', '') mocked_execute.return_value = ('em0\n', '')
mock_has_carrier.return_value = False mock_has_carrier.return_value = False
mock_get_mac.return_value = '00:0c:29:8c:11:b1'
interfaces = self.hardware.list_network_interfaces() interfaces = self.hardware.list_network_interfaces()
self.assertEqual(1, len(interfaces)) self.assertEqual(1, len(interfaces))
self.assertEqual('eth0', interfaces[0].name) self.assertEqual('eth0', interfaces[0].name)
@ -6326,12 +6368,11 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
def test_list_network_interfaces_with_vendor_info(self, def test_list_network_interfaces_with_vendor_info(self,
mock_has_carrier, mock_has_carrier,
mock_get_mac,
mocked_execute, mocked_execute,
mocked_open, mocked_open,
mocked_exists, mocked_exists,
mocked_listdir, mocked_listdir,
mocked_ifaddresses, mocked_net_if_addrs,
mockedget_managers, mockedget_managers,
mocked_lshw): mocked_lshw):
mocked_listdir.return_value = ['lo', 'eth0'] mocked_listdir.return_value = ['lo', 'eth0']
@ -6341,13 +6382,20 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
mac = '00:0c:29:8c:11:b1' mac = '00:0c:29:8c:11:b1'
read_mock.side_effect = ['0x15b3\n', '0x1014\n'] read_mock.side_effect = ['0x15b3\n', '0x1014\n']
mocked_ifaddresses.return_value = { mocked_net_if_addrs.return_value = {
netifaces.AF_INET: [{'addr': '192.168.1.2'}], 'lo': [
netifaces.AF_INET6: [{'addr': 'fd00::101'}] FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'eth0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
]
} }
mocked_execute.return_value = ('em0\n', '') mocked_execute.return_value = ('em0\n', '')
mock_has_carrier.return_value = True mock_has_carrier.return_value = True
mock_get_mac.return_value = mac
interfaces = self.hardware.list_network_interfaces() interfaces = self.hardware.list_network_interfaces()
self.assertEqual(1, len(interfaces)) self.assertEqual(1, len(interfaces))
self.assertEqual('eth0', interfaces[0].name) self.assertEqual('eth0', interfaces[0].name)
@ -6361,12 +6409,11 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
def test_list_network_interfaces_with_bond(self, def test_list_network_interfaces_with_bond(self,
mock_has_carrier, mock_has_carrier,
mock_get_mac,
mocked_execute, mocked_execute,
mocked_open, mocked_open,
mocked_exists, mocked_exists,
mocked_listdir, mocked_listdir,
mocked_ifaddresses, mocked_net_if_addrs,
mockedget_managers, mockedget_managers,
mocked_lshw): mocked_lshw):
mocked_listdir.return_value = ['lo', 'bond0'] mocked_listdir.return_value = ['lo', 'bond0']
@ -6375,16 +6422,20 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
mocked_open.return_value.__exit__ = mock.Mock() mocked_open.return_value.__exit__ = mock.Mock()
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
read_mock.side_effect = ['1'] read_mock.side_effect = ['1']
mocked_ifaddresses.return_value = { mocked_net_if_addrs.return_value = {
netifaces.AF_INET: [{'addr': '192.168.1.2'}], 'lo': [
netifaces.AF_INET6: [{'addr': 'fd00::101'}] FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'bond0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
]
} }
mocked_execute.return_value = ('\n', '') mocked_execute.return_value = ('\n', '')
mock_has_carrier.return_value = True mock_has_carrier.return_value = True
mock_get_mac.side_effect = [
'00:0c:29:8c:11:b1',
None,
]
interfaces = self.hardware.list_network_interfaces() interfaces = self.hardware.list_network_interfaces()
self.assertEqual(1, len(interfaces)) self.assertEqual(1, len(interfaces))
self.assertEqual('bond0', interfaces[0].name) self.assertEqual('bond0', interfaces[0].name)
@ -6397,12 +6448,11 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
def test_list_network_vlan_interfaces(self, def test_list_network_vlan_interfaces(self,
mock_has_carrier, mock_has_carrier,
mock_get_mac,
mocked_execute, mocked_execute,
mocked_open, mocked_open,
mocked_exists, mocked_exists,
mocked_listdir, mocked_listdir,
mocked_ifaddresses, mocked_net_if_addrs,
mockedget_managers, mockedget_managers,
mocked_lshw): mocked_lshw):
CONF.set_override('enable_vlan_interfaces', 'eth0.100') CONF.set_override('enable_vlan_interfaces', 'eth0.100')
@ -6412,13 +6462,25 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
mocked_open.return_value.__exit__ = mock.Mock() mocked_open.return_value.__exit__ = mock.Mock()
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
read_mock.side_effect = ['1'] read_mock.side_effect = ['1']
mocked_ifaddresses.return_value = { mocked_net_if_addrs.return_value = {
netifaces.AF_INET: [{'addr': '192.168.1.2'}], 'lo': [
netifaces.AF_INET6: [{'addr': 'fd00::101'}] FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'eth0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
],
'eth0.100': [
FakeAddr(socket.AF_INET, '192.168.2.2'),
FakeAddr(socket.AF_INET6, 'fd00::1000::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
]
} }
mocked_execute.return_value = ('em0\n', '') mocked_execute.return_value = ('em0\n', '')
mock_get_mac.mock_has_carrier = True mock_has_carrier.return_value = True
mock_get_mac.return_value = '00:0c:29:8c:11:b1'
interfaces = self.hardware.list_network_interfaces() interfaces = self.hardware.list_network_interfaces()
self.assertEqual(2, len(interfaces)) self.assertEqual(2, len(interfaces))
self.assertEqual('eth0', interfaces[0].name) self.assertEqual('eth0', interfaces[0].name)
@ -6434,12 +6496,11 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
def test_list_network_vlan_interfaces_using_lldp(self, def test_list_network_vlan_interfaces_using_lldp(self,
mocked_lldp_info, mocked_lldp_info,
mock_has_carrier, mock_has_carrier,
mock_get_mac,
mocked_execute, mocked_execute,
mocked_open, mocked_open,
mocked_exists, mocked_exists,
mocked_listdir, mocked_listdir,
mocked_ifaddresses, mocked_net_if_addrs,
mockedget_managers, mockedget_managers,
mocked_lshw): mocked_lshw):
CONF.set_override('collect_lldp', True) CONF.set_override('collect_lldp', True)
@ -6451,13 +6512,34 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
mocked_open.return_value.__exit__ = mock.Mock() mocked_open.return_value.__exit__ = mock.Mock()
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
read_mock.side_effect = ['1'] read_mock.side_effect = ['1']
mocked_net_if_addrs.return_value = {
'lo': [
FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'eth0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
],
'eth0.100': [
FakeAddr(socket.AF_INET, '192.168.100.2'),
FakeAddr(socket.AF_INET6, 'fd00:0100::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:c1')
],
'eth0.101': [
FakeAddr(socket.AF_INET, '192.168.101.2'),
FakeAddr(socket.AF_INET6, 'fd00:0101::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:c2')
]
}
mocked_lldp_info.return_value = {'eth0': [ mocked_lldp_info.return_value = {'eth0': [
(0, b''), (0, b''),
(127, b'\x00\x80\xc2\x03\x00d\x08vlan-100'), (127, b'\x00\x80\xc2\x03\x00d\x08vlan-100'),
(127, b'\x00\x80\xc2\x03\x00e\x08vlan-101')] (127, b'\x00\x80\xc2\x03\x00e\x08vlan-101')]
} }
mock_has_carrier.return_value = True mock_has_carrier.return_value = True
mock_get_mac.return_value = '00:0c:29:8c:11:b1'
interfaces = self.hardware.list_network_interfaces() interfaces = self.hardware.list_network_interfaces()
self.assertEqual(3, len(interfaces)) self.assertEqual(3, len(interfaces))
self.assertEqual('eth0', interfaces[0].name) self.assertEqual('eth0', interfaces[0].name)
@ -6469,22 +6551,21 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
] ]
self.assertEqual(expected_lldp_info, interfaces[0].lldp) self.assertEqual(expected_lldp_info, interfaces[0].lldp)
self.assertEqual('eth0.100', interfaces[1].name) self.assertEqual('eth0.100', interfaces[1].name)
self.assertEqual('00:0c:29:8c:11:b1', interfaces[1].mac_address) self.assertEqual('00:0c:29:8c:11:c1', interfaces[1].mac_address)
self.assertIsNone(interfaces[1].lldp) self.assertIsNone(interfaces[1].lldp)
self.assertEqual('eth0.101', interfaces[2].name) self.assertEqual('eth0.101', interfaces[2].name)
self.assertEqual('00:0c:29:8c:11:b1', interfaces[2].mac_address) self.assertEqual('00:0c:29:8c:11:c2', interfaces[2].mac_address)
self.assertIsNone(interfaces[2].lldp) self.assertIsNone(interfaces[2].lldp)
@mock.patch.object(netutils, 'LOG', autospec=True) @mock.patch.object(netutils, 'LOG', autospec=True)
def test_list_network_vlan_invalid_int(self, def test_list_network_vlan_invalid_int(self,
mocked_log, mocked_log,
mock_has_carrier, mock_has_carrier,
mock_get_mac,
mocked_execute, mocked_execute,
mocked_open, mocked_open,
mocked_exists, mocked_exists,
mocked_listdir, mocked_listdir,
mocked_ifaddresses, mocked_net_if_addrs,
mockedget_managers, mockedget_managers,
mocked_lshw): mocked_lshw):
CONF.set_override('collect_lldp', True) CONF.set_override('collect_lldp', True)
@ -6495,13 +6576,20 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
mocked_open.return_value.__exit__ = mock.Mock() mocked_open.return_value.__exit__ = mock.Mock()
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
read_mock.side_effect = ['1'] read_mock.side_effect = ['1']
mocked_ifaddresses.return_value = { mocked_net_if_addrs.return_value = {
netifaces.AF_INET: [{'addr': '192.168.1.2'}], 'lo': [
netifaces.AF_INET6: [{'addr': 'fd00::101'}] FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'eth0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
]
} }
mocked_execute.return_value = ('em0\n', '') mocked_execute.return_value = ('em0\n', '')
mock_get_mac.mock_has_carrier = True mock_has_carrier.return_value = True
mock_get_mac.return_value = '00:0c:29:8c:11:b1'
self.hardware.list_network_interfaces() self.hardware.list_network_interfaces()
mocked_log.warning.assert_called_once_with( mocked_log.warning.assert_called_once_with(
@ -6511,12 +6599,11 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
def test_list_network_vlan_interfaces_using_lldp_all(self, def test_list_network_vlan_interfaces_using_lldp_all(self,
mocked_lldp_info, mocked_lldp_info,
mock_has_carrier, mock_has_carrier,
mock_get_mac,
mocked_execute, mocked_execute,
mocked_open, mocked_open,
mocked_exists, mocked_exists,
mocked_listdir, mocked_listdir,
mocked_ifaddresses, mocked_net_if_addrs,
mockedget_managers, mockedget_managers,
mocked_lshw): mocked_lshw):
CONF.set_override('collect_lldp', True) CONF.set_override('collect_lldp', True)
@ -6528,6 +6615,43 @@ class TestListNetworkInterfaces(base.IronicAgentTest):
mocked_open.return_value.__exit__ = mock.Mock() mocked_open.return_value.__exit__ = mock.Mock()
read_mock = mocked_open.return_value.read read_mock = mocked_open.return_value.read
read_mock.side_effect = ['1'] read_mock.side_effect = ['1']
mocked_net_if_addrs.return_value = {
'lo': [
FakeAddr(socket.AF_INET, '127.0.0.1'),
FakeAddr(socket.AF_INET6, '::1'),
FakeAddr(socket.AF_PACKET, '00:00:00:00:00:00')
],
'eth0': [
FakeAddr(socket.AF_INET, '192.168.1.2'),
FakeAddr(socket.AF_INET6, 'fd00::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b1')
],
'eth1': [
FakeAddr(socket.AF_INET, '192.168.2.2'),
FakeAddr(socket.AF_INET6, 'fd00:1000::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:b2')
],
'eth0.100': [
FakeAddr(socket.AF_INET, '192.168.100.2'),
FakeAddr(socket.AF_INET6, 'fd00:0100::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:c1')
],
'eth0.101': [
FakeAddr(socket.AF_INET, '192.168.101.2'),
FakeAddr(socket.AF_INET6, 'fd00:0101::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:c2')
],
'eth1.102': [
FakeAddr(socket.AF_INET, '192.168.102.2'),
FakeAddr(socket.AF_INET6, 'fd00:1102::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:d1')
],
'eth1.103': [
FakeAddr(socket.AF_INET, '192.168.103.2'),
FakeAddr(socket.AF_INET6, 'fd00:1103::101'),
FakeAddr(socket.AF_PACKET, '00:0c:29:8c:11:d2')
]
}
mocked_lldp_info.return_value = {'eth0': [ mocked_lldp_info.return_value = {'eth0': [
(0, b''), (0, b''),
(127, b'\x00\x80\xc2\x03\x00d\x08vlan-100'), (127, b'\x00\x80\xc2\x03\x00d\x08vlan-100'),

View File

@ -1,6 +1,5 @@
pbr>=2.0.0 # Apache-2.0 pbr>=2.0.0 # Apache-2.0
eventlet>=0.18.2 # MIT eventlet>=0.18.2 # MIT
netifaces>=0.10.4 # MIT
oslo.config>=5.2.0 # Apache-2.0 oslo.config>=5.2.0 # Apache-2.0
oslo.concurrency>=3.26.0 # Apache-2.0 oslo.concurrency>=3.26.0 # Apache-2.0
oslo.log>=4.6.1 # Apache-2.0 oslo.log>=4.6.1 # Apache-2.0