]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Convert all incoming protocol numbers to string
authorAnn Kamyshnikova <akamyshnikova@mirantis.com>
Mon, 10 Nov 2014 14:05:54 +0000 (17:05 +0300)
committerAdam Gandelman <adamg@ubuntu.com>
Tue, 25 Nov 2014 18:56:32 +0000 (10:56 -0800)
PostgreSQL is more sensitive for types than MySQL when it selects
something from columns in database. So it fails when it tries to
select from string field comparing with integer value.

Added unit test to verify conversion of protocol numbers to
strings.

Closes-bug:#1381379

(cherry picked from commit bc34f26302faea116be7e61051c32f8787530836)

Conflicts:
neutron/tests/unit/test_extension_security_group.py

Change-Id: I0a29595403a07c66888871088d5549705a097f68

neutron/db/securitygroups_db.py
neutron/extensions/securitygroup.py
neutron/tests/unit/test_extension_security_group.py

index 23b5c80cb1136ce06855bb7cefd50cea7431dc10..9237cf31f266b45885fad8518cb4d1546c996888 100644 (file)
@@ -296,7 +296,11 @@ class SecurityGroupDbMixin(ext_sg.SecurityGroupPluginBase):
     def _get_ip_proto_number(self, protocol):
         if protocol is None:
             return
-        return IP_PROTOCOL_MAP.get(protocol, protocol)
+        # According to bug 1381379, protocol is always set to string to avoid
+        # problems with comparing int and string in PostgreSQL. Here this
+        # string is converted to int to give an opportunity to use it as
+        # before.
+        return int(IP_PROTOCOL_MAP.get(protocol, protocol))
 
     def _validate_port_range(self, rule):
         """Check that port_range is valid."""
index 89b9d8a74bb4fb6e1b8f092f32bc0ceea4c35e61..3524117ee9e928ebb244b7630ac990ec52dbab7b 100644 (file)
@@ -116,7 +116,10 @@ def convert_protocol(value):
     try:
         val = int(value)
         if val >= 0 and val <= 255:
-            return value
+            # Set value of protocol number to string due to bug 1381379,
+            # PostgreSQL fails when it tries to compare integer with string,
+            # that exists in db.
+            return str(value)
         raise SecurityGroupRuleInvalidProtocol(
             protocol=value, values=sg_supported_protocols)
     except (ValueError, TypeError):
index 4f52ba08077aac162241033af7ef42a5fdfb89df..07a5cb0b3949f532aee8da67026bee4b17ae65f7 100644 (file)
@@ -1446,6 +1446,9 @@ class TestConvertProtocol(base.BaseTestCase):
             self.assertRaises(ext_sg.SecurityGroupRuleInvalidProtocol,
                               ext_sg.convert_protocol, val)
 
+    def test_convert_numeric_protocol_to_string(self):
+        self.assertIsInstance(ext_sg.convert_protocol(2), str)
+
 
 class TestSecurityGroupsXML(TestSecurityGroups):
     fmt = 'xml'