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

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