From bf9b3d57bf1898a86cadb9683911e1e0cccd4644 Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Tue, 29 Oct 2024 09:20:32 -0400 Subject: [PATCH] selftests/unit/utils/cpu.py: support the context manager pattern The current mock method on the two tests modified here do not support the context manager pattern. Let's use the standard library support for mocking open that supports it. This is needed for future lint check improvements that will enforce the use of open() as a context manager to avoid leaking resources. The test_set_idle_state_withsetstate() test has a significant change of behavior: one io.BytesIO instance was used for the writes that, in real life, would happen on two different files. The proposed change brings a more realistic, with a single write to a single file (although it only checks half of the values now). Still, I believe this is an improvement, including the extra check for the file that was actually written to. Reference: https://docs.python.org/3/library/unittest.mock.html#mock-open Signed-off-by: Cleber Rosa --- selftests/unit/utils/cpu.py | 44 +++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/selftests/unit/utils/cpu.py b/selftests/unit/utils/cpu.py index 50f14ff8a5..3bda99f013 100644 --- a/selftests/unit/utils/cpu.py +++ b/selftests/unit/utils/cpu.py @@ -201,9 +201,8 @@ def test_get_idle_state_off(self): "glob.glob", return_value=["/sys/devices/system/cpu/cpu0/cpuidle/state1"], ): - with unittest.mock.patch( - "builtins.open", return_value=io.BytesIO(b"0") - ): + mocked_open = unittest.mock.mock_open(read_data=b"0") + with unittest.mock.patch("builtins.open", mocked_open): self.assertEqual(cpu.get_idle_state(), retval) def test_get_idle_state_on(self): @@ -213,51 +212,62 @@ def test_get_idle_state_on(self): "glob.glob", return_value=["/sys/devices/system/cpu/cpu0/cpuidle/state1"], ): - with unittest.mock.patch( - "builtins.open", return_value=io.BytesIO(b"1") - ): + mocked_open = unittest.mock.mock_open(read_data=b"1") + with unittest.mock.patch("builtins.open", mocked_open): self.assertEqual(cpu.get_idle_state(), retval) def test_set_idle_state_default(self): - output = io.BytesIO() with unittest.mock.patch("avocado.utils.cpu.online_list", return_value=[0]): with unittest.mock.patch( "glob.glob", return_value=["/sys/devices/system/cpu/cpu0/cpuidle/state1"], ): - with unittest.mock.patch("builtins.open", return_value=output): + mocked_open = unittest.mock.mock_open() + with unittest.mock.patch("builtins.open", mocked_open): cpu.set_idle_state() - self.assertEqual(output.getvalue(), b"1") + mocked_open.assert_called_with( + "/sys/devices/system/cpu/cpu0/cpuidle/state0/disable", "wb" + ) + mocked_fo = mocked_open() + mocked_fo.write.assert_called_once_with(b"1") def test_set_idle_state_withstateno(self): - output = io.BytesIO() with unittest.mock.patch("avocado.utils.cpu.online_list", return_value=[0]): with unittest.mock.patch( "glob.glob", return_value=["/sys/devices/system/cpu/cpu0/cpuidle/state2"], ): - with unittest.mock.patch("builtins.open", return_value=output): + mocked_open = unittest.mock.mock_open() + with unittest.mock.patch("builtins.open", mocked_open): cpu.set_idle_state(disable=False, state_number="2") - self.assertEqual(output.getvalue(), b"0") + mocked_open.assert_called_with( + "/sys/devices/system/cpu/cpu0/cpuidle/state2/disable", "wb" + ) + mocked_fo = mocked_open() + mocked_fo.write.assert_called_once_with(b"0") def test_set_idle_state_withsetstate(self): - output = io.BytesIO() with unittest.mock.patch("avocado.utils.cpu.online_list", return_value=[0, 2]): with unittest.mock.patch( "glob.glob", return_value=["/sys/devices/system/cpu/cpu0/cpuidle/state1"], ): - with unittest.mock.patch("builtins.open", return_value=output): + mocked_open = unittest.mock.mock_open() + with unittest.mock.patch("builtins.open", mocked_open): cpu.set_idle_state(setstate={0: {0: True}, 2: {0: False}}) - self.assertEqual(output.getvalue(), b"10") + mocked_open.assert_called_with( + "/sys/devices/system/cpu/cpu2/cpuidle/state0/disable", "wb" + ) + mocked_fo = mocked_open() + mocked_fo.write.assert_called_with(b"0") def test_set_idle_state_disable(self): - output = io.BytesIO() function = "avocado.utils.cpu.online_list" state_file = "/sys/devices/system/cpu/cpu0/cpuidle/state1" with unittest.mock.patch(function, return_value=[0, 2]): with unittest.mock.patch("glob.glob", return_value=[state_file]): - with unittest.mock.patch("builtins.open", return_value=output): + mocked_open = unittest.mock.mock_open() + with unittest.mock.patch("builtins.open", mocked_open): with self.assertRaises(TypeError): cpu.set_idle_state(disable=1)