From eb7377a94adcb450f22ff1dc8705427c676e5e76 Mon Sep 17 00:00:00 2001 From: Srinivas Sakhamuri Date: Fri, 15 Jul 2016 01:27:24 -0600 Subject: [PATCH] Use flavor name for checking flavor existence Flavor existence check with ID returns the flavor even though that flavor is deleted. Using flavor name is more consistent with deleted flavors and works across openstack releases reliably Change-Id: I563fab6eeb849405bc520c1cae425a1ff0d56ff2 --- rally/plugins/openstack/cleanup/resources.py | 9 +++++++++ .../plugins/openstack/cleanup/test_resources.py | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/rally/plugins/openstack/cleanup/resources.py b/rally/plugins/openstack/cleanup/resources.py index 835724d620..bd5882e854 100644 --- a/rally/plugins/openstack/cleanup/resources.py +++ b/rally/plugins/openstack/cleanup/resources.py @@ -15,6 +15,7 @@ from boto import exception as boto_exception from neutronclient.common import exceptions as neutron_exceptions +from novaclient import exceptions as nova_exc from oslo_config import cfg from saharaclient.api import base as saharaclient_base @@ -144,6 +145,14 @@ class NovaFlavors(base.ResourceManager): return [r for r in self._manager().list() if utils.name_matches_object(r.name, nova_utils.NovaScenario)] + def is_deleted(self): + try: + self._manager().get(self.name()) + except nova_exc.NotFound: + return True + + return False + @base.resource("nova", "floating_ips_bulk", order=next(_nova_order), admin_required=True) diff --git a/tests/unit/plugins/openstack/cleanup/test_resources.py b/tests/unit/plugins/openstack/cleanup/test_resources.py index 878e56228d..15d15c494a 100644 --- a/tests/unit/plugins/openstack/cleanup/test_resources.py +++ b/tests/unit/plugins/openstack/cleanup/test_resources.py @@ -17,6 +17,7 @@ from boto import exception as boto_exception import ddt import mock from neutronclient.common import exceptions as neutron_exceptions +from novaclient import exceptions as nova_exc from rally.common import utils from rally.plugins.openstack.cleanup import resources @@ -132,6 +133,21 @@ class NovaFlavorsTestCase(test.TestCase): mock_name_matches_object.assert_has_calls( [mock.call(r.name, nutils.NovaScenario) for r in flavors]) + @mock.patch("%s.base.ResourceManager._manager" % BASE) + def test_is_deleted(self, mock_resource_manager__manager): + exc = nova_exc.NotFound(404) + mock_resource_manager__manager().get.side_effect = exc + flavor = resources.NovaFlavors() + flavor.raw_resource = mock.MagicMock() + self.assertEqual(True, flavor.is_deleted()) + + @mock.patch("%s.base.ResourceManager._manager" % BASE) + def test_is_deleted_fail(self, mock_resource_manager__manager): + mock_resource_manager__manager().get.side_effect = TypeError() + flavor = resources.NovaFlavors() + flavor.raw_resource = mock.MagicMock() + self.assertRaises(TypeError, flavor.is_deleted) + class NovaSecurityGroupTestCase(test.TestCase):