From 63c0bbc5c942b57175f655a2fa73c9b9ea427513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Tue, 16 May 2017 10:28:55 +0200 Subject: [PATCH] iso-wrapper: Capture debug information for mounting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Occasionally we have seen the mount command fail. The default error message says to set some environment variables and try again. We can just always set the environment and only print the output on failure. Signed-off-by: Lubomír Sedlář --- pungi/wrappers/iso.py | 10 +++++++++- tests/test_iso_wrapper.py | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pungi/wrappers/iso.py b/pungi/wrappers/iso.py index ab97e19f..5cf39e70 100644 --- a/pungi/wrappers/iso.py +++ b/pungi/wrappers/iso.py @@ -411,7 +411,15 @@ def mount(image, logger=None): # use guestmount to mount the image, which doesn't require root privileges # LIBGUESTFS_BACKEND=direct: running qemu directly without libvirt with util.temp_dir(prefix='iso-mount-') as mount_dir: - run(["LIBGUESTFS_BACKEND=direct", "guestmount", "-a", image, "-m", "/dev/sda", mount_dir]) + env = {'LIBGUESTFS_BACKEND': 'direct', 'LIBGUESTFS_DEBUG': '1', 'LIBGUESTFS_TRACE': '1'} + cmd = ["guestmount", "-a", image, "-m", "/dev/sda", mount_dir] + ret, out = run(cmd, env=env, can_fail=True) + if ret != 0: + # The mount command failed, something is wrong. Log the output and raise an exception. + if logger: + logger.log_error('Command %s exited with %s and output:\n%s' + % (cmd, ret, out)) + raise RuntimeError('Failed to mount %s' % image) try: yield mount_dir finally: diff --git a/tests/test_iso_wrapper.py b/tests/test_iso_wrapper.py index 18d15623..2e8f91f9 100644 --- a/tests/test_iso_wrapper.py +++ b/tests/test_iso_wrapper.py @@ -41,6 +41,7 @@ class TestIsoUtils(unittest.TestCase): @mock.patch('pungi.util.run_unmount_cmd') @mock.patch('pungi.wrappers.iso.run') def test_mount_iso(self, mock_run, mock_unmount): + mock_run.return_value = (0, '') with iso.mount('dummy') as temp_dir: self.assertTrue(os.path.isdir(temp_dir)) self.assertEqual(len(mock_run.call_args_list), 1) @@ -50,6 +51,7 @@ class TestIsoUtils(unittest.TestCase): @mock.patch('pungi.util.run_unmount_cmd') @mock.patch('pungi.wrappers.iso.run') def test_mount_iso_always_unmounts(self, mock_run, mock_unmount): + mock_run.return_value = (0, '') try: with iso.mount('dummy') as temp_dir: self.assertTrue(os.path.isdir(temp_dir)) @@ -59,3 +61,15 @@ class TestIsoUtils(unittest.TestCase): self.assertEqual(len(mock_run.call_args_list), 1) self.assertEqual(len(mock_unmount.call_args_list), 1) self.assertFalse(os.path.isdir(temp_dir)) + + @mock.patch('pungi.util.run_unmount_cmd') + @mock.patch('pungi.wrappers.iso.run') + def test_mount_iso_raises_on_error(self, mock_run, mock_unmount): + log = mock.Mock() + mock_run.return_value = (1, 'Boom') + with self.assertRaises(RuntimeError): + with iso.mount('dummy', logger=log) as temp_dir: + self.assertTrue(os.path.isdir(temp_dir)) + self.assertEqual(len(mock_run.call_args_list), 1) + self.assertEqual(len(mock_unmount.call_args_list), 0) + self.assertEqual(len(log.mock_calls), 1)