From db5528d1e2bd923501229ce941e0d70315b4b743 Mon Sep 17 00:00:00 2001 From: mulhern Date: Mon, 9 Oct 2023 13:46:55 -0400 Subject: [PATCH 1/2] Make blockdev calls lazy This way, the calls only occur if the result will be used. Signed-off-by: mulhern --- test_harness.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/test_harness.py b/test_harness.py index 8532ba7..6ee0dc1 100644 --- a/test_harness.py +++ b/test_harness.py @@ -27,6 +27,20 @@ _SIZE_OF_DEVICE = 1024**4 # 1 TiB +class _LogBlockdev: # pylint: disable=too-few-public-methods + """ + Allows only running blockdev commands if the result will be logged. + """ + + def __init__(self, option, device): + self.cmd = ["blockdev", option, device] + + def __str__(self): + with subprocess.Popen(self.cmd, stdout=subprocess.PIPE) as proc: + output = proc.stdout.readline().strip().decode("utf-8") + return f"output of {self.cmd}: {output}" + + def _make_loopbacked_devices(num): """ Make the requisite number of loopbacked devices. @@ -52,6 +66,9 @@ def _make_loopbacked_devices(num): devices.append(device) + for option in ["--getss", "--getpbsz", "--getiomin", "--getioopt"]: + logging.debug("%s", _LogBlockdev(option, device)) + return devices @@ -63,12 +80,6 @@ def _run_command(num_devices, command): :param list command: the command to be run """ devices = _make_loopbacked_devices(num_devices) - for device in devices: - for option in ["--getss", "--getpbsz", "--getiomin", "--getioopt"]: - diagcmd = ["blockdev", option, device] - with subprocess.Popen(diagcmd, stdout=subprocess.PIPE) as proc: - blockdevoutput = proc.stdout.readline().strip().decode("utf-8") - logging.debug("output of %s: %s", diagcmd, blockdevoutput) command = command + list(itertools.chain(*[["--disk", dev] for dev in devices])) subprocess.run(command, check=True) From 3b1799ff469f8cc2bf406f6a6c0dd26ee4cb30aa Mon Sep 17 00:00:00 2001 From: mulhern Date: Tue, 10 Oct 2023 09:56:22 -0400 Subject: [PATCH 2/2] Make it impossible for blockdev calls to cause exception Signed-off-by: mulhern --- test_harness.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test_harness.py b/test_harness.py index 6ee0dc1..e56036a 100644 --- a/test_harness.py +++ b/test_harness.py @@ -36,8 +36,12 @@ def __init__(self, option, device): self.cmd = ["blockdev", option, device] def __str__(self): - with subprocess.Popen(self.cmd, stdout=subprocess.PIPE) as proc: - output = proc.stdout.readline().strip().decode("utf-8") + try: + with subprocess.Popen(self.cmd, stdout=subprocess.PIPE) as proc: + output = proc.stdout.readline().strip().decode("utf-8") + except: # pylint: disable=bare-except + return f"could not gather output of {self.cmd}" + return f"output of {self.cmd}: {output}"