From: gsegatti Date: Wed, 19 Oct 2022 02:56:18 +0000 (-0300) Subject: Adding custom retry amount to Machine.run() X-Git-Tag: v14~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7448c8275e71aa0f3628f1d046815a3da482ec36;p=thirdparty%2Fmkosi.git Adding custom retry amount to Machine.run() This PR aims to introduce a variable "retry_amount" passed via Machine's constructor. This variable will define how many times a command will attempt to run when using a VM. Default value remain as 30 if not explicitly set. --- diff --git a/mkosi/machine.py b/mkosi/machine.py index e5feeb1d6..51171d03c 100644 --- a/mkosi/machine.py +++ b/mkosi/machine.py @@ -48,9 +48,10 @@ class LogfileAdapter: class Machine: - def __init__(self, args: Sequence[str] = []) -> None: + def __init__(self, args: Sequence[str] = [], *, num_retries: int = 30) -> None: # Remains None until image is built and booted, then receives pexpect process. self._serial: Optional[pexpect.spawn] = None + self.num_retries = num_retries self.exit_code: int = -1 self.stack = contextlib.ExitStack() self.config: MkosiConfig @@ -174,7 +175,7 @@ class Machine: # The retry logic only applies when running commands against a VM. - for _ in range(0, 30): + for _ in range(0, self.num_retries): try: return run(cmdline, check=check, stdout=stdout, stderr=stderr, text=True, timeout=timeout) except subprocess.CalledProcessError as e: @@ -209,15 +210,17 @@ def skip_not_supported() -> Iterator[None]: class MkosiMachineTest(unittest.TestCase): args: Sequence[str] + num_retries: int machine: Machine - def __init_subclass__(cls, args: Sequence[str] = []) -> None: + def __init_subclass__(cls, args: Sequence[str] = [], *, num_retries: int = 30) -> None: cls.args = args + cls.num_retries = num_retries @classmethod def setUpClass(cls) -> None: with skip_not_supported(): - cls.machine = Machine(cls.args) + cls.machine = Machine(cls.args, num_retries = cls.num_retries) verb = cls.machine.config.verb no_nspawn = parse_boolean(os.getenv("MKOSI_TEST_NO_NSPAWN", "0"))