commit dea710d1e6d7f74ea0157593a7e614200aa486b2 Author: Brian Rosmaita Date: Mon Oct 12 17:47:38 2020 -0400 Preparing for removal of six.reraise() Add unit tests to make sure the behavior of two functions using six.rereaise() will be the same once six usage is removed. Change-Id: Ie75beeb3f52aac26757500b39b76f2811ff97ac9 diff --git a/cinder/tests/unit/image/test_glance.py b/cinder/tests/unit/image/test_glance.py index 0e99ca0..1afc619 100644 --- a/cinder/tests/unit/image/test_glance.py +++ b/cinder/tests/unit/image/test_glance.py @@ -16,6 +16,7 @@ import datetime import itertools +import traceback from unittest import mock import ddt @@ -721,6 +722,62 @@ class TestGlanceImageService(test.TestCase): self.assertRaises(exception.ImageNotAuthorized, service.download, self.context, image_id, writer) + def test_client_translated_exc_includes_original_traceback(self): + image_id = 1 # doesn't matter + original_exc = glanceclient.exc.Forbidden(image_id) + + class MyGlanceStubClient(glance_stubs.StubGlanceClient): + def get(self, image_id): + raise original_exc + + client = MyGlanceStubClient() + service = self._create_image_service(client) + writer = NullWriter() + + exc = self.assertRaises(exception.ImageNotAuthorized, + service.download, + self.context, + image_id, + writer) + + original = traceback.extract_tb(original_exc.__traceback__) + original.reverse() + received = traceback.extract_tb(exc.__traceback__) + received.reverse() + # verify that we have the same traceback as original_exc + for orig, recd in zip(original, received): + self.assertEqual(orig, recd) + + # note that the received exception contains more frames in + # its traceback than the original + self.assertGreater(len(received), len(original)) + + def test_plain_translated_exc_includes_original_traceback(self): + original_exc = glanceclient.exc.Forbidden() + + class MyGlanceStubClient(glance_stubs.StubGlanceClient): + def list(self): + raise original_exc + + client = MyGlanceStubClient() + service = self._create_image_service(client) + + exc = self.assertRaises(exception.NotAuthorized, + service.detail, + self.context) + + original = traceback.extract_tb(original_exc.__traceback__) + original.reverse() + received = traceback.extract_tb(exc.__traceback__) + received.reverse() + # verify that we have the same traceback as original_exc + for orig, recd in zip(original, received): + self.assertEqual(orig, recd) + + # note that the received exception contains more frames in + # its traceback than the original + self.assertGreater(len(received), len(original)) + def test_client_httpforbidden_converts_to_imagenotauthed(self): class MyGlanceStubClient(glance_stubs.StubGlanceClient): """A client that raises a HTTPForbidden exception."""