Commit 00ed1eeb authored by Andrei Vagin's avatar Andrei Vagin Committed by Pavel Emelyanov

test: make zdtm.py python2/python3 compatible

Cc: Adrian Reber <areber@redhat.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent a6748591
...@@ -8,8 +8,8 @@ def create_fds(): ...@@ -8,8 +8,8 @@ def create_fds():
raise Exception("Unable to mount tmpfs") raise Exception("Unable to mount tmpfs")
tfifo = os.path.join(tdir, "test_fifo") tfifo = os.path.join(tdir, "test_fifo")
os.mkfifo(tfifo) os.mkfifo(tfifo)
fd2 = open(tfifo, "w+") fd2 = open(tfifo, "w+b", buffering=0)
fd1 = open(tfifo, "r") fd1 = open(tfifo, "rb")
os.system("umount -l %s" % tdir) os.system("umount -l %s" % tdir)
os.rmdir(tdir) os.rmdir(tdir)
......
...@@ -2,7 +2,7 @@ import os ...@@ -2,7 +2,7 @@ import os
def create_fds(): def create_fds():
(fd1, fd2) = os.pipe() (fd1, fd2) = os.pipe()
return (os.fdopen(fd2, "w"), os.fdopen(fd1, "r")) return (os.fdopen(fd2, "wb"), os.fdopen(fd1, "rb"))
def filename(pipef): def filename(pipef):
return 'pipe:[%d]' % os.fstat(pipef.fileno()).st_ino return 'pipe:[%d]' % os.fstat(pipef.fileno()).st_ino
......
...@@ -3,7 +3,7 @@ import os ...@@ -3,7 +3,7 @@ import os
def create_fds(): def create_fds():
(sk1, sk2) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM) (sk1, sk2) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)
return (sk1.makefile("w"), sk2.makefile("r")) return (sk1.makefile("wb"), sk2.makefile("rb"))
def __sock_ino(sockf): def __sock_ino(sockf):
return os.fstat(sockf.fileno()).st_ino return os.fstat(sockf.fileno()).st_ino
......
...@@ -6,7 +6,7 @@ def child_prep(fd): ...@@ -6,7 +6,7 @@ def child_prep(fd):
def create_fds(): def create_fds():
(fd1, fd2) = pty.openpty() (fd1, fd2) = pty.openpty()
return (os.fdopen(fd2, "w"), os.fdopen(fd1, "r")) return (os.fdopen(fd2, "wb"), os.fdopen(fd1, "rb"))
def filename(pipef): def filename(pipef):
st = os.fstat(pipef.fileno()) st = os.fstat(pipef.fileno())
......
#!/usr/bin/env python2 #!/usr/bin/env python
# vim: noet ts=8 sw=8 sts=8 # vim: noet ts=8 sw=8 sts=8
from __future__ import absolute_import, division, print_function, unicode_literals
from builtins import (str, open, range, zip, int)
import argparse import argparse
import glob import glob
import os import os
...@@ -35,10 +38,10 @@ def traceit(f, e, a): ...@@ -35,10 +38,10 @@ def traceit(f, e, a):
global prev_line global prev_line
line = linecache.getline(fil, lineno) line = linecache.getline(fil, lineno)
if line == prev_line: if line == prev_line:
print " ..." print(" ...")
else: else:
prev_line = line prev_line = line
print "+%4d: %s" % (lineno, line.rstrip()) print("+%4d: %s" % (lineno, line.rstrip()))
return traceit return traceit
...@@ -120,7 +123,7 @@ def check_core_files(): ...@@ -120,7 +123,7 @@ def check_core_files():
for i in reports: for i in reports:
add_to_report(i, os.path.basename(i)) add_to_report(i, os.path.basename(i))
print_sep(i) print_sep(i)
print open(i).read() print(open(i).read())
print_sep(i) print_sep(i)
return True return True
...@@ -171,7 +174,7 @@ class ns_flavor: ...@@ -171,7 +174,7 @@ class ns_flavor:
# run in parallel # run in parallel
try: try:
os.makedirs(self.root + os.path.dirname(fname)) os.makedirs(self.root + os.path.dirname(fname))
except OSError, e: except OSError as e:
if e.errno != errno.EEXIST: if e.errno != errno.EEXIST:
raise raise
dst = tempfile.mktemp(".tso", "", self.root + os.path.dirname(fname)) dst = tempfile.mktemp(".tso", "", self.root + os.path.dirname(fname))
...@@ -185,10 +188,11 @@ class ns_flavor: ...@@ -185,10 +188,11 @@ class ns_flavor:
# This Mayakovsky-style code gets list of libraries a binary # This Mayakovsky-style code gets list of libraries a binary
# needs minus vdso and gate .so-s # needs minus vdso and gate .so-s
libs = map(lambda x: x[1] == '=>' and x[2] or x[0], libs = map(lambda x: x[1] == '=>' and x[2] or x[0],
map(lambda x: x.split(), map(lambda x: str(x).split(),
filter(lambda x: not xl.match(x), filter(lambda x: not xl.match(x),
map(lambda x: x.strip(), map(lambda x: str(x).strip(),
filter(lambda x: x.startswith('\t'), ldd.stdout.readlines()))))) filter(lambda x: str(x).startswith('\t'), ldd.stdout.read().decode('ascii').splitlines())))))
ldd.wait() ldd.wait()
for lib in libs: for lib in libs:
...@@ -200,19 +204,19 @@ class ns_flavor: ...@@ -200,19 +204,19 @@ class ns_flavor:
name = "/dev/" + name name = "/dev/" + name
if not rdev: if not rdev:
if not os.access(name, os.F_OK): if not os.access(name, os.F_OK):
print "Skipping %s at root" % name print("Skipping %s at root" % name)
return return
else: else:
rdev = os.stat(name).st_rdev rdev = os.stat(name).st_rdev
name = self.root + name name = self.root + name
os.mknod(name, stat.S_IFCHR, rdev) os.mknod(name, stat.S_IFCHR, rdev)
os.chmod(name, 0666) os.chmod(name, 0o666)
def __construct_root(self): def __construct_root(self):
for dir in self.__root_dirs: for dir in self.__root_dirs:
os.mkdir(self.root + dir) os.mkdir(self.root + dir)
os.chmod(self.root + dir, 0777) os.chmod(self.root + dir, 0o777)
for ldir in ["/bin", "/sbin", "/lib", "/lib64"]: for ldir in ["/bin", "/sbin", "/lib", "/lib64"]:
os.symlink(".." + ldir, self.root + "/usr" + ldir) os.symlink(".." + ldir, self.root + "/usr" + ldir)
...@@ -239,9 +243,9 @@ class ns_flavor: ...@@ -239,9 +243,9 @@ class ns_flavor:
with open(os.path.abspath(__file__)) as o: with open(os.path.abspath(__file__)) as o:
fcntl.flock(o, fcntl.LOCK_EX) fcntl.flock(o, fcntl.LOCK_EX)
if not os.access(self.root + "/.constructed", os.F_OK): if not os.access(self.root + "/.constructed", os.F_OK):
print "Construct root for %s" % l_bins[0] print("Construct root for %s" % l_bins[0])
self.__construct_root() self.__construct_root()
os.mknod(self.root + "/.constructed", stat.S_IFREG | 0600) os.mknod(self.root + "/.constructed", stat.S_IFREG | 0o600)
for b in l_bins: for b in l_bins:
self.__copy_libs(b) self.__copy_libs(b)
...@@ -257,7 +261,7 @@ class ns_flavor: ...@@ -257,7 +261,7 @@ class ns_flavor:
def clean(): def clean():
for d in ns_flavor.__root_dirs: for d in ns_flavor.__root_dirs:
p = './' + d p = './' + d
print 'Remove %s' % p print('Remove %s' % p)
if os.access(p, os.F_OK): if os.access(p, os.F_OK):
shutil.rmtree('./' + d) shutil.rmtree('./' + d)
...@@ -273,7 +277,7 @@ class userns_flavor(ns_flavor): ...@@ -273,7 +277,7 @@ class userns_flavor(ns_flavor):
def init(self, l_bins, x_bins): def init(self, l_bins, x_bins):
# To be able to create roots_yard in CRIU # To be able to create roots_yard in CRIU
os.chmod(".", os.stat(".").st_mode | 0077) os.chmod(".", os.stat(".").st_mode | 0o077)
ns_flavor.init(self, l_bins, x_bins) ns_flavor.init(self, l_bins, x_bins)
@staticmethod @staticmethod
...@@ -282,7 +286,7 @@ class userns_flavor(ns_flavor): ...@@ -282,7 +286,7 @@ class userns_flavor(ns_flavor):
flavors = {'h': host_flavor, 'ns': ns_flavor, 'uns': userns_flavor} flavors = {'h': host_flavor, 'ns': ns_flavor, 'uns': userns_flavor}
flavors_codes = dict(zip(xrange(len(flavors)), sorted(flavors.keys()))) flavors_codes = dict(zip(range(len(flavors)), sorted(flavors.keys())))
# #
# Helpers # Helpers
...@@ -302,7 +306,7 @@ def tail(path): ...@@ -302,7 +306,7 @@ def tail(path):
stdout = subprocess.PIPE) stdout = subprocess.PIPE)
out = p.stdout.readline() out = p.stdout.readline()
p.wait() p.wait()
return out return out.decode()
def rpidfile(path): def rpidfile(path):
...@@ -314,12 +318,12 @@ def wait_pid_die(pid, who, tmo = 30): ...@@ -314,12 +318,12 @@ def wait_pid_die(pid, who, tmo = 30):
while stime < tmo: while stime < tmo:
try: try:
os.kill(int(pid), 0) os.kill(int(pid), 0)
except OSError, e: except OSError as e:
if e.errno != errno.ESRCH: if e.errno != errno.ESRCH:
print e print(e)
break break
print "Wait for %s(%d) to die for %f" % (who, pid, stime) print("Wait for %s(%d) to die for %f" % (who, pid, stime))
time.sleep(stime) time.sleep(stime)
stime *= 2 stime *= 2
else: else:
...@@ -338,12 +342,12 @@ def test_flag(tdesc, flag): ...@@ -338,12 +342,12 @@ def test_flag(tdesc, flag):
# #
class test_fail_exc: class test_fail_exc(Exception):
def __init__(self, step): def __init__(self, step):
self.step = step self.step = step
class test_fail_expected_exc: class test_fail_expected_exc(Exception):
def __init__(self, cr_action): def __init__(self, cr_action):
self.cr_action = cr_action self.cr_action = cr_action
...@@ -396,12 +400,12 @@ class zdtm_test: ...@@ -396,12 +400,12 @@ class zdtm_test:
# Add write perms for .out and .pid files # Add write perms for .out and .pid files
for b in self._bins: for b in self._bins:
p = os.path.dirname(b) p = os.path.dirname(b)
os.chmod(p, os.stat(p).st_mode | 0222) os.chmod(p, os.stat(p).st_mode | 0o222)
def start(self): def start(self):
self.__flavor.init(self._bins, self._deps) self.__flavor.init(self._bins, self._deps)
print "Start test" print("Start test")
env = self._env env = self._env
if not self.__freezer.kernel: if not self.__freezer.kernel:
...@@ -414,7 +418,7 @@ class zdtm_test: ...@@ -414,7 +418,7 @@ class zdtm_test:
env['ZDTM_GROUPS'] = "27495 48244" env['ZDTM_GROUPS'] = "27495 48244"
self.__add_wperms() self.__add_wperms()
else: else:
print "Test is SUID" print("Test is SUID")
if self.__flavor.ns: if self.__flavor.ns:
env['ZDTM_NEWNS'] = "1" env['ZDTM_NEWNS'] = "1"
...@@ -435,7 +439,7 @@ class zdtm_test: ...@@ -435,7 +439,7 @@ class zdtm_test:
try: try:
os.kill(int(self.getpid()), 0) os.kill(int(self.getpid()), 0)
except Exception, e: except Exception as e:
raise test_fail_exc("start: %s" % e) raise test_fail_exc("start: %s" % e)
if not self.static(): if not self.static():
...@@ -446,7 +450,7 @@ class zdtm_test: ...@@ -446,7 +450,7 @@ class zdtm_test:
def kill(self, sig = signal.SIGKILL): def kill(self, sig = signal.SIGKILL):
self.__freezer.thaw() self.__freezer.thaw()
if self.__pid: if self.__pid:
print "Send the %d signal to %s" % (sig, self.__pid) print("Send the %d signal to %s" % (sig, self.__pid))
os.kill(int(self.__pid), sig) os.kill(int(self.__pid), sig)
self.gone(sig == signal.SIGKILL) self.gone(sig == signal.SIGKILL)
...@@ -458,12 +462,12 @@ class zdtm_test: ...@@ -458,12 +462,12 @@ class zdtm_test:
self.kill(signal.SIGTERM) self.kill(signal.SIGTERM)
res = tail(self.__name + '.out') res = tail(self.__name + '.out')
if 'PASS' not in res.split(): if 'PASS' not in list(map(lambda s: s.strip(), res.split())):
if os.access(self.__name + '.out.inprogress', os.F_OK): if os.access(self.__name + '.out.inprogress', os.F_OK):
print_sep(self.__name + '.out.inprogress') print_sep(self.__name + '.out.inprogress')
print open(self.__name + '.out.inprogress').read() print(open(self.__name + '.out.inprogress').read())
print_sep(self.__name + '.out.inprogress') print_sep(self.__name + '.out.inprogress')
raise test_fail_exc("result check") raise test_fail_exc("result check: %s" % list(map(lambda s: s.strip(), res.split())) + res)
def getpid(self): def getpid(self):
if self.__pid == 0: if self.__pid == 0:
...@@ -505,9 +509,9 @@ class zdtm_test: ...@@ -505,9 +509,9 @@ class zdtm_test:
def print_output(self): def print_output(self):
if os.access(self.__name + '.out', os.R_OK): if os.access(self.__name + '.out', os.R_OK):
print "Test output: " + "=" * 32 print("Test output: " + "=" * 32)
print open(self.__name + '.out').read() print(open(self.__name + '.out').read())
print " <<< " + "=" * 32 print(" <<< " + "=" * 32)
def static(self): def static(self):
return self.__name.split('/')[1] == 'static' return self.__name.split('/')[1] == 'static'
...@@ -532,7 +536,7 @@ class zdtm_test: ...@@ -532,7 +536,7 @@ class zdtm_test:
class inhfd_test: class inhfd_test:
def __init__(self, name, desc, flavor, freezer): def __init__(self, name, desc, flavor, freezer):
self.__name = os.path.basename(name) self.__name = os.path.basename(name)
print "Load %s" % name print("Load %s" % name)
self.__fdtyp = imp.load_source(self.__name, name) self.__fdtyp = imp.load_source(self.__name, name)
self.__my_file = None self.__my_file = None
self.__peer_pid = 0 self.__peer_pid = 0
...@@ -565,8 +569,8 @@ class inhfd_test: ...@@ -565,8 +569,8 @@ class inhfd_test:
os.close(start_pipe[1]) os.close(start_pipe[1])
try: try:
data = peer_file.read(16) data = peer_file.read(16)
except Exception, e: except Exception as e:
print "Unable to read a peer file: %s" % e print("Unable to read a peer file: %s" % e)
sys.exit(1) sys.exit(1)
sys.exit(data == self.__message and 42 or 2) sys.exit(data == self.__message and 42 or 2)
...@@ -628,7 +632,7 @@ class groups_test(zdtm_test): ...@@ -628,7 +632,7 @@ class groups_test(zdtm_test):
if flavor.ns: if flavor.ns:
self.__real_name = name self.__real_name = name
self.__subs = map(lambda x: x.strip(), open(name).readlines()) self.__subs = map(lambda x: x.strip(), open(name).readlines())
print "Subs:\n%s" % '\n'.join(self.__subs) print("Subs:\n%s" % '\n'.join(self.__subs))
else: else:
self.__real_name = '' self.__real_name = ''
self.__subs = [] self.__subs = []
...@@ -688,7 +692,7 @@ class criu_cli: ...@@ -688,7 +692,7 @@ class criu_cli:
env = dict(os.environ, ASAN_OPTIONS = "log_path=asan.log:disable_coredump=0:detect_leaks=0") env = dict(os.environ, ASAN_OPTIONS = "log_path=asan.log:disable_coredump=0:detect_leaks=0")
if fault: if fault:
print "Forcing %s fault" % fault print("Forcing %s fault" % fault)
env['CRIU_FAULT'] = fault env['CRIU_FAULT'] = fault
cr = subprocess.Popen(strace + [criu_bin, action] + args, env = env, preexec_fn = preexec) cr = subprocess.Popen(strace + [criu_bin, action] + args, env = env, preexec_fn = preexec)
...@@ -799,8 +803,8 @@ class criu_rpc: ...@@ -799,8 +803,8 @@ class criu_rpc:
p.criu = criu p.criu = criu
else: else:
raise test_fail_exc('RPC for %s required' % action) raise test_fail_exc('RPC for %s required' % action)
except crpc.CRIUExceptionExternal, e: except crpc.CRIUExceptionExternal as e:
print "Fail", e print("Fail", e)
ret = -1 ret = -1
else: else:
ret = 0 ret = 0
...@@ -872,7 +876,7 @@ class criu: ...@@ -872,7 +876,7 @@ class criu:
self.__test = test self.__test = test
self.__dump_path = "dump/" + test.getname() + "/" + test.getpid() self.__dump_path = "dump/" + test.getname() + "/" + test.getpid()
if os.path.exists(self.__dump_path): if os.path.exists(self.__dump_path):
for i in xrange(100): for i in range(100):
newpath = self.__dump_path + "." + str(i) newpath = self.__dump_path + "." + str(i)
if not os.path.exists(newpath): if not os.path.exists(newpath):
os.rename(self.__dump_path, newpath) os.rename(self.__dump_path, newpath)
...@@ -884,7 +888,7 @@ class criu: ...@@ -884,7 +888,7 @@ class criu:
def cleanup(self): def cleanup(self):
if self.__dump_path: if self.__dump_path:
print "Removing %s" % self.__dump_path print("Removing %s" % self.__dump_path)
shutil.rmtree(self.__dump_path) shutil.rmtree(self.__dump_path)
def __ddir(self): def __ddir(self):
...@@ -903,7 +907,8 @@ class criu: ...@@ -903,7 +907,8 @@ class criu:
with open(os.path.join(self.__ddir(), action + '.cropt'), 'w') as f: with open(os.path.join(self.__ddir(), action + '.cropt'), 'w') as f:
f.write(' '.join(s_args) + '\n') f.write(' '.join(s_args) + '\n')
print "Run criu " + action
print("Run criu " + action)
strace = [] strace = []
if self.__sat: if self.__sat:
...@@ -949,7 +954,7 @@ class criu: ...@@ -949,7 +954,7 @@ class criu:
# create a clean directory for images # create a clean directory for images
os.rename(__ddir, __ddir + ".fail") os.rename(__ddir, __ddir + ".fail")
os.mkdir(__ddir) os.mkdir(__ddir)
os.chmod(__ddir, 0777) os.chmod(__ddir, 0o777)
else: else:
# on restore we move only a log file, because we need images # on restore we move only a log file, because we need images
os.rename(os.path.join(__ddir, log), os.path.join(__ddir, log + ".fail")) os.rename(os.path.join(__ddir, log), os.path.join(__ddir, log + ".fail"))
...@@ -957,7 +962,7 @@ class criu: ...@@ -957,7 +962,7 @@ class criu:
# PID of one of restored processes. # PID of one of restored processes.
open("/proc/sys/kernel/ns_last_pid", "w+").write(ns_last_pid) open("/proc/sys/kernel/ns_last_pid", "w+").write(ns_last_pid)
# try again without faults # try again without faults
print "Run criu " + action print("Run criu " + action)
ret = self.__criu.run(action, s_args, False, strace, preexec) ret = self.__criu.run(action, s_args, False, strace, preexec)
grep_errors(os.path.join(__ddir, log)) grep_errors(os.path.join(__ddir, log))
if ret == 0: if ret == 0:
...@@ -979,7 +984,7 @@ class criu: ...@@ -979,7 +984,7 @@ class criu:
def dump(self, action, opts = []): def dump(self, action, opts = []):
self.__iter += 1 self.__iter += 1
os.mkdir(self.__ddir()) os.mkdir(self.__ddir())
os.chmod(self.__ddir(), 0777) os.chmod(self.__ddir(), 0o777)
a_opts = ["-t", self.__test.getpid()] a_opts = ["-t", self.__test.getpid()]
if self.__prev_dump_iter: if self.__prev_dump_iter:
...@@ -987,7 +992,7 @@ class criu: ...@@ -987,7 +992,7 @@ class criu:
self.__prev_dump_iter = self.__iter self.__prev_dump_iter = self.__iter
if self.__page_server: if self.__page_server:
print "Adding page server" print("Adding page server")
ps_opts = ["--port", "12345"] ps_opts = ["--port", "12345"]
if self.__dedup: if self.__dedup:
...@@ -1081,23 +1086,23 @@ class criu: ...@@ -1081,23 +1086,23 @@ class criu:
@staticmethod @staticmethod
def available(): def available():
if not os.access(criu_bin, os.X_OK): if not os.access(criu_bin, os.X_OK):
print "CRIU binary not built" print("CRIU binary not built")
sys.exit(1) sys.exit(1)
def kill(self): def kill(self):
if self.__lazy_pages_p: if self.__lazy_pages_p:
self.__lazy_pages_p.terminate() self.__lazy_pages_p.terminate()
print "criu lazy-pages exited with %s" % self.__lazy_pages_p.wait() print("criu lazy-pages exited with %s" % self.__lazy_pages_p.wait())
grep_errors(os.path.join(self.__ddir(), "lazy-pages.log")) grep_errors(os.path.join(self.__ddir(), "lazy-pages.log"))
self.__lazy_pages_p = None self.__lazy_pages_p = None
if self.__page_server_p: if self.__page_server_p:
self.__page_server_p.terminate() self.__page_server_p.terminate()
print "criu page-server exited with %s" % self.__page_server_p.wait() print("criu page-server exited with %s" % self.__page_server_p.wait())
grep_errors(os.path.join(self.__ddir(), "page-server.log")) grep_errors(os.path.join(self.__ddir(), "page-server.log"))
self.__page_server_p = None self.__page_server_p = None
if self.__dump_process: if self.__dump_process:
self.__dump_process.terminate() self.__dump_process.terminate()
print "criu dump exited with %s" % self.__dump_process.wait() print("criu dump exited with %s" % self.__dump_process.wait())
grep_errors(os.path.join(self.__ddir(), "dump.log")) grep_errors(os.path.join(self.__ddir(), "dump.log"))
self.__dump_process = None self.__dump_process = None
...@@ -1105,7 +1110,7 @@ class criu: ...@@ -1105,7 +1110,7 @@ class criu:
def try_run_hook(test, args): def try_run_hook(test, args):
hname = test.getname() + '.hook' hname = test.getname() + '.hook'
if os.access(hname, os.X_OK): if os.access(hname, os.X_OK):
print "Running %s(%s)" % (hname, ', '.join(args)) print("Running %s(%s)" % (hname, ', '.join(args)))
hook = subprocess.Popen([hname] + args) hook = subprocess.Popen([hname] + args)
if hook.wait() != 0: if hook.wait() != 0:
raise test_fail_exc("hook " + " ".join(args)) raise test_fail_exc("hook " + " ".join(args))
...@@ -1123,7 +1128,7 @@ def init_sbs(): ...@@ -1123,7 +1128,7 @@ def init_sbs():
global do_sbs global do_sbs
do_sbs = True do_sbs = True
else: else:
print "Can't do step-by-step in this runtime" print("Can't do step-by-step in this runtime")
def sbs(what): def sbs(what):
...@@ -1136,7 +1141,7 @@ def sbs(what): ...@@ -1136,7 +1141,7 @@ def sbs(what):
# #
def iter_parm(opt, dflt): def iter_parm(opt, dflt):
x = ((opt or str(dflt)) + ":0").split(':') x = ((opt or str(dflt)) + ":0").split(':')
return (xrange(0, int(x[0])), float(x[1])) return (range(0, int(x[0])), float(x[1]))
def cr(cr_api, test, opts): def cr(cr_api, test, opts):
...@@ -1199,7 +1204,7 @@ def get_visible_state(test): ...@@ -1199,7 +1204,7 @@ def get_visible_state(test):
cmaps = [[0, 0, ""]] cmaps = [[0, 0, ""]]
last = 0 last = 0
for mp in open("/proc/%s/root/proc/%s/maps" % (test.getpid(), pid)): for mp in open("/proc/%s/root/proc/%s/maps" % (test.getpid(), pid)):
m = map(lambda x: int('0x' + x, 0), mp.split()[0].split('-')) m = list(map(lambda x: int('0x' + x, 0), mp.split()[0].split('-')))
m.append(mp.split()[1]) m.append(mp.split()[1])
...@@ -1221,7 +1226,7 @@ def get_visible_state(test): ...@@ -1221,7 +1226,7 @@ def get_visible_state(test):
r = re.compile("^\S+\s\S+\s\S+\s(\S+)\s(\S+)") r = re.compile("^\S+\s\S+\s\S+\s(\S+)\s(\S+)")
for m in open("/proc/%s/root/proc/%s/mountinfo" % (test.getpid(), pid)): for m in open("/proc/%s/root/proc/%s/mountinfo" % (test.getpid(), pid)):
cmounts.append(r.match(m).groups()) cmounts.append(r.match(m).groups())
except IOError, e: except IOError as e:
if e.errno != errno.EINVAL: if e.errno != errno.EINVAL:
raise e raise e
mounts[pid] = cmounts mounts[pid] = cmounts
...@@ -1235,8 +1240,8 @@ def check_visible_state(test, state, opts): ...@@ -1235,8 +1240,8 @@ def check_visible_state(test, state, opts):
fnew = new[0][pid] fnew = new[0][pid]
fold = state[0][pid] fold = state[0][pid]
if fnew != fold: if fnew != fold:
print "%s: Old files lost: %s" % (pid, fold - fnew) print("%s: Old files lost: %s" % (pid, fold - fnew))
print "%s: New files appeared: %s" % (pid, fnew - fold) print("%s: New files appeared: %s" % (pid, fnew - fold))
raise test_fail_exc("fds compare") raise test_fail_exc("fds compare")
old_maps = state[1][pid] old_maps = state[1][pid]
...@@ -1247,29 +1252,29 @@ def check_visible_state(test, state, opts): ...@@ -1247,29 +1252,29 @@ def check_visible_state(test, state, opts):
if vsyscall in new_maps and vsyscall not in old_maps: if vsyscall in new_maps and vsyscall not in old_maps:
new_maps.remove(vsyscall) new_maps.remove(vsyscall)
if old_maps != new_maps: if old_maps != new_maps:
print "%s: Old maps lost: %s" % (pid, old_maps - new_maps) print("%s: Old maps lost: %s" % (pid, old_maps - new_maps))
print "%s: New maps appeared: %s" % (pid, new_maps - old_maps) print("%s: New maps appeared: %s" % (pid, new_maps - old_maps))
if not opts['fault']: # skip parasite blob if not opts['fault']: # skip parasite blob
raise test_fail_exc("maps compare") raise test_fail_exc("maps compare")
old_mounts = state[2][pid] old_mounts = state[2][pid]
new_mounts = new[2][pid] new_mounts = new[2][pid]
for i in xrange(len(old_mounts)): for i in range(len(old_mounts)):
m = old_mounts.pop(0) m = old_mounts.pop(0)
if m in new_mounts: if m in new_mounts:
new_mounts.remove(m) new_mounts.remove(m)
else: else:
old_mounts.append(m) old_mounts.append(m)
if old_mounts or new_mounts: if old_mounts or new_mounts:
print "%s: Old mounts lost: %s" % (pid, old_mounts) print("%s: Old mounts lost: %s" % (pid, old_mounts))
print "%s: New mounts appeared: %s" % (pid, new_mounts) print("%s: New mounts appeared: %s" % (pid, new_mounts))
raise test_fail_exc("mounts compare") raise test_fail_exc("mounts compare")
if '--link-remap' in test.getdopts(): if '--link-remap' in test.getdopts():
import glob import glob
link_remap_list = glob.glob(os.path.dirname(test.getname()) + '/link_remap*') link_remap_list = glob.glob(os.path.dirname(test.getname()) + '/link_remap*')
if link_remap_list: if link_remap_list:
print "%s: link-remap files left: %s" % (test.getname(), link_remap_list) print("%s: link-remap files left: %s" % (test.getname(), link_remap_list))
raise test_fail_exc("link remaps left") raise test_fail_exc("link remaps left")
...@@ -1337,7 +1342,7 @@ def cmp_ns(ns1, match, ns2, msg): ...@@ -1337,7 +1342,7 @@ def cmp_ns(ns1, match, ns2, msg):
ns1_ino = os.stat(ns1).st_ino ns1_ino = os.stat(ns1).st_ino
ns2_ino = os.stat(ns2).st_ino ns2_ino = os.stat(ns2).st_ino
if eval("%r %s %r" % (ns1_ino, match, ns2_ino)): if eval("%r %s %r" % (ns1_ino, match, ns2_ino)):
print "%s match (%r %s %r) fail" % (msg, ns1_ino, match, ns2_ino) print("%s match (%r %s %r) fail" % (msg, ns1_ino, match, ns2_ino))
raise test_fail_exc("%s compare" % msg) raise test_fail_exc("%s compare" % msg)
...@@ -1353,8 +1358,8 @@ def pstree_each_pid(root_pid): ...@@ -1353,8 +1358,8 @@ def pstree_each_pid(root_pid):
pid_line = f_children.readline().strip(" \n") pid_line = f_children.readline().strip(" \n")
if pid_line: if pid_line:
child_pids += pid_line.split(" ") child_pids += pid_line.split(" ")
except Exception, e: except Exception as e:
print "Unable to read /proc/*/children: %s" % e print("Unable to read /proc/*/children: %s" % e)
return # process is dead return # process is dead
yield root_pid yield root_pid
...@@ -1370,8 +1375,8 @@ def is_proc_stopped(pid): ...@@ -1370,8 +1375,8 @@ def is_proc_stopped(pid):
for line in f_status.readlines(): for line in f_status.readlines():
if line.startswith("State:"): if line.startswith("State:"):
return line.split(":", 1)[1].strip().split(" ")[0] return line.split(":", 1)[1].strip().split(" ")[0]
except Exception, e: except Exception as e:
print "Unable to read a thread status: %s" % e print("Unable to read a thread status: %s" % e)
pass # process is dead pass # process is dead
return None return None
...@@ -1382,8 +1387,8 @@ def is_proc_stopped(pid): ...@@ -1382,8 +1387,8 @@ def is_proc_stopped(pid):
thread_dirs = [] thread_dirs = []
try: try:
thread_dirs = os.listdir(tasks_dir) thread_dirs = os.listdir(tasks_dir)
except Exception, e: except Exception as e:
print "Unable to read threads: %s" % e print("Unable to read threads: %s" % e)
pass # process is dead pass # process is dead
for thread_dir in thread_dirs: for thread_dir in thread_dirs:
...@@ -1407,8 +1412,8 @@ def pstree_signal(root_pid, signal): ...@@ -1407,8 +1412,8 @@ def pstree_signal(root_pid, signal):
for pid in pstree_each_pid(root_pid): for pid in pstree_each_pid(root_pid):
try: try:
os.kill(int(pid), signal) os.kill(int(pid), signal)
except Exception, e: except Exception as e:
print "Unable to kill %d: %s" % (pid, e) print("Unable to kill %d: %s" % (pid, e))
pass # process is dead pass # process is dead
...@@ -1416,7 +1421,7 @@ def do_run_test(tname, tdesc, flavs, opts): ...@@ -1416,7 +1421,7 @@ def do_run_test(tname, tdesc, flavs, opts):
tcname = tname.split('/')[0] tcname = tname.split('/')[0]
tclass = test_classes.get(tcname, None) tclass = test_classes.get(tcname, None)
if not tclass: if not tclass:
print "Unknown test class %s" % tcname print("Unknown test class %s" % tcname)
return return
if opts['report']: if opts['report']:
...@@ -1427,7 +1432,6 @@ def do_run_test(tname, tdesc, flavs, opts): ...@@ -1427,7 +1432,6 @@ def do_run_test(tname, tdesc, flavs, opts):
fcg = get_freezer(opts['freezecg']) fcg = get_freezer(opts['freezecg'])
for f in flavs: for f in flavs:
print
print_sep("Run %s in %s" % (tname, f)) print_sep("Run %s in %s" % (tname, f))
if opts['dry_run']: if opts['dry_run']:
continue continue
...@@ -1507,23 +1511,23 @@ class Launcher: ...@@ -1507,23 +1511,23 @@ class Launcher:
self.__junit_test_cases = [] self.__junit_test_cases = []
self.__file_report = open(reportname, 'a') self.__file_report = open(reportname, 'a')
print >> self.__file_report, "TAP version 13" print(u"TAP version 13", file=self.__file_report)
print >> self.__file_report, "# Hardware architecture: " + arch print(u"# Hardware architecture: " + arch, file=self.__file_report)
print >> self.__file_report, "# Timestamp: " + now.strftime("%Y-%m-%d %H:%M") + " (GMT+1)" print(u"# Timestamp: " + now.strftime("%Y-%m-%d %H:%M") + " (GMT+1)", file=self.__file_report)
print >> self.__file_report, "# " print(u"# ", file=self.__file_report)
print >> self.__file_report, "1.." + str(nr_tests) print(u"1.." + str(nr_tests), file=self.__file_report)
self.__taint = open("/proc/sys/kernel/tainted").read() self.__taint = open("/proc/sys/kernel/tainted").read()
if int(self.__taint, 0) != 0: if int(self.__taint, 0) != 0:
print "The kernel is tainted: %r" % self.__taint print("The kernel is tainted: %r" % self.__taint)
if not opts["ignore_taint"]: if not opts["ignore_taint"]:
raise Exception("The kernel is tainted: %r" % self.__taint) raise Exception("The kernel is tainted: %r" % self.__taint)
def __show_progress(self, msg): def __show_progress(self, msg):
perc = self.__nr * 16 / self.__total perc = int(self.__nr * 16 / self.__total)
print "=== Run %d/%d %s %s" % (self.__nr, self.__total, '=' * perc + '-' * (16 - perc), msg) print("=== Run %d/%d %s %s" % (self.__nr, self.__total, '=' * perc + '-' * (16 - perc), msg))
def skip(self, name, reason): def skip(self, name, reason):
print "Skipping %s (%s)" % (name, reason) print("Skipping %s (%s)" % (name, reason))
self.__nr += 1 self.__nr += 1
self.__runtest += 1 self.__runtest += 1
self.__nr_skip += 1 self.__nr_skip += 1
...@@ -1533,8 +1537,8 @@ class Launcher: ...@@ -1533,8 +1537,8 @@ class Launcher:
tc.add_skipped_info(reason) tc.add_skipped_info(reason)
self.__junit_test_cases.append(tc) self.__junit_test_cases.append(tc)
if self.__file_report: if self.__file_report:
testline = "ok %d - %s # SKIP %s" % (self.__runtest, name, reason) testline = u"ok %d - %s # SKIP %s" % (self.__runtest, name, reason)
print >> self.__file_report, testline print(testline, file=self.__file_report)
def run_test(self, name, desc, flavor): def run_test(self, name, desc, flavor):
...@@ -1586,21 +1590,21 @@ class Launcher: ...@@ -1586,21 +1590,21 @@ class Launcher:
failed_flavor = decode_flav(os.WEXITSTATUS(status)) failed_flavor = decode_flav(os.WEXITSTATUS(status))
self.__failed.append([sub['name'], failed_flavor]) self.__failed.append([sub['name'], failed_flavor])
if self.__file_report: if self.__file_report:
testline = "not ok %d - %s # flavor %s" % (self.__runtest, sub['name'], failed_flavor) testline = u"not ok %d - %s # flavor %s" % (self.__runtest, sub['name'], failed_flavor)
output = open(sub['log']).read() output = open(sub['log']).read()
details = {'output': output} details = {'output': output}
tc.add_error_info(output = output) tc.add_error_info(output = output)
print >> self.__file_report, testline print(testline, file=self.__file_report)
print >> self.__file_report, yaml.dump(details, explicit_start=True, explicit_end=True, default_style='|') print("%s" % yaml.dump(details, explicit_start=True, explicit_end=True, default_style='|'), file=self.__file_report)
if sub['log']: if sub['log']:
add_to_output(sub['log']) add_to_output(sub['log'])
else: else:
if self.__file_report: if self.__file_report:
testline = "ok %d - %s" % (self.__runtest, sub['name']) testline = u"ok %d - %s" % (self.__runtest, sub['name'])
print >> self.__file_report, testline print(testline, file=self.__file_report)
if sub['log']: if sub['log']:
print open(sub['log']).read() print(open(sub['log']).read())
os.unlink(sub['log']) os.unlink(sub['log'])
return True return True
...@@ -1638,7 +1642,7 @@ class Launcher: ...@@ -1638,7 +1642,7 @@ class Launcher:
print_sep("%d TEST(S) FAILED (TOTAL %d/SKIPPED %d)" print_sep("%d TEST(S) FAILED (TOTAL %d/SKIPPED %d)"
% (len(self.__failed), self.__total, self.__nr_skip), "#") % (len(self.__failed), self.__total, self.__nr_skip), "#")
for failed in self.__failed: for failed in self.__failed:
print " * %s(%s)" % (failed[0], failed[1]) print(" * %s(%s)" % (failed[0], failed[1]))
else: else:
print_sep("ALL TEST(S) PASSED (TOTAL %d/SKIPPED %d)" print_sep("ALL TEST(S) PASSED (TOTAL %d/SKIPPED %d)"
% (self.__total, self.__nr_skip), "#") % (self.__total, self.__nr_skip), "#")
...@@ -1662,7 +1666,7 @@ def all_tests(opts): ...@@ -1662,7 +1666,7 @@ def all_tests(opts):
if stat.S_IFMT(st.st_mode) in [stat.S_IFLNK, stat.S_IFSOCK]: if stat.S_IFMT(st.st_mode) in [stat.S_IFLNK, stat.S_IFSOCK]:
continue continue
files.append(fp) files.append(fp)
excl = map(lambda x: os.path.join(desc['dir'], x), desc['exclude']) excl = list(map(lambda x: os.path.join(desc['dir'], x), desc['exclude']))
tlist = filter(lambda x: tlist = filter(lambda x:
not x.endswith('.checkskip') and not x.endswith('.checkskip') and
not x.endswith('.hook') and not x.endswith('.hook') and
...@@ -1694,16 +1698,16 @@ def self_checkskip(tname): ...@@ -1694,16 +1698,16 @@ def self_checkskip(tname):
def print_fname(fname, typ): def print_fname(fname, typ):
print "=[%s]=> %s" % (typ, fname) print("=[%s]=> %s" % (typ, fname))
def print_sep(title, sep = "=", width = 80): def print_sep(title, sep = "=", width = 80):
print (" " + title + " ").center(width, sep) print((" " + title + " ").center(width, sep))
def print_error(line): def print_error(line):
line = line.rstrip() line = line.rstrip()
print line print(line)
if line.endswith('>'): # combine pie output if line.endswith('>'): # combine pie output
return True return True
return False return False
...@@ -1739,7 +1743,7 @@ def run_tests(opts): ...@@ -1739,7 +1743,7 @@ def run_tests(opts):
if opts['pre'] or opts['snaps']: if opts['pre'] or opts['snaps']:
if not criu.check("mem_dirty_track"): if not criu.check("mem_dirty_track"):
print "Tracking memory is not available" print("Tracking memory is not available")
return return
if opts['all']: if opts['all']:
...@@ -1754,29 +1758,30 @@ def run_tests(opts): ...@@ -1754,29 +1758,30 @@ def run_tests(opts):
run_all = False run_all = False
elif opts['from']: elif opts['from']:
if not os.access(opts['from'], os.R_OK): if not os.access(opts['from'], os.R_OK):
print "No such file" print("No such file")
return return
torun = map(lambda x: x.strip(), open(opts['from'])) torun = map(lambda x: x.strip(), open(opts['from']))
opts['keep_going'] = False opts['keep_going'] = False
run_all = True run_all = True
else: else:
print "Specify test with -t <name> or -a" print("Specify test with -t <name> or -a")
return return
torun = list(torun)
if opts['keep_going'] and len(torun) < 2: if opts['keep_going'] and len(torun) < 2:
print "[WARNING] Option --keep-going is more useful when running multiple tests" print("[WARNING] Option --keep-going is more useful when running multiple tests")
opts['keep_going'] = False opts['keep_going'] = False
if opts['exclude']: if opts['exclude']:
excl = re.compile(".*(" + "|".join(opts['exclude']) + ")") excl = re.compile(".*(" + "|".join(opts['exclude']) + ")")
print "Compiled exclusion list" print("Compiled exclusion list")
if opts['report']: if opts['report']:
init_report(opts['report']) init_report(opts['report'])
if opts['parallel'] and opts['freezecg']: if opts['parallel'] and opts['freezecg']:
print "Parallel launch with freezer not supported" print("Parallel launch with freezer not supported")
opts['parallel'] = None opts['parallel'] = None
if opts['join_ns']: if opts['join_ns']:
...@@ -1792,7 +1797,7 @@ def run_tests(opts): ...@@ -1792,7 +1797,7 @@ def run_tests(opts):
raise Exception("UFFD is not supported, cannot run with --lazy-pages") raise Exception("UFFD is not supported, cannot run with --lazy-pages")
if not uffd_noncoop: if not uffd_noncoop:
# Most tests will work with 4.3 - 4.11 # Most tests will work with 4.3 - 4.11
print "[WARNING] Non-cooperative UFFD is missing, some tests might spuriously fail" print("[WARNING] Non-cooperative UFFD is missing, some tests might spuriously fail")
launcher = Launcher(opts, len(torun)) launcher = Launcher(opts, len(torun))
try: try:
...@@ -1819,7 +1824,7 @@ def run_tests(opts): ...@@ -1819,7 +1824,7 @@ def run_tests(opts):
feat_list = tdesc.get('feature', "") feat_list = tdesc.get('feature', "")
for feat in feat_list.split(): for feat in feat_list.split():
if feat not in features: if feat not in features:
print "Checking feature %s" % feat print("Checking feature %s" % feat)
features[feat] = criu.check(feat) features[feat] = criu.check(feat)
if not features[feat]: if not features[feat]:
...@@ -1896,9 +1901,9 @@ def show_test_info(t): ...@@ -1896,9 +1901,9 @@ def show_test_info(t):
def list_tests(opts): def list_tests(opts):
tlist = all_tests(opts) tlist = all_tests(opts)
if opts['info']: if opts['info']:
print sti_fmt % ('Name', 'Flavors', 'Flags') print(sti_fmt % ('Name', 'Flavors', 'Flags'))
tlist = map(lambda x: show_test_info(x), tlist) tlist = map(lambda x: show_test_info(x), tlist)
print '\n'.join(tlist) print('\n'.join(tlist))
class group: class group:
...@@ -1948,14 +1953,14 @@ class group: ...@@ -1948,14 +1953,14 @@ class group:
f.write("echo 'All %s scripts OK'\n" % ext) f.write("echo 'All %s scripts OK'\n" % ext)
f.close() f.close()
os.chmod(fname + ext, 0700) os.chmod(fname + ext, 0o700)
def dump(self, fname): def dump(self, fname):
f = open(fname, "w") f = open(fname, "w")
for t in self.__tests: for t in self.__tests:
f.write(t + '\n') f.write(t + '\n')
f.close() f.close()
os.chmod(fname, 0700) os.chmod(fname, 0o700)
if len(self.__desc) or len(self.__deps): if len(self.__desc) or len(self.__deps):
f = open(fname + '.desc', "w") f = open(fname + '.desc', "w")
...@@ -1982,7 +1987,7 @@ def group_tests(opts): ...@@ -1982,7 +1987,7 @@ def group_tests(opts):
random.shuffle(tlist) random.shuffle(tlist)
if opts['exclude']: if opts['exclude']:
excl = re.compile(".*(" + "|".join(opts['exclude']) + ")") excl = re.compile(".*(" + "|".join(opts['exclude']) + ")")
print "Compiled exclusion list" print("Compiled exclusion list")
for t in tlist: for t in tlist:
if excl and excl.match(t): if excl and excl.match(t):
...@@ -2013,11 +2018,11 @@ def group_tests(opts): ...@@ -2013,11 +2018,11 @@ def group_tests(opts):
g.dump(fn) g.dump(fn)
nr += 1 nr += 1
print "Generated %d group(s)" % nr print("Generated %d group(s)" % nr)
def clean_stuff(opts): def clean_stuff(opts):
print "Cleaning %s" % opts['what'] print("Cleaning %s" % opts['what'])
if opts['what'] == 'nsroot': if opts['what'] == 'nsroot':
for f in flavors: for f in flavors:
f = flavors[f] f = flavors[f]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment