mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/5] test: teach pytest the emulate.pl tricks
@ 2023-06-19  9:52 Ahmad Fatoum
  2023-06-19  9:52 ` [PATCH 1/5] test: add pytest.ini with defaults Ahmad Fatoum
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2023-06-19  9:52 UTC (permalink / raw)
  To: barebox; +Cc: rcz

test/emulate.pl duplicates a lot of what labgrid is already doing
in order to instantiate virtualized devices and to allow interactive
use of emulated barebox with the same environment config.

In preparation for phasing out emulate.pl, let's teach pytest how to
replace it. As a first step we add these new pytest options:

  --interactive	start Qemu interactively using labgrid environment
  --dry-run     print Qemu command line that would've been used
  --rng         instaiate RNG device
  --console     instaiate console device
  --blk         instaiate block device
  --qemu=OPT    pass OPT to qemu

The intention is to ultimately move as much as possible of this handling
into labgrid itself, e.g. labgrid already has provisions for fixing up
disk options.

Ahmad Fatoum (5):
  test: add pytest.ini with defaults
  test: have pytest --interactive start interactive Qemu session
  test: lookup qemu binary in path if no tools key exists
  test: add support for passing devices on command line
  test: don't hardcode origin of OVMF.fd

 .github/workflows/test-labgrid-pytest.yml     |  4 -
 pytest.ini                                    |  2 +
 test/arm/a15@multi_v7_defconfig.yaml          |  4 +-
 test/arm/a9@multi_v7_defconfig.yaml           |  4 +-
 test/arm/multi_v8_defconfig.yaml              |  4 +-
 test/arm/qemu-raspi0@multi_v7_defconfig.yaml  |  4 +-
 .../arm/qemu-raspi1ap@multi_v7_defconfig.yaml |  4 +-
 test/arm/qemu-raspi2b@multi_v7_defconfig.yaml |  4 +-
 .../qemu-sabrelite@multi_v7_defconfig.yaml    |  4 +-
 test/arm/virt@multi_v7_defconfig.yaml         |  4 +-
 test/conftest.py                              | 78 +++++++++++++++++
 test/mips/be@qemu-malta_defconfig.yaml        |  4 +-
 test/mips/le@qemu-malta_defconfig.yaml        |  4 +-
 test/mips/qemu-malta64el_defconfig.yaml       |  4 +-
 test/openrisc/generic_defconfig.yaml          |  4 +-
 test/riscv/qemu-virt64@rv64i_defconfig.yaml   |  4 +-
 test/riscv/qemu@virt32_defconfig.yaml         |  4 +-
 test/riscv/sifive@rv64i_defconfig.yaml        |  4 +-
 test/strategy.py                              | 87 ++++++++++++++++++-
 test/x86/pc@efi_defconfig.yaml                | 10 +--
 test/x86/q35@efi_defconfig.yaml               | 10 +--
 test/x86/virtio@efi_defconfig.yaml            | 10 +--
 22 files changed, 187 insertions(+), 74 deletions(-)
 create mode 100644 pytest.ini

-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/5] test: add pytest.ini with defaults
  2023-06-19  9:52 [PATCH 0/5] test: teach pytest the emulate.pl tricks Ahmad Fatoum
@ 2023-06-19  9:52 ` Ahmad Fatoum
  2023-06-19  9:52 ` [PATCH 2/5] test: have pytest --interactive start interactive Qemu session Ahmad Fatoum
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2023-06-19  9:52 UTC (permalink / raw)
  To: barebox; +Cc: rcz, Ahmad Fatoum

This allows running tests with just

  pytest --lg-env test/arm/multi_v8_defconfig.yaml

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 pytest.ini | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 pytest.ini

diff --git a/pytest.ini b/pytest.ini
new file mode 100644
index 000000000000..4aa8bbfbd628
--- /dev/null
+++ b/pytest.ini
@@ -0,0 +1,2 @@
+[pytest]
+testpaths = test/py
-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/5] test: have pytest --interactive start interactive Qemu session
  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
  2023-06-19  9:52 ` [PATCH 3/5] test: lookup qemu binary in path if no tools key exists Ahmad Fatoum
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2023-06-19  9:52 UTC (permalink / raw)
  To: barebox; +Cc: rcz, Ahmad Fatoum

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




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 3/5] test: lookup qemu binary in path if no tools key exists
  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 ` Ahmad Fatoum
  2023-06-19  9:52 ` [PATCH 4/5] test: add support for passing devices on command line Ahmad Fatoum
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2023-06-19  9:52 UTC (permalink / raw)
  To: barebox; +Cc: rcz, Ahmad Fatoum

This is already the behavior on labgrid master, but until there's a
release using that, we amend the BareboxTestStrategy to achieve the same
result with existing labgrid releases.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 test/arm/a15@multi_v7_defconfig.yaml            |  4 +---
 test/arm/a9@multi_v7_defconfig.yaml             |  4 +---
 test/arm/multi_v8_defconfig.yaml                |  4 +---
 test/arm/qemu-raspi0@multi_v7_defconfig.yaml    |  4 +---
 test/arm/qemu-raspi1ap@multi_v7_defconfig.yaml  |  4 +---
 test/arm/qemu-raspi2b@multi_v7_defconfig.yaml   |  4 +---
 test/arm/qemu-sabrelite@multi_v7_defconfig.yaml |  4 +---
 test/arm/virt@multi_v7_defconfig.yaml           |  4 +---
 test/mips/be@qemu-malta_defconfig.yaml          |  4 +---
 test/mips/le@qemu-malta_defconfig.yaml          |  4 +---
 test/mips/qemu-malta64el_defconfig.yaml         |  4 +---
 test/openrisc/generic_defconfig.yaml            |  4 +---
 test/riscv/qemu-virt64@rv64i_defconfig.yaml     |  4 +---
 test/riscv/qemu@virt32_defconfig.yaml           |  4 +---
 test/riscv/sifive@rv64i_defconfig.yaml          |  4 +---
 test/strategy.py                                | 12 ++++++++++++
 test/x86/pc@efi_defconfig.yaml                  |  4 +---
 test/x86/q35@efi_defconfig.yaml                 |  4 +---
 test/x86/virtio@efi_defconfig.yaml              |  4 +---
 19 files changed, 30 insertions(+), 54 deletions(-)

diff --git a/test/arm/a15@multi_v7_defconfig.yaml b/test/arm/a15@multi_v7_defconfig.yaml
index 941e914ab2d6..dfa73fb3f5fd 100644
--- a/test/arm/a15@multi_v7_defconfig.yaml
+++ b/test/arm/a15@multi_v7_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-arm
         machine: vexpress-a15
         cpu: cortex-a15
         memory: 1024M
@@ -14,7 +14,5 @@ targets:
       BareboxTestStrategy: {}
 images:
   barebox-vexpress-ca15.img: !template "$LG_BUILDDIR/images/barebox-vexpress-ca15.img"
-tools:
-  qemu: /usr/bin/qemu-system-arm
 imports:
   -  ../strategy.py
diff --git a/test/arm/a9@multi_v7_defconfig.yaml b/test/arm/a9@multi_v7_defconfig.yaml
index fefee153cf91..8e8220102c90 100644
--- a/test/arm/a9@multi_v7_defconfig.yaml
+++ b/test/arm/a9@multi_v7_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-arm
         machine: vexpress-a9
         cpu: cortex-a9
         memory: 1024M
@@ -14,7 +14,5 @@ targets:
       BareboxTestStrategy: {}
 images:
   barebox-vexpress-ca9.img: !template "$LG_BUILDDIR/images/barebox-vexpress-ca9.img"
-tools:
-  qemu: /usr/bin/qemu-system-arm
 imports:
   -  ../strategy.py
diff --git a/test/arm/multi_v8_defconfig.yaml b/test/arm/multi_v8_defconfig.yaml
index 5da5dfeb7e27..d8f8ab5cbff3 100644
--- a/test/arm/multi_v8_defconfig.yaml
+++ b/test/arm/multi_v8_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-aarch64
         machine: virt,highmem=off
         cpu: cortex-a57
         memory: 1024M
@@ -18,7 +18,5 @@ targets:
       tuxmake_arch: arm64
 images:
   barebox-dt-2nd.img: !template "$LG_BUILDDIR/images/barebox-dt-2nd.img"
-tools:
-  qemu: /usr/bin/qemu-system-aarch64
 imports:
   -  ../strategy.py
diff --git a/test/arm/qemu-raspi0@multi_v7_defconfig.yaml b/test/arm/qemu-raspi0@multi_v7_defconfig.yaml
index e6ba6410879f..0a9a727c641f 100644
--- a/test/arm/qemu-raspi0@multi_v7_defconfig.yaml
+++ b/test/arm/qemu-raspi0@multi_v7_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-arm
         machine: raspi0
         cpu: arm1176
         memory: 512M
@@ -14,7 +14,5 @@ targets:
       BareboxTestStrategy: {}
 images:
   barebox-raspberry-pi.img: !template "$LG_BUILDDIR/images/barebox-raspberry-pi.img"
-tools:
-  qemu: /usr/bin/qemu-system-arm
 imports:
   -  ../strategy.py
diff --git a/test/arm/qemu-raspi1ap@multi_v7_defconfig.yaml b/test/arm/qemu-raspi1ap@multi_v7_defconfig.yaml
index 1abd6721cb65..0950481a63ea 100644
--- a/test/arm/qemu-raspi1ap@multi_v7_defconfig.yaml
+++ b/test/arm/qemu-raspi1ap@multi_v7_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-arm
         machine: raspi1ap
         cpu: arm1176
         memory: 512M
@@ -14,7 +14,5 @@ targets:
       BareboxTestStrategy: {}
 images:
   barebox-raspberry-pi.img: !template "$LG_BUILDDIR/images/barebox-raspberry-pi.img"
-tools:
-  qemu: /usr/bin/qemu-system-arm
 imports:
   -  ../strategy.py
diff --git a/test/arm/qemu-raspi2b@multi_v7_defconfig.yaml b/test/arm/qemu-raspi2b@multi_v7_defconfig.yaml
index bd92343c80fc..72471c0a7f30 100644
--- a/test/arm/qemu-raspi2b@multi_v7_defconfig.yaml
+++ b/test/arm/qemu-raspi2b@multi_v7_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-arm
         machine: raspi2b
         cpu: cortex-a7
         memory: 1G
@@ -14,7 +14,5 @@ targets:
       BareboxTestStrategy: {}
 images:
   barebox-raspberry-pi.img: !template "$LG_BUILDDIR/images/barebox-raspberry-pi.img"
-tools:
-  qemu: /usr/bin/qemu-system-arm
 imports:
   -  ../strategy.py
diff --git a/test/arm/qemu-sabrelite@multi_v7_defconfig.yaml b/test/arm/qemu-sabrelite@multi_v7_defconfig.yaml
index 948618a90f1e..77b7f9e58913 100644
--- a/test/arm/qemu-sabrelite@multi_v7_defconfig.yaml
+++ b/test/arm/qemu-sabrelite@multi_v7_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-arm
         machine: sabrelite
         cpu: cortex-a9
         memory: 1024M
@@ -16,7 +16,5 @@ targets:
 images:
   barebox-dt-2nd.img: !template "$LG_BUILDDIR/images/barebox-dt-2nd.img"
   imx6q-sabreliste.dtb: !template "$LG_BUILDDIR/arch/arm/dts/imx6q-sabrelite.dtb"
-tools:
-  qemu: /usr/bin/qemu-system-arm
 imports:
   -  ../strategy.py
diff --git a/test/arm/virt@multi_v7_defconfig.yaml b/test/arm/virt@multi_v7_defconfig.yaml
index 66ecf20690d1..203f17bfc7f0 100644
--- a/test/arm/virt@multi_v7_defconfig.yaml
+++ b/test/arm/virt@multi_v7_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-arm
         machine: virt
         cpu: cortex-a7
         memory: 1024M
@@ -16,7 +16,5 @@ targets:
       - virtio-mmio
 images:
   barebox-dt-2nd.img: !template "$LG_BUILDDIR/images/barebox-dt-2nd.img"
-tools:
-  qemu: /usr/bin/qemu-system-arm
 imports:
   -  ../strategy.py
diff --git a/test/mips/be@qemu-malta_defconfig.yaml b/test/mips/be@qemu-malta_defconfig.yaml
index 774023cd8451..5f438e4b900a 100644
--- a/test/mips/be@qemu-malta_defconfig.yaml
+++ b/test/mips/be@qemu-malta_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-mips
         machine: malta
         cpu: M14Kc
         memory: 256M
@@ -16,7 +16,5 @@ targets:
       - virtio-pci
 images:
   barebox-qemu-malta.img: !template "$LG_BUILDDIR/images/barebox-qemu-malta.img"
-tools:
-  qemu: /usr/bin/qemu-system-mips
 imports:
   -  ../strategy.py
diff --git a/test/mips/le@qemu-malta_defconfig.yaml b/test/mips/le@qemu-malta_defconfig.yaml
index 8fc8c4dae925..ecf9484dcc1e 100644
--- a/test/mips/le@qemu-malta_defconfig.yaml
+++ b/test/mips/le@qemu-malta_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-mipsel
         machine: malta
         cpu: M14Kc
         memory: 256M
@@ -19,7 +19,5 @@ targets:
         - CONFIG_CPU_LITTLE_ENDIAN=y
 images:
   barebox-qemu-malta.img.swapped: !template "$LG_BUILDDIR/images/barebox-qemu-malta.img.swapped"
-tools:
-  qemu: /usr/bin/qemu-system-mipsel
 imports:
   -  ../strategy.py
diff --git a/test/mips/qemu-malta64el_defconfig.yaml b/test/mips/qemu-malta64el_defconfig.yaml
index 22562d5b7a6a..bf8563e6623b 100644
--- a/test/mips/qemu-malta64el_defconfig.yaml
+++ b/test/mips/qemu-malta64el_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-mips64el
         machine: malta
         cpu: 5KEf
         memory: 256M
@@ -16,7 +16,5 @@ targets:
       - virtio-pci
 images:
   barebox-qemu-malta.img.swapped: !template "$LG_BUILDDIR/images/barebox-qemu-malta.img.swapped"
-tools:
-  qemu: /usr/bin/qemu-system-mips64el
 imports:
   -  ../strategy.py
diff --git a/test/openrisc/generic_defconfig.yaml b/test/openrisc/generic_defconfig.yaml
index 0a2d4a7a18a0..93ba9586c439 100644
--- a/test/openrisc/generic_defconfig.yaml
+++ b/test/openrisc/generic_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-or1k
         machine: or1k-sim
         cpu: or1200
         memory: 256M
@@ -14,7 +14,5 @@ targets:
       BareboxTestStrategy: {}
 images:
   barebox: !template "$LG_BUILDDIR/barebox"
-tools:
-  qemu: /usr/bin/qemu-system-or1k
 imports:
   -  ../strategy.py
diff --git a/test/riscv/qemu-virt64@rv64i_defconfig.yaml b/test/riscv/qemu-virt64@rv64i_defconfig.yaml
index ee121f153619..c920413a17bf 100644
--- a/test/riscv/qemu-virt64@rv64i_defconfig.yaml
+++ b/test/riscv/qemu-virt64@rv64i_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-riscv64
         machine: virt
         cpu: rv64
         memory: 256M
@@ -16,7 +16,5 @@ targets:
       - virtio-mmio
 images:
   barebox-dt-2nd.img: !template "$LG_BUILDDIR/images/barebox-dt-2nd.img"
-tools:
-  qemu: /usr/bin/qemu-system-riscv64
 imports:
   -  ../strategy.py
diff --git a/test/riscv/qemu@virt32_defconfig.yaml b/test/riscv/qemu@virt32_defconfig.yaml
index 5c602635d4c5..da1bb5bddd3e 100644
--- a/test/riscv/qemu@virt32_defconfig.yaml
+++ b/test/riscv/qemu@virt32_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-riscv32
         machine: virt
         cpu: rv32
         memory: 256M
@@ -21,7 +21,5 @@ targets:
 images:
   barebox-dt-2nd.img: !template "$LG_BUILDDIR/images/barebox-dt-2nd.img"
   opensbi-riscv32-generic-fw_dynamic.bin: !template "$LG_BUILDDIR/opensbi-riscv32-generic-fw_dynamic.bin"
-tools:
-  qemu: /usr/bin/qemu-system-riscv32
 imports:
   -  ../strategy.py
diff --git a/test/riscv/sifive@rv64i_defconfig.yaml b/test/riscv/sifive@rv64i_defconfig.yaml
index 303f96299766..ce4ea0e1e27e 100644
--- a/test/riscv/sifive@rv64i_defconfig.yaml
+++ b/test/riscv/sifive@rv64i_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-riscv64
         machine: sifive_u
         cpu: sifive-u54
         memory: 256M
@@ -14,7 +14,5 @@ targets:
       BareboxTestStrategy: {}
 images:
   barebox-hifive-unleashed.img: !template "$LG_BUILDDIR/images/barebox-hifive-unleashed.img"
-tools:
-  qemu: /usr/bin/qemu-system-riscv64
 imports:
   -  ../strategy.py
diff --git a/test/strategy.py b/test/strategy.py
index 81650a7600bd..65cdae4fbf04 100644
--- a/test/strategy.py
+++ b/test/strategy.py
@@ -4,6 +4,7 @@ import attr
 import pytest
 import subprocess
 import os
+import shutil
 import sys
 
 from labgrid import target_factory, step, driver
@@ -33,6 +34,7 @@ class BareboxTestStrategy(Strategy):
         super().__attrs_post_init__()
         if isinstance(self.console, driver.QEMUDriver):
             self.qemu = self.console
+        self.patchtools()
 
     @step(args=['status'])
     def transition(self, status, *, step):
@@ -110,6 +112,16 @@ class BareboxTestStrategy(Strategy):
 
         return cmd
 
+    def patchtools(self):
+        # https://github.com/labgrid-project/labgrid/commit/69fd553c6969526b609d0be6bb81f0c35f08d1d0
+        if self.qemu is None:
+            return
+
+        if 'tools' not in self.target.env.config.data:
+            self.target.env.config.data['tools'] = {}
+        self.target.env.config.data["tools"][self.qemu.qemu_bin] = \
+                shutil.which(self.qemu.qemu_bin)
+
 def quote_cmd(cmd):
     quoted = map(lambda s : s if s.find(" ") == -1 else "'" + s + "'", cmd)
     return " ".join(quoted)
diff --git a/test/x86/pc@efi_defconfig.yaml b/test/x86/pc@efi_defconfig.yaml
index 280f5dcee9a6..e818409703f8 100644
--- a/test/x86/pc@efi_defconfig.yaml
+++ b/test/x86/pc@efi_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-x86_64
         machine: pc
         cpu: Nehalem
         memory: 1024M
@@ -25,7 +25,5 @@ targets:
 images:
   barebox.efi: !template "$LG_BUILDDIR/barebox.efi"
   OVMF.fd: !template "$LG_BUILDDIR/OVMF.fd"
-tools:
-  qemu: /usr/bin/qemu-system-x86_64
 imports:
   -  ../strategy.py
diff --git a/test/x86/q35@efi_defconfig.yaml b/test/x86/q35@efi_defconfig.yaml
index dcb3f604c0ac..71c44e8cd45f 100644
--- a/test/x86/q35@efi_defconfig.yaml
+++ b/test/x86/q35@efi_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-x86_64
         machine: q35
         cpu: Nehalem
         memory: 1024M
@@ -25,7 +25,5 @@ targets:
 images:
   barebox.efi: !template "$LG_BUILDDIR/barebox.efi"
   OVMF.fd: !template "$LG_BUILDDIR/OVMF.fd"
-tools:
-  qemu: /usr/bin/qemu-system-x86_64
 imports:
   -  ../strategy.py
diff --git a/test/x86/virtio@efi_defconfig.yaml b/test/x86/virtio@efi_defconfig.yaml
index 326fcbc68999..389089363872 100644
--- a/test/x86/virtio@efi_defconfig.yaml
+++ b/test/x86/virtio@efi_defconfig.yaml
@@ -2,7 +2,7 @@ targets:
   main:
     drivers:
       QEMUDriver:
-        qemu_bin: qemu
+        qemu_bin: qemu-system-x86_64
         machine: pc
         cpu: Nehalem
         memory: 1024M
@@ -26,7 +26,5 @@ targets:
 images:
   barebox.efi: !template "$LG_BUILDDIR/barebox.efi"
   OVMF.fd: !template "$LG_BUILDDIR/OVMF.fd"
-tools:
-  qemu: /usr/bin/qemu-system-x86_64
 imports:
   -  ../strategy.py
-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 4/5] test: add support for passing devices on command line
  2023-06-19  9:52 [PATCH 0/5] test: teach pytest the emulate.pl tricks Ahmad Fatoum
                   ` (2 preceding siblings ...)
  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
  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
  5 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2023-06-19  9:52 UTC (permalink / raw)
  To: barebox; +Cc: rcz, Ahmad Fatoum

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




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 5/5] test: don't hardcode origin of OVMF.fd
  2023-06-19  9:52 [PATCH 0/5] test: teach pytest the emulate.pl tricks Ahmad Fatoum
                   ` (3 preceding siblings ...)
  2023-06-19  9:52 ` [PATCH 4/5] test: add support for passing devices on command line Ahmad Fatoum
@ 2023-06-19  9:52 ` Ahmad Fatoum
  2023-06-26  9:55 ` [PATCH 0/5] test: teach pytest the emulate.pl tricks Sascha Hauer
  5 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2023-06-19  9:52 UTC (permalink / raw)
  To: barebox; +Cc: rcz, Ahmad Fatoum

Labgrid will look up the bios key in images, so we had an ugly
workaround of the "runner" (test/emulate.pl or script running in CI)
"downloading" /usr/share/qemu/OVMF.fd into the build directory and
referencing that.

Qemu already has a search path for BIOS images (controllable by -L),
where OVMF.fd, which would already point at /usr/share/qemu on Debian,
so let's make use of this by passing literal -bios OVMF.fd without
involving Labgrid automagic.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 .github/workflows/test-labgrid-pytest.yml | 4 ----
 test/x86/pc@efi_defconfig.yaml            | 6 +-----
 test/x86/q35@efi_defconfig.yaml           | 6 +-----
 test/x86/virtio@efi_defconfig.yaml        | 6 +-----
 4 files changed, 3 insertions(+), 19 deletions(-)

diff --git a/.github/workflows/test-labgrid-pytest.yml b/.github/workflows/test-labgrid-pytest.yml
index efbb51181b18..0f34f43e8dff 100644
--- a/.github/workflows/test-labgrid-pytest.yml
+++ b/.github/workflows/test-labgrid-pytest.yml
@@ -56,10 +56,6 @@ jobs:
         ./MAKEALL -O build-${{matrix.arch}} -k test/kconfig/enable_self_test.kconf \
                 -k test/kconfig/disable_target_tools.kconf ${{matrix.defconfig}}
 
-        if [ ${{matrix.arch}} = "x86" ]; then
-          cp /usr/share/ovmf/OVMF.fd build-${{matrix.arch}}
-        fi
-
         if [ ${{matrix.arch}} = "riscv" ]; then
           cp /usr/share/qemu/opensbi-riscv32-generic-fw_dynamic.bin build-${{matrix.arch}}
         fi
diff --git a/test/x86/pc@efi_defconfig.yaml b/test/x86/pc@efi_defconfig.yaml
index e818409703f8..ae06fbf69744 100644
--- a/test/x86/pc@efi_defconfig.yaml
+++ b/test/x86/pc@efi_defconfig.yaml
@@ -7,8 +7,7 @@ targets:
         cpu: Nehalem
         memory: 1024M
         kernel: barebox.efi
-        bios: OVMF.fd
-        extra_args: ''
+        extra_args: '-bios OVMF.fd'
       BareboxDriver:
         prompt: 'barebox@[^:]+:[^ ]+ '
         bootstring: 'commandline:'
@@ -20,10 +19,7 @@ targets:
       kconfig_add:
         - CONFIG_DRIVER_SERIAL_NS16550=y
         - CONFIG_CONSOLE_ACTIVATE_FIRST=y # avoid duplicate output
-      download:
-        OVMF.fd: /usr/share/qemu/OVMF.fd
 images:
   barebox.efi: !template "$LG_BUILDDIR/barebox.efi"
-  OVMF.fd: !template "$LG_BUILDDIR/OVMF.fd"
 imports:
   -  ../strategy.py
diff --git a/test/x86/q35@efi_defconfig.yaml b/test/x86/q35@efi_defconfig.yaml
index 71c44e8cd45f..578a3654fe9b 100644
--- a/test/x86/q35@efi_defconfig.yaml
+++ b/test/x86/q35@efi_defconfig.yaml
@@ -7,8 +7,7 @@ targets:
         cpu: Nehalem
         memory: 1024M
         kernel: barebox.efi
-        bios: OVMF.fd
-        extra_args: -global ICH9-LPC.noreboot=false
+        extra_args: '-bios OVMF.fd -global ICH9-LPC.noreboot=false'
       BareboxDriver:
         prompt: 'barebox@[^:]+:[^ ]+ '
         bootstring: 'commandline:'
@@ -20,10 +19,7 @@ targets:
       kconfig_add:
         - CONFIG_DRIVER_SERIAL_NS16550=y
         - CONFIG_CONSOLE_ACTIVATE_FIRST=y # avoid duplicate output
-      download:
-        OVMF.fd: /usr/share/qemu/OVMF.fd
 images:
   barebox.efi: !template "$LG_BUILDDIR/barebox.efi"
-  OVMF.fd: !template "$LG_BUILDDIR/OVMF.fd"
 imports:
   -  ../strategy.py
diff --git a/test/x86/virtio@efi_defconfig.yaml b/test/x86/virtio@efi_defconfig.yaml
index 389089363872..83340d19ddef 100644
--- a/test/x86/virtio@efi_defconfig.yaml
+++ b/test/x86/virtio@efi_defconfig.yaml
@@ -7,8 +7,7 @@ targets:
         cpu: Nehalem
         memory: 1024M
         kernel: barebox.efi
-        bios: OVMF.fd
-        extra_args: ''
+        extra_args: '-bios OVMF.fd'
       BareboxDriver:
         prompt: 'barebox@[^:]+:[^ ]+ '
         bootstring: 'commandline:'
@@ -21,10 +20,7 @@ targets:
         - test/kconfig/virtio-pci.cfg
         - CONFIG_DRIVER_SERIAL_NS16550=y
         - CONFIG_CONSOLE_ACTIVATE_FIRST=y # avoid duplicate output
-      download:
-        OVMF.fd: /usr/share/qemu/OVMF.fd
 images:
   barebox.efi: !template "$LG_BUILDDIR/barebox.efi"
-  OVMF.fd: !template "$LG_BUILDDIR/OVMF.fd"
 imports:
   -  ../strategy.py
-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/5] test: teach pytest the emulate.pl tricks
  2023-06-19  9:52 [PATCH 0/5] test: teach pytest the emulate.pl tricks Ahmad Fatoum
                   ` (4 preceding siblings ...)
  2023-06-19  9:52 ` [PATCH 5/5] test: don't hardcode origin of OVMF.fd Ahmad Fatoum
@ 2023-06-26  9:55 ` Sascha Hauer
  5 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2023-06-26  9:55 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox, rcz

On Mon, Jun 19, 2023 at 11:52:35AM +0200, Ahmad Fatoum wrote:
> test/emulate.pl duplicates a lot of what labgrid is already doing
> in order to instantiate virtualized devices and to allow interactive
> use of emulated barebox with the same environment config.
> 
> In preparation for phasing out emulate.pl, let's teach pytest how to
> replace it. As a first step we add these new pytest options:
> 
>   --interactive	start Qemu interactively using labgrid environment
>   --dry-run     print Qemu command line that would've been used
>   --rng         instaiate RNG device
>   --console     instaiate console device
>   --blk         instaiate block device
>   --qemu=OPT    pass OPT to qemu
> 
> The intention is to ultimately move as much as possible of this handling
> into labgrid itself, e.g. labgrid already has provisions for fixing up
> disk options.
> 
> Ahmad Fatoum (5):
>   test: add pytest.ini with defaults
>   test: have pytest --interactive start interactive Qemu session
>   test: lookup qemu binary in path if no tools key exists
>   test: add support for passing devices on command line
>   test: don't hardcode origin of OVMF.fd

Applied, thanks

Sascha

> 
>  .github/workflows/test-labgrid-pytest.yml     |  4 -
>  pytest.ini                                    |  2 +
>  test/arm/a15@multi_v7_defconfig.yaml          |  4 +-
>  test/arm/a9@multi_v7_defconfig.yaml           |  4 +-
>  test/arm/multi_v8_defconfig.yaml              |  4 +-
>  test/arm/qemu-raspi0@multi_v7_defconfig.yaml  |  4 +-
>  .../arm/qemu-raspi1ap@multi_v7_defconfig.yaml |  4 +-
>  test/arm/qemu-raspi2b@multi_v7_defconfig.yaml |  4 +-
>  .../qemu-sabrelite@multi_v7_defconfig.yaml    |  4 +-
>  test/arm/virt@multi_v7_defconfig.yaml         |  4 +-
>  test/conftest.py                              | 78 +++++++++++++++++
>  test/mips/be@qemu-malta_defconfig.yaml        |  4 +-
>  test/mips/le@qemu-malta_defconfig.yaml        |  4 +-
>  test/mips/qemu-malta64el_defconfig.yaml       |  4 +-
>  test/openrisc/generic_defconfig.yaml          |  4 +-
>  test/riscv/qemu-virt64@rv64i_defconfig.yaml   |  4 +-
>  test/riscv/qemu@virt32_defconfig.yaml         |  4 +-
>  test/riscv/sifive@rv64i_defconfig.yaml        |  4 +-
>  test/strategy.py                              | 87 ++++++++++++++++++-
>  test/x86/pc@efi_defconfig.yaml                | 10 +--
>  test/x86/q35@efi_defconfig.yaml               | 10 +--
>  test/x86/virtio@efi_defconfig.yaml            | 10 +--
>  22 files changed, 187 insertions(+), 74 deletions(-)
>  create mode 100644 pytest.ini
> 
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-06-26  9:56 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox