From c436e173c4162c940523a2978799193672ff9cd3 Mon Sep 17 00:00:00 2001 From: Ryan Harper Date: Mon, 2 Apr 2018 13:33:39 -0600 Subject: cc_resizefs, util: handle no /dev/zfs The zfs/zpool commands will hang for 10 seconds if /dev/zfs is not present (bug 1760173). This is a common occurence for containers using zfs as rootfs. Additionally handle missing zpool command or other errors that may occur while executing the zpool command. --- tests/unittests/test_util.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index 8685b8e2..50101906 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -366,8 +366,11 @@ class TestMountinfoParsing(helpers.ResourceUsingTestCase): expected = ('none', 'tmpfs', '/run/lock') self.assertEqual(expected, util.parse_mount_info('/run/lock', lines)) + @mock.patch('cloudinit.util.os') @mock.patch('cloudinit.util.subp') - def test_get_device_info_from_zpool(self, zpool_output): + def test_get_device_info_from_zpool(self, zpool_output, m_os): + # mock /dev/zfs exists + m_os.path.exists.return_value = True # mock subp command from util.get_mount_info_fs_on_zpool zpool_output.return_value = ( self.readResource('zpool_status_simple.txt'), '' @@ -376,9 +379,31 @@ class TestMountinfoParsing(helpers.ResourceUsingTestCase): ret = util.get_device_info_from_zpool('vmzroot') self.assertEqual('gpt/system', ret) self.assertIsNotNone(ret) + m_os.path.exists.assert_called_with('/dev/zfs') + @mock.patch('cloudinit.util.os') + def test_get_device_info_from_zpool_no_dev_zfs(self, m_os): + # mock /dev/zfs missing + m_os.path.exists.return_value = False + # save function return values and do asserts + ret = util.get_device_info_from_zpool('vmzroot') + self.assertIsNone(ret) + + @mock.patch('cloudinit.util.os') + @mock.patch('cloudinit.util.subp') + def test_get_device_info_from_zpool_handles_no_zpool(self, m_sub, m_os): + """Handle case where there is no zpool command""" + # mock /dev/zfs exists + m_os.path.exists.return_value = True + m_sub.side_effect = util.ProcessExecutionError("No zpool cmd") + ret = util.get_device_info_from_zpool('vmzroot') + self.assertIsNone(ret) + + @mock.patch('cloudinit.util.os') @mock.patch('cloudinit.util.subp') - def test_get_device_info_from_zpool_on_error(self, zpool_output): + def test_get_device_info_from_zpool_on_error(self, zpool_output, m_os): + # mock /dev/zfs exists + m_os.path.exists.return_value = True # mock subp command from util.get_mount_info_fs_on_zpool zpool_output.return_value = ( self.readResource('zpool_status_simple.txt'), 'error' -- cgit v1.2.3