commit 454eee65ef827cd3305a37b7bb3674d8408d0879 Author: Goutham Pacha Ravi Date: Wed Jan 16 16:31:21 2019 -0800 [Python3] Fix python3 bugs in code - The BaseException class no longer has a "message" attribute in python 3 [1]. On the contrary, the string representation of an Exception object will print all the exception args in all supported python versions, so use that instead. - Functional tests were run with a specific locale, remove those annotations so we can handle unicode encoding and decoding in python3 envs. - Cleanup errors were not being handled correctly, cleanup the code so we don't have spurious failures. - In python3, dict.keys() gives you a view for lazy loading, so convert occurrences to lists as expected in our functional tests. - Start capturing STDOUT and STDERR in tox envs to enable troubleshooting. Co-Authored-By: Goutham Pacha Ravi Closes-Bug: #1811627 Closes-Bug: #1811516 Change-Id: Idaa2fb9b60451b3fbd298e19574195f2d663c6f4 [1] https://www.python.org/dev/peps/pep-0352/#transition-plan (cherry picked from commit b955ac9064b15d00e8966dda745814c146c8af01) Signed-off-by: Goutham Pacha Ravi (cherry picked from commit c0ce858da632355404906b6b05ee8de232a1b6fa) diff --git a/manilaclient/shell.py b/manilaclient/shell.py index c73cb4b..76417ff 100644 --- a/manilaclient/shell.py +++ b/manilaclient/shell.py @@ -687,10 +687,7 @@ def main(): sys.exit(130) except Exception as e: logger.debug(e, exc_info=1) - message = e.message - if not isinstance(message, six.string_types): - message = str(message) - print("ERROR: %s" % encodeutils.safe_encode(message), file=sys.stderr) + print("ERROR: %s" % six.text_type(e), file=sys.stderr) sys.exit(1) diff --git a/manilaclient/tests/functional/base.py b/manilaclient/tests/functional/base.py index ca3faa5..6a09a57 100644 --- a/manilaclient/tests/functional/base.py +++ b/manilaclient/tests/functional/base.py @@ -41,14 +41,12 @@ class handle_cleanup_exceptions(object): return self def __exit__(self, exc_type, exc_value, exc_traceback): - if not (isinstance(exc_value, - (lib_exc.NotFound, lib_exc.Forbidden)) or - CONF.suppress_errors_in_cleanup): - return False # Do not suppress error if any - if exc_traceback: - LOG.error("Suppressed cleanup error: " - "\n%s", traceback.format_exc()) - return True # Suppress error if any + if isinstance(exc_value, (lib_exc.NotFound, lib_exc.Forbidden)): + return True + elif CONF.suppress_errors_in_cleanup: + LOG.error("Suppressed cleanup error: \n%s", traceback.format_exc()) + return True + return False # Don't suppress cleanup errors class BaseTestCase(base.ClientTestBase): diff --git a/manilaclient/tests/functional/client.py b/manilaclient/tests/functional/client.py index 9ade0a0..e0ef806 100644 --- a/manilaclient/tests/functional/client.py +++ b/manilaclient/tests/functional/client.py @@ -44,7 +44,7 @@ def not_found_wrapper(f): return f(self, *args, **kwargs) except tempest_lib_exc.CommandFailed as e: for regexp in ('No (\w+) with a name or ID', 'not(.*){0,5}found'): - if re.search(regexp, e.stderr): + if re.search(regexp, six.text_type(e.stderr)): # Raise appropriate 'NotFound' error raise tempest_lib_exc.NotFound() raise @@ -58,7 +58,7 @@ def forbidden_wrapper(f): try: return f(self, *args, **kwargs) except tempest_lib_exc.CommandFailed as e: - if re.search('HTTP 403', e.stderr): + if re.search('HTTP 403', six.text_type(e.stderr)): # Raise appropriate 'Forbidden' error. raise tempest_lib_exc.Forbidden() raise diff --git a/manilaclient/tests/functional/test_shares_metadata.py b/manilaclient/tests/functional/test_shares_metadata.py index b14f7ed..6ba8340 100644 --- a/manilaclient/tests/functional/test_shares_metadata.py +++ b/manilaclient/tests/functional/test_shares_metadata.py @@ -74,7 +74,7 @@ class SharesMetadataReadWriteTest(base.BaseTestCase): self.user_client.set_share_metadata(share["id"], md) # Unset share metadata - self.user_client.unset_share_metadata(share["id"], md.keys()) + self.user_client.unset_share_metadata(share["id"], list(md.keys())) # Verify deletion of share metadata metadata = self.user_client.get_share_metadata(share["id"]) diff --git a/releasenotes/notes/bug-1811516-bug-1811627-python3-fixes-4b26130027b2c076.yaml b/releasenotes/notes/bug-1811516-bug-1811627-python3-fixes-4b26130027b2c076.yaml new file mode 100644 index 0000000..5398069 --- /dev/null +++ b/releasenotes/notes/bug-1811516-bug-1811627-python3-fixes-4b26130027b2c076.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + The shell utility has been fixed to report errors correctly on + python3 environments. diff --git a/tox.ini b/tox.ini index ed6bb38..7d0281f 100644 --- a/tox.ini +++ b/tox.ini @@ -7,9 +7,8 @@ skipsdist = True [testenv] install_command = pip install {opts} {packages} setenv = VIRTUAL_ENV={envdir} - LANG=en_US.UTF-8 - LANGUAGE=en_US:en - LC_ALL=C + OS_STDOUT_CAPTURE=1 + OS_STDERR_CAPTURE=1 whitelist_externals = find deps = @@ -54,9 +53,6 @@ commands = -b html releasenotes/source releasenotes/build/html [testenv:functional] -# TODO(gouthamr): Remove this when https://review.opendev.org/757651/ -# merges -basepython = python2.7 setenv = {[testenv]setenv} OS_TEST_PATH = ./manilaclient/tests/functional