From 724febf5ca3fe7067cecbf9bd3444b02728e4f7b Mon Sep 17 00:00:00 2001 From: Anirudha Bose Date: Thu, 18 Aug 2016 18:45:58 +0530 Subject: [PATCH] qa/workunits: PEP8ify Signed-off-by: Anirudha Bose --- qa/workunits/fs/misc/filelock_deadlock.py | 25 +++-- qa/workunits/fs/misc/filelock_interrupt.py | 19 ++-- qa/workunits/mon/ping.py | 29 ++--- qa/workunits/mon/test_mon_config_key.py | 122 +++++++++++---------- qa/workunits/rest/test.py | 2 +- 5 files changed, 105 insertions(+), 92 deletions(-) diff --git a/qa/workunits/fs/misc/filelock_deadlock.py b/qa/workunits/fs/misc/filelock_deadlock.py index 32322ba66af..3ebc9777bd2 100755 --- a/qa/workunits/fs/misc/filelock_deadlock.py +++ b/qa/workunits/fs/misc/filelock_deadlock.py @@ -1,24 +1,26 @@ #!/usr/bin/python -import time -import os -import fcntl import errno +import fcntl +import os import signal import struct +import time + def handler(signum, frame): pass + def lock_two(f1, f2): lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 10, 0, 0) fcntl.fcntl(f1, fcntl.F_SETLKW, lockdata) time.sleep(10) # don't wait forever - signal.signal(signal.SIGALRM, handler); - signal.alarm(10); - exitcode = 0; + signal.signal(signal.SIGALRM, handler) + signal.alarm(10) + exitcode = 0 try: fcntl.fcntl(f2, fcntl.F_SETLKW, lockdata) except IOError as e: @@ -27,8 +29,9 @@ def lock_two(f1, f2): elif e.errno == errno.EINTR: exitcode = 2 else: - exitcode = 3; - os._exit(exitcode); + exitcode = 3 + os._exit(exitcode) + def main(): pid1 = os.fork() @@ -52,13 +55,13 @@ def main(): deadlk_count = 0 i = 0 while i < 3: - pid, status = os.wait(); + pid, status = os.wait() exitcode = status >> 8 if exitcode == 1: - deadlk_count = deadlk_count + 1; + deadlk_count += 1 elif exitcode != 0: raise RuntimeError("unexpect exit code of child") - i = i + 1 + i += 1 if deadlk_count != 1: raise RuntimeError("unexpect count of EDEADLK") diff --git a/qa/workunits/fs/misc/filelock_interrupt.py b/qa/workunits/fs/misc/filelock_interrupt.py index 3ce559e09c3..2a413a66e83 100755 --- a/qa/workunits/fs/misc/filelock_interrupt.py +++ b/qa/workunits/fs/misc/filelock_interrupt.py @@ -1,21 +1,22 @@ #!/usr/bin/python -import time -import fcntl import errno +import fcntl import signal import struct """ introduced by Linux 3.15 """ -fcntl.F_OFD_GETLK=36 -fcntl.F_OFD_SETLK=37 -fcntl.F_OFD_SETLKW=38 +fcntl.F_OFD_GETLK = 36 +fcntl.F_OFD_SETLK = 37 +fcntl.F_OFD_SETLKW = 38 + def handler(signum, frame): pass + def main(): f1 = open("testfile", 'w') f2 = open("testfile", 'w') @@ -25,8 +26,8 @@ def main(): """ is flock interruptable? """ - signal.signal(signal.SIGALRM, handler); - signal.alarm(5); + signal.signal(signal.SIGALRM, handler) + signal.alarm(5) try: fcntl.flock(f2, fcntl.LOCK_EX) except IOError as e: @@ -53,8 +54,8 @@ def main(): """ is poxis lock interruptable? """ - signal.signal(signal.SIGALRM, handler); - signal.alarm(5); + signal.signal(signal.SIGALRM, handler) + signal.alarm(5) try: lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0) fcntl.fcntl(f2, fcntl.F_OFD_SETLKW, lockdata) diff --git a/qa/workunits/mon/ping.py b/qa/workunits/mon/ping.py index 266667d0e39..1773c736931 100755 --- a/qa/workunits/mon/ping.py +++ b/qa/workunits/mon/ping.py @@ -18,8 +18,8 @@ class UnexpectedReturn(Exception): if isinstance(cmd, list): self.cmd = ' '.join(cmd) else: - 'cmd needs to be either a list or a str' assert isinstance(cmd, string) or isinstance(cmd, unicode), \ + 'cmd needs to be either a list or a str' self.cmd = cmd self.cmd = str(self.cmd) self.ret = int(ret) @@ -28,7 +28,8 @@ class UnexpectedReturn(Exception): def __str__(self): return repr('{c}: expected return {e}, got {r} ({o})'.format( - c=self.cmd, e=self.expected, r=self.ret, o=self.msg)) + c=self.cmd, e=self.expected, r=self.ret, o=self.msg)) + def call(cmd): if isinstance(cmd, list): @@ -40,12 +41,12 @@ def call(cmd): print('call: {0}'.format(args)) proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (procout,procerr) = proc.communicate(None) + procout, procerr = proc.communicate(None) - return (proc.returncode, procout, procerr) + return proc.returncode, procout, procerr -def expect(cmd, expected_ret): +def expect(cmd, expected_ret): try: (r, out, err) = call(cmd) except ValueError as e: @@ -67,8 +68,8 @@ def get_quorum_status(timeout=300): j = json.loads(out) return j -def main(): +def main(): quorum_status = get_quorum_status() mon_names = [mon['name'] for mon in quorum_status['monmap']['mons']] @@ -79,8 +80,8 @@ def main(): reply = json.loads(out) assert reply['mon_status']['name'] == m, \ - 'reply obtained from mon.{0}, expected mon.{1}'.format( - reply['mon_status']['name'], m) + 'reply obtained from mon.{0}, expected mon.{1}'.format( + reply['mon_status']['name'], m) print('test out-of-quorum reply') for m in mon_names: @@ -89,20 +90,20 @@ def main(): quorum_status = get_quorum_status() assert m not in quorum_status['quorum_names'], \ - 'mon.{0} was not supposed to be in quorum ({1})'.format( - m, quorum_status['quorum_names']) + 'mon.{0} was not supposed to be in quorum ({1})'.format( + m, quorum_status['quorum_names']) out = expect('ceph ping mon.{0}'.format(m), 0) reply = json.loads(out) mon_status = reply['mon_status'] assert mon_status['name'] == m, \ - 'reply obtained from mon.{0}, expected mon.{1}'.format( - mon_status['name'], m) + 'reply obtained from mon.{0}, expected mon.{1}'.format( + mon_status['name'], m) assert mon_status['state'] == 'electing', \ - 'mon.{0} is in state {1}, expected electing'.format( - m,mon_status['state']) + 'mon.{0} is in state {1}, expected electing'.format( + m, mon_status['state']) expect('ceph daemon mon.{0} quorum enter'.format(m), 0) diff --git a/qa/workunits/mon/test_mon_config_key.py b/qa/workunits/mon/test_mon_config_key.py index 20f64c412ba..9e7537a743c 100755 --- a/qa/workunits/mon/test_mon_config_key.py +++ b/qa/workunits/mon/test_mon_config_key.py @@ -9,17 +9,16 @@ # License version 2.1, as published by the Free Software # Foundation. See file COPYING. # -import sys -import os +import argparse import base64 -import time import errno +import logging +import os import random -import subprocess import string -import logging -import argparse - +import subprocess +import sys +import time # # Accepted Environment variables: @@ -34,7 +33,7 @@ import argparse # -LOG = logging.getLogger(os.path.basename(sys.argv[0].replace('.py',''))) +LOG = logging.getLogger(os.path.basename(sys.argv[0].replace('.py', ''))) SIZES = [ (0, 0), @@ -46,21 +45,22 @@ SIZES = [ (4096, 0), (4097, -errno.EFBIG), (8192, -errno.EFBIG) - ] +] OPS = { - 'put':['existing','new'], - 'del':['existing','enoent'], - 'exists':['existing','enoent'], - 'get':['existing','enoent'] - } + 'put': ['existing', 'new'], + 'del': ['existing', 'enoent'], + 'exists': ['existing', 'enoent'], + 'get': ['existing', 'enoent'] +} + +CONFIG_PUT = [] # list: keys +CONFIG_DEL = [] # list: keys +CONFIG_EXISTING = {} # map: key -> size -CONFIG_PUT = [] #list: keys -CONFIG_DEL = [] #list: keys -CONFIG_EXISTING = {} #map: key -> size def run_cmd(cmd, expects=0): - full_cmd = [ 'ceph', 'config-key' ] + cmd + full_cmd = ['ceph', 'config-key'] + cmd if expects < 0: expects = -expects @@ -69,14 +69,14 @@ def run_cmd(cmd, expects=0): cmdlog.debug('{fc}'.format(fc=' '.join(full_cmd))) proc = subprocess.Popen(full_cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) stdout = [] stderr = [] while True: try: - (out, err) = proc.communicate() + out, err = proc.communicate() if out is not None: stdout += out.decode().split('\n') cmdlog.debug('stdout: {s}'.format(s=out)) @@ -89,8 +89,8 @@ def run_cmd(cmd, expects=0): if ret != expects: cmdlog.error('cmd > {cmd}'.format(cmd=full_cmd)) - cmdlog.error('expected return \'{expected}\' got \'{got}\''.format( - expected=expects,got=ret)) + cmdlog.error("expected return '{expected}' got '{got}'".format( + expected=expects, got=ret)) cmdlog.error('stdout') for i in stdout: cmdlog.error('{x}'.format(x=i)) @@ -98,24 +98,29 @@ def run_cmd(cmd, expects=0): for i in stderr: cmdlog.error('{x}'.format(x=i)) -#end run_cmd + +# end run_cmd def gen_data(size, rnd): chars = string.ascii_letters + string.digits - return ''.join(rnd.choice(chars) for i in range(size)) + return ''.join(rnd.choice(chars) for _ in range(size)) + def gen_key(rnd): return gen_data(20, rnd) + def gen_tmp_file_path(rnd): file_name = gen_data(20, rnd) - file_path = os.path.join('/tmp', 'ceph-test.'+file_name) + file_path = os.path.join('/tmp', 'ceph-test.' + file_name) return file_path + def destroy_tmp_file(fpath): if os.path.exists(fpath) and os.path.isfile(fpath): os.unlink(fpath) + def write_data_file(data, rnd): file_path = gen_tmp_file_path(rnd) data_file = open(file_path, 'a+') @@ -123,44 +128,46 @@ def write_data_file(data, rnd): data_file.write(data) data_file.close() return file_path -#end write_data_file + + +# end write_data_file def choose_random_op(rnd): op = rnd.choice( list(OPS.keys()) ) sop = rnd.choice(OPS[op]) - return (op, sop) + return op, sop def parse_args(args): parser = argparse.ArgumentParser( - description='Test the monitor\'s \'config-key\' API', - ) + description="Test the monitor's 'config-key' API", + ) parser.add_argument( '-v', '--verbose', action='store_true', help='be more verbose', - ) + ) parser.add_argument( '-s', '--seed', metavar='SEED', help='use SEED instead of generating it in run-time', - ) + ) parser.add_argument( '-d', '--duration', metavar='SECS', help='run test for SECS seconds (default: 300)', - ) + ) parser.set_defaults( seed=None, duration=300, verbose=False, - ) + ) return parser.parse_args(args) -def main(): +def main(): args = parse_args(sys.argv[1:]) verbose = args.verbose @@ -178,7 +185,7 @@ def main(): if verbose: loglevel = logging.DEBUG - logging.basicConfig(level=loglevel,) + logging.basicConfig(level=loglevel) LOG.info('seed: {s}'.format(s=seed)) @@ -194,7 +201,7 @@ def main(): via_file = (rnd.uniform(0, 100) < 50.0) expected = 0 - cmd = [ 'put' ] + cmd = ['put'] key = None if sop == 'existing': @@ -203,10 +210,10 @@ def main(): continue key = rnd.choice(CONFIG_PUT) assert key in CONFIG_EXISTING, \ - 'key \'{k_}\' not in CONFIG_EXISTING'.format(k_=key) + "key '{k_}' not in CONFIG_EXISTING".format(k_=key) - expected = 0 # the store just overrides the value if the key exists - #end if sop == 'existing' + expected = 0 # the store just overrides the value if the key exists + # end if sop == 'existing' elif sop == 'new': for x in range(0, 10): key = gen_key(rnd) @@ -223,14 +230,15 @@ def main(): assert key is not None, \ 'key must be != None' - cmd += [ key ] + cmd += [key] (size, error) = rnd.choice(SIZES) if size > 25: via_file = True data = gen_data(size, rnd) - if error == 0: # only add if we expect the put to be successful + + if error == 0: # only add if we expect the put to be successful if sop == 'new': CONFIG_PUT.append(key) CONFIG_EXISTING[key] = size @@ -238,14 +246,14 @@ def main(): if via_file: data_file = write_data_file(data, rnd) - cmd += [ '-i', data_file ] + cmd += ['-i', data_file] else: - cmd += [ data ] + cmd += [data] op_log.debug('size: {sz}, via: {v}'.format( sz=size, v='file: {f}'.format(f=data_file) if via_file == True else 'cli') - ) + ) run_cmd(cmd, expects=expected) if via_file: destroy_tmp_file(data_file) @@ -253,7 +261,7 @@ def main(): elif op == 'del': expected = 0 - cmd = [ 'del' ] + cmd = ['del'] key = None if sop == 'existing': @@ -262,7 +270,7 @@ def main(): continue key = rnd.choice(CONFIG_PUT) assert key in CONFIG_EXISTING, \ - 'key \'{k_}\' not in CONFIG_EXISTING'.format(k_=key) + "key '{k_}' not in CONFIG_EXISTING".format(k_=key) if sop == 'enoent': for x in range(0, 10): @@ -280,7 +288,7 @@ def main(): assert key is not None, \ 'key must be != None' - cmd += [ key ] + cmd += [key] op_log.debug('key: {k}'.format(k=key)) run_cmd(cmd, expects=expected) if sop == 'existing': @@ -291,7 +299,7 @@ def main(): elif op == 'exists': expected = 0 - cmd = [ 'exists' ] + cmd = ['exists'] key = None if sop == 'existing': @@ -300,7 +308,7 @@ def main(): continue key = rnd.choice(CONFIG_PUT) assert key in CONFIG_EXISTING, \ - 'key \'{k_}\' not in CONFIG_EXISTING'.format(k_=key) + "key '{k_}' not in CONFIG_EXISTING".format(k_=key) if sop == 'enoent': for x in range(0, 10): @@ -318,14 +326,14 @@ def main(): assert key is not None, \ 'key must be != None' - cmd += [ key ] + cmd += [key] op_log.debug('key: {k}'.format(k=key)) run_cmd(cmd, expects=expected) continue elif op == 'get': expected = 0 - cmd = [ 'get' ] + cmd = ['get'] key = None if sop == 'existing': @@ -334,7 +342,7 @@ def main(): continue key = rnd.choice(CONFIG_PUT) assert key in CONFIG_EXISTING, \ - 'key \'{k_}\' not in CONFIG_EXISTING'.format(k_=key) + "key '{k_}' not in CONFIG_EXISTING".format(k_=key) if sop == 'enoent': for x in range(0, 10): @@ -353,7 +361,7 @@ def main(): 'key must be != None' file_path = gen_tmp_file_path(rnd) - cmd += [ key, '-o', file_path ] + cmd += [key, '-o', file_path] op_log.debug('key: {k}'.format(k=key)) run_cmd(cmd, expects=expected) if sop == 'existing': @@ -362,7 +370,7 @@ def main(): except IOError as err: if err.errno == errno.ENOENT: assert CONFIG_EXISTING[key] == 0, \ - 'error opening \'{fp}\': {e}'.format(fp=file_path,e=err) + "error opening '{fp}': {e}".format(fp=file_path, e=err) continue else: assert False, \ @@ -374,8 +382,8 @@ def main(): break cnt += len(read_data) assert cnt == CONFIG_EXISTING[key], \ - 'wrong size from store for key \'{k}\': {sz}, expected {es}'.format( - k=key,sz=cnt,es=CONFIG_EXISTING[key]) + "wrong size from store for key '{k}': {sz}, expected {es}".format( + k=key, sz=cnt, es=CONFIG_EXISTING[key]) destroy_tmp_file(file_path) continue else: diff --git a/qa/workunits/rest/test.py b/qa/workunits/rest/test.py index 649e39005fa..f12cf2be135 100755 --- a/qa/workunits/rest/test.py +++ b/qa/workunits/rest/test.py @@ -29,6 +29,7 @@ def expect(url, method, respcode, contenttype, extra_hdrs=None, data=None): fail(r, failmsg) return r + def expect_nofail(url, method, respcode, contenttype, extra_hdrs=None, data=None): @@ -293,7 +294,6 @@ if __name__ == '__main__': r = expect('osd/ls', 'GET', 200, 'xml', XMLHDR) assert(r.tree.find('output/osds/osd') is not None) - expect('osd/pause', 'PUT', 200, '') r = expect('osd/dump', 'GET', 200, 'json', JSONHDR) assert('pauserd,pausewr' in r.myjson['output']['flags']) -- 2.47.3