From e07ad4a570c6588323bac0a5417167e4fd690b28 Mon Sep 17 00:00:00 2001 From: Ksenija Stanojevic Date: Tue, 9 May 2023 14:21:50 -0700 Subject: Remove mount NTFS error message (#2134) Provide an option to suppress error logging from mount_cb as some errors can be expected error and handled appropriately by DataSources. For example: failure to mount NTFS volumes on VMs that do not have NTFS drivers. --- cloudinit/sources/DataSourceAzure.py | 1 + cloudinit/util.py | 26 ++++++++++++++++---------- tests/unittests/sources/test_azure.py | 4 +++- tests/unittests/test_util.py | 27 ++++++++++++++++++++++++--- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py index 923644e1..863a06b5 100644 --- a/cloudinit/sources/DataSourceAzure.py +++ b/cloudinit/sources/DataSourceAzure.py @@ -1596,6 +1596,7 @@ def can_dev_be_reformatted(devpath, preserve_ntfs): count_files, mtype="ntfs", update_env_for_mount={"LANG": "C"}, + log_error=False, ) except util.MountFailedError as e: evt.description = "cannot mount ntfs" diff --git a/cloudinit/util.py b/cloudinit/util.py index b0d2ddb0..7651ae67 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1908,7 +1908,12 @@ def mounts(): def mount_cb( - device, callback, data=None, mtype=None, update_env_for_mount=None + device, + callback, + data=None, + mtype=None, + update_env_for_mount=None, + log_error=True, ): """ Mount the device, call method 'callback' passing the directory @@ -1968,15 +1973,16 @@ def mount_cb( mountpoint = tmpd break except (IOError, OSError) as exc: - LOG.debug( - "Failed to mount device: '%s' with type: '%s' " - "using mount command: '%s', " - "which caused exception: %s", - device, - mtype, - " ".join(mountcmd), - exc, - ) + if log_error: + LOG.debug( + "Failed to mount device: '%s' with type: '%s' " + "using mount command: '%s', " + "which caused exception: %s", + device, + mtype, + " ".join(mountcmd), + exc, + ) failure_reason = exc if not mountpoint: raise MountFailedError( diff --git a/tests/unittests/sources/test_azure.py b/tests/unittests/sources/test_azure.py index 47e65658..f1964fde 100644 --- a/tests/unittests/sources/test_azure.py +++ b/tests/unittests/sources/test_azure.py @@ -2245,7 +2245,9 @@ class TestCanDevBeReformatted(CiTestCase): # return sorted by partition number return sorted(ret, key=lambda d: d[0]) - def mount_cb(device, callback, mtype, update_env_for_mount): + def mount_cb( + device, callback, mtype, update_env_for_mount, log_error=False + ): self.assertEqual("ntfs", mtype) self.assertEqual("C", update_env_for_mount.get("LANG")) p = self.tmp_dir() diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index 256eb291..763bd2bd 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -1573,6 +1573,30 @@ class TestMountCb: mock.call(mock.ANY, mock.sentinel.data) ] == callback.call_args_list + @pytest.mark.parametrize("log_error", [True, False]) + @mock.patch(M_PATH + "subp.subp") + def test_mount_cb_log(self, m_subp, log_error, caplog): + log_msg = ( + "Failed to mount device: '/dev/fake0' with type: " + "'ntfs' using mount command:" + ) + m_subp.side_effect = subp.ProcessExecutionError( + "", "unknown filesystem type 'ntfs'" + ) + callback = mock.Mock(autospec=True) + with pytest.raises(Exception): + util.mount_cb( + "/dev/fake0", + callback, + mtype="ntfs", + update_env_for_mount={"LANG": "C"}, + log_error=log_error, + ) + if log_error: + assert log_msg in caplog.text + else: + assert log_msg not in caplog.text + @mock.patch(M_PATH + "write_file") class TestEnsureFile: @@ -2073,7 +2097,6 @@ class TestMountinfoParsing(helpers.ResourceUsingTestCase): self.assertEqual(expected, util.parse_mount_info("/", lines)) def test_precise_ext4_root(self): - lines = helpers.readResource("mountinfo_precise_ext4.txt").splitlines() expected = ("/dev/mapper/vg0-root", "ext4", "/") @@ -2516,7 +2539,6 @@ class TestEncode(helpers.TestCase): class TestProcessExecutionError(helpers.TestCase): - template = ( "{description}\n" "Command: {cmd}\n" @@ -2827,7 +2849,6 @@ class TestHuman2Bytes: util.human2bytes(test_i) def test_ibibytes2bytes(self): - assert util.human2bytes("0.5GiB") == 536870912 assert util.human2bytes("100MiB") == 104857600 -- cgit v1.2.1