From 239d0378a23a64ea1d374becc4724ab1603157dc Mon Sep 17 00:00:00 2001 From: Xuhan Peng Date: Tue, 15 Apr 2014 11:12:16 +0800 Subject: [PATCH] Validate IPv6 modes in API when IP version is 4 Add a validation to ipv6_ra_mode and ipv6_address_mode with ip version. An InvalidInput error is prompted when ipv6_ra_mode or ipv6_address_mode is specified in subnet create and update API and ip version is 4. Change-Id: I9a0356f23e6b162c31f2d289a34f4bd261cee91e Closes-Bug: 1307788 --- neutron/db/db_base_plugin_v2.py | 9 ++++++ neutron/tests/unit/test_db_plugin.py | 41 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/neutron/db/db_base_plugin_v2.py b/neutron/db/db_base_plugin_v2.py index 1be937cba..8f485c86f 100644 --- a/neutron/db/db_base_plugin_v2.py +++ b/neutron/db/db_base_plugin_v2.py @@ -1108,6 +1108,15 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2, for rt in s['host_routes']: self._validate_host_route(rt, ip_ver) + if ip_ver == 4: + if attributes.is_attr_set(s.get('ipv6_ra_mode')): + raise n_exc.InvalidInput( + error_message=(_("ipv6_ra_mode is not valid when " + "ip_version is 4"))) + if attributes.is_attr_set(s.get('ipv6_address_mode')): + raise n_exc.InvalidInput( + error_message=(_("ipv6_address_mode is not valid when " + "ip_version is 4"))) if ip_ver == 6: self._validate_ipv6_attributes(s, cur_subnet) diff --git a/neutron/tests/unit/test_db_plugin.py b/neutron/tests/unit/test_db_plugin.py index d6bf0c090..0808b6994 100644 --- a/neutron/tests/unit/test_db_plugin.py +++ b/neutron/tests/unit/test_db_plugin.py @@ -3050,6 +3050,25 @@ class TestSubnetsV2(NeutronDbPluginV2TestCase): ipv6_ra_mode=mode, ipv6_address_mode=None) + def test_create_subnet_ipv6_ra_mode_ip_version_4(self): + cidr = '10.0.2.0/24' + with testlib_api.ExpectedException( + webob.exc.HTTPClientError) as ctx_manager: + self._test_create_subnet(cidr=cidr, ip_version=4, + ipv6_ra_mode=constants.DHCPV6_STATEFUL) + self.assertEqual(ctx_manager.exception.code, + webob.exc.HTTPClientError.code) + + def test_create_subnet_ipv6_address_mode_ip_version_4(self): + cidr = '10.0.2.0/24' + with testlib_api.ExpectedException( + webob.exc.HTTPClientError) as ctx_manager: + self._test_create_subnet( + cidr=cidr, ip_version=4, + ipv6_address_mode=constants.DHCPV6_STATEFUL) + self.assertEqual(ctx_manager.exception.code, + webob.exc.HTTPClientError.code) + def test_update_subnet_no_gateway(self): with self.subnet() as subnet: data = {'subnet': {'gateway_ip': '11.0.0.1'}} @@ -3258,6 +3277,28 @@ class TestSubnetsV2(NeutronDbPluginV2TestCase): self.assertEqual(res.status_int, webob.exc.HTTPClientError.code) + def test_update_subnet_ipv6_ra_mode_ip_version_4(self): + with self.network() as network: + with self.subnet(network=network) as subnet: + data = {'subnet': {'ipv6_ra_mode': + constants.DHCPV6_STATEFUL}} + req = self.new_update_request('subnets', data, + subnet['subnet']['id']) + res = req.get_response(self.api) + self.assertEqual(res.status_int, + webob.exc.HTTPClientError.code) + + def test_update_subnet_ipv6_address_mode_ip_version_4(self): + with self.network() as network: + with self.subnet(network=network) as subnet: + data = {'subnet': {'ipv6_address_mode': + constants.DHCPV6_STATEFUL}} + req = self.new_update_request('subnets', data, + subnet['subnet']['id']) + res = req.get_response(self.api) + self.assertEqual(res.status_int, + webob.exc.HTTPClientError.code) + def test_show_subnet(self): with self.network() as network: with self.subnet(network=network) as subnet: -- 2.45.2