cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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