From 4aab6827c80679129502f5a9ecde1760642d0da8 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Fri, 26 Jun 2020 14:51:42 -0400 Subject: [PATCH 1/3] test_util: add (partial) testing for util.mount_cb --- cloudinit/tests/test_util.py | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/cloudinit/tests/test_util.py b/cloudinit/tests/test_util.py index 0d9a8fd90f4..6f4c7cb36f6 100644 --- a/cloudinit/tests/test_util.py +++ b/cloudinit/tests/test_util.py @@ -704,4 +704,72 @@ def test_read_conf_from_cmdline_config(self, expected_cfg, cmdline): assert expected_cfg == util.read_conf_from_cmdline(cmdline=cmdline) +class TestMountCb: + """Tests for ``util.mount_cb``. + + These tests consider the "unit" under test to be ``util.mount_cb`` and + ``util.unmounter``, which is only used by ``mount_cb``. + + TODO: + * Test default mtype determination + * Test the if/else branch that actually performs the mounting operation + """ + + @pytest.yield_fixture + def already_mounted_device_and_mountdict(self): + """Mock an already-mounted device, and yield (device, mount dict)""" + device = "/dev/fake0" + mountpoint = "/mnt/fake" + with mock.patch("cloudinit.util.subp.subp"): + with mock.patch("cloudinit.util.mounts") as m_mounts: + mounts = {device: {"mountpoint": mountpoint}} + m_mounts.return_value = mounts + yield device, mounts[device] + + @pytest.fixture + def already_mounted_device(self, already_mounted_device_and_mountdict): + """already_mounted_device_and_mountdict, but return only the device""" + return already_mounted_device_and_mountdict[0] + + @pytest.mark.parametrize("invalid_mtype", [int(0), float(0.0), dict()]) + def test_typeerror_raised_for_invalid_mtype(self, invalid_mtype): + with pytest.raises(TypeError): + util.mount_cb(mock.Mock(), mock.Mock(), mtype=invalid_mtype) + + @mock.patch("cloudinit.util.subp.subp") + def test_already_mounted_does_not_mount_or_umount_anything( + self, m_subp, already_mounted_device + ): + util.mount_cb(already_mounted_device, mock.Mock()) + + assert 0 == m_subp.call_count + + @pytest.mark.parametrize("trailing_slash_in_mounts", ["/", ""]) + def test_already_mounted_calls_callback( + self, already_mounted_device_and_mountdict, trailing_slash_in_mounts + ): + device, mount_dict = already_mounted_device_and_mountdict + mountpoint = mount_dict["mountpoint"] + mount_dict["mountpoint"] += trailing_slash_in_mounts + + callback = mock.Mock() + util.mount_cb(device, callback) + + # The mountpoint passed to callback should always have a trailing + # slash, regardless of the input + assert [mock.call(mountpoint + "/")] == callback.call_args_list + + def test_already_mounted_calls_callback_with_data( + self, already_mounted_device + ): + callback = mock.Mock() + util.mount_cb( + already_mounted_device, callback, data=mock.sentinel.data + ) + + assert [ + mock.call(mock.ANY, mock.sentinel.data) + ] == callback.call_args_list + + # vi: ts=4 expandtab From a79c138010e5e22e99a978dcaa45d666671b0521 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Mon, 29 Jun 2020 08:50:26 -0400 Subject: [PATCH 2/3] test_util: update TODO formatting --- cloudinit/tests/test_util.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cloudinit/tests/test_util.py b/cloudinit/tests/test_util.py index 6f4c7cb36f6..8f9e6e1fb15 100644 --- a/cloudinit/tests/test_util.py +++ b/cloudinit/tests/test_util.py @@ -710,9 +710,8 @@ class TestMountCb: These tests consider the "unit" under test to be ``util.mount_cb`` and ``util.unmounter``, which is only used by ``mount_cb``. - TODO: - * Test default mtype determination - * Test the if/else branch that actually performs the mounting operation + TODO: Test default mtype determination + TODO: Test the if/else branch that actually performs the mounting operation """ @pytest.yield_fixture From a70252a75d6d0d79023fdf8fd7f1ce7b5c5fac4a Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Mon, 29 Jun 2020 09:00:49 -0400 Subject: [PATCH 3/3] test_util: fix parameter order --- cloudinit/tests/test_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinit/tests/test_util.py b/cloudinit/tests/test_util.py index 8f9e6e1fb15..096a3037ce3 100644 --- a/cloudinit/tests/test_util.py +++ b/cloudinit/tests/test_util.py @@ -745,7 +745,7 @@ def test_already_mounted_does_not_mount_or_umount_anything( @pytest.mark.parametrize("trailing_slash_in_mounts", ["/", ""]) def test_already_mounted_calls_callback( - self, already_mounted_device_and_mountdict, trailing_slash_in_mounts + self, trailing_slash_in_mounts, already_mounted_device_and_mountdict ): device, mount_dict = already_mounted_device_and_mountdict mountpoint = mount_dict["mountpoint"]