Blame SOURCES/kvm-iotests-Add-failure-matching-to-common.qemu.patch

357786
From 2ffef795b5aed20b6fc2fba3951ff8f5c97cef5a Mon Sep 17 00:00:00 2001
357786
From: Jeffrey Cody <jcody@redhat.com>
357786
Date: Tue, 28 Aug 2018 21:08:17 +0200
357786
Subject: [PATCH 04/29] iotests: Add failure matching to common.qemu
357786
357786
RH-Author: Jeffrey Cody <jcody@redhat.com>
357786
Message-id: <7850ea2daac60087a2cd5e300f8b7042b167ef24.1535490170.git.jcody@redhat.com>
357786
Patchwork-id: 81955
357786
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 2/3] iotests: Add failure matching to common.qemu
357786
Bugzilla: 1605026
357786
RH-Acked-by: Max Reitz <mreitz@redhat.com>
357786
RH-Acked-by: John Snow <jsnow@redhat.com>
357786
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
357786
From: Max Reitz <mreitz@redhat.com>
357786
357786
Currently, common.qemu only allows to match for results indicating
357786
success.  The only way to fail is by provoking a timeout.  However,
357786
sometimes we do have a defined failure output and can match for that,
357786
which saves us from having to wait for the timeout in case of failure.
357786
Because failure can sometimes just result in a _notrun in the test, it
357786
is actually important to care about being able to fail quickly.
357786
357786
Also, sometimes we simply do not get any specific output in case of
357786
success.  The only way to handle this currently would be to define an
357786
error message as the string to look for, which means that actual success
357786
results in a timeout.  This is really bad because it unnecessarily slows
357786
down a succeeding test.
357786
357786
Therefore, this patch adds a new parameter $success_or_failure to
357786
_timed_wait_for and _send_qemu_cmd.  Setting this to a non-empty string
357786
makes both commands expect two match parameters: If the first matches,
357786
the function succeeds.  If the second matches, the function fails.
357786
357786
Signed-off-by: Max Reitz <mreitz@redhat.com>
357786
Message-id: 20180406151731.4285-2-mreitz@redhat.com
357786
Signed-off-by: Max Reitz <mreitz@redhat.com>
357786
(cherry picked from commit 81c6ddf49a76a663cea16c07a07d51b67c853209)
357786
Signed-off-by: Jeff Cody <jcody@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 tests/qemu-iotests/common.qemu | 58 +++++++++++++++++++++++++++++++++++++-----
357786
 1 file changed, 51 insertions(+), 7 deletions(-)
357786
357786
diff --git a/tests/qemu-iotests/common.qemu b/tests/qemu-iotests/common.qemu
357786
index 85f66b8..f285484 100644
357786
--- a/tests/qemu-iotests/common.qemu
357786
+++ b/tests/qemu-iotests/common.qemu
357786
@@ -52,11 +52,29 @@ _in_fd=4
357786
 # response is not echoed out.
357786
 # If $mismatch_only is set, only non-matching responses will
357786
 # be echoed.
357786
+#
357786
+# If $success_or_failure is set, the meaning of the arguments is
357786
+# changed as follows:
357786
+# $2: A string to search for in the response; if found, this indicates
357786
+#     success and ${QEMU_STATUS[$1]} is set to 0.
357786
+# $3: A string to search for in the response; if found, this indicates
357786
+#     failure and the test is either aborted (if $qemu_error_no_exit
357786
+#     is not set) or ${QEMU_STATUS[$1]} is set to -1 (otherwise).
357786
 function _timed_wait_for()
357786
 {
357786
     local h=${1}
357786
     shift
357786
 
357786
+    if [ -z "${success_or_failure}" ]; then
357786
+        success_match=${*}
357786
+        failure_match=
357786
+    else
357786
+        success_match=${1}
357786
+        failure_match=${2}
357786
+    fi
357786
+
357786
+    timeout=yes
357786
+
357786
     QEMU_STATUS[$h]=0
357786
     while IFS= read -t ${QEMU_COMM_TIMEOUT} resp <&${QEMU_OUT[$h]}
357786
     do
357786
@@ -64,10 +82,18 @@ function _timed_wait_for()
357786
             echo "${resp}" | _filter_testdir | _filter_qemu \
357786
                            | _filter_qemu_io | _filter_qmp | _filter_hmp
357786
         fi
357786
-        grep -q "${*}" < <(echo "${resp}")
357786
+        if [ -n "${failure_match}" ]; then
357786
+            grep -q "${failure_match}" < <(echo "${resp}")
357786
+            if [ $? -eq 0 ]; then
357786
+                timeout=
357786
+                break
357786
+            fi
357786
+        fi
357786
+        grep -q "${success_match}" < <(echo "${resp}")
357786
         if [ $? -eq 0 ]; then
357786
             return
357786
-        elif [ -z "${silent}" ] && [ -n "${mismatch_only}" ]; then
357786
+        fi
357786
+        if [ -z "${silent}" ] && [ -n "${mismatch_only}" ]; then
357786
             echo "${resp}" | _filter_testdir | _filter_qemu \
357786
                            | _filter_qemu_io | _filter_qmp | _filter_hmp
357786
         fi
357786
@@ -75,8 +101,12 @@ function _timed_wait_for()
357786
     done
357786
     QEMU_STATUS[$h]=-1
357786
     if [ -z "${qemu_error_no_exit}" ]; then
357786
-        echo "Timeout waiting for ${*} on handle ${h}"
357786
-        exit 1  # Timeout means the test failed
357786
+        if [ -n "${timeout}" ]; then
357786
+            echo "Timeout waiting for ${success_match} on handle ${h}"
357786
+        else
357786
+            echo "Wrong response matching ${failure_match} on handle ${h}"
357786
+        fi
357786
+        exit 1  # Timeout or wrong match mean the test failed
357786
     fi
357786
 }
357786
 
357786
@@ -96,6 +126,11 @@ function _timed_wait_for()
357786
 # If $qemu_error_no_exit is set, then even if the expected response
357786
 # is not seen, we will not exit.  $QEMU_STATUS[$1] will be set it -1 in
357786
 # that case.
357786
+#
357786
+# If $success_or_failure is set, then the last two strings are the
357786
+# strings the response will be scanned for.  The first of the two
357786
+# indicates success, the latter indicates failure.  Failure is handled
357786
+# like a timeout.
357786
 function _send_qemu_cmd()
357786
 {
357786
     local h=${1}
357786
@@ -109,14 +144,23 @@ function _send_qemu_cmd()
357786
         use_error="no"
357786
     fi
357786
     # This array element extraction is done to accommodate pathnames with spaces
357786
-    cmd=${@: 1:${#@}-1}
357786
-    shift $(($# - 1))
357786
+    if [ -z "${success_or_failure}" ]; then
357786
+        cmd=${@: 1:${#@}-1}
357786
+        shift $(($# - 1))
357786
+    else
357786
+        cmd=${@: 1:${#@}-2}
357786
+        shift $(($# - 2))
357786
+    fi
357786
 
357786
     while [ ${count} -gt 0 ]
357786
     do
357786
         echo "${cmd}" >&${QEMU_IN[${h}]}
357786
         if [ -n "${1}" ]; then
357786
-            qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}"
357786
+            if [ -z "${success_or_failure}" ]; then
357786
+                qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}"
357786
+            else
357786
+                qemu_error_no_exit=${use_error} _timed_wait_for ${h} "${1}" "${2}"
357786
+            fi
357786
             if [ ${QEMU_STATUS[$h]} -eq 0 ]; then
357786
                 return
357786
             fi
357786
-- 
357786
1.8.3.1
357786