From 1639819a20fb2227551508df5b0aef6e0d0c59e0 Mon Sep 17 00:00:00 2001 From: vikas Date: Thu, 24 Mar 2016 00:21:06 -0700 Subject: [PATCH] check the interface type in the request check for interface type while creating l2-gateway. it should be list of dict.if not raise an appropriate message. for ex: request body for l2-gateway-create is as below. {"l2_gateway": {"name": "gw2", "devices": [{"interfaces": [{"name": "i1"}]}, "device_name": "switch2"}]}' Change-Id: I65430efa1a3ed4416e649f0377d9dff7fafc5b57 Closes-Bug: 1548361 --- .../l2gateway/common/l2gw_validators.py | 6 + .../services/l2gateway/common/__init__.py | 0 .../l2gateway/common/test_l2gw_validators.py | 112 ++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 networking_l2gw/tests/unit/services/l2gateway/common/__init__.py create mode 100644 networking_l2gw/tests/unit/services/l2gateway/common/test_l2gw_validators.py diff --git a/networking_l2gw/services/l2gateway/common/l2gw_validators.py b/networking_l2gw/services/l2gateway/common/l2gw_validators.py index 02691964..c1c2caee 100644 --- a/networking_l2gw/services/l2gateway/common/l2gw_validators.py +++ b/networking_l2gw/services/l2gateway/common/l2gw_validators.py @@ -41,7 +41,13 @@ def validate_gwdevice_list(data, valid_values=None): if not interface_data: msg = _("Cannot create a gateway with an empty interfaces") return msg + if not isinstance(interface_data, list): + msg = _("interfaces format is not a type list of dicts") + return msg for int_dict in interface_data: + if not isinstance(int_dict, dict): + msg = _("interfaces format is not a type dict") + return msg err_msg = attributes._validate_dict(int_dict, None) if not int_dict.get('name'): msg = _("Cannot create a gateway with an empty " diff --git a/networking_l2gw/tests/unit/services/l2gateway/common/__init__.py b/networking_l2gw/tests/unit/services/l2gateway/common/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/networking_l2gw/tests/unit/services/l2gateway/common/test_l2gw_validators.py b/networking_l2gw/tests/unit/services/l2gateway/common/test_l2gw_validators.py new file mode 100644 index 00000000..50216af5 --- /dev/null +++ b/networking_l2gw/tests/unit/services/l2gateway/common/test_l2gw_validators.py @@ -0,0 +1,112 @@ +# Copyright (c) 2016 OpenStack Foundation +# +# 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. + +from neutron.common import exceptions +from neutron.tests import base + +from networking_l2gw.services.l2gateway.common import l2gw_validators + + +class TestL2gwValidators(base.BaseTestCase): + + def test_validate_gwdevice_list(self): + ret_msg = l2gw_validators.validate_gwdevice_list(None) + msg = "Cannot create a gateway with an empty device list" + self.assertEqual(msg, ret_msg) + + def test_for_empty_device_name(self): + test_device = [{"interfaces": "port"}] + ret_msg = l2gw_validators.validate_gwdevice_list(test_device) + msg = "Cannot create a gateway with an empty device_name" + self.assertEqual(msg, ret_msg) + + def test_for_empty_interface(self): + test_device = [{"device_name": "switch1"}] + ret_msg = l2gw_validators.validate_gwdevice_list(test_device) + msg = "Cannot create a gateway with an empty interfaces" + self.assertEqual(msg, ret_msg) + + def test_for_interfaces_type_list_of_dicts(self): + test_device = [{"device_name": "switch1", "interfaces": "port"}] + ret_msg = l2gw_validators.validate_gwdevice_list(test_device) + msg = "interfaces format is not a type list of dicts" + self.assertEqual(msg, ret_msg) + + def test_for_interface_type_dict(self): + test_device = [{"device_name": "switch1", "interfaces": ["port"]}] + ret_msg = l2gw_validators.validate_gwdevice_list(test_device) + msg = "interfaces format is not a type dict" + self.assertEqual(msg, ret_msg) + + def test_for_empty_interface_name(self): + test_device = [{"device_name": "switch1", + "interfaces": [{'name': ""}]}] + ret_msg = l2gw_validators.validate_gwdevice_list(test_device) + msg = "Cannot create a gateway with an empty interface name" + self.assertEqual(msg, ret_msg) + + def test_segmentation_id_in_interface(self): + test_device = [{"device_name": "switch1", + "interfaces": [{'name': "i1", 'segmentation_id': ''}]}] + ret_msg = l2gw_validators.validate_gwdevice_list(test_device) + msg = "segmentation_id_list should not be empty" + self.assertEqual(msg, ret_msg) + + def test_segmentation_id_type(self): + test_device = [{"device_name": "switch1", + "interfaces": [{'name': "i1", + 'segmentation_id': '67'}]}] + ret_msg = l2gw_validators.validate_gwdevice_list(test_device) + msg = "segmentation_id type should be of list type " + self.assertEqual(msg, ret_msg) + + def test_validate_network_mapping_list_with_seg_id(self): + network_mapping = {'segmentation_id': 67} + check_vlan = True + self.assertRaises(exceptions.InvalidInput, + l2gw_validators.validate_network_mapping_list, + network_mapping, check_vlan) + + def test_validate_network_mapping_list_without_seg_id(self): + network_mapping = {'segmentation_id': ''} + check_vlan = False + self.assertRaises(exceptions.InvalidInput, + l2gw_validators.validate_network_mapping_list, + network_mapping, check_vlan) + + def test_validate_network_mapping_list_for_empty_network_id(self): + network_mapping = {'segmentation_id': '', 'network_id': ''} + check_vlan = True + self.assertRaises(exceptions.InvalidInput, + l2gw_validators.validate_network_mapping_list, + network_mapping, check_vlan) + + def test_is_valid_vlan_id_for_non_integer_value(self): + seg_id = 'a' + self.assertRaises(exceptions.InvalidInput, + l2gw_validators.is_valid_vlan_id, + seg_id) + + def test_is_valid_vlan_id_for_value_less_than_0(self): + seg_id = -1 + self.assertRaises(exceptions.InvalidInput, + l2gw_validators.is_valid_vlan_id, + seg_id) + + def test_is_valid_vlan_id_for_value_greater_than_4095(self): + seg_id = 4096 + self.assertRaises(exceptions.InvalidInput, + l2gw_validators.is_valid_vlan_id, + seg_id)