nixos/test: Use retry() in all looping functions that need timeouts
This commit is contained in:
parent
39bf8a7b28
commit
e05ae69904
|
@ -312,8 +312,13 @@ class Machine:
|
||||||
self.monitor.send(message)
|
self.monitor.send(message)
|
||||||
return self.wait_for_monitor_prompt()
|
return self.wait_for_monitor_prompt()
|
||||||
|
|
||||||
def wait_for_unit(self, unit: str, user: Optional[str] = None) -> bool:
|
def wait_for_unit(self, unit: str, user: Optional[str] = None) -> None:
|
||||||
while True:
|
"""Wait for a systemd unit to get into "active" state.
|
||||||
|
Throws exceptions on "failed" and "inactive" states as well as
|
||||||
|
after timing out.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def check_active(_: Any) -> bool:
|
||||||
info = self.get_unit_info(unit, user)
|
info = self.get_unit_info(unit, user)
|
||||||
state = info["ActiveState"]
|
state = info["ActiveState"]
|
||||||
if state == "failed":
|
if state == "failed":
|
||||||
|
@ -329,8 +334,10 @@ class Machine:
|
||||||
'unit "{}" is inactive and there ' "are no pending jobs"
|
'unit "{}" is inactive and there ' "are no pending jobs"
|
||||||
).format(unit)
|
).format(unit)
|
||||||
)
|
)
|
||||||
if state == "active":
|
|
||||||
return True
|
return state == "active"
|
||||||
|
|
||||||
|
retry(check_active)
|
||||||
|
|
||||||
def get_unit_info(self, unit: str, user: Optional[str] = None) -> Dict[str, str]:
|
def get_unit_info(self, unit: str, user: Optional[str] = None) -> Dict[str, str]:
|
||||||
status, lines = self.systemctl('--no-pager show "{}"'.format(unit), user)
|
status, lines = self.systemctl('--no-pager show "{}"'.format(unit), user)
|
||||||
|
@ -421,18 +428,34 @@ class Machine:
|
||||||
)
|
)
|
||||||
|
|
||||||
def wait_until_succeeds(self, command: str) -> str:
|
def wait_until_succeeds(self, command: str) -> str:
|
||||||
|
"""Wait until a command returns success and return its output.
|
||||||
|
Throws an exception on timeout.
|
||||||
|
"""
|
||||||
|
output = ""
|
||||||
|
|
||||||
|
def check_success(_: Any) -> bool:
|
||||||
|
nonlocal output
|
||||||
|
status, output = self.execute(command)
|
||||||
|
return status == 0
|
||||||
|
|
||||||
with self.nested("waiting for success: {}".format(command)):
|
with self.nested("waiting for success: {}".format(command)):
|
||||||
while True:
|
retry(check_success)
|
||||||
status, output = self.execute(command)
|
return output
|
||||||
if status == 0:
|
|
||||||
return output
|
|
||||||
|
|
||||||
def wait_until_fails(self, command: str) -> str:
|
def wait_until_fails(self, command: str) -> str:
|
||||||
|
"""Wait until a command returns failure.
|
||||||
|
Throws an exception on timeout.
|
||||||
|
"""
|
||||||
|
output = ""
|
||||||
|
|
||||||
|
def check_failure(_: Any) -> bool:
|
||||||
|
nonlocal output
|
||||||
|
status, output = self.execute(command)
|
||||||
|
return status != 0
|
||||||
|
|
||||||
with self.nested("waiting for failure: {}".format(command)):
|
with self.nested("waiting for failure: {}".format(command)):
|
||||||
while True:
|
retry(check_failure)
|
||||||
status, output = self.execute(command)
|
return output
|
||||||
if status != 0:
|
|
||||||
return output
|
|
||||||
|
|
||||||
def wait_for_shutdown(self) -> None:
|
def wait_for_shutdown(self) -> None:
|
||||||
if not self.booted:
|
if not self.booted:
|
||||||
|
@ -453,25 +476,38 @@ class Machine:
|
||||||
)
|
)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def wait_until_tty_matches(self, tty: str, regexp: str) -> bool:
|
def wait_until_tty_matches(self, tty: str, regexp: str) -> None:
|
||||||
|
"""Wait until the visible output on the chosen TTY matches regular
|
||||||
|
expression. Throws an exception on timeout.
|
||||||
|
"""
|
||||||
matcher = re.compile(regexp)
|
matcher = re.compile(regexp)
|
||||||
|
|
||||||
|
def tty_matches(last: bool) -> bool:
|
||||||
|
text = self.get_tty_text(tty)
|
||||||
|
if last:
|
||||||
|
self.log(
|
||||||
|
f"Last chance to match /{regexp}/ on TTY{tty}, "
|
||||||
|
f"which currently contains: {text}"
|
||||||
|
)
|
||||||
|
return len(matcher.findall(text)) > 0
|
||||||
|
|
||||||
with self.nested("waiting for {} to appear on tty {}".format(regexp, tty)):
|
with self.nested("waiting for {} to appear on tty {}".format(regexp, tty)):
|
||||||
while True:
|
retry(tty_matches)
|
||||||
text = self.get_tty_text(tty)
|
|
||||||
if len(matcher.findall(text)) > 0:
|
|
||||||
return True
|
|
||||||
|
|
||||||
def send_chars(self, chars: List[str]) -> None:
|
def send_chars(self, chars: List[str]) -> None:
|
||||||
with self.nested("sending keys ‘{}‘".format(chars)):
|
with self.nested("sending keys ‘{}‘".format(chars)):
|
||||||
for char in chars:
|
for char in chars:
|
||||||
self.send_key(char)
|
self.send_key(char)
|
||||||
|
|
||||||
def wait_for_file(self, filename: str) -> bool:
|
def wait_for_file(self, filename: str) -> None:
|
||||||
|
"""Waits until the file exists in machine's file system."""
|
||||||
|
|
||||||
|
def check_file(_: Any) -> bool:
|
||||||
|
status, _ = self.execute("test -e {}".format(filename))
|
||||||
|
return status == 0
|
||||||
|
|
||||||
with self.nested("waiting for file ‘{}‘".format(filename)):
|
with self.nested("waiting for file ‘{}‘".format(filename)):
|
||||||
while True:
|
retry(check_file)
|
||||||
status, _ = self.execute("test -e {}".format(filename))
|
|
||||||
if status == 0:
|
|
||||||
return True
|
|
||||||
|
|
||||||
def wait_for_open_port(self, port: int) -> None:
|
def wait_for_open_port(self, port: int) -> None:
|
||||||
def port_is_open(_: Any) -> bool:
|
def port_is_open(_: Any) -> bool:
|
||||||
|
@ -494,8 +530,8 @@ class Machine:
|
||||||
def stop_job(self, jobname: str, user: Optional[str] = None) -> Tuple[int, str]:
|
def stop_job(self, jobname: str, user: Optional[str] = None) -> Tuple[int, str]:
|
||||||
return self.systemctl("stop {}".format(jobname), user)
|
return self.systemctl("stop {}".format(jobname), user)
|
||||||
|
|
||||||
def wait_for_job(self, jobname: str) -> bool:
|
def wait_for_job(self, jobname: str) -> None:
|
||||||
return self.wait_for_unit(jobname)
|
self.wait_for_unit(jobname)
|
||||||
|
|
||||||
def connect(self) -> None:
|
def connect(self) -> None:
|
||||||
if self.connected:
|
if self.connected:
|
||||||
|
@ -700,18 +736,20 @@ class Machine:
|
||||||
"""Wait until it is possible to connect to the X server. Note that
|
"""Wait until it is possible to connect to the X server. Note that
|
||||||
testing the existence of /tmp/.X11-unix/X0 is insufficient.
|
testing the existence of /tmp/.X11-unix/X0 is insufficient.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def check_x(_: Any) -> bool:
|
||||||
|
cmd = (
|
||||||
|
"journalctl -b SYSLOG_IDENTIFIER=systemd | "
|
||||||
|
+ 'grep "Reached target Current graphical"'
|
||||||
|
)
|
||||||
|
status, _ = self.execute(cmd)
|
||||||
|
if status != 0:
|
||||||
|
return False
|
||||||
|
status, _ = self.execute("[ -e /tmp/.X11-unix/X0 ]")
|
||||||
|
return status == 0
|
||||||
|
|
||||||
with self.nested("waiting for the X11 server"):
|
with self.nested("waiting for the X11 server"):
|
||||||
while True:
|
retry(check_x)
|
||||||
cmd = (
|
|
||||||
"journalctl -b SYSLOG_IDENTIFIER=systemd | "
|
|
||||||
+ 'grep "Reached target Current graphical"'
|
|
||||||
)
|
|
||||||
status, _ = self.execute(cmd)
|
|
||||||
if status != 0:
|
|
||||||
continue
|
|
||||||
status, _ = self.execute("[ -e /tmp/.X11-unix/X0 ]")
|
|
||||||
if status == 0:
|
|
||||||
return
|
|
||||||
|
|
||||||
def get_window_names(self) -> List[str]:
|
def get_window_names(self) -> List[str]:
|
||||||
return self.succeed(
|
return self.succeed(
|
||||||
|
|
Loading…
Reference in New Issue