From c00b4b31276f9ae21fa0a7a213391277bda4cfa7 Mon Sep 17 00:00:00 2001 From: Ann Kamyshnikova Date: Mon, 10 Nov 2014 17:05:54 +0300 Subject: [PATCH] Convert all incoming protocol numbers to string 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 | 6 +++++- neutron/extensions/securitygroup.py | 5 ++++- neutron/tests/unit/test_extension_security_group.py | 3 +++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/neutron/db/securitygroups_db.py b/neutron/db/securitygroups_db.py index 23b5c80cb..9237cf31f 100644 --- a/neutron/db/securitygroups_db.py +++ b/neutron/db/securitygroups_db.py @@ -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.""" diff --git a/neutron/extensions/securitygroup.py b/neutron/extensions/securitygroup.py index 89b9d8a74..3524117ee 100644 --- a/neutron/extensions/securitygroup.py +++ b/neutron/extensions/securitygroup.py @@ -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): diff --git a/neutron/tests/unit/test_extension_security_group.py b/neutron/tests/unit/test_extension_security_group.py index 4f52ba080..07a5cb0b3 100644 --- a/neutron/tests/unit/test_extension_security_group.py +++ b/neutron/tests/unit/test_extension_security_group.py @@ -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' -- 2.45.2