From 726f343832950e5160c048bca281c862d63b9632 Mon Sep 17 00:00:00 2001 From: chenhb Date: Tue, 13 Nov 2018 20:34:46 +0800 Subject: [PATCH] [cleanup]catch 404 exception while there is not any neutron security group In some environment, the security group is not configured, so we need to catch 404 exception which is raised by security group list. Change-Id: Iaa5d2af6040a54011b8cbf788797c2c199f30ab4 --- rally_openstack/cleanup/resources.py | 15 ++++++++++----- tests/unit/cleanup/test_resources.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/rally_openstack/cleanup/resources.py b/rally_openstack/cleanup/resources.py index 6263353d..723b7b8c 100644 --- a/rally_openstack/cleanup/resources.py +++ b/rally_openstack/cleanup/resources.py @@ -466,11 +466,16 @@ class NeutronRouter(NeutronMixin): tenant_resource=True) class NeutronSecurityGroup(NeutronMixin): def list(self): - tenant_sgs = super(NeutronSecurityGroup, self).list() - # NOTE(pirsriva): Filter out "default" security group deletion - # by non-admin role user - return filter(lambda r: r["name"] != "default", - tenant_sgs) + try: + tenant_sgs = super(NeutronSecurityGroup, self).list() + # NOTE(pirsriva): Filter out "default" security group deletion + # by non-admin role user + return filter(lambda r: r["name"] != "default", + tenant_sgs) + except Exception as e: + if getattr(e, "status_code", 400) == 404: + return [] + raise @base.resource("neutron", "quota", order=next(_neutron_order), diff --git a/tests/unit/cleanup/test_resources.py b/tests/unit/cleanup/test_resources.py index 500292fa..ff3bf582 100644 --- a/tests/unit/cleanup/test_resources.py +++ b/tests/unit/cleanup/test_resources.py @@ -576,6 +576,24 @@ class NeutronSecurityGroupTestCase(test.TestCase): neut.user.neutron().list_security_groups.assert_called_once_with( tenant_id=neut.tenant_uuid) + def test_list_with_not_found(self): + + class NotFound(Exception): + status_code = 404 + + neut = resources.NeutronSecurityGroup() + neut.user = mock.MagicMock() + neut._resource = "security_group" + neut.tenant_uuid = "user_tenant" + + neut.user.neutron().list_security_groups.side_effect = NotFound() + + expected_result = [] + self.assertEqual(expected_result, list(neut.list())) + + neut.user.neutron().list_security_groups.assert_called_once_with( + tenant_id=neut.tenant_uuid) + class NeutronQuotaTestCase(test.TestCase):