···
def send_monitor_command(self, command: str) -> str:
-
with self.nested("sending monitor command: {}".format(command)):
-
message = ("{}\n".format(command)).encode()
assert self.monitor is not None
self.monitor.send(message)
return self.wait_for_monitor_prompt()
···
info = self.get_unit_info(unit, user)
state = info["ActiveState"]
-
raise Exception('unit "{}" reached state "{}"'.format(unit, state))
status, jobs = self.systemctl("list-jobs --full 2>&1", user)
···
info = self.get_unit_info(unit, user)
if info["ActiveState"] == state:
-
'unit "{}" is inactive and there ' "are no pending jobs"
-
"waiting for unit {}{}".format(
-
unit, f" with user {user}" if user is not None else ""
retry(check_active, timeout)
def get_unit_info(self, unit: str, user: Optional[str] = None) -> Dict[str, str]:
-
status, lines = self.systemctl('--no-pager show "{}"'.format(unit), user)
-
'retrieving systemctl info for unit "{}" {} failed with exit code {}'.format(
-
unit, "" if user is None else 'under user "{}"'.format(user), status
line_pattern = re.compile(r"^([^=]+)=(.*)$")
···
q = q.replace("'", "\\'")
-
"su -l {} --shell /bin/sh -c "
-
"$'XDG_RUNTIME_DIR=/run/user/`id -u` "
-
return self.execute("systemctl {}".format(q))
def require_unit_state(self, unit: str, require_state: str = "active") -> None:
-
"checking if unit ‘{}’ has reached state '{}'".format(unit, require_state)
info = self.get_unit_info(unit)
state = info["ActiveState"]
if state != require_state:
-
"Expected unit ‘{}’ to to be in state ".format(unit)
-
+ "'{}' but it is in state ‘{}’".format(require_state, state)
def _next_newline_closed_block_from_shell(self) -> str:
···
"""Execute each command and check that it succeeds."""
-
with self.nested("must succeed: {}".format(command)):
(status, out) = self.execute(command, timeout=timeout)
-
self.log("output: {}".format(out))
-
"command `{}` failed (exit code {})".format(command, status)
···
"""Execute each command and check that it fails."""
-
with self.nested("must fail: {}".format(command)):
(status, out) = self.execute(command, timeout=timeout)
-
"command `{}` unexpectedly succeeded".format(command)
···
status, output = self.execute(command, timeout=timeout)
-
with self.nested("waiting for success: {}".format(command)):
retry(check_success, timeout)
···
status, output = self.execute(command, timeout=timeout)
-
with self.nested("waiting for failure: {}".format(command)):
···
def get_tty_text(self, tty: str) -> str:
status, output = self.execute(
-
"fold -w$(stty -F /dev/tty{0} size | "
-
"awk '{{print $2}}') /dev/vcs{0}".format(tty)
···
return len(matcher.findall(text)) > 0
-
with self.nested("waiting for {} to appear on tty {}".format(regexp, tty)):
def send_chars(self, chars: str, delay: Optional[float] = 0.01) -> None:
-
with self.nested("sending keys ‘{}‘".format(chars)):
self.send_key(char, delay)
···
"""Waits until the file exists in machine's file system."""
def check_file(_: Any) -> bool:
-
status, _ = self.execute("test -e {}".format(filename))
-
with self.nested("waiting for file ‘{}‘".format(filename)):
def wait_for_open_port(self, port: int, addr: str = "localhost") -> None:
def port_is_open(_: Any) -> bool:
-
status, _ = self.execute("nc -z {} {}".format(addr, port))
-
with self.nested("waiting for TCP port {} on {}".format(port, addr)):
def wait_for_closed_port(self, port: int, addr: str = "localhost") -> None:
def port_is_closed(_: Any) -> bool:
-
status, _ = self.execute("nc -z {} {}".format(addr, port))
-
"waiting for TCP port {} on {} to be closed".format(port, addr)
def start_job(self, jobname: str, user: Optional[str] = None) -> Tuple[int, str]:
-
return self.systemctl("start {}".format(jobname), user)
def stop_job(self, jobname: str, user: Optional[str] = None) -> Tuple[int, str]:
-
return self.systemctl("stop {}".format(jobname), user)
def wait_for_job(self, jobname: str) -> None:
self.wait_for_unit(jobname)
···
self.log("connected to guest root shell")
-
self.log("(connecting took {:.2f} seconds)".format(toc - tic))
def screenshot(self, filename: str) -> None:
word_pattern = re.compile(r"^\w+$")
if word_pattern.match(filename):
-
filename = os.path.join(self.out_dir, "{}.png".format(filename))
-
tmp = "{}.ppm".format(filename)
-
"making screenshot {}".format(filename),
{"image": os.path.basename(filename)},
-
self.send_monitor_command("screendump {}".format(tmp))
-
ret = subprocess.run("pnmtopng {} > {}".format(tmp, filename), shell=True)
raise Exception("Cannot convert screenshot")
···
def dump_tty_contents(self, tty: str) -> None:
"""Debugging: Dump the contents of the TTY<n>"""
-
self.execute("fold -w 80 /dev/vcs{} | systemd-cat".format(tty))
def _get_screen_text_variants(self, model_ids: Iterable[int]) -> List[str]:
with tempfile.TemporaryDirectory() as tmpdir:
···
-
self.log("Last OCR attempt failed. Text was: {}".format(variants))
-
with self.nested("waiting for {} to appear on screen".format(regex)):
def wait_for_console_text(self, regex: str) -> None:
-
with self.nested("waiting for {} to appear on console".format(regex)):
# Buffer the console output, this is needed
# to match multiline regexes.
···
def send_key(self, key: str, delay: Optional[float] = 0.01) -> None:
key = CHAR_TO_KEY.get(key, key)
-
self.send_monitor_command("sendkey {}".format(key))
···
self.pid = self.process.pid
-
self.log("QEMU running (pid {})".format(self.pid))
def cleanup_statedir(self) -> None:
shutil.rmtree(self.state_dir)
···
names = self.get_window_names()
-
"Last chance to match {} on the window list,".format(regexp)
+ " which currently contains: "
···
"""Forward a TCP port on the host to a TCP port on the guest.
Useful during interactive testing.
-
self.send_monitor_command(
-
"hostfwd_add tcp::{}-:{}".format(host_port, guest_port)
"""Make the machine unreachable by shutting down eth1 (the multicast