From f446b1c4137e17894c7b1917b14e39a46e116631 Mon Sep 17 00:00:00 2001 From: Zhongyue Luo Date: Mon, 5 Nov 2012 23:16:03 +0800 Subject: [PATCH] Removes regex validation of UUIDs in dhcp_agent Removed re and quantum.api.v2 imports Removed _validate_field method Use uuidutils.is_uuid_like for uuid validation Change-Id: I35e751969cf1f9ff7850fc01b8e4a3f6d788918e --- quantum/agent/dhcp_agent.py | 18 +++++------------- quantum/tests/unit/test_dhcp_agent.py | 15 +++------------ 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/quantum/agent/dhcp_agent.py b/quantum/agent/dhcp_agent.py index 0e60a94a2..bcc3aa9d1 100644 --- a/quantum/agent/dhcp_agent.py +++ b/quantum/agent/dhcp_agent.py @@ -16,7 +16,6 @@ # under the License. import os -import re import socket import sys import uuid @@ -29,7 +28,6 @@ from quantum.agent.linux import dhcp from quantum.agent.linux import interface from quantum.agent.linux import ip_lib from quantum.agent import rpc as agent_rpc -from quantum.api.v2 import attributes from quantum.common import exceptions from quantum.common import topics from quantum.openstack.common import cfg @@ -38,6 +36,7 @@ from quantum.openstack.common import importutils from quantum.openstack.common import jsonutils from quantum.openstack.common import log as logging from quantum.openstack.common.rpc import proxy +from quantum.openstack.common import uuidutils LOG = logging.getLogger(__name__) NS_PREFIX = 'qdhcp-' @@ -500,15 +499,6 @@ class DhcpLeaseRelay(object): else: os.makedirs(dirname, 0755) - def _validate_field(self, value, regex): - """Validate value against a regular expression and return if valid.""" - match = re.match(regex, value) - - if match: - return value - raise ValueError(_("Value %s does not match regex: %s") % - (value, regex)) - def _handler(self, client_sock, client_addr): """Handle incoming lease relay stream connection. @@ -521,8 +511,10 @@ class DhcpLeaseRelay(object): data = jsonutils.loads(msg) client_sock.close() - network_id = self._validate_field(data['network_id'], - attributes.UUID_PATTERN) + network_id = data['network_id'] + if not uuidutils.is_uuid_like(network_id): + raise ValueError(_("Network ID %s is not a valid UUID") % + (network_id)) ip_address = str(netaddr.IPAddress(data['ip_address'])) lease_remaining = int(data['lease_remaining']) self.callback(network_id, ip_address, lease_remaining) diff --git a/quantum/tests/unit/test_dhcp_agent.py b/quantum/tests/unit/test_dhcp_agent.py index 5456d5570..e00b9b331 100644 --- a/quantum/tests/unit/test_dhcp_agent.py +++ b/quantum/tests/unit/test_dhcp_agent.py @@ -854,16 +854,6 @@ class TestDhcpLeaseRelay(unittest.TestCase): exists.assert_called_once_with( cfg.CONF.dhcp_lease_relay_socket) - def test_validate_field_valid(self): - relay = dhcp_agent.DhcpLeaseRelay(None) - retval = relay._validate_field('1b', '\d[a-f]') - self.assertEqual(retval, '1b') - - def test_validate_field_invalid(self): - relay = dhcp_agent.DhcpLeaseRelay(None) - with self.assertRaises(ValueError): - retval = relay._validate_field('zz', '\d[a-f]') - def test_handler_valid_data(self): network_id = 'cccccccc-cccc-cccc-cccc-cccccccccccc' ip_address = '192.168.1.9' @@ -898,8 +888,9 @@ class TestDhcpLeaseRelay(unittest.TestCase): relay = dhcp_agent.DhcpLeaseRelay(handler) - with mock.patch.object(relay, '_validate_field') as validate: - validate.side_effect = ValueError + with mock.patch('quantum.openstack.common.' + 'uuidutils.is_uuid_like') as validate: + validate.return_value = False with mock.patch.object(dhcp_agent.LOG, 'warn') as log: -- 2.45.2