Merge "[Verify] Fixing tokens expiration issue when deleting Tempest resources"

This commit is contained in:
Jenkins 2015-11-16 14:18:38 +00:00 committed by Gerrit Code Review
commit dda2a81116
2 changed files with 50 additions and 23 deletions

View File

@ -291,9 +291,14 @@ class TempestResourcesContext(object):
_write_config(self.conf_path, self.conf)
def __exit__(self, exc_type, exc_value, exc_traceback):
self._cleanup_roles()
self._cleanup_resource("image", self._created_images)
self._cleanup_resource("flavor", self._created_flavors)
# Tempest tests may take more than 1 hour and we should remove all
# cached clients sessions to avoid tokens expiration when deleting
# Tempest resources.
self.clients.clear()
self._cleanup_tempest_roles()
self._cleanup_images()
self._cleanup_flavors()
if "neutron" in self.available_services:
self._cleanup_network_resources()
@ -366,19 +371,25 @@ class TempestResourcesContext(object):
return net
def _cleanup_roles(self):
def _cleanup_tempest_roles(self):
keystoneclient = self.clients.keystone()
for role in self._created_roles:
LOG.debug("Deleting role '%s'" % role.name)
role.delete()
keystoneclient.roles.delete(role.id)
def _cleanup_resource(self, resource_type, created_resources):
for res in created_resources:
LOG.debug("Deleting %s '%s'" % (resource_type, res.name))
if res.id == self.conf.get("compute", "%s_ref" % resource_type):
self.conf.set("compute", "%s_ref" % resource_type, "")
else:
self.conf.set("compute", "%s_ref_alt" % resource_type, "")
res.delete()
def _cleanup_images(self):
glanceclient = self.clients.glance()
for image in self._created_images:
LOG.debug("Deleting image '%s'" % image.name)
glanceclient.images.delete(image.id)
self._remove_opt_value_from_config(image.id)
def _cleanup_flavors(self):
novaclient = self.clients.nova()
for flavor in self._created_flavors:
LOG.debug("Deleting flavor '%s'" % flavor.name)
novaclient.flavors.delete(flavor.id)
self._remove_opt_value_from_config(flavor.id)
def _cleanup_network_resources(self):
neutron_wrapper = network.NeutronWrapper(self.clients, task=None)

View File

@ -425,21 +425,37 @@ class TempestResourcesContextTestCase(test.TestCase):
self.assertEqual("id1", network.id)
self.assertEqual("id1", self.context._created_networks[0].id)
def test__cleanup_roles(self):
self.context._created_roles = [mock.MagicMock(), mock.MagicMock()]
def test__cleanup_tempest_roles(self):
self.context._created_roles = [fakes.FakeRole(), fakes.FakeRole()]
self.context._cleanup_roles()
for role in self.context._created_roles:
self.assertEqual(role.delete.call_count, 1)
self.context._cleanup_tempest_roles()
client = self.context.clients.keystone()
self.assertEqual(client.roles.delete.call_count, 2)
def test__cleanup_images(self):
self.context._created_images = [fakes.FakeImage(id="id1"),
fakes.FakeImage(id="id2")]
self.context.conf.set("compute", "image_ref", "id1")
self.context.conf.set("compute", "image_ref_alt", "id2")
self.context._cleanup_images()
client = self.context.clients.glance()
self.assertEqual(client.images.delete.call_count, 2)
self.assertEqual("", self.context.conf.get("compute", "image_ref"))
self.assertEqual("", self.context.conf.get("compute", "image_ref_alt"))
def test__cleanup_flavors(self):
self.context._created_flavors = [fakes.FakeFlavor(id="id1"),
fakes.FakeFlavor(id="id2")]
def test__cleanup_resource(self):
created_flavors = [mock.MagicMock(id="id1"), mock.MagicMock(id="id2")]
self.context.conf.set("compute", "flavor_ref", "id1")
self.context.conf.set("compute", "flavor_ref_alt", "id2")
self.context._cleanup_resource("flavor", created_flavors)
for flavor in self.context._created_flavors:
self.assertEqual(flavor.delete.call_count, 1)
self.context._cleanup_flavors()
client = self.context.clients.nova()
self.assertEqual(client.flavors.delete.call_count, 2)
self.assertEqual("", self.context.conf.get("compute", "flavor_ref"))
self.assertEqual("", self.context.conf.get("compute",