BuildAPI: Improve exception processing in service.UnmountPath.
These are post-submit changes suggested on https://crrev.com/c/2327231/2.
BUG=chromium:1095661
TEST=run_tests
Change-Id: I483ee37feb324e26157d3a121763b5af50b045b4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2329394
Tested-by: Michael Mortensen <mmortensen@google.com>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Michael Mortensen <mmortensen@google.com>
diff --git a/service/sdk.py b/service/sdk.py
index ef9aef7..1713be8 100644
--- a/service/sdk.py
+++ b/service/sdk.py
@@ -25,9 +25,17 @@
class UnmountError(Error):
"""An error raised when unmount fails."""
- def __init__(self, message):
- self.message = message
- super(UnmountError, self).__init__(message)
+ def __init__(self, path, cmd_error=None, fs_debug=None):
+ super(UnmountError, self).__init__(path, cmd_error, fs_debug)
+ self.path = path
+ self.cmd_error = cmd_error
+ self.fs_debug = fs_debug
+
+ def __str__(self):
+ return (f'Umount failed: {self.cmd_error.result.stdout}.\n'
+ f'fuser output={self.fs_debug.fuser}\n'
+ f'lsof output={self.fs_debug.lsof}\n'
+ f'ps output={self.fs_debug.ps}\n')
class CreateArguments(object):
@@ -232,9 +240,7 @@
osutils.UmountTree(path.path)
except cros_build_lib.RunCommandError as e:
fs_debug = cros_sdk_lib.GetFileSystemDebug(path.path, run_ps=True)
- msg = (f'Umount failed: {e.result.error}.\nfuser output={fs_debug.fuser}\n'
- f'lsof output={fs_debug.lsof}\nps output=fs_debug.ps\n')
- raise UnmountError(msg)
+ raise UnmountError(path.path, e, fs_debug)
def GetChrootVersion(chroot_path=None):
diff --git a/service/sdk_unittest.py b/service/sdk_unittest.py
index d781e2f..ed3f350 100644
--- a/service/sdk_unittest.py
+++ b/service/sdk_unittest.py
@@ -95,7 +95,10 @@
'umount failure'))
with self.assertRaises(sdk.UnmountError) as unmount_assert:
sdk.UnmountPath(path_proto)
- self.assertIn('Umount failed:', unmount_assert.exception.message)
+ # Unpack the underlying (thrown) exception from the assertRaises context
+ # manager exception attribute.
+ unmount_exception = unmount_assert.exception
+ self.assertIn('Umount failed:', str(unmount_exception))
class CreateTest(cros_test_lib.RunCommandTempDirTestCase):