Blame SOURCES/kvm-Add-functional-acceptance-tests-infrastructure.patch

ae23c9
From 687a123ab2165fa3adf9e3469577c22008125270 Mon Sep 17 00:00:00 2001
ae23c9
From: Yash Mankad <ymankad@redhat.com>
ae23c9
Date: Wed, 12 Dec 2018 00:14:35 +0000
ae23c9
Subject: [PATCH 07/13] Add functional/acceptance tests infrastructure
ae23c9
MIME-Version: 1.0
ae23c9
Content-Type: text/plain; charset=UTF-8
ae23c9
Content-Transfer-Encoding: 8bit
ae23c9
ae23c9
RH-Author: Yash Mankad <ymankad@redhat.com>
ae23c9
Message-id: <b1ac81d7ccd9e43f36a74dc6a008974484508c7f.1544573601.git.ymankad@redhat.com>
ae23c9
Patchwork-id: 83432
ae23c9
O-Subject: [RHEL-8.0 qemu-kvm PATCH v2 1/7] Add functional/acceptance tests infrastructure
ae23c9
Bugzilla: 1655807
ae23c9
RH-Acked-by: Eduardo Habkost <ehabkost@redhat.com>
ae23c9
RH-Acked-by: John Snow <jsnow@redhat.com>
ae23c9
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
ae23c9
ae23c9
From: Cleber Rosa <crosa@redhat.com>
ae23c9
ae23c9
This patch adds the very minimum infrastructure necessary for writing
ae23c9
and running functional/acceptance tests, including:
ae23c9
ae23c9
 * Documentation
ae23c9
 * The avocado_qemu.Test base test class
ae23c9
 * One example tests (version.py)
ae23c9
ae23c9
Additional functionality is expected to be added along the tests that
ae23c9
require them.
ae23c9
ae23c9
Signed-off-by: Cleber Rosa <crosa@redhat.com>
ae23c9
Message-Id: <20180530184156.15634-2-crosa@redhat.com>
ae23c9
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
ae23c9
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
ae23c9
[ehabkost: fix typo on testing.rst]
ae23c9
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
ae23c9
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
ae23c9
(cherry picked from commit c3d7e8c90db208b1d876f8d6458c2dfca169137f)
ae23c9
Signed-off-by: Yash Mankad <ymankad@redhat.com>
ae23c9
ae23c9
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ae23c9
---
ae23c9
 docs/devel/testing.rst                    | 192 ++++++++++++++++++++++++++++++
ae23c9
 tests/acceptance/README.rst               |  10 ++
ae23c9
 tests/acceptance/avocado_qemu/__init__.py |  54 +++++++++
ae23c9
 tests/acceptance/version.py               |  24 ++++
ae23c9
 4 files changed, 280 insertions(+)
ae23c9
 create mode 100644 tests/acceptance/README.rst
ae23c9
 create mode 100644 tests/acceptance/avocado_qemu/__init__.py
ae23c9
 create mode 100644 tests/acceptance/version.py
ae23c9
ae23c9
diff --git a/docs/devel/testing.rst b/docs/devel/testing.rst
ae23c9
index 0ca1a2d..f33e5a8 100644
ae23c9
--- a/docs/devel/testing.rst
ae23c9
+++ b/docs/devel/testing.rst
ae23c9
@@ -484,3 +484,195 @@ supported. To start the fuzzer, run
ae23c9
 
ae23c9
 Alternatively, some command different from "qemu-img info" can be tested, by
ae23c9
 changing the ``-c`` option.
ae23c9
+
ae23c9
+Acceptance tests using the Avocado Framework
ae23c9
+============================================
ae23c9
+
ae23c9
+The ``tests/acceptance`` directory hosts functional tests, also known
ae23c9
+as acceptance level tests.  They're usually higher level tests, and
ae23c9
+may interact with external resources and with various guest operating
ae23c9
+systems.
ae23c9
+
ae23c9
+These tests are written using the Avocado Testing Framework (which must
ae23c9
+be installed separately) in conjunction with a the ``avocado_qemu.Test``
ae23c9
+class, implemented at ``tests/acceptance/avocado_qemu``.
ae23c9
+
ae23c9
+Tests based on ``avocado_qemu.Test`` can easily:
ae23c9
+
ae23c9
+ * Customize the command line arguments given to the convenience
ae23c9
+   ``self.vm`` attribute (a QEMUMachine instance)
ae23c9
+
ae23c9
+ * Interact with the QEMU monitor, send QMP commands and check
ae23c9
+   their results
ae23c9
+
ae23c9
+ * Interact with the guest OS, using the convenience console device
ae23c9
+   (which may be useful to assert the effectiveness and correctness of
ae23c9
+   command line arguments or QMP commands)
ae23c9
+
ae23c9
+ * Interact with external data files that accompany the test itself
ae23c9
+   (see ``self.get_data()``)
ae23c9
+
ae23c9
+ * Download (and cache) remote data files, such as firmware and kernel
ae23c9
+   images
ae23c9
+
ae23c9
+ * Have access to a library of guest OS images (by means of the
ae23c9
+   ``avocado.utils.vmimage`` library)
ae23c9
+
ae23c9
+ * Make use of various other test related utilities available at the
ae23c9
+   test class itself and at the utility library:
ae23c9
+
ae23c9
+   - http://avocado-framework.readthedocs.io/en/latest/api/test/avocado.html#avocado.Test
ae23c9
+   - http://avocado-framework.readthedocs.io/en/latest/api/utils/avocado.utils.html
ae23c9
+
ae23c9
+Installation
ae23c9
+------------
ae23c9
+
ae23c9
+To install Avocado and its dependencies, run:
ae23c9
+
ae23c9
+.. code::
ae23c9
+
ae23c9
+  pip install --user avocado-framework
ae23c9
+
ae23c9
+Alternatively, follow the instructions on this link:
ae23c9
+
ae23c9
+  http://avocado-framework.readthedocs.io/en/latest/GetStartedGuide.html#installing-avocado
ae23c9
+
ae23c9
+Overview
ae23c9
+--------
ae23c9
+
ae23c9
+This directory provides the ``avocado_qemu`` Python module, containing
ae23c9
+the ``avocado_qemu.Test`` class.  Here's a simple usage example:
ae23c9
+
ae23c9
+.. code::
ae23c9
+
ae23c9
+  from avocado_qemu import Test
ae23c9
+
ae23c9
+
ae23c9
+  class Version(Test):
ae23c9
+      """
ae23c9
+      :avocado: enable
ae23c9
+      :avocado: tags=quick
ae23c9
+      """
ae23c9
+      def test_qmp_human_info_version(self):
ae23c9
+          self.vm.launch()
ae23c9
+          res = self.vm.command('human-monitor-command',
ae23c9
+                                command_line='info version')
ae23c9
+          self.assertRegexpMatches(res, r'^(\d+\.\d+\.\d)')
ae23c9
+
ae23c9
+To execute your test, run:
ae23c9
+
ae23c9
+.. code::
ae23c9
+
ae23c9
+  avocado run version.py
ae23c9
+
ae23c9
+Tests may be classified according to a convention by using docstring
ae23c9
+directives such as ``:avocado: tags=TAG1,TAG2``.  To run all tests
ae23c9
+in the current directory, tagged as "quick", run:
ae23c9
+
ae23c9
+.. code::
ae23c9
+
ae23c9
+  avocado run -t quick .
ae23c9
+
ae23c9
+The ``avocado_qemu.Test`` base test class
ae23c9
+-----------------------------------------
ae23c9
+
ae23c9
+The ``avocado_qemu.Test`` class has a number of characteristics that
ae23c9
+are worth being mentioned right away.
ae23c9
+
ae23c9
+First of all, it attempts to give each test a ready to use QEMUMachine
ae23c9
+instance, available at ``self.vm``.  Because many tests will tweak the
ae23c9
+QEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``)
ae23c9
+is left to the test writer.
ae23c9
+
ae23c9
+At test "tear down", ``avocado_qemu.Test`` handles the QEMUMachine
ae23c9
+shutdown.
ae23c9
+
ae23c9
+QEMUMachine
ae23c9
+~~~~~~~~~~~
ae23c9
+
ae23c9
+The QEMUMachine API is already widely used in the Python iotests,
ae23c9
+device-crash-test and other Python scripts.  It's a wrapper around the
ae23c9
+execution of a QEMU binary, giving its users:
ae23c9
+
ae23c9
+ * the ability to set command line arguments to be given to the QEMU
ae23c9
+   binary
ae23c9
+
ae23c9
+ * a ready to use QMP connection and interface, which can be used to
ae23c9
+   send commands and inspect its results, as well as asynchronous
ae23c9
+   events
ae23c9
+
ae23c9
+ * convenience methods to set commonly used command line arguments in
ae23c9
+   a more succinct and intuitive way
ae23c9
+
ae23c9
+QEMU binary selection
ae23c9
+~~~~~~~~~~~~~~~~~~~~~
ae23c9
+
ae23c9
+The QEMU binary used for the ``self.vm`` QEMUMachine instance will
ae23c9
+primarily depend on the value of the ``qemu_bin`` parameter.  If it's
ae23c9
+not explicitly set, its default value will be the result of a dynamic
ae23c9
+probe in the same source tree.  A suitable binary will be one that
ae23c9
+targets the architecture matching host machine.
ae23c9
+
ae23c9
+Based on this description, test writers will usually rely on one of
ae23c9
+the following approaches:
ae23c9
+
ae23c9
+1) Set ``qemu_bin``, and use the given binary
ae23c9
+
ae23c9
+2) Do not set ``qemu_bin``, and use a QEMU binary named like
ae23c9
+   "${arch}-softmmu/qemu-system-${arch}", either in the current
ae23c9
+   working directory, or in the current source tree.
ae23c9
+
ae23c9
+The resulting ``qemu_bin`` value will be preserved in the
ae23c9
+``avocado_qemu.Test`` as an attribute with the same name.
ae23c9
+
ae23c9
+Attribute reference
ae23c9
+-------------------
ae23c9
+
ae23c9
+Besides the attributes and methods that are part of the base
ae23c9
+``avocado.Test`` class, the following attributes are available on any
ae23c9
+``avocado_qemu.Test`` instance.
ae23c9
+
ae23c9
+vm
ae23c9
+~~
ae23c9
+
ae23c9
+A QEMUMachine instance, initially configured according to the given
ae23c9
+``qemu_bin`` parameter.
ae23c9
+
ae23c9
+qemu_bin
ae23c9
+~~~~~~~~
ae23c9
+
ae23c9
+The preserved value of the ``qemu_bin`` parameter or the result of the
ae23c9
+dynamic probe for a QEMU binary in the current working directory or
ae23c9
+source tree.
ae23c9
+
ae23c9
+Parameter reference
ae23c9
+-------------------
ae23c9
+
ae23c9
+To understand how Avocado parameters are accessed by tests, and how
ae23c9
+they can be passed to tests, please refer to::
ae23c9
+
ae23c9
+  http://avocado-framework.readthedocs.io/en/latest/WritingTests.html#accessing-test-parameters
ae23c9
+
ae23c9
+Parameter values can be easily seen in the log files, and will look
ae23c9
+like the following:
ae23c9
+
ae23c9
+.. code::
ae23c9
+
ae23c9
+  PARAMS (key=qemu_bin, path=*, default=x86_64-softmmu/qemu-system-x86_64) => 'x86_64-softmmu/qemu-system-x86_64
ae23c9
+
ae23c9
+qemu_bin
ae23c9
+~~~~~~~~
ae23c9
+
ae23c9
+The exact QEMU binary to be used on QEMUMachine.
ae23c9
+
ae23c9
+Uninstalling Avocado
ae23c9
+--------------------
ae23c9
+
ae23c9
+If you've followed the installation instructions above, you can easily
ae23c9
+uninstall Avocado.  Start by listing the packages you have installed::
ae23c9
+
ae23c9
+  pip list --user
ae23c9
+
ae23c9
+And remove any package you want with::
ae23c9
+
ae23c9
+  pip uninstall <package_name>
ae23c9
diff --git a/tests/acceptance/README.rst b/tests/acceptance/README.rst
ae23c9
new file mode 100644
ae23c9
index 0000000..89260fa
ae23c9
--- /dev/null
ae23c9
+++ b/tests/acceptance/README.rst
ae23c9
@@ -0,0 +1,10 @@
ae23c9
+============================================
ae23c9
+Acceptance tests using the Avocado Framework
ae23c9
+============================================
ae23c9
+
ae23c9
+This directory contains functional tests, also known as acceptance
ae23c9
+level tests.  They're usually higher level, and may interact with
ae23c9
+external resources and with various guest operating systems.
ae23c9
+
ae23c9
+For more information, please refer to ``docs/devel/testing.rst``,
ae23c9
+section "Acceptance tests using the Avocado Framework".
ae23c9
diff --git a/tests/acceptance/avocado_qemu/__init__.py b/tests/acceptance/avocado_qemu/__init__.py
ae23c9
new file mode 100644
ae23c9
index 0000000..1e54fd5
ae23c9
--- /dev/null
ae23c9
+++ b/tests/acceptance/avocado_qemu/__init__.py
ae23c9
@@ -0,0 +1,54 @@
ae23c9
+# Test class and utilities for functional tests
ae23c9
+#
ae23c9
+# Copyright (c) 2018 Red Hat, Inc.
ae23c9
+#
ae23c9
+# Author:
ae23c9
+#  Cleber Rosa <crosa@redhat.com>
ae23c9
+#
ae23c9
+# This work is licensed under the terms of the GNU GPL, version 2 or
ae23c9
+# later.  See the COPYING file in the top-level directory.
ae23c9
+
ae23c9
+import os
ae23c9
+import sys
ae23c9
+
ae23c9
+import avocado
ae23c9
+
ae23c9
+SRC_ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
ae23c9
+SRC_ROOT_DIR = os.path.abspath(os.path.dirname(SRC_ROOT_DIR))
ae23c9
+sys.path.append(os.path.join(SRC_ROOT_DIR, 'scripts'))
ae23c9
+
ae23c9
+from qemu import QEMUMachine
ae23c9
+
ae23c9
+def is_readable_executable_file(path):
ae23c9
+    return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK)
ae23c9
+
ae23c9
+
ae23c9
+def pick_default_qemu_bin():
ae23c9
+    """
ae23c9
+    Picks the path of a QEMU binary, starting either in the current working
ae23c9
+    directory or in the source tree root directory.
ae23c9
+    """
ae23c9
+    arch = os.uname()[4]
ae23c9
+    qemu_bin_relative_path = os.path.join("%s-softmmu" % arch,
ae23c9
+                                          "qemu-system-%s" % arch)
ae23c9
+    if is_readable_executable_file(qemu_bin_relative_path):
ae23c9
+        return qemu_bin_relative_path
ae23c9
+
ae23c9
+    qemu_bin_from_src_dir_path = os.path.join(SRC_ROOT_DIR,
ae23c9
+                                              qemu_bin_relative_path)
ae23c9
+    if is_readable_executable_file(qemu_bin_from_src_dir_path):
ae23c9
+        return qemu_bin_from_src_dir_path
ae23c9
+
ae23c9
+
ae23c9
+class Test(avocado.Test):
ae23c9
+    def setUp(self):
ae23c9
+        self.vm = None
ae23c9
+        self.qemu_bin = self.params.get('qemu_bin',
ae23c9
+                                        default=pick_default_qemu_bin())
ae23c9
+        if self.qemu_bin is None:
ae23c9
+            self.cancel("No QEMU binary defined or found in the source tree")
ae23c9
+        self.vm = QEMUMachine(self.qemu_bin)
ae23c9
+
ae23c9
+    def tearDown(self):
ae23c9
+        if self.vm is not None:
ae23c9
+            self.vm.shutdown()
ae23c9
diff --git a/tests/acceptance/version.py b/tests/acceptance/version.py
ae23c9
new file mode 100644
ae23c9
index 0000000..13b0a74
ae23c9
--- /dev/null
ae23c9
+++ b/tests/acceptance/version.py
ae23c9
@@ -0,0 +1,24 @@
ae23c9
+# Version check example test
ae23c9
+#
ae23c9
+# Copyright (c) 2018 Red Hat, Inc.
ae23c9
+#
ae23c9
+# Author:
ae23c9
+#  Cleber Rosa <crosa@redhat.com>
ae23c9
+#
ae23c9
+# This work is licensed under the terms of the GNU GPL, version 2 or
ae23c9
+# later.  See the COPYING file in the top-level directory.
ae23c9
+
ae23c9
+
ae23c9
+from avocado_qemu import Test
ae23c9
+
ae23c9
+
ae23c9
+class Version(Test):
ae23c9
+    """
ae23c9
+    :avocado: enable
ae23c9
+    :avocado: tags=quick
ae23c9
+    """
ae23c9
+    def test_qmp_human_info_version(self):
ae23c9
+        self.vm.launch()
ae23c9
+        res = self.vm.command('human-monitor-command',
ae23c9
+                              command_line='info version')
ae23c9
+        self.assertRegexpMatches(res, r'^(\d+\.\d+\.\d)')
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9