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

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