From 6ea2730ea669b6cf7ac668f606f63628ef8918b7 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Thu, 2 Jan 2020 20:41:57 -0800 Subject: [PATCH] qa: fix various py3 cephfs qa bugs Signed-off-by: Patrick Donnelly --- qa/tasks/cephfs/mount.py | 51 +++++++++++------------ qa/tasks/cephfs/test_cap_flush.py | 4 +- qa/tasks/cephfs/test_journal_migration.py | 2 +- qa/tasks/cephfs/test_pool_perm.py | 2 +- qa/tasks/cephfs/test_strays.py | 35 ++++++++-------- 5 files changed, 45 insertions(+), 49 deletions(-) diff --git a/qa/tasks/cephfs/mount.py b/qa/tasks/cephfs/mount.py index b729d801ab7..4d7375f6d5c 100644 --- a/qa/tasks/cephfs/mount.py +++ b/qa/tasks/cephfs/mount.py @@ -233,20 +233,20 @@ class CephFSMount(object): pyscript = dedent(""" import time - f = open("{path}", 'w') - f.write('content') - f.flush() - f.write('content2') - while True: - time.sleep(1) + with open("{path}", 'w') as f: + f.write('content') + f.flush() + f.write('content2') + while True: + time.sleep(1) """).format(path=path) else: pyscript = dedent(""" import time - f = open("{path}", 'r') - while True: - time.sleep(1) + with open("{path}", 'r') as f: + while True: + time.sleep(1) """).format(path=path) rproc = self._run_python(pyscript) @@ -357,7 +357,7 @@ class CephFSMount(object): f1 = open("{path}-1", 'r') try: fcntl.flock(f1, fcntl.LOCK_EX | fcntl.LOCK_NB) - except IOError, e: + except IOError as e: if e.errno == errno.EAGAIN: pass else: @@ -367,7 +367,7 @@ class CephFSMount(object): try: lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0) fcntl.fcntl(f2, fcntl.F_SETLK, lockdata) - except IOError, e: + except IOError as e: if e.errno == errno.EAGAIN: pass else: @@ -401,7 +401,7 @@ class CephFSMount(object): time.sleep(1) if not {loop}: break - except IOError, e: + except IOError as e: pass os.close(fd) """).format(path=path, loop=str(loop)) @@ -427,11 +427,10 @@ class CephFSMount(object): return self.run_python(dedent(""" import zlib path = "{path}" - f = open(path, 'w') - for i in range(0, {size}): - val = zlib.crc32("%s" % i) & 7 - f.write(chr(val)) - f.close() + with open(path, 'w') as f: + for i in range(0, {size}): + val = zlib.crc32(str(i).encode('utf-8')) & 7 + f.write(chr(val)) """.format( path=os.path.join(self.mountpoint, filename), size=size @@ -442,15 +441,14 @@ class CephFSMount(object): return self.run_python(dedent(""" import zlib path = "{path}" - f = open(path, 'r') - bytes = f.read() - f.close() + with open(path, 'r') as f: + bytes = f.read() if len(bytes) != {size}: raise RuntimeError("Bad length {{0}} vs. expected {{1}}".format( len(bytes), {size} )) for i, b in enumerate(bytes): - val = zlib.crc32("%s" % i) & 7 + val = zlib.crc32(str(i).encode('utf-8')) & 7 if b != chr(val): raise RuntimeError("Bad data at offset {{0}}".format(i)) """.format( @@ -511,12 +509,11 @@ class CephFSMount(object): for i in range(0, n): fname = "{{0}}_{{1}}".format(abs_path, i) - h = open(fname, 'w') - h.write('content') - if {sync}: - h.flush() - os.fsync(h.fileno()) - h.close() + with open(fname, 'w') as f: + f.write('content') + if {sync}: + f.flush() + os.fsync(f.fileno()) """).format(abs_path=abs_path, count=count, sync=str(sync)) self.run_python(pyscript) diff --git a/qa/tasks/cephfs/test_cap_flush.py b/qa/tasks/cephfs/test_cap_flush.py index cea6ff3f9b0..cb105368126 100644 --- a/qa/tasks/cephfs/test_cap_flush.py +++ b/qa/tasks/cephfs/test_cap_flush.py @@ -23,7 +23,7 @@ class TestCapFlush(CephFSTestCase): import os os.mkdir("{0}") fd = os.open("{0}", os.O_RDONLY) - os.fchmod(fd, 0777) + os.fchmod(fd, 0o777) os.fsync(fd) """).format(dir_path) self.mount_a.run_python(py_script) @@ -43,7 +43,7 @@ class TestCapFlush(CephFSTestCase): os.setgid(65534) os.setuid(65534) fd = os.open("{1}", os.O_CREAT | os.O_RDWR, 0644) - os.fchmod(fd, 0640) + os.fchmod(fd, 0o640) """).format(dir_path, file_name) self.mount_a.run_python(py_script) diff --git a/qa/tasks/cephfs/test_journal_migration.py b/qa/tasks/cephfs/test_journal_migration.py index de4867ef0a5..9d1d399caa8 100644 --- a/qa/tasks/cephfs/test_journal_migration.py +++ b/qa/tasks/cephfs/test_journal_migration.py @@ -78,7 +78,7 @@ class TestJournalMigration(CephFSTestCase): "--path", "/tmp/journal.json"], 0) p = self.fs.tool_remote.run( args=[ - "python", + "python3", "-c", "import json; print(len(json.load(open('/tmp/journal.json'))))" ], diff --git a/qa/tasks/cephfs/test_pool_perm.py b/qa/tasks/cephfs/test_pool_perm.py index f0efc43ec63..a1f234a2253 100644 --- a/qa/tasks/cephfs/test_pool_perm.py +++ b/qa/tasks/cephfs/test_pool_perm.py @@ -20,7 +20,7 @@ class TestPoolPerm(CephFSTestCase): ret = os.read(fd, 1024) else: os.write(fd, b'content') - except OSError, e: + except OSError as e: if e.errno != errno.EPERM: raise else: diff --git a/qa/tasks/cephfs/test_strays.py b/qa/tasks/cephfs/test_strays.py index a294cc46087..5dd1524a324 100644 --- a/qa/tasks/cephfs/test_strays.py +++ b/qa/tasks/cephfs/test_strays.py @@ -44,11 +44,10 @@ class TestStrays(CephFSTestCase): size = {size} file_count = {file_count} os.mkdir(os.path.join(mount_path, subdir)) - for i in xrange(0, file_count): + for i in range(0, file_count): filename = "{{0}}_{{1}}.bin".format(i, size) - f = open(os.path.join(mount_path, subdir, filename), 'w') - f.write(size * 'x') - f.close() + with open(os.path.join(mount_path, subdir, filename), 'w') as f: + f.write(size * 'x') """.format( mount_path=self.mount_a.mountpoint, size=1024, @@ -151,12 +150,11 @@ class TestStrays(CephFSTestCase): size_unit = {size_unit} file_multiplier = {file_multiplier} os.mkdir(os.path.join(mount_path, subdir)) - for i in xrange(0, file_multiplier): - for size in xrange(0, {size_range}*size_unit, size_unit): + for i in range(0, file_multiplier): + for size in range(0, {size_range}*size_unit, size_unit): filename = "{{0}}_{{1}}.bin".format(i, size / size_unit) - f = open(os.path.join(mount_path, subdir, filename), 'w') - f.write(size * 'x') - f.close() + with open(os.path.join(mount_path, subdir, filename), 'w') as f: + f.write(size * 'x') """.format( mount_path=self.mount_a.mountpoint, size_unit=size_unit, @@ -835,7 +833,8 @@ class TestStrays(CephFSTestCase): path = os.path.join("{path}", "subdir") os.mkdir(path) for n in range(0, {file_count}): - open(os.path.join(path, "%s" % n), 'w').write("%s" % n) + with open(os.path.join(path, "%s" % n), 'w') as f: + f.write(str(n)) """.format( path=self.mount_a.mountpoint, file_count=LOW_LIMIT+1 @@ -852,7 +851,8 @@ class TestStrays(CephFSTestCase): path = os.path.join("{path}", "subdir2") os.mkdir(path) for n in range(0, {file_count}): - open(os.path.join(path, "%s" % n), 'w').write("%s" % n) + with open(os.path.join(path, "%s" % n), 'w') as f: + f.write(str(n)) dfd = os.open(path, os.O_DIRECTORY) os.fsync(dfd) """.format( @@ -875,7 +875,8 @@ class TestStrays(CephFSTestCase): import os path = os.path.join("{path}", "subdir2") for n in range({file_count}, ({file_count}*3)//2): - open(os.path.join(path, "%s" % n), 'w').write("%s" % n) + with open(os.path.join(path, "%s" % n), 'w') as f: + f.write(str(n)) """.format( path=self.mount_a.mountpoint, file_count=LOW_LIMIT @@ -890,9 +891,8 @@ class TestStrays(CephFSTestCase): os.mkdir(path) for n in range({file_count}): fpath = os.path.join(path, "%s" % n) - f = open(fpath, 'w') - f.write("%s" % n) - f.close() + with open(fpath, 'w') as f: + f.write(str(n)) os.unlink(fpath) """.format( path=self.mount_a.mountpoint, @@ -915,9 +915,8 @@ class TestStrays(CephFSTestCase): os.mkdir(path) for n in range({file_count}): fpath = os.path.join(path, "%s" % n) - f = open(fpath, 'w') - f.write("%s" % n) - f.close() + with open(fpath, 'w') as f: + f.write(str(n)) os.unlink(fpath) """.format( path=self.mount_a.mountpoint, -- 2.39.5