Kolla: Include bridge name for host's ipv4s.

Sometime we need to know if a domu's interface is conneted
to a same bridge (network) as dom0's interface. This commit
is to add bridge name for each ipv4 entry in the xenapi facts.

Change-Id: I8a6e1507cfa2c935b18e2fed04abaa06df9b0e51
This commit is contained in:
Jianghua Wang 2018-01-25 09:24:17 +00:00
parent ad90523b23
commit 15f56c9f9c
2 changed files with 66 additions and 1 deletions

View File

@ -33,17 +33,58 @@ class CommonUtilFuncTestCase(base.TestCase):
self.assertEqual(hostname, 'Fake_host_name') self.assertEqual(hostname, 'Fake_host_name')
mock_client.ssh.assert_called_with('hostname') mock_client.ssh.assert_called_with('hostname')
def test_get_host_ipv4s(self): def test_get_iface_bridge(self):
mock_client = mock.Mock()
bridge = 'xapi1'
mock_client.ssh.return_value = (0, '\n%s\n' % bridge, '')
ret = common_function.get_iface_bridge('fake_iface',
mock_client)
self.assertEqual(ret, bridge)
def test_get_iface_bridge_internal(self):
mock_client = mock.Mock()
bridge = 'xapi1'
err = 'ovs-vsctl: no interface named %s' % bridge
mock_client.ssh.side_effect = [(1, '', err), # iface-to-br
(0, '', ''), # br-exists
]
ret = common_function.get_iface_bridge(bridge,
mock_client)
self.assertEqual(ret, bridge)
self.assertEqual(mock_client.ssh.call_count, 2)
def test_get_iface_bridge_none(self):
mock_client = mock.Mock()
iface = 'eth9'
err = 'ovs-vsctl: no interface named %s' % iface
mock_client.ssh.side_effect = [(1, '', err), # iface-to-br
(2, '', ''), # br-exists
]
ret = common_function.get_iface_bridge(iface,
mock_client)
self.assertEqual(ret, None)
self.assertEqual(mock_client.ssh.call_count, 2)
@mock.patch.object(common_function, 'get_iface_bridge')
def test_get_host_ipv4s(self, mock_br):
mock_client = mock.Mock() mock_client = mock.Mock()
out = u'xenbr0 10.71.64.118/20\n' out = u'xenbr0 10.71.64.118/20\n'
out += 'xenapi 169.254.0.1/16\n' out += 'xenapi 169.254.0.1/16\n'
mock_client.ssh.return_value = (0, out, '') mock_client.ssh.return_value = (0, out, '')
mock_br.side_effect = ['xenbr0', 'xenapi']
ipv4s = common_function.get_host_ipv4s(mock_client) ipv4s = common_function.get_host_ipv4s(mock_client)
expect = [ expect = [
{ {
"address": "10.71.64.118", "address": "10.71.64.118",
"bridge": "xenbr0",
"broadcast": "10.71.79.255", "broadcast": "10.71.79.255",
"interface": "xenbr0", "interface": "xenbr0",
"netmask": "255.255.240.0", "netmask": "255.255.240.0",
@ -51,6 +92,7 @@ class CommonUtilFuncTestCase(base.TestCase):
}, },
{ {
"address": "169.254.0.1", "address": "169.254.0.1",
"bridge": "xenapi",
"broadcast": "169.254.255.255", "broadcast": "169.254.255.255",
"interface": "xenapi", "interface": "xenapi",
"netmask": "255.255.0.0", "netmask": "255.255.0.0",

View File

@ -102,6 +102,28 @@ def get_remote_hostname(host_client):
return hostname return hostname
def get_iface_bridge(iface, host_client):
# Get bridge name for interface in the host
# return 1 means that it doesn't find a bridge for this interface.
ret, out, _ = host_client.ssh('ovs-vsctl iface-to-br %s' % iface,
allowed_return_codes=[0, 1])
if ret == 0:
return out.strip()
# Get no bridge for this interface; check if it's the internal
# interface for a bridge. The return code 2 means bridge with
# this name doesn't exist.
bridge = iface
ret, _, _ = host_client.ssh('ovs-vsctl br-exists %s' % bridge,
allowed_return_codes=[0, 2])
if ret == 0:
return bridge
# Reaching here, means this interface doesn't belong to any bridge.
return None
def get_host_ipv4s(host_client): def get_host_ipv4s(host_client):
# Get host's IPs (v4 only) via the host_client connected to the host. # Get host's IPs (v4 only) via the host_client connected to the host.
ipv4s = [] ipv4s = []
@ -115,6 +137,7 @@ def get_host_ipv4s(host_client):
network = net_if.network network = net_if.network
ipv4 = {} ipv4 = {}
ipv4['interface'] = interface ipv4['interface'] = interface
ipv4['bridge'] = get_iface_bridge(interface, host_client)
ipv4['address'], _ = ipv4_address.split('/') ipv4['address'], _ = ipv4_address.split('/')
ipv4['broadcast'] = str(network.broadcast_address) ipv4['broadcast'] = str(network.broadcast_address)
ipv4['network'] = str(network.network_address) ipv4['network'] = str(network.network_address)