Merge "Validate interface_mappings on Linux bridge init"

This commit is contained in:
Jenkins 2015-08-06 22:03:15 +00:00 committed by Gerrit Code Review
commit 78e6ddb903
4 changed files with 68 additions and 5 deletions

View File

@ -79,6 +79,7 @@ class NetworkSegment(object):
class LinuxBridgeManager(object): class LinuxBridgeManager(object):
def __init__(self, interface_mappings): def __init__(self, interface_mappings):
self.interface_mappings = interface_mappings self.interface_mappings = interface_mappings
self.validate_interface_mappings()
self.ip = ip_lib.IPWrapper() self.ip = ip_lib.IPWrapper()
# VXLAN related parameters: # VXLAN related parameters:
self.local_ip = cfg.CONF.VXLAN.local_ip self.local_ip = cfg.CONF.VXLAN.local_ip
@ -95,6 +96,14 @@ class LinuxBridgeManager(object):
# Store network mapping to segments # Store network mapping to segments
self.network_map = {} self.network_map = {}
def validate_interface_mappings(self):
for physnet, interface in self.interface_mappings.items():
if not ip_lib.device_exists(interface):
LOG.error(_LE("Interface %(intf)s for physical network %(net)s"
" does not exist. Agent terminated!"),
{'intf': interface, 'net': physnet})
sys.exit(1)
def interface_exists_on_bridge(self, bridge, interface): def interface_exists_on_bridge(self, bridge, interface):
directory = '/sys/class/net/%s/brif' % bridge directory = '/sys/class/net/%s/brif' % bridge
for filename in os.listdir(directory): for filename in os.listdir(directory):

View File

@ -73,8 +73,9 @@ class IpLibTestFramework(functional_base.BaseSudoTestCase):
:return: A tuntap ip_lib.IPDevice :return: A tuntap ip_lib.IPDevice
""" """
ip = ip_lib.IPWrapper(namespace=attr.namespace) ip = ip_lib.IPWrapper(namespace=attr.namespace)
ip.netns.add(attr.namespace) if attr.namespace:
self.addCleanup(ip.netns.delete, attr.namespace) ip.netns.add(attr.namespace)
self.addCleanup(ip.netns.delete, attr.namespace)
tap_device = ip.add_tuntap(attr.name) tap_device = ip.add_tuntap(attr.name)
self.addCleanup(self._safe_delete_device, tap_device) self.addCleanup(self._safe_delete_device, tap_device)
tap_device.link.set_address(attr.mac_address) tap_device.link.set_address(attr.mac_address)

View File

@ -0,0 +1,47 @@
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from oslo_log import log as logging
import testtools
from neutron.plugins.ml2.drivers.linuxbridge.agent import \
linuxbridge_neutron_agent
from neutron.tests.functional.agent.linux import test_ip_lib
LOG = logging.getLogger(__name__)
lba = linuxbridge_neutron_agent
class LinuxBridgeAgentTests(test_ip_lib.IpLibTestFramework):
def setUp(self):
super(LinuxBridgeAgentTests, self).setUp()
agent_rpc = ('neutron.agent.rpc.PluginApi')
mock.patch(agent_rpc).start()
mock.patch('neutron.agent.rpc.PluginReportStateAPI').start()
def test_validate_interface_mappings(self):
mappings = {'physnet1': 'int1', 'physnet2': 'int2'}
with testtools.ExpectedException(SystemExit):
lba.LinuxBridgeManager(mappings)
self.manage_device(
self.generate_device_details()._replace(namespace=None,
name='int1'))
with testtools.ExpectedException(SystemExit):
lba.LinuxBridgeManager(mappings)
self.manage_device(
self.generate_device_details()._replace(namespace=None,
name='int2'))
lba.LinuxBridgeManager(mappings)

View File

@ -50,7 +50,9 @@ class TestLinuxBridge(base.BaseTestCase):
interface_mappings = {'physnet1': 'eth1'} interface_mappings = {'physnet1': 'eth1'}
with mock.patch.object(ip_lib.IPWrapper, with mock.patch.object(ip_lib.IPWrapper,
'get_device_by_ip', return_value=None): 'get_device_by_ip', return_value=None),\
mock.patch.object(ip_lib, 'device_exists',
return_value=True):
self.linux_bridge = linuxbridge_neutron_agent.LinuxBridgeManager( self.linux_bridge = linuxbridge_neutron_agent.LinuxBridgeManager(
interface_mappings) interface_mappings)
@ -355,7 +357,9 @@ class TestLinuxBridgeManager(base.BaseTestCase):
self.interface_mappings = {'physnet1': 'eth1'} self.interface_mappings = {'physnet1': 'eth1'}
with mock.patch.object(ip_lib.IPWrapper, with mock.patch.object(ip_lib.IPWrapper,
'get_device_by_ip', return_value=None): 'get_device_by_ip', return_value=None),\
mock.patch.object(ip_lib, 'device_exists',
return_value=True):
self.lbm = linuxbridge_neutron_agent.LinuxBridgeManager( self.lbm = linuxbridge_neutron_agent.LinuxBridgeManager(
self.interface_mappings) self.interface_mappings)
@ -949,7 +953,9 @@ class TestLinuxBridgeRpcCallbacks(base.BaseTestCase):
self.agent_id = 1 self.agent_id = 1
with mock.patch.object( with mock.patch.object(
ip_lib.IPWrapper, ip_lib.IPWrapper,
'get_device_by_ip', return_value=None): 'get_device_by_ip', return_value=None),\
mock.patch.object(ip_lib, 'device_exists',
return_value=True):
self.br_mgr = (linuxbridge_neutron_agent. self.br_mgr = (linuxbridge_neutron_agent.
LinuxBridgeManager({'physnet1': 'eth1'})) LinuxBridgeManager({'physnet1': 'eth1'}))