[PATCH 0/3] tests/functional: Convert ARM bFLT linux-user avocado test

Philippe Mathieu-Daudé posted 3 patches 3 months ago
tests/avocado/avocado_qemu/__init__.py | 19 +--------
tests/avocado/load_bflt.py             | 54 --------------------------
tests/functional/qemu_test/__init__.py |  2 +-
tests/functional/qemu_test/testcase.py | 17 ++++++++
tests/functional/qemu_test/utils.py    |  9 +++++
tests/functional/test_arm_bflt.py      | 44 +++++++++++++++++++++
6 files changed, 72 insertions(+), 73 deletions(-)
delete mode 100644 tests/avocado/load_bflt.py
create mode 100755 tests/functional/test_arm_bflt.py
[PATCH 0/3] tests/functional: Convert ARM bFLT linux-user avocado test
Posted by Philippe Mathieu-Daudé 3 months ago
Convert the single user-mode test.

Warning, missing rework in tests/functional/meson.build,
however dirty tested using:

-- >8 --
diff --git a/tests/functional/meson.build b/tests/functional/meson.build
index f8e482a87c..44f8c2aa48 100644
--- a/tests/functional/meson.build
+++ b/tests/functional/meson.build
@@ -29,4 +29,3 @@ tests_generic = [
 tests_arm_thorough = [
-  'arm_canona1100',
-  'arm_n8x0',
+  'arm_bflt',
 ]
@@ -124,3 +123,3 @@ foreach speed : ['quick', 'thorough']
   foreach dir : target_dirs
-    if not dir.endswith('-softmmu')
+    if dir.endswith('-softmmu')
       continue
@@ -129,3 +128,3 @@ foreach speed : ['quick', 'thorough']
     target_base = dir.split('-')[0]
-    test_emulator = emulators['qemu-system-' + target_base]
+    test_emulator = emulators['qemu-' + target_base]

@@ -146,3 +145,3 @@ foreach speed : ['quick', 'thorough']
     test_env.set('QEMU_TEST_QEMU_BINARY',
-                 meson.global_build_root() / 'qemu-system-' + target_base)
+                 meson.global_build_root() / 'qemu-' + target_base)
     test_env.set('QEMU_BUILD_ROOT', meson.project_build_root())
---

  $ make check-func-arm SPEED=thorough QEMU_TEST_ALLOW_UNTRUSTED_CODE=1
  1/1 qemu:func-thorough+func-arm-thorough+thorough / func-arm-arm_bflt  OK    0.22s   1 subtests passed

Based-on: <20240821082748.65853-1-thuth@redhat.com>
Based-on: <20240822095045.72643-1-philmd@linaro.org>

Philippe Mathieu-Daudé (3):
  tests/functional: Add QemuUserTest class
  tests/functional: Convert ARM bFLT linux-user avocado test
  tests/avocado: Remove unused QemuUserTest class

 tests/avocado/avocado_qemu/__init__.py | 19 +--------
 tests/avocado/load_bflt.py             | 54 --------------------------
 tests/functional/qemu_test/__init__.py |  2 +-
 tests/functional/qemu_test/testcase.py | 17 ++++++++
 tests/functional/qemu_test/utils.py    |  9 +++++
 tests/functional/test_arm_bflt.py      | 44 +++++++++++++++++++++
 6 files changed, 72 insertions(+), 73 deletions(-)
 delete mode 100644 tests/avocado/load_bflt.py
 create mode 100755 tests/functional/test_arm_bflt.py

-- 
2.45.2


[PATCH 1/3] tests/functional: Add QemuUserTest class
Posted by Philippe Mathieu-Daudé 3 months ago
Per commit 5334df4822 ("tests/avocado: Introduce
QemuUserTest base class"):

  Similarly to the 'System' Test base class with methods
  for testing system emulation, the QemuUserTest class
  contains methods useful to test user-mode emulation.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 tests/functional/qemu_test/__init__.py |  2 +-
 tests/functional/qemu_test/testcase.py | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/tests/functional/qemu_test/__init__.py b/tests/functional/qemu_test/__init__.py
index db05c8f412..6e9b017264 100644
--- a/tests/functional/qemu_test/__init__.py
+++ b/tests/functional/qemu_test/__init__.py
@@ -11,4 +11,4 @@
 from .cmd import has_cmd, has_cmds, run_cmd, is_readable_executable_file, \
     interrupt_interactive_console_until_pattern, wait_for_console_pattern, \
     exec_command, exec_command_and_wait_for_pattern
-from .testcase import QemuSystemTest, QemuBaseTest
+from .testcase import QemuBaseTest, QemuUserTest, QemuSystemTest
diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
index 18314be9d1..aa0146265a 100644
--- a/tests/functional/qemu_test/testcase.py
+++ b/tests/functional/qemu_test/testcase.py
@@ -13,6 +13,7 @@
 
 import logging
 import os
+import subprocess
 import pycotap
 import sys
 import unittest
@@ -70,6 +71,22 @@ def main():
         unittest.main(module = None, testRunner = tr, argv=["__dummy__", path])
 
 
+class QemuUserTest(QemuBaseTest):
+
+    def setUp(self):
+        super().setUp('qemu-')
+        self._ldpath = []
+
+    def add_ldpath(self, ldpath):
+        self._ldpath.append(os.path.abspath(ldpath))
+
+    def run_cmd(self, bin_path, args=[]):
+        return subprocess.run([self.qemu_bin]
+                              + ["-L %s" % ldpath for ldpath in self._ldpath]
+                              + [bin_path]
+                              + args,
+                              text=True, capture_output=True)
+
 class QemuSystemTest(QemuBaseTest):
     """Facilitates system emulation tests."""
 
-- 
2.45.2


Re: [PATCH 1/3] tests/functional: Add QemuUserTest class
Posted by Thomas Huth 3 months ago
On 22/08/2024 12.42, Philippe Mathieu-Daudé wrote:
> Per commit 5334df4822 ("tests/avocado: Introduce
> QemuUserTest base class"):
> 
>    Similarly to the 'System' Test base class with methods
>    for testing system emulation, the QemuUserTest class
>    contains methods useful to test user-mode emulation.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   tests/functional/qemu_test/__init__.py |  2 +-
>   tests/functional/qemu_test/testcase.py | 17 +++++++++++++++++
>   2 files changed, 18 insertions(+), 1 deletion(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>



[PATCH 2/3] tests/functional: Convert ARM bFLT linux-user avocado test
Posted by Philippe Mathieu-Daudé 3 months ago
Straight forward conversion. Update the SHA1 hashes to
SHA256 hashes since SHA1 should not be used anymore nowadays.
Expose cpio_extract() in qemu_test.utils for possible reuse.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
Missing meson plumbing, i.e. adding to tests_arm_thorough[]
---
 tests/avocado/load_bflt.py          | 54 -----------------------------
 tests/functional/qemu_test/utils.py |  9 +++++
 tests/functional/test_arm_bflt.py   | 44 +++++++++++++++++++++++
 3 files changed, 53 insertions(+), 54 deletions(-)
 delete mode 100644 tests/avocado/load_bflt.py
 create mode 100755 tests/functional/test_arm_bflt.py

diff --git a/tests/avocado/load_bflt.py b/tests/avocado/load_bflt.py
deleted file mode 100644
index 264489ee25..0000000000
--- a/tests/avocado/load_bflt.py
+++ /dev/null
@@ -1,54 +0,0 @@
-# Test the bFLT loader format
-#
-# Copyright (C) 2019 Philippe Mathieu-Daudé <f4bug@amsat.org>
-#
-# SPDX-License-Identifier: GPL-2.0-or-later
-
-import os
-import bz2
-import subprocess
-
-from avocado import skipUnless
-from avocado_qemu import QemuUserTest
-from avocado_qemu import has_cmd
-
-
-class LoadBFLT(QemuUserTest):
-
-    def extract_cpio(self, cpio_path):
-        """
-        Extracts a cpio archive into the test workdir
-
-        :param cpio_path: path to the cpio archive
-        """
-        cwd = os.getcwd()
-        os.chdir(self.workdir)
-        with bz2.open(cpio_path, 'rb') as archive_cpio:
-            subprocess.run(['cpio', '-i'], input=archive_cpio.read(),
-                           stderr=subprocess.DEVNULL)
-        os.chdir(cwd)
-
-    @skipUnless(*has_cmd('cpio'))
-    @skipUnless(os.getenv('AVOCADO_ALLOW_UNTRUSTED_CODE'), 'untrusted code')
-    def test_stm32(self):
-        """
-        :avocado: tags=arch:arm
-        :avocado: tags=linux_user
-        :avocado: tags=quick
-        """
-        # See https://elinux.org/STM32#User_Space
-        rootfs_url = ('https://elinux.org/images/5/51/'
-                      'Stm32_mini_rootfs.cpio.bz2')
-        rootfs_hash = '9f065e6ba40cce7411ba757f924f30fcc57951e6'
-        rootfs_path_bz2 = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash)
-        busybox_path = os.path.join(self.workdir, "bin/busybox")
-
-        self.extract_cpio(rootfs_path_bz2)
-
-        res = self.run(busybox_path)
-        ver = 'BusyBox v1.24.0.git (2015-02-03 22:17:13 CET) multi-call binary.'
-        self.assertIn(ver, res.stdout_text)
-
-        res = self.run(busybox_path, ['uname', '-a'])
-        unm = 'armv7l GNU/Linux'
-        self.assertIn(unm, res.stdout_text)
diff --git a/tests/functional/qemu_test/utils.py b/tests/functional/qemu_test/utils.py
index 99eae5fc45..2a1cb60d38 100644
--- a/tests/functional/qemu_test/utils.py
+++ b/tests/functional/qemu_test/utils.py
@@ -12,6 +12,7 @@
 import lzma
 import os
 import shutil
+import subprocess
 import tarfile
 
 def archive_extract(archive, dest_dir, member=None):
@@ -45,3 +46,11 @@ def lzma_uncompress(xz_path, output_path):
         except:
             os.remove(output_path)
             raise
+
+def cpio_extract(cpio_handle, output_path):
+    cwd = os.getcwd()
+    os.chdir(output_path)
+    subprocess.run(['cpio', '-i'],
+                   input=cpio_handle.read(),
+                   stderr=subprocess.DEVNULL)
+    os.chdir(cwd)
diff --git a/tests/functional/test_arm_bflt.py b/tests/functional/test_arm_bflt.py
new file mode 100755
index 0000000000..281925d11a
--- /dev/null
+++ b/tests/functional/test_arm_bflt.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+#
+# Test the bFLT loader format
+#
+# Copyright (C) 2019 Philippe Mathieu-Daudé <f4bug@amsat.org>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import os
+import bz2
+
+from qemu_test import QemuUserTest, Asset
+from qemu_test import has_cmd
+from qemu_test.utils import cpio_extract
+from unittest import skipUnless
+
+
+class LoadBFLT(QemuUserTest):
+
+    ASSET_ROOTFS = Asset(
+        ('https://elinux.org/images/5/51/Stm32_mini_rootfs.cpio.bz2'),
+         'eefb788e4980c9e8d6c9d60ce7d15d4da6bf4fbc6a80f487673824600d5ba9cc')
+
+    @skipUnless(*has_cmd('cpio'))
+    @skipUnless(os.getenv('QEMU_TEST_ALLOW_UNTRUSTED_CODE'), 'untrusted code')
+    def test_stm32(self):
+        # See https://elinux.org/STM32#User_Space
+        rootfs_path_bz2 = self.ASSET_ROOTFS.fetch()
+        busybox_path = os.path.join(self.workdir, "bin/busybox")
+
+        with bz2.open(rootfs_path_bz2, 'rb') as cpio_handle:
+            cpio_extract(cpio_handle, self.workdir)
+
+        res = self.run_cmd(busybox_path)
+        ver = 'BusyBox v1.24.0.git (2015-02-03 22:17:13 CET) multi-call binary.'
+        self.assertIn(ver, res.stdout)
+
+        res = self.run_cmd(busybox_path, ['uname', '-a'])
+        unm = 'armv7l GNU/Linux'
+        self.assertIn(unm, res.stdout)
+
+
+if __name__ == '__main__':
+    QemuUserTest.main()
-- 
2.45.2


[PATCH 3/3] tests/avocado: Remove unused QemuUserTest class
Posted by Philippe Mathieu-Daudé 3 months ago
The single test that was using the QemuUserTest class
has been converted to the functional test framework.
This class is now unused, remove it.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 tests/avocado/avocado_qemu/__init__.py | 19 +------------------
 1 file changed, 1 insertion(+), 18 deletions(-)

diff --git a/tests/avocado/avocado_qemu/__init__.py b/tests/avocado/avocado_qemu/__init__.py
index 0d57addfea..0e4ecea7a0 100644
--- a/tests/avocado/avocado_qemu/__init__.py
+++ b/tests/avocado/avocado_qemu/__init__.py
@@ -17,7 +17,7 @@
 import uuid
 
 import avocado
-from avocado.utils import process, ssh
+from avocado.utils import ssh
 from avocado.utils.path import find_command
 
 from qemu.machine import QEMUMachine
@@ -384,23 +384,6 @@ def tearDown(self):
         super().tearDown()
 
 
-class QemuUserTest(QemuBaseTest):
-    """Facilitates user-mode emulation tests."""
-
-    def setUp(self):
-        self._ldpath = []
-        super().setUp('qemu-')
-
-    def add_ldpath(self, ldpath):
-        self._ldpath.append(os.path.abspath(ldpath))
-
-    def run(self, bin_path, args=[]):
-        qemu_args = " ".join(["-L %s" % ldpath for ldpath in self._ldpath])
-        bin_args = " ".join(args)
-        return process.run("%s %s %s %s" % (self.qemu_bin, qemu_args,
-                                            bin_path, bin_args))
-
-
 class LinuxSSHMixIn:
     """Contains utility methods for interacting with a guest via SSH."""
 
-- 
2.45.2


Re: [PATCH 3/3] tests/avocado: Remove unused QemuUserTest class
Posted by Thomas Huth 3 months ago
On 22/08/2024 12.42, Philippe Mathieu-Daudé wrote:
> The single test that was using the QemuUserTest class
> has been converted to the functional test framework.
> This class is now unused, remove it.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   tests/avocado/avocado_qemu/__init__.py | 19 +------------------
>   1 file changed, 1 insertion(+), 18 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>