Blame SOURCES/kvm-iotests-change-qmp_log-filters-to-expect-QMP-objects.patch

383d26
From 2a6c4e6212c3f342132a2aca22c84bbf886dc79b Mon Sep 17 00:00:00 2001
383d26
From: John Snow <jsnow@redhat.com>
383d26
Date: Wed, 20 Mar 2019 16:16:22 +0100
383d26
Subject: [PATCH 024/163] iotests: change qmp_log filters to expect QMP objects
383d26
 only
383d26
383d26
RH-Author: John Snow <jsnow@redhat.com>
383d26
Message-id: <20190320161631.14841-11-jsnow@redhat.com>
383d26
Patchwork-id: 84946
383d26
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 10/19] iotests: change qmp_log filters to expect QMP objects only
383d26
Bugzilla: 1668956
383d26
RH-Acked-by: Max Reitz <mreitz@redhat.com>
383d26
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
383d26
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
383d26
As laid out in the previous commit's message:
383d26
383d26
```
383d26
Several places in iotests deal with serializing objects into JSON
383d26
strings, but to add pretty-printing it seems desirable to localize
383d26
all of those cases.
383d26
383d26
log() seems like a good candidate for that centralized behavior.
383d26
log() can already serialize json objects, but when it does so,
383d26
it assumes filters=[] operates on QMP objects, not strings.
383d26
383d26
qmp_log currently operates by dumping outgoing and incoming QMP
383d26
objects into strings and filtering them assuming that filters=[]
383d26
are string filters.
383d26
```
383d26
383d26
Therefore:
383d26
383d26
Change qmp_log to treat filters as if they're always qmp object filters,
383d26
then change the logging call to rely on log()'s ability to serialize QMP
383d26
objects, so we're not duplicating that effort.
383d26
383d26
Add a qmp version of filter_testfiles and adjust the only caller using
383d26
it for qmp_log to use the qmp version.
383d26
383d26
Signed-off-by: John Snow <jsnow@redhat.com>
383d26
Message-Id: <20181221093529.23855-10-jsnow@redhat.com>
383d26
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
383d26
Signed-off-by: Eric Blake <eblake@redhat.com>
383d26
(cherry picked from commit 08fcd6111e1949f456e1b232ebeeb0cc17019a92)
383d26
Signed-off-by: John Snow <jsnow@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 tests/qemu-iotests/206        |  4 ++--
383d26
 tests/qemu-iotests/iotests.py | 28 +++++++++++++++++++++++++---
383d26
 2 files changed, 27 insertions(+), 5 deletions(-)
383d26
383d26
diff --git a/tests/qemu-iotests/206 b/tests/qemu-iotests/206
383d26
index e92550f..5bb738b 100755
383d26
--- a/tests/qemu-iotests/206
383d26
+++ b/tests/qemu-iotests/206
383d26
@@ -27,7 +27,7 @@ iotests.verify_image_format(supported_fmts=['qcow2'])
383d26
 
383d26
 def blockdev_create(vm, options):
383d26
     result = vm.qmp_log('blockdev-create',
383d26
-                        filters=[iotests.filter_testfiles],
383d26
+                        filters=[iotests.filter_qmp_testfiles],
383d26
                         job_id='job0', options=options)
383d26
 
383d26
     if 'return' in result:
383d26
@@ -55,7 +55,7 @@ with iotests.FilePath('t.qcow2') as disk_path, \
383d26
                           'size': 0 })
383d26
 
383d26
     vm.qmp_log('blockdev-add',
383d26
-               filters=[iotests.filter_testfiles],
383d26
+               filters=[iotests.filter_qmp_testfiles],
383d26
                driver='file', filename=disk_path,
383d26
                node_name='imgfile')
383d26
 
383d26
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
383d26
index 30d198a..d2a8fbd 100644
383d26
--- a/tests/qemu-iotests/iotests.py
383d26
+++ b/tests/qemu-iotests/iotests.py
383d26
@@ -240,10 +240,33 @@ def filter_qmp_event(event):
383d26
         event['timestamp']['microseconds'] = 'USECS'
383d26
     return event
383d26
 
383d26
+def filter_qmp(qmsg, filter_fn):
383d26
+    '''Given a string filter, filter a QMP object's values.
383d26
+    filter_fn takes a (key, value) pair.'''
383d26
+    # Iterate through either lists or dicts;
383d26
+    if isinstance(qmsg, list):
383d26
+        items = enumerate(qmsg)
383d26
+    else:
383d26
+        items = qmsg.items()
383d26
+
383d26
+    for k, v in items:
383d26
+        if isinstance(v, list) or isinstance(v, dict):
383d26
+            qmsg[k] = filter_qmp(v, filter_fn)
383d26
+        else:
383d26
+            qmsg[k] = filter_fn(k, v)
383d26
+    return qmsg
383d26
+
383d26
 def filter_testfiles(msg):
383d26
     prefix = os.path.join(test_dir, "%s-" % (os.getpid()))
383d26
     return msg.replace(prefix, 'TEST_DIR/PID-')
383d26
 
383d26
+def filter_qmp_testfiles(qmsg):
383d26
+    def _filter(key, value):
383d26
+        if key == 'filename' or key == 'backing-file':
383d26
+            return filter_testfiles(value)
383d26
+        return value
383d26
+    return filter_qmp(qmsg, _filter)
383d26
+
383d26
 def filter_generated_node_ids(msg):
383d26
     return re.sub("#block[0-9]+", "NODE_NAME", msg)
383d26
 
383d26
@@ -459,10 +482,9 @@ class VM(qtest.QEMUQtestMachine):
383d26
             ("execute", cmd),
383d26
             ("arguments", ordered_kwargs(kwargs))
383d26
         ))
383d26
-        logmsg = json.dumps(full_cmd)
383d26
-        log(logmsg, filters)
383d26
+        log(full_cmd, filters)
383d26
         result = self.qmp(cmd, **kwargs)
383d26
-        log(json.dumps(result, sort_keys=True), filters)
383d26
+        log(result, filters)
383d26
         return result
383d26
 
383d26
     def run_job(self, job, auto_finalize=True, auto_dismiss=False):
383d26
-- 
383d26
1.8.3.1
383d26