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

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