From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: rcz@pengutronix.de, Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/5] test: have pytest --interactive start interactive Qemu session
Date: Mon, 19 Jun 2023 11:52:37 +0200 [thread overview]
Message-ID: <20230619095240.4168216-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230619095240.4168216-1-a.fatoum@pengutronix.de>
test/emulate.pl duplicates a lot of what labgrid is already doing.
In preparation for phasing out emulate.pl, let's teach pytest to
replace it. As a first step we add these two new pytest options:
--interactive start Qemu interactively using labgrid environment
--dry-run print Qemu command line that would've been used
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
test/conftest.py | 8 ++++++
test/strategy.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 76 insertions(+), 1 deletion(-)
diff --git a/test/conftest.py b/test/conftest.py
index 1a043a91fa78..794574799ff7 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -25,3 +25,11 @@ def pytest_configure(config):
if os.environ['LG_BUILDDIR'] is not None:
os.environ['LG_BUILDDIR'] = os.path.realpath(os.environ['LG_BUILDDIR'])
+
+def pytest_addoption(parser):
+ parser.addoption('--interactive', action='store_const', const='qemu_interactive',
+ dest='lg_initial_state',
+ help=('(for debugging) skip tests and just start Qemu interactively'))
+ parser.addoption('--dry-run', action='store_const', const='qemu_dry_run',
+ dest='lg_initial_state',
+ help=('(for debugging) skip tests and just print Qemu command line'))
diff --git a/test/strategy.py b/test/strategy.py
index 1fe1b7d818e9..81650a7600bd 100644
--- a/test/strategy.py
+++ b/test/strategy.py
@@ -1,14 +1,20 @@
import enum
import attr
+import pytest
+import subprocess
+import os
+import sys
-from labgrid import target_factory, step
+from labgrid import target_factory, step, driver
from labgrid.strategy import Strategy, StrategyError
class Status(enum.Enum):
unknown = 0
off = 1
barebox = 2
+ qemu_dry_run = 3
+ qemu_interactive = 4
@target_factory.reg_driver
@attr.s(eq=False)
@@ -21,9 +27,12 @@ class BareboxTestStrategy(Strategy):
}
status = attr.ib(default=Status.unknown)
+ qemu = None
def __attrs_post_init__(self):
super().__attrs_post_init__()
+ if isinstance(self.console, driver.QEMUDriver):
+ self.qemu = self.console
@step(args=['status'])
def transition(self, status, *, step):
@@ -51,3 +60,61 @@ class BareboxTestStrategy(Strategy):
format(self.status, status)
)
self.status = status
+
+ def force(self, state):
+ self.transition(Status.off) # pylint: disable=missing-kwoa
+
+ if state == "qemu_dry_run" or state == "qemu_interactive":
+ cmd = self.get_qemu_base_args()
+
+ cmd.append("-serial")
+ cmd.append("mon:stdio")
+ cmd.append("-trace")
+ cmd.append("file=/dev/null")
+
+ with open("/dev/tty", "r+b", buffering=0) as tty:
+ tty.write(bytes("running: \n{}\n".format(quote_cmd(cmd)), "UTF-8"))
+ if state == "qemu_dry_run":
+ pytest.exit('Dry run. Terminating.')
+ subprocess.run(cmd, stdin=tty, stdout=tty, stderr=tty)
+
+ pytest.exit('Interactive session terminated')
+ else:
+ pytest.exit('Can only force to: qemu_dry_run, qemu_interactive')
+
+ def get_qemu_base_args(self):
+ if self.qemu is None:
+ pytest.exit('interactive mode only supported with QEMUDriver')
+
+ try:
+ # https://github.com/labgrid-project/labgrid/pull/1212
+ cmd = self.qemu.get_qemu_base_args()
+ except AttributeError:
+ self.qemu.on_activate()
+ orig = self.qemu._cmd
+ cmd = []
+
+ list_iter = enumerate(orig)
+ for i, opt in list_iter:
+ if opt == "-S":
+ continue
+ opt2 = double_opt(opt, orig, i)
+ if (opt2.startswith("-chardev socket,id=serialsocket") or
+ opt2 == "-serial chardev:serialsocket" or
+ opt2 == "-qmp stdio"):
+ # skip over two elements at once
+ next(list_iter, None)
+ continue
+
+ cmd.append(opt)
+
+ return cmd
+
+def quote_cmd(cmd):
+ quoted = map(lambda s : s if s.find(" ") == -1 else "'" + s + "'", cmd)
+ return " ".join(quoted)
+
+def double_opt(opt, orig, i):
+ if opt == orig[-1]:
+ return opt
+ return " ".join([opt, orig[i + 1]])
--
2.39.2
next prev parent reply other threads:[~2023-06-19 9:54 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-19 9:52 [PATCH 0/5] test: teach pytest the emulate.pl tricks Ahmad Fatoum
2023-06-19 9:52 ` [PATCH 1/5] test: add pytest.ini with defaults Ahmad Fatoum
2023-06-19 9:52 ` Ahmad Fatoum [this message]
2023-06-19 9:52 ` [PATCH 3/5] test: lookup qemu binary in path if no tools key exists Ahmad Fatoum
2023-06-19 9:52 ` [PATCH 4/5] test: add support for passing devices on command line Ahmad Fatoum
2023-06-19 9:52 ` [PATCH 5/5] test: don't hardcode origin of OVMF.fd Ahmad Fatoum
2023-06-26 9:55 ` [PATCH 0/5] test: teach pytest the emulate.pl tricks Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230619095240.4168216-3-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=rcz@pengutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox