]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Refactor command running in integration tests
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 17 Jun 2022 14:13:49 +0000 (10:13 -0400)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 17 Jun 2022 18:25:35 +0000 (14:25 -0400)
Let's move run_command_image() into Machine.run(), introduce
run_systemd_cmdline() to get the systemd-run command line, and
remove all arguments from run_ssh() that aren't required anymore
now.

mkosi/__init__.py
mkosi/machine.py

index 071e2daee9b884db84cfa50dd887926788950865..fd5caf794d60ccc6a726dd1fc34cc2e3522c6ed3 100644 (file)
@@ -66,7 +66,6 @@ from typing import (
 )
 
 from .backend import (
-    _FILE,
     ARG_DEBUG,
     Distribution,
     ManifestFormat,
@@ -8059,14 +8058,8 @@ def find_address(args: MkosiArgs) -> Tuple[str, str]:
     die("Container/VM address not found")
 
 
-def run_command_image(args: MkosiArgs, commands: Sequence[str], timeout: int, check: bool, stdout: _FILE = sys.stdout, stderr: _FILE = sys.stderr) -> CompletedProcess:
-    if args.verb == Verb.qemu:
-        return run_ssh(args, commands, check, stdout, stderr, timeout)
-    elif args.verb == Verb.boot:
-        cmdline = ["systemd-run", "--quiet", "--wait", "--pipe", "-M", machine_name(args), "/usr/bin/env", *commands]
-        return run(cmdline, check=check, stdout=stdout, stderr=stderr, text=True, timeout=timeout)
-    else:
-        return run(run_shell_cmdline(args, pipe=True, commands=commands), check=check, stdout=stdout, stderr=stderr, text=True, timeout=timeout)
+def run_systemd_cmdline(args: MkosiArgs, commands: Sequence[str]) -> List[str]:
+    return ["systemd-run", "--quiet", "--wait", "--pipe", "-M", machine_name(args), "/usr/bin/env", *commands]
 
 
 def run_ssh_cmdline(args: MkosiArgs, commands: Optional[Sequence[str]] = None) -> Sequence[str]:
@@ -8102,15 +8095,8 @@ def run_ssh_cmdline(args: MkosiArgs, commands: Optional[Sequence[str]] = None) -
     return cmd
 
 
-def run_ssh(
-    args: MkosiArgs,
-    commands: Optional[Sequence[str]] = None,
-    check: bool = True,
-    stdout: _FILE = sys.stdout,
-    stderr: _FILE = sys.stderr,
-    timeout: Optional[int] = None,
-) -> CompletedProcess:
-    return run(run_ssh_cmdline(args, commands), check=check, stdout=stdout, stderr=stderr, text=True, timeout=timeout)
+def run_ssh(args: MkosiArgs) -> CompletedProcess:
+    return run(run_ssh_cmdline(args), stdout=sys.stdout, stderr=sys.stderr)
 
 
 def run_serve(args: MkosiArgs) -> None:
index 6a6a810e5fd21266e3f04867b005d7795a2186d3..956cc03d3f23726c40375ce3156b9cf76fb760ea 100644 (file)
@@ -26,12 +26,13 @@ from . import (
     parse_args,
     parse_boolean,
     prepend_to_environ_path,
-    run_command_image,
     run_qemu_cmdline,
     run_shell_cmdline,
+    run_ssh_cmdline,
+    run_systemd_cmdline,
     unlink_output,
 )
-from .backend import MkosiArgs, MkosiNotSupportedException, Verb, die
+from .backend import MkosiArgs, MkosiNotSupportedException, Verb, die, run
 
 
 class LogfileAdapter:
@@ -162,7 +163,14 @@ class Machine:
         stdout: Union[int, TextIO] = subprocess.PIPE if capture_output else sys.stdout
         stderr: Union[int, TextIO] = subprocess.PIPE if capture_output else sys.stderr
 
-        return run_command_image(self.args, commands, timeout, check, stdout, stderr)
+        if self.args.verb == Verb.qemu:
+            cmdline = run_ssh_cmdline(self.args, commands)
+        elif self.args.verb == Verb.boot:
+            cmdline = run_systemd_cmdline(self.args, commands)
+        else:
+            cmdline = run_shell_cmdline(self.args, pipe=True, commands=commands)
+
+        return run(cmdline, check=check, stdout=stdout, stderr=stderr, text=True, timeout=timeout)
 
     def kill(self) -> None:
         self.__exit__(None, None, None)