Skip to content

Commit

Permalink
selftests/unit/utils/cpu.py: support the context manager pattern
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
clebergnu committed Oct 29, 2024
1 parent b58d392 commit bf9b3d5
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions selftests/unit/utils/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand Down

0 comments on commit bf9b3d5

Please sign in to comment.