mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: rcz@pengutronix.de, Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/5] test: add support for passing devices on command line
Date: Mon, 19 Jun 2023 11:52:39 +0200	[thread overview]
Message-ID: <20230619095240.4168216-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230619095240.4168216-1-a.fatoum@pengutronix.de>

The test/emulate.pl script support a number of options to create
RNG, block and console devices for use inside Qemu. In preparation for
phasing out the perl script, add these as pytest options. These
currently fix up options into the QEMUDriver extra_args key, but the
intention is to move the logic into upstream QEMUDriver over time.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 test/conftest.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++
 test/strategy.py |  6 +++++
 2 files changed, 76 insertions(+)

diff --git a/test/conftest.py b/test/conftest.py
index 794574799ff7..2bca3c37e5fb 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -33,3 +33,73 @@ def pytest_addoption(parser):
     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'))
+    parser.addoption('--rng', action='count', dest='qemu_rng',
+        help=('instantiate Virt I/O random number generator'))
+    parser.addoption('--console', action='count', dest='qemu_console', default=0,
+        help=('Pass an extra console (Virt I/O or ns16550_pci) to emulated barebox'))
+    parser.addoption('--blk', action='append', dest='qemu_block',
+        default=[], metavar="FILE",
+        help=('Pass block device to emulated barebox. Can be specified more than once'))
+    parser.addoption('--qemu', action='append', dest='qemu_arg',
+        default=[], metavar="option",
+        help=('Pass option to QEMU as is'))
+
+@pytest.fixture(scope="session")
+def strategy(request, target, pytestconfig):
+    try:
+        strategy = target.get_driver("Strategy")
+    except NoDriverFoundError as e:
+        pytest.exit(e)
+
+    try:
+        features = target.env.config.data["targets"]["main"]["features"]
+    except KeyError:
+        features = []
+
+    virtio = None
+
+    if "virtio-mmio" in features:
+        virtio = "device"
+    if "virtio-pci" in features:
+        virtio = "pci,disable-legacy=on,disable-modern=off"
+        features.append("pci")
+
+    if virtio and pytestconfig.option.qemu_rng:
+        for i in range(pytestconfig.option.qemu_rng):
+            strategy.append_qemu_args("-device", f"virtio-rng-{virtio}")
+
+    for i in range(pytestconfig.option.qemu_console):
+        if virtio and i == 0:
+            strategy.append_qemu_args(
+                "-device", f"virtio-serial-{virtio}",
+                "-chardev", f"pty,id=virtcon{i}",
+                "-device", f"virtconsole,chardev=virtcon{i},name=console.virtcon{i}"
+            )
+            continue
+
+        # ns16550 serial driver only works with x86 so far
+        if 'pci' in features:
+            strategy.append_qemu_args(
+                "-chardev", f"pty,id=pcicon{i}",
+                "-device", f"pci-serial,chardev=pcicon{i}"
+            )
+        else:
+            pytest.exit("barebox currently supports only a single extra virtio console\n", 1)
+
+    for i, blk in enumerate(pytestconfig.option.qemu_block):
+        if virtio:
+            strategy.append_qemu_args(
+                "-drive", f"if=none,file={blk},format=raw,id=hd{i}",
+                "-device", f"virtio-blk-{virtio},drive=hd{i}"
+            )
+        else:
+            pytest.exit("--blk unsupported for target\n", 1)
+
+    for arg in pytestconfig.option.qemu_arg:
+        strategy.append_qemu_args(arg)
+
+    state = request.config.option.lg_initial_state
+    if state is not None:
+        strategy.force(state)
+
+    return strategy
diff --git a/test/strategy.py b/test/strategy.py
index 65cdae4fbf04..8aa58151f6b8 100644
--- a/test/strategy.py
+++ b/test/strategy.py
@@ -122,6 +122,12 @@ class BareboxTestStrategy(Strategy):
         self.target.env.config.data["tools"][self.qemu.qemu_bin] = \
                 shutil.which(self.qemu.qemu_bin)
 
+    def append_qemu_args(self, *args):
+        if self.qemu is None:
+            pytest.exit('Qemu option supplied for non-Qemu target')
+        for arg in args:
+            self.console.extra_args += " " + arg
+
 def quote_cmd(cmd):
     quoted = map(lambda s : s if s.find(" ") == -1 else "'" + s + "'", cmd)
     return " ".join(quoted)
-- 
2.39.2




  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 ` [PATCH 2/5] test: have pytest --interactive start interactive Qemu session Ahmad Fatoum
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 ` Ahmad Fatoum [this message]
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-5-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