Fix six.reraise bug in rbac_rule_validation
This PS fixes an issue [0] when calling six.reraise in
rbac_rule_validation after a generic Exception has been
caught. Instead of using six.reraise and changing the
exception's detailed error message unnecessarily,
excutils.save_and_reraise_exception from oslo_utils
is used instead.
[0] http://logs.openstack.org/97/507697/5/check/legacy-tempest-dsvm-patrole-member/d2530aa/job-output.txt.gz#_2017-11-02_20_04_35_107548
Change-Id: I99f2cc60f301172847dd8f98fbc7deff4d5e94e4
diff --git a/patrole_tempest_plugin/rbac_rule_validation.py b/patrole_tempest_plugin/rbac_rule_validation.py
index 540d006..927c803 100644
--- a/patrole_tempest_plugin/rbac_rule_validation.py
+++ b/patrole_tempest_plugin/rbac_rule_validation.py
@@ -17,6 +17,7 @@
import sys
import testtools
+from oslo_utils import excutils
import six
from tempest import config
@@ -158,14 +159,14 @@
raise exceptions.Forbidden(
"%s Exception was: %s" % (msg, e))
except Exception as e:
- exc_info = sys.exc_info()
- error_details = exc_info[1].__str__()
- msg = ("An unexpected exception has occurred during test: %s. "
- "Exception was: %s"
- % (test_func.__name__, error_details))
- test_status = ('Error, %s' % (error_details))
- LOG.error(msg)
- six.reraise(exc_info[0], exc_info[0](msg), exc_info[2])
+ with excutils.save_and_reraise_exception():
+ exc_info = sys.exc_info()
+ error_details = six.text_type(exc_info[1])
+ msg = ("An unexpected exception has occurred during test: "
+ "%s. Exception was: %s" % (test_func.__name__,
+ error_details))
+ test_status = 'Error, %s' % (error_details)
+ LOG.error(msg)
else:
if not allowed:
LOG.error("Role %s was allowed to perform %s",
diff --git a/patrole_tempest_plugin/tests/unit/test_rbac_rule_validation.py b/patrole_tempest_plugin/tests/unit/test_rbac_rule_validation.py
index 3065cfe..82f0428 100644
--- a/patrole_tempest_plugin/tests/unit/test_rbac_rule_validation.py
+++ b/patrole_tempest_plugin/tests/unit/test_rbac_rule_validation.py
@@ -203,17 +203,16 @@
def test_policy(*args):
raise exceptions.Forbidden('Test message')
- error_re = ("An unexpected exception has occurred during test: "
- "test_policy. Exception was: Forbidden\nDetails: Test "
- "message")
+ error_msg = ("An unexpected exception has occurred during test: "
+ "test_policy. Exception was: Forbidden\nDetails: Test "
+ "message")
for allowed in [True, False]:
mock_authority.PolicyAuthority.return_value.allowed.\
return_value = allowed
-
- self.assertRaisesRegex(exceptions.Forbidden, '.* ' + error_re,
+ self.assertRaisesRegex(exceptions.Forbidden, 'Test message',
test_policy, self.mock_test_args)
- self.assertIn(error_re, mock_log.error.mock_calls[0][1][0])
+ self.assertIn(error_msg, mock_log.error.mock_calls[0][1][0])
mock_log.error.reset_mock()
@mock.patch.object(rbac_rv, 'LOG', autospec=True)