From 1da4b87b724f7c2991bd3a502066000658c11f55 Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Wed, 11 Jul 2012 07:45:49 -0400 Subject: [PATCH] Create DHCP agent tap device from port ID Fixes bug 1022806 Change-Id: If29ffdc35d02f8e18789173f250c1eab28a258fc --- quantum/agent/dhcp_agent.py | 8 +++--- quantum/agent/linux/interface.py | 4 --- quantum/tests/unit/test_dhcp_agent.py | 36 ++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/quantum/agent/dhcp_agent.py b/quantum/agent/dhcp_agent.py index 36643a5de5..29b8a9d14d 100644 --- a/quantum/agent/dhcp_agent.py +++ b/quantum/agent/dhcp_agent.py @@ -206,8 +206,10 @@ class DeviceManager(object): LOG.error(_('You must specify an interface driver')) self.driver = importutils.import_object(conf.interface_driver, conf) - def get_interface_name(self, network): - return ('tap' + network.id)[:self.driver.DEV_NAME_LEN] + def get_interface_name(self, network, port=None): + if not port: + port = self._get_or_create_port(network) + return ('tap' + port.id)[:self.driver.DEV_NAME_LEN] def get_device_id(self, network): # There could be more than one dhcp server per network, so create @@ -217,8 +219,8 @@ class DeviceManager(object): return 'dhcp%s-%s' % (host_uuid, network.id) def setup(self, network, reuse_existing=False): - interface_name = self.get_interface_name(network) port = self._get_or_create_port(network) + interface_name = self.get_interface_name(network, port) if ip_lib.device_exists(interface_name): if not reuse_existing: diff --git a/quantum/agent/linux/interface.py b/quantum/agent/linux/interface.py index aed549b758..9a4c6531d5 100644 --- a/quantum/agent/linux/interface.py +++ b/quantum/agent/linux/interface.py @@ -139,10 +139,6 @@ class BridgeInterfaceDriver(LinuxInterfaceDriver): def plug(self, network_id, port_id, device_name, mac_address): """Plugin the interface.""" - bridge = self.get_bridge(network_id) - - self.check_bridge_exists(bridge) - if not ip_lib.device_exists(device_name): device = ip_lib.IPDevice(device_name, self.conf.root_helper) try: diff --git a/quantum/tests/unit/test_dhcp_agent.py b/quantum/tests/unit/test_dhcp_agent.py index 9255b4c546..2f4c1ee64e 100644 --- a/quantum/tests/unit/test_dhcp_agent.py +++ b/quantum/tests/unit/test_dhcp_agent.py @@ -233,7 +233,7 @@ class TestDeviceManager(unittest.TestCase): tenant_id='aaaaaaaa-aaaa-aaaa-aaaaaaaaaaaa', subnets=fake_subnets) - fake_port = FakeModel('12345678-aaaa-aaaa-1234567890ab', + fake_port = FakeModel('12345678-1234-aaaa-1234567890ab', mac_address='aa:bb:cc:dd:ee:ff') port_dict = dict(mac_address='aa:bb:cc:dd:ee:ff', allocations=[], id=1) @@ -270,7 +270,7 @@ class TestDeviceManager(unittest.TestCase): self.mock_driver.assert_has_calls([ mock.call.plug('12345678-1234-5678-1234567890ab', - '12345678-aaaa-aaaa-1234567890ab', + '12345678-1234-aaaa-1234567890ab', 'tap12345678-12', 'aa:bb:cc:dd:ee:ff'), mock.call.init_l3(mock.ANY, 'tap12345678-12')] @@ -284,13 +284,43 @@ class TestDeviceManager(unittest.TestCase): tenant_id='aaaaaaaa-aaaa-aaaa-aaaaaaaaaaaa', subnets=fake_subnets) + fake_port = FakeModel('12345678-1234-aaaa-1234567890ab', + mac_address='aa:bb:cc:dd:ee:ff') + + port_dict = dict(mac_address='aa:bb:cc:dd:ee:ff', allocations=[], id=1) + + self.client_inst.create_port.return_value = dict(port=port_dict) + self.device_exists.return_value = False + + # fake the db + filter_by_result = mock.Mock() + filter_by_result.one = mock.Mock(return_value=fake_port) + + self.filter_called = False + + def get_filter_results(*args, **kwargs): + if self.filter_called: + return filter_by_result + else: + self.filter_called = True + raise sqlsoup.SQLAlchemyError() + + return filter_results.pop(0) + + mock_db = mock.Mock() + mock_db.ports = mock.Mock(name='ports2') + mock_db.ports.filter_by = mock.Mock( + name='filter_by', + side_effect=get_filter_results) + with mock.patch('quantum.agent.linux.interface.NullDriver') as dvr_cls: mock_driver = mock.MagicMock() mock_driver.DEV_NAME_LEN = ( interface.LinuxInterfaceDriver.DEV_NAME_LEN) + mock_driver.port = fake_port dvr_cls.return_value = mock_driver - dh = dhcp_agent.DeviceManager(self.conf, None) + dh = dhcp_agent.DeviceManager(self.conf, mock_db) dh.destroy(fake_network) dvr_cls.assert_called_once_with(self.conf)