]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Removes regex validation of UUIDs in dhcp_agent
authorZhongyue Luo <zhongyue.nah@intel.com>
Mon, 5 Nov 2012 15:16:03 +0000 (23:16 +0800)
committerZhongyue Luo <zhongyue.nah@intel.com>
Mon, 5 Nov 2012 16:12:10 +0000 (00:12 +0800)
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
quantum/tests/unit/test_dhcp_agent.py

index 0e60a94a281f8c8b406a70f53f100077266d685d..bcc3aa9d18ad111a8c7e4bf8aed919f9746facf9 100644 (file)
@@ -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)
index 5456d557083ca2ceeb826bc9cbf30079b2268117..e00b9b3313dfa1d8a6de769d28ff6804012c4baa 100644 (file)
@@ -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: