From dfacba0f2d7586df51962c2b927e5272358ea3c1 Mon Sep 17 00:00:00 2001 From: Li Xipeng Date: Tue, 23 Feb 2016 13:54:34 -0800 Subject: [PATCH] Add 169.254.169.254 when enable force_metadata When enable force_metadata in dhcp.ini, and create a network and a subnet, none 169.254.169.254/24 ip info set in related namespace(qdhcp-XXX) with `ip a` command. In this case, vms could not get metadata any more. Change-Id: Ibd73824658c9759d32fa53ffcf41f2b719c1028b Closes-Bug: #1549793 --- neutron/agent/linux/dhcp.py | 2 +- neutron/tests/unit/agent/linux/test_dhcp.py | 54 ++++++++++++++++----- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index 775f79f45c8..757524ec3f5 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -1258,7 +1258,7 @@ class DeviceManager(object): net = netaddr.IPNetwork(subnet.cidr) ip_cidrs.append('%s/%s' % (gateway, net.prefixlen)) - if self.conf.enable_isolated_metadata: + if self.conf.force_metadata or self.conf.enable_isolated_metadata: ip_cidrs.append(METADATA_DEFAULT_CIDR) self.driver.init_l3(interface_name, ip_cidrs, diff --git a/neutron/tests/unit/agent/linux/test_dhcp.py b/neutron/tests/unit/agent/linux/test_dhcp.py index 58db0c99447..37f1a727291 100644 --- a/neutron/tests/unit/agent/linux/test_dhcp.py +++ b/neutron/tests/unit/agent/linux/test_dhcp.py @@ -2023,6 +2023,8 @@ class TestDeviceManager(TestConfBase): # Create DeviceManager. self.conf.register_opt(cfg.BoolOpt('enable_isolated_metadata', default=False)) + self.conf.register_opt(cfg.BoolOpt('force_metadata', + default=False)) plugin = mock.Mock() device = mock.Mock() mock_IPDevice.return_value = device @@ -2092,15 +2094,16 @@ class TestDeviceManager(TestConfBase): self._test_setup(self.mock_load_interface_driver, self.mock_ip_lib, use_gateway_ips=True) - def test_setup_reserved(self): - """Test reserved port case of DeviceManager's DHCP port setup - logic. - """ - + def _test_setup_reserved(self, enable_isolated_metadata=False, + force_metadata=False): with mock.patch.object(dhcp.ip_lib, 'IPDevice') as mock_IPDevice: # Create DeviceManager. - self.conf.register_opt(cfg.BoolOpt('enable_isolated_metadata', - default=False)) + self.conf.register_opt( + cfg.BoolOpt('enable_isolated_metadata', + default=enable_isolated_metadata)) + self.conf.register_opt( + cfg.BoolOpt('force_metadata', + default=force_metadata)) plugin = mock.Mock() device = mock.Mock() mock_IPDevice.return_value = device @@ -2127,19 +2130,48 @@ class TestDeviceManager(TestConfBase): plugin.update_dhcp_port.assert_called_with(reserved_port.id, mock.ANY) + except_ips = ['192.168.0.6/24'] + if enable_isolated_metadata or force_metadata: + except_ips.append(dhcp.METADATA_DEFAULT_CIDR) mgr.driver.init_l3.assert_called_with('ns-XXX', - ['192.168.0.6/24'], + except_ips, namespace='qdhcp-ns') + def test_setup_reserved_and_disable_metadata(self): + """Test reserved port case of DeviceManager's DHCP port setup + logic which metadata disabled. + """ + self._test_setup_reserved() + + def test_setup_reserved_with_isolated_metadata_enable(self): + """Test reserved port case of DeviceManager's DHCP port setup + logic which isolated_ metadata enabled. + """ + self._test_setup_reserved(enable_isolated_metadata=True) + + def test_setup_reserved_with_force_metadata_enable(self): + """Test reserved port case of DeviceManager's DHCP port setup + logic which force_metadata enabled. + """ + self._test_setup_reserved(force_metadata=True) + + def test_setup_reserved_and_enable_metadata(self): + """Test reserved port case of DeviceManager's DHCP port setup + logic which both isolated_metadata and force_metadata enabled. + """ + self._test_setup_reserved(enable_isolated_metadata=True, + force_metadata=True) + def test_setup_reserved_2(self): """Test scenario where a network has two reserved ports, and update_dhcp_port fails for the first of those. """ - with mock.patch.object(dhcp.ip_lib, 'IPDevice') as mock_IPDevice: # Create DeviceManager. - self.conf.register_opt(cfg.BoolOpt('enable_isolated_metadata', - default=False)) + self.conf.register_opt( + cfg.BoolOpt('enable_isolated_metadata', default=False)) + self.conf.register_opt( + cfg.BoolOpt('force_metadata', default=False)) plugin = mock.Mock() device = mock.Mock() mock_IPDevice.return_value = device