Skip to content

Commit

Permalink
test: add unit tests, remove @timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tomli380576 committed Dec 23, 2024
1 parent 9dcb9d5 commit f51a4a2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
50 changes: 22 additions & 28 deletions providers/base/bin/reboot_check_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,34 +311,28 @@ def wait_for_graphical_target(self, max_wait_seconds: int) -> bool:
:return: whether graphical.target was reached within max_wait_seconds
"""

@timeout(max_wait_seconds)
def check_systemd():
print("Checking if DUT has reached graphical.target...")
while True:
try:
out = sp.run(
[
"systemd-analyze",
"critical-chain",
"graphical.target",
"--no-pager",
],
stdout=sp.DEVNULL,
stderr=sp.DEVNULL,
)
if out.returncode == 0:
print("Graphical target reached!")
return True
else:
time.sleep(1)
except sp.CalledProcessError:
pass

try:
if check_systemd():
return True
except TimeoutError:
return False
start = time.time()
print("Checking if DUT has reached graphical.target...")
while time.time() - start < max_wait_seconds:
try:
out = sp.run(
[
"systemd-analyze",
"critical-chain",
"graphical.target",
"--no-pager",
],
stdout=sp.DEVNULL,
stderr=sp.DEVNULL,
)
if out.returncode == 0:
print("Graphical target reached!")
return True
else:
time.sleep(1)
except sp.CalledProcessError:
pass

return False

def parse_unity_support_output(
Expand Down
31 changes: 31 additions & 0 deletions providers/base/tests/test_reboot_check_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import typing as T
import subprocess as sp
from checkbox_support.helpers.timeout import mock_timeout


def do_nothing(args: T.List[str], **kwargs):
Expand Down Expand Up @@ -148,6 +149,36 @@ def test_is_hardware_renderer_available_fail(
tester = RCT.HardwareRendererTester()
self.assertFalse(tester.is_hardware_renderer_available())

def test_slow_boot_scenario(self):

def fake_time(delta: int):
# fake a time.time() delta using closure
call_idx = ["start"]

def wrapped():
if call_idx[0] == "start":
call_idx[0] = "end"
return 0 # when time.time is initially called
else:
return delta # the "last" time when time.time is called

return wrapped

with patch("subprocess.run") as mock_run, patch(
"time.sleep"
) as mock_sleep, patch("time.time") as mock_time:
mock_run.side_effect = lambda *args, **kwargs: sp.CompletedProcess(
[],
1,
"systemd says it's not ready",
"graphical target not reached blah",
)
mock_sleep.side_effect = do_nothing
mock_time.side_effect = fake_time(3)
tester = RCT.HardwareRendererTester()

self.assertFalse(tester.wait_for_graphical_target(2))


class InfoDumpTests(unittest.TestCase):
@classmethod
Expand Down

0 comments on commit f51a4a2

Please sign in to comment.