From ccb21ca7a9906523a972e32df904744885183234 Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Sun, 23 Dec 2018 00:36:44 +0900 Subject: [PATCH] python3: Fix handling of other protocol in SG rule In python3 we cannot compare None with an integer, while this works in python2. Change-Id: I1321ea68f08241db377a58ed6a22306c63aba204 Closes-Bug: #1789402 --- openstack_dashboard/api/neutron.py | 4 ++-- openstack_dashboard/test/test_data/neutron_data.py | 13 ++++++++++++- openstack_dashboard/test/unit/api/test_neutron.py | 14 +++++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/openstack_dashboard/api/neutron.py b/openstack_dashboard/api/neutron.py index c4ea0828f3..9472d99d82 100644 --- a/openstack_dashboard/api/neutron.py +++ b/openstack_dashboard/api/neutron.py @@ -432,9 +432,9 @@ class SecurityGroupManager(object): """ if not cidr: cidr = None - if from_port < 0: + if isinstance(from_port, int) and from_port < 0: from_port = None - if to_port < 0: + if isinstance(to_port, int) and to_port < 0: to_port = None if isinstance(ip_protocol, int) and ip_protocol < 0: ip_protocol = None diff --git a/openstack_dashboard/test/test_data/neutron_data.py b/openstack_dashboard/test/test_data/neutron_data.py index ffdf686b7e..d5f4310ae1 100644 --- a/openstack_dashboard/test/test_data/neutron_data.py +++ b/openstack_dashboard/test/test_data/neutron_data.py @@ -548,6 +548,16 @@ def data(TEST): 'tenant_id': secgroup['tenant_id'], 'description': 'Ingress HTTP from SG #1', } + rule_ip_proto = { + 'id': uuidutils.generate_uuid(), + 'direction': u'ingress', 'ethertype': u'IPv4', + 'port_range_min': None, 'port_range_max': None, + 'protocol': u'99', 'remote_group_id': None, + 'remote_ip_prefix': u'0.0.0.0/24', + 'security_group_id': secgroup['id'], + 'tenant_id': secgroup['tenant_id'], + 'description': 'Ingress custom IP protocol 99', + } rule_all_tcp = { 'id': uuidutils.generate_uuid(), 'direction': u'egress', 'ethertype': u'IPv4', @@ -561,7 +571,8 @@ def data(TEST): rules = [] if not default_only: - rules += [rule_tcp_80, rule_icmp, rule_group, rule_all_tcp] + rules += [rule_tcp_80, rule_icmp, rule_group, rule_all_tcp, + rule_ip_proto] rules += [rule_egress_ipv4, rule_egress_ipv6] secgroup['security_group_rules'] = rules diff --git a/openstack_dashboard/test/unit/api/test_neutron.py b/openstack_dashboard/test/unit/api/test_neutron.py index c024706a4b..a5a69bdcb3 100644 --- a/openstack_dashboard/test/unit/api/test_neutron.py +++ b/openstack_dashboard/test/unit/api/test_neutron.py @@ -1250,9 +1250,17 @@ class NeutronApiSecurityGroupTests(test.APIMockTestCase): def test_security_group_rule_create_without_desc(self): self._test_security_group_rule_create(with_desc=False) - def _test_security_group_rule_create(self, with_desc): - sg_rule = [r for r in self.api_security_group_rules.list() - if r['protocol'] == 'tcp' and r['remote_ip_prefix']][0] + def test_security_group_rule_create_with_custom_protocol(self): + self._test_security_group_rule_create(custom_ip_proto=True) + + def _test_security_group_rule_create(self, with_desc=False, + custom_ip_proto=False): + if custom_ip_proto: + sg_rule = [r for r in self.api_security_group_rules.list() + if r['protocol'] == '99'][0] + else: + sg_rule = [r for r in self.api_security_group_rules.list() + if r['protocol'] == 'tcp' and r['remote_ip_prefix']][0] sg_id = sg_rule['security_group_id'] secgroup = [sg for sg in self.api_security_groups.list() if sg['id'] == sg_id][0]