yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-iotests-Use-Python-byte-strings-where-appropriate.patch

Pablo Greco e6a3ae
From 3b36d09996dbcbfc15cb83b2287940243022fd6a Mon Sep 17 00:00:00 2001
Pablo Greco e6a3ae
From: Pino Toscano <ptoscano@redhat.com>
Pablo Greco e6a3ae
Date: Mon, 8 Jul 2019 15:25:58 +0100
Pablo Greco e6a3ae
Subject: [PATCH 12/39] iotests: Use Python byte strings where appropriate
Pablo Greco e6a3ae
MIME-Version: 1.0
Pablo Greco e6a3ae
Content-Type: text/plain; charset=UTF-8
Pablo Greco e6a3ae
Content-Transfer-Encoding: 8bit
Pablo Greco e6a3ae
Pablo Greco e6a3ae
RH-Author: Pino Toscano <ptoscano@redhat.com>
Pablo Greco e6a3ae
Message-id: <20190708152601.21123-8-ptoscano@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 89424
Pablo Greco e6a3ae
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH v3 07/10] iotests: Use Python byte strings where appropriate
Pablo Greco e6a3ae
Bugzilla: 1513367
Pablo Greco e6a3ae
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Max Reitz <mreitz@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
From: Max Reitz <mreitz@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Since byte strings are no longer the default in Python 3, we have to
Pablo Greco e6a3ae
explicitly use them where we need to, which is mostly when working with
Pablo Greco e6a3ae
structures.  It also means that we need to open a file in binary mode
Pablo Greco e6a3ae
when we want to use structures.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
On the other hand, we have to accomodate for the fact that some
Pablo Greco e6a3ae
functions (still) work with byte strings but we want to use unicode
Pablo Greco e6a3ae
strings (in Python 3 at least, and it does not matter in Python 2).
Pablo Greco e6a3ae
This includes base64 encoding, but it is most notable when working with
Pablo Greco e6a3ae
the subprocess module: Either we set universal_newlines to True so that
Pablo Greco e6a3ae
the default streams are opened in text mode (hence this parameter is
Pablo Greco e6a3ae
aliased as "text" as of 3.7), or, if that is not possible, we have to
Pablo Greco e6a3ae
decode the output to a normal string.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Signed-off-by: Max Reitz <mreitz@redhat.com>
Pablo Greco e6a3ae
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
Pablo Greco e6a3ae
Message-Id: <20181022135307.14398-4-mreitz@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Pablo Greco e6a3ae
(cherry picked from commit 8eb5e6746feaf9e021b69ea2521899f8dc889033)
Pablo Greco e6a3ae
Signed-off-by: Pino Toscano <ptoscano@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 scripts/qtest.py                         |  2 +-
Pablo Greco e6a3ae
 tests/qemu-iotests/044                   |  8 ++++----
Pablo Greco e6a3ae
 tests/qemu-iotests/149                   |  8 +++++---
Pablo Greco e6a3ae
 tests/qemu-iotests/207                   |  4 ++--
Pablo Greco e6a3ae
 tests/qemu-iotests/iotests.py            | 11 +++++++----
Pablo Greco e6a3ae
 tests/qemu-iotests/nbd-fault-injector.py |  4 ++--
Pablo Greco e6a3ae
 tests/qemu-iotests/qcow2.py              | 10 +++++-----
Pablo Greco e6a3ae
 7 files changed, 26 insertions(+), 21 deletions(-)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/scripts/qtest.py b/scripts/qtest.py
Pablo Greco e6a3ae
index df0daf2..adf1fe3 100644
Pablo Greco e6a3ae
--- a/scripts/qtest.py
Pablo Greco e6a3ae
+++ b/scripts/qtest.py
Pablo Greco e6a3ae
@@ -64,7 +64,7 @@ class QEMUQtestProtocol(object):
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         @param qtest_cmd: qtest command text to be sent
Pablo Greco e6a3ae
         """
Pablo Greco e6a3ae
-        self._sock.sendall(qtest_cmd + "\n")
Pablo Greco e6a3ae
+        self._sock.sendall((qtest_cmd + "\n").encode('utf-8'))
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     def close(self):
Pablo Greco e6a3ae
         self._sock.close()
Pablo Greco e6a3ae
diff --git a/tests/qemu-iotests/044 b/tests/qemu-iotests/044
Pablo Greco e6a3ae
index 11ea0f4..69e736f 100755
Pablo Greco e6a3ae
--- a/tests/qemu-iotests/044
Pablo Greco e6a3ae
+++ b/tests/qemu-iotests/044
Pablo Greco e6a3ae
@@ -53,21 +53,21 @@ class TestRefcountTableGrowth(iotests.QMPTestCase):
Pablo Greco e6a3ae
             fd.seek(off_reftable)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
             for i in xrange(0, h.refcount_table_clusters):
Pablo Greco e6a3ae
-                sector = ''.join(struct.pack('>Q',
Pablo Greco e6a3ae
+                sector = b''.join(struct.pack('>Q',
Pablo Greco e6a3ae
                     off_refblock + i * 64 * 512 + j * 512)
Pablo Greco e6a3ae
                     for j in xrange(0, 64))
Pablo Greco e6a3ae
                 fd.write(sector)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
             # Write the refcount blocks
Pablo Greco e6a3ae
             assert(fd.tell() == off_refblock)
Pablo Greco e6a3ae
-            sector = ''.join(struct.pack('>H', 1) for j in xrange(0, 64 * 256))
Pablo Greco e6a3ae
+            sector = b''.join(struct.pack('>H', 1) for j in range(0, 64 * 256))
Pablo Greco e6a3ae
             for block in xrange(0, h.refcount_table_clusters):
Pablo Greco e6a3ae
                 fd.write(sector)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
             # Write the L1 table
Pablo Greco e6a3ae
             assert(fd.tell() == off_l1)
Pablo Greco e6a3ae
             assert(off_l2 + 512 * h.l1_size == off_data)
Pablo Greco e6a3ae
-            table = ''.join(struct.pack('>Q', (1 << 63) | off_l2 + 512 * j)
Pablo Greco e6a3ae
+            table = b''.join(struct.pack('>Q', (1 << 63) | off_l2 + 512 * j)
Pablo Greco e6a3ae
                 for j in xrange(0, h.l1_size))
Pablo Greco e6a3ae
             fd.write(table)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
@@ -85,7 +85,7 @@ class TestRefcountTableGrowth(iotests.QMPTestCase):
Pablo Greco e6a3ae
                 remaining = remaining - 1024 * 512
Pablo Greco e6a3ae
                 off = off + 1024 * 512
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-            table = ''.join(struct.pack('>Q', (1 << 63) | off + 512 * j)
Pablo Greco e6a3ae
+            table = b''.join(struct.pack('>Q', (1 << 63) | off + 512 * j)
Pablo Greco e6a3ae
                 for j in xrange(0, remaining / 512))
Pablo Greco e6a3ae
             fd.write(table)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
diff --git a/tests/qemu-iotests/149 b/tests/qemu-iotests/149
Pablo Greco e6a3ae
index d3ffa25..87174b1 100755
Pablo Greco e6a3ae
--- a/tests/qemu-iotests/149
Pablo Greco e6a3ae
+++ b/tests/qemu-iotests/149
Pablo Greco e6a3ae
@@ -79,7 +79,7 @@ class LUKSConfig(object):
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     def first_password_base64(self):
Pablo Greco e6a3ae
         (pw, slot) = self.first_password()
Pablo Greco e6a3ae
-        return base64.b64encode(pw)
Pablo Greco e6a3ae
+        return base64.b64encode(pw.encode('ascii')).decode('ascii')
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     def active_slots(self):
Pablo Greco e6a3ae
         slots = []
Pablo Greco e6a3ae
@@ -98,7 +98,8 @@ def verify_passwordless_sudo():
Pablo Greco e6a3ae
     proc = subprocess.Popen(args,
Pablo Greco e6a3ae
                             stdin=subprocess.PIPE,
Pablo Greco e6a3ae
                             stdout=subprocess.PIPE,
Pablo Greco e6a3ae
-                            stderr=subprocess.STDOUT)
Pablo Greco e6a3ae
+                            stderr=subprocess.STDOUT,
Pablo Greco e6a3ae
+                            universal_newlines=True)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     msg = proc.communicate()[0]
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
@@ -116,7 +117,8 @@ def cryptsetup(args, password=None):
Pablo Greco e6a3ae
     proc = subprocess.Popen(fullargs,
Pablo Greco e6a3ae
                             stdin=subprocess.PIPE,
Pablo Greco e6a3ae
                             stdout=subprocess.PIPE,
Pablo Greco e6a3ae
-                            stderr=subprocess.STDOUT)
Pablo Greco e6a3ae
+                            stderr=subprocess.STDOUT,
Pablo Greco e6a3ae
+                            universal_newlines=True)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     msg = proc.communicate(password)[0]
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
diff --git a/tests/qemu-iotests/207 b/tests/qemu-iotests/207
Pablo Greco e6a3ae
index 8202bd1..d45bf72 100755
Pablo Greco e6a3ae
--- a/tests/qemu-iotests/207
Pablo Greco e6a3ae
+++ b/tests/qemu-iotests/207
Pablo Greco e6a3ae
@@ -109,7 +109,7 @@ with iotests.FilePath('t.img') as disk_path, \
Pablo Greco e6a3ae
     md5_key = subprocess.check_output(
Pablo Greco e6a3ae
         'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' +
Pablo Greco e6a3ae
         'cut -d" " -f3 | base64 -d | md5sum -b | cut -d" " -f1',
Pablo Greco e6a3ae
-        shell=True).rstrip()
Pablo Greco e6a3ae
+        shell=True).rstrip().decode('ascii')
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     vm.launch()
Pablo Greco e6a3ae
     blockdev_create(vm, { 'driver': 'ssh',
Pablo Greco e6a3ae
@@ -147,7 +147,7 @@ with iotests.FilePath('t.img') as disk_path, \
Pablo Greco e6a3ae
     sha1_key = subprocess.check_output(
Pablo Greco e6a3ae
         'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' +
Pablo Greco e6a3ae
         'cut -d" " -f3 | base64 -d | sha1sum -b | cut -d" " -f1',
Pablo Greco e6a3ae
-        shell=True).rstrip()
Pablo Greco e6a3ae
+        shell=True).rstrip().decode('ascii')
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     vm.launch()
Pablo Greco e6a3ae
     blockdev_create(vm, { 'driver': 'ssh',
Pablo Greco e6a3ae
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
Pablo Greco e6a3ae
index 0f6980a..3d41ff0 100644
Pablo Greco e6a3ae
--- a/tests/qemu-iotests/iotests.py
Pablo Greco e6a3ae
+++ b/tests/qemu-iotests/iotests.py
Pablo Greco e6a3ae
@@ -104,7 +104,8 @@ def qemu_img_pipe(*args):
Pablo Greco e6a3ae
     '''Run qemu-img and return its output'''
Pablo Greco e6a3ae
     subp = subprocess.Popen(qemu_img_args + list(args),
Pablo Greco e6a3ae
                             stdout=subprocess.PIPE,
Pablo Greco e6a3ae
-                            stderr=subprocess.STDOUT)
Pablo Greco e6a3ae
+                            stderr=subprocess.STDOUT,
Pablo Greco e6a3ae
+                            universal_newlines=True)
Pablo Greco e6a3ae
     exitcode = subp.wait()
Pablo Greco e6a3ae
     if exitcode < 0:
Pablo Greco e6a3ae
         sys.stderr.write('qemu-img received signal %i: %s\n' % (-exitcode, ' '.join(qemu_img_args + list(args))))
Pablo Greco e6a3ae
@@ -128,7 +129,8 @@ def qemu_io(*args):
Pablo Greco e6a3ae
     '''Run qemu-io and return the stdout data'''
Pablo Greco e6a3ae
     args = qemu_io_args + list(args)
Pablo Greco e6a3ae
     subp = subprocess.Popen(args, stdout=subprocess.PIPE,
Pablo Greco e6a3ae
-                            stderr=subprocess.STDOUT)
Pablo Greco e6a3ae
+                            stderr=subprocess.STDOUT,
Pablo Greco e6a3ae
+                            universal_newlines=True)
Pablo Greco e6a3ae
     exitcode = subp.wait()
Pablo Greco e6a3ae
     if exitcode < 0:
Pablo Greco e6a3ae
         sys.stderr.write('qemu-io received signal %i: %s\n' % (-exitcode, ' '.join(args)))
Pablo Greco e6a3ae
@@ -149,7 +151,8 @@ class QemuIoInteractive:
Pablo Greco e6a3ae
         self.args = qemu_io_args + list(args)
Pablo Greco e6a3ae
         self._p = subprocess.Popen(self.args, stdin=subprocess.PIPE,
Pablo Greco e6a3ae
                                    stdout=subprocess.PIPE,
Pablo Greco e6a3ae
-                                   stderr=subprocess.STDOUT)
Pablo Greco e6a3ae
+                                   stderr=subprocess.STDOUT,
Pablo Greco e6a3ae
+                                   universal_newlines=True)
Pablo Greco e6a3ae
         assert self._p.stdout.read(9) == 'qemu-io> '
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     def close(self):
Pablo Greco e6a3ae
@@ -192,7 +195,7 @@ def compare_images(img1, img2, fmt1=imgfmt, fmt2=imgfmt):
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 def create_image(name, size):
Pablo Greco e6a3ae
     '''Create a fully-allocated raw image with sector markers'''
Pablo Greco e6a3ae
-    file = open(name, 'w')
Pablo Greco e6a3ae
+    file = open(name, 'wb')
Pablo Greco e6a3ae
     i = 0
Pablo Greco e6a3ae
     while i < size:
Pablo Greco e6a3ae
         sector = struct.pack('>l504xl', i / 512, i / 512)
Pablo Greco e6a3ae
diff --git a/tests/qemu-iotests/nbd-fault-injector.py b/tests/qemu-iotests/nbd-fault-injector.py
Pablo Greco e6a3ae
index f9193c0..09668f6 100755
Pablo Greco e6a3ae
--- a/tests/qemu-iotests/nbd-fault-injector.py
Pablo Greco e6a3ae
+++ b/tests/qemu-iotests/nbd-fault-injector.py
Pablo Greco e6a3ae
@@ -86,7 +86,7 @@ def recvall(sock, bufsize):
Pablo Greco e6a3ae
             raise Exception('unexpected disconnect')
Pablo Greco e6a3ae
         chunks.append(chunk)
Pablo Greco e6a3ae
         received += len(chunk)
Pablo Greco e6a3ae
-    return ''.join(chunks)
Pablo Greco e6a3ae
+    return b''.join(chunks)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 class Rule(object):
Pablo Greco e6a3ae
     def __init__(self, name, event, io, when):
Pablo Greco e6a3ae
@@ -176,7 +176,7 @@ def handle_connection(conn, use_export):
Pablo Greco e6a3ae
         req = read_request(conn)
Pablo Greco e6a3ae
         if req.type == NBD_CMD_READ:
Pablo Greco e6a3ae
             write_reply(conn, 0, req.handle)
Pablo Greco e6a3ae
-            conn.send('\0' * req.len, event='data')
Pablo Greco e6a3ae
+            conn.send(b'\0' * req.len, event='data')
Pablo Greco e6a3ae
         elif req.type == NBD_CMD_WRITE:
Pablo Greco e6a3ae
             _ = conn.recv(req.len, event='data')
Pablo Greco e6a3ae
             write_reply(conn, 0, req.handle)
Pablo Greco e6a3ae
diff --git a/tests/qemu-iotests/qcow2.py b/tests/qemu-iotests/qcow2.py
Pablo Greco e6a3ae
index b95a837..b392972 100755
Pablo Greco e6a3ae
--- a/tests/qemu-iotests/qcow2.py
Pablo Greco e6a3ae
+++ b/tests/qemu-iotests/qcow2.py
Pablo Greco e6a3ae
@@ -10,7 +10,7 @@ class QcowHeaderExtension:
Pablo Greco e6a3ae
     def __init__(self, magic, length, data):
Pablo Greco e6a3ae
         if length % 8 != 0:
Pablo Greco e6a3ae
             padding = 8 - (length % 8)
Pablo Greco e6a3ae
-            data += "\0" * padding
Pablo Greco e6a3ae
+            data += b"\0" * padding
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         self.magic  = magic
Pablo Greco e6a3ae
         self.length = length
Pablo Greco e6a3ae
@@ -103,7 +103,7 @@ class QcowHeader:
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         fd.seek(self.header_length)
Pablo Greco e6a3ae
         extensions = self.extensions
Pablo Greco e6a3ae
-        extensions.append(QcowHeaderExtension(0, 0, ""))
Pablo Greco e6a3ae
+        extensions.append(QcowHeaderExtension(0, 0, b""))
Pablo Greco e6a3ae
         for ex in extensions:
Pablo Greco e6a3ae
             buf = struct.pack('>II', ex.magic, ex.length)
Pablo Greco e6a3ae
             fd.write(buf)
Pablo Greco e6a3ae
@@ -137,8 +137,8 @@ class QcowHeader:
Pablo Greco e6a3ae
         for ex in self.extensions:
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
             data = ex.data[:ex.length]
Pablo Greco e6a3ae
-            if all(c in string.printable for c in data):
Pablo Greco e6a3ae
-                data = "'%s'" % data
Pablo Greco e6a3ae
+            if all(c in string.printable.encode('ascii') for c in data):
Pablo Greco e6a3ae
+                data = "'%s'" % data.decode('ascii')
Pablo Greco e6a3ae
             else:
Pablo Greco e6a3ae
                 data = "<binary>"
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
@@ -178,7 +178,7 @@ def cmd_add_header_ext(fd, magic, data):
Pablo Greco e6a3ae
         sys.exit(1)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     h = QcowHeader(fd)
Pablo Greco e6a3ae
-    h.extensions.append(QcowHeaderExtension.create(magic, data))
Pablo Greco e6a3ae
+    h.extensions.append(QcowHeaderExtension.create(magic, data.encode('ascii')))
Pablo Greco e6a3ae
     h.update(fd)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 def cmd_add_header_ext_stdio(fd, magic):
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae