Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 compatibility for workunits #10815

Merged
merged 11 commits into from Sep 19, 2016
5 changes: 2 additions & 3 deletions qa/workunits/ceph-disk/ceph-disk-test.py
Expand Up @@ -64,7 +64,7 @@ def __init__(self):
self.conf = configobj.ConfigObj('/etc/ceph/ceph.conf')

def save_conf(self):
self.conf.write(open('/etc/ceph/ceph.conf', 'w'))
self.conf.write(open('/etc/ceph/ceph.conf', 'wb'))

@staticmethod
def helper(command):
Expand Down Expand Up @@ -97,8 +97,7 @@ def sh(command):
return "".join(lines)

def unused_disks(self, pattern='[vs]d.'):
names = filter(
lambda x: re.match(pattern, x), os.listdir("/sys/block"))
names = [x for x in os.listdir("/sys/block") if re.match(pattern, x)]
if not names:
return []
disks = json.loads(
Expand Down
11 changes: 7 additions & 4 deletions qa/workunits/ceph-disk/ceph-disk.sh
Expand Up @@ -24,16 +24,19 @@ perl -pi -e 's|pid file.*|pid file = /var/run/ceph/\$cluster-\$name.pid|' /etc/c

PATH=$(dirname $0):$(dirname $0)/..:$PATH

if ! which py.test > /dev/null; then
echo "py.test not installed"
: ${PYTHON:=python}
PY_VERSION=$($PYTHON --version 2>&1)

if ! ${PYTHON} -m pytest --version > /dev/null 2>&1; then
echo "py.test not installed for ${PY_VERSION}"
exit 1
fi

sudo env PATH=$(dirname $0):$(dirname $0)/..:$PATH py.test -s -v $(dirname $0)/ceph-disk-test.py
sudo env PATH=$(dirname $0):$(dirname $0)/..:$PATH ${PYTHON} -m pytest -s -v $(dirname $0)/ceph-disk-test.py
result=$?

# own whatever was created as a side effect of the py.test run
# so that it can successfully be removed later on by a non privileged
# so that it can successfully be removed later on by a non privileged
# process
sudo chown -R $(id -u) $(dirname $0)
exit $result
25 changes: 14 additions & 11 deletions qa/workunits/fs/misc/direct_io.py
@@ -1,29 +1,31 @@
#!/usr/bin/python

import os
import json
import mmap
import os
import subprocess
import json


def get_data_pool():
cmd = ['ceph', 'fs', 'ls', '--format=json-pretty']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out = proc.communicate()[0]
return json.loads(out)[0]['data_pools'][0]


def main():
fd = os.open("testfile", os.O_RDWR | os.O_CREAT | os.O_TRUNC | os.O_DIRECT, 0644)
fd = os.open("testfile", os.O_RDWR | os.O_CREAT | os.O_TRUNC | os.O_DIRECT, 0o644)

ino = os.fstat(fd).st_ino
obj_name = "{ino:x}.00000000".format(ino=ino)
pool_name = get_data_pool()

buf = mmap.mmap(-1, 1);
buf.write('1');
os.write(fd, buf);
buf = mmap.mmap(-1, 1)
buf.write('1')
os.write(fd, buf)

proc = subprocess.Popen(['rados', '-p', pool_name, 'get', obj_name, 'tmpfile'])
proc.wait();
proc.wait()

with open('tmpfile', 'r') as tmpf:
out = tmpf.read()
Expand All @@ -34,14 +36,15 @@ def main():
tmpf.write('2')

proc = subprocess.Popen(['rados', '-p', pool_name, 'put', obj_name, 'tmpfile'])
proc.wait();
proc.wait()

os.lseek(fd, 0, os.SEEK_SET)
out = os.read(fd, 1);
out = os.read(fd, 1)
if out != '2':
raise RuntimeError("data were not directly read from object store")

os.close(fd);
print 'ok'
os.close(fd)
print('ok')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need

from __future__ import print_function

for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tchaikov Since the print statement is a very simple one, we don't need that line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oic, print() is introduced in py2.6, and we've bumped the supported python version to >= 2.6



main()
30 changes: 17 additions & 13 deletions qa/workunits/fs/misc/filelock_deadlock.py
@@ -1,34 +1,37 @@
#!/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, e:
except IOError as e:
if e.errno == errno.EDEADLK:
exitcode = 1
elif e.errno == errno.EINTR:
exitcode = 2
else:
exitcode = 3;
os._exit(exitcode);
exitcode = 3
os._exit(exitcode)


def main():
pid1 = os.fork()
Expand All @@ -52,17 +55,18 @@ 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")

print 'ok'
print('ok')


main()
32 changes: 17 additions & 15 deletions 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')
Expand All @@ -25,11 +26,11 @@ 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, e:
except IOError as e:
if e.errno != errno.EINTR:
raise
else:
Expand All @@ -40,11 +41,11 @@ def main():
lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 10, 0, 0)
try:
fcntl.fcntl(f1, fcntl.F_OFD_SETLK, lockdata)
except IOError, e:
except IOError as e:
if e.errno != errno.EINVAL:
raise
else:
print 'kernel does not support fcntl.F_OFD_SETLK'
print('kernel does not support fcntl.F_OFD_SETLK')
return

lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 10, 10, 0, 0)
Expand All @@ -53,12 +54,12 @@ 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)
except IOError, e:
except IOError as e:
if e.errno != errno.EINTR:
raise
else:
Expand All @@ -70,7 +71,7 @@ def main():
try:
lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 10, 10, 0, 0)
fcntl.fcntl(f1, fcntl.F_OFD_SETLK, lockdata)
except IOError, e:
except IOError as e:
if e.errno == errno.EAGAIN:
pass
else:
Expand All @@ -80,6 +81,7 @@ def main():
fcntl.fcntl(f1, fcntl.F_OFD_SETLK, lockdata)
fcntl.fcntl(f2, fcntl.F_OFD_SETLK, lockdata)

print 'ok'
print('ok')


main()
16 changes: 8 additions & 8 deletions qa/workunits/fs/multiclient_sync_read_eof.py
Expand Up @@ -12,33 +12,33 @@ def main():
parser.add_argument('fn')
args = parser.parse_args()

file(os.path.join(args.mnt1, args.fn), 'w')
f1 = file(os.path.join(args.mnt1, args.fn), 'r+')
f2 = file(os.path.join(args.mnt2, args.fn), 'r+')
open(os.path.join(args.mnt1, args.fn), 'w')
f1 = open(os.path.join(args.mnt1, args.fn), 'r+')
f2 = open(os.path.join(args.mnt2, args.fn), 'r+')

f1.write('foo')
f1.flush()
a = f2.read(3)
print 'got "%s"' % a
print('got "%s"' % a)
assert a == 'foo'
f2.write('bar')
f2.flush()
a = f1.read(3)
print 'got "%s"' % a
print('got "%s"' % a)
assert a == 'bar'

## test short reads
f1.write('short')
f1.flush()
a = f2.read(100)
print 'got "%s"' % a
print('got "%s"' % a)
assert a == 'short'
f2.write('longer')
f2.flush()
a = f1.read(1000)
print 'got "%s"' % a
print('got "%s"' % a)
assert a == 'longer'

print 'ok'
print('ok')

main()