From 90eb39874f2dc385e84aa8e212bfa61b22a018c6 Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Mon, 19 Aug 2013 15:11:06 +0200 Subject: pylibmount: add regression tests Signed-off-by: Ondrej Oprala Signed-off-by: Karel Zak --- libmount/python/Makemodule.am | 2 + libmount/python/test_mount_context.py | 172 +++++++++++++++++++++++++++++++ libmount/python/test_mount_tab.py | 162 +++++++++++++++++++++++++++++ libmount/python/test_mount_tab_update.py | 58 +++++++++++ 4 files changed, 394 insertions(+) create mode 100755 libmount/python/test_mount_context.py create mode 100755 libmount/python/test_mount_tab.py create mode 100755 libmount/python/test_mount_tab_update.py (limited to 'libmount/python') diff --git a/libmount/python/Makemodule.am b/libmount/python/Makemodule.am index bafa8ada8..fd574ef12 100644 --- a/libmount/python/Makemodule.am +++ b/libmount/python/Makemodule.am @@ -21,6 +21,8 @@ pylibmount_la_CFLAGS = \ pylibmount_la_LDFLAGS = \ -avoid-version -module -shared -export-dynamic +EXTRA_DIST += libmount/python/libmount/__init__.py + CLEANFILES += *.img endif # BUILD_PYLIBMOUNT diff --git a/libmount/python/test_mount_context.py b/libmount/python/test_mount_context.py new file mode 100755 index 000000000..3508cf60f --- /dev/null +++ b/libmount/python/test_mount_context.py @@ -0,0 +1,172 @@ +#!/bin/python2 +import os +import sys +import stat +import errno +import libmount as mnt + +def usage(tss): + print "\nUsage:\n\t{:s} [testoptions]\nTests:\n".format(sys.argv[0]) + for i in tss: + print "\t{15:-s}".format(i[0]) + if i[2] != "": + print " {:s}\n".format(i[2]) + + print("\n") + return 1 + +def mnt_run_test(tss, argv): + rc = -1 + if ((len(argv) < 2) or (argv[1] == "--help") or (argv[1] == "-h")): + return usage(tss) + + #mnt_init_debug(0) + + i=() + for i in tss: + if i[0] == argv[1]: + rc = i[1](i, argv[1:]) + if rc: + print "FAILED [rc={:d}]".format(rc) + break + + if ((rc < 0) and (i == ())): + return usage(tss) + return not not rc #because !!rc is too mainstream for python + +def test_mount(ts, argv): + idx = 1 + rc = 0 + + if len(argv) < 2: + return -errno.EINVAL + + cxt = mnt.Cxt() + + if argv[idx] == "-o": + cxt.options = argv[idx+1] + idx += 2 + if argv[idx] == "-t": + cxt.fstype = argv[idx+1] + idx += 2 + if len(argv) == idx + 1: + cxt.target = argv[idx] + idx+=1 + elif (len(argv) == idx + 2): + cxt.source = argv[idx] + idx += 1 + cxt.target = argv[idx] + idx += 1 + + try: + cxt.mount() + except Exception: + print "failed to mount" + return -1 + print "successfully mounted" + return rc + +def test_umount(ts, argv): + idx = 1 + rc = 0 + if len(argv) < 2: + return -errno.EINVAL + + cxt = mnt.Cxt() + + if argv[idx] == "-t": + cxt.options = argv[idx+1] + idx += 2 + + if argv[idx] == "-f": + cxt.enable_force(True) + + if argv[idx] == "-l": + cxt.enable_lazy(True) + idx += 1 + elif argv[idx] == "-r": + cxt.enable_rdonly_umount(True) + idx += 1 + + if len(argv) == idx + 1: + cxt.target = argv[idx] + idx += 1 + else: + return -errno.EINVAL + try: + cxt.umount() + except Exception: + print "failed to umount" + return 1 + print "successfully umounted" + return rc + +def test_flags(ts, argv): + idx = 1 + rc = 0 + opt = "" + flags = 0 + cxt = mnt.Cxt() + + if argv[idx] == "-o": + cxt.options = argv[idx + 1] + idx += 2 + if len(argv) == idx + 1: + cxt.target = argv[idx] + idx += 1 + + try: + cxt.prepare_mount() + # catch ioerror here + except IOError as e: + print "failed to prepare mount {:s}".format(e.strerror) + + opt = cxt.fs.options + if (opt): + print "options: {:s}", opt + + print "flags: {08:lx}".format(cxt.mflags()) + return rc + +def test_mountall(ts, argv): + mntrc = 1 + ignored = 1 + idx = 1 + cxt = mnt.Cxt() + + if len(argv) > 2: + if argv[idx] == "-O": + cxt.options_pattern = argv[idx+1] + idx += 2 + if argv[idx] == "-t": + cxt.fstype_pattern = argv[idx+1] + idx += 2 + + i = () + while (cxt.next_mount()): + tgt = i.target + if (ignored == 1): + print "{:s}: ignored: not match".format(tgt) + elif (ignored == 2): + print "{:s}: ignored: already mounted".format(tgt) + elif (not cxt.status): + if (mntrc > 0): + # ?? errno = mntrc + print "{:s}: mount failed".format(tgt) + else: + print "{:s}: mount failed".format(tgt) + else: + print "{:s}: successfully mounted".format(tgt) + + return 0 + + +tss = ( + ( "--mount", test_mount, "[-o ] [-t ] | " ), + ( "--umount", test_umount, "[-t ] [-f][-l][-r] |" ), + ( "--mount-all", test_mountall, "[-O ] [-t ] " ) +) +os.umask(stat.S_IWGRP | stat.S_IWOTH) #to be compatible with mount(8) + +sys.exit(mnt_run_test(tss, sys.argv)) diff --git a/libmount/python/test_mount_tab.py b/libmount/python/test_mount_tab.py new file mode 100755 index 000000000..e22f2f86e --- /dev/null +++ b/libmount/python/test_mount_tab.py @@ -0,0 +1,162 @@ +#!/bin/python2 +import os +import sys +import stat +import errno +import functools as ft +import libmount as mnt + +def usage(tss): + print "\nUsage:\n\t{:s} [testoptions]\nTests:\n".format(sys.argv[0]) + for i in tss: + print "\t{15:-s}".format(i[0]) + if i[2] != "": + print " {:s}\n".format(i[2]) + + print("\n") + return 1 + +def mnt_run_test(tss, argv): + rc = -1 + if ((len(argv) < 2) or (argv[1] == "--help") or (argv[1] == "-h")): + return usage(tss) + + #mnt_init_debug(0) + + i=() + for i in tss: + if i[0] == argv[1]: + rc = i[1](i, argv[1:]) + if rc: + print "FAILED [rc={:d}]".format(rc) + break + + if ((rc < 0) and (i == ())): + return usage(tss) + return not not rc #because !!rc is too mainstream for python + +def parser_errcb(tb, fname, line): + print "{:s}:{:d}: parse error".format(fname, line) + return 1 + +def create_table(f, comments): + if not f: + return None + tb = mnt.Tab() + tb.enable_comments(comments) + tb.errcb = parser_errcb + + try: + tb.parse_file(f) + except Exception: + print "{:s}: parsing failed".format(f) + return None + return tb + +def test_copy_fs(ts, argv): + rc = -1 + tb = create_table(argv[1], False) + fs = tb.find_target("/", mnt.MNT_ITER_FORWARD) + if not fs: + return rc + + print "ORIGINAL:" + fs.print_debug(sys.stdout) + + fs = fs.copy_fs(None) + if not fs: + return rc + print "COPY:" + fs.print_debug(sys.stdout) + return 0 + +def test_parse(ts, argv): + parse_comments = False + + if len(argv) == 3 and argv[2] == "--comments": + parse_comments = True + tb = create_table(argv[1], parse_comments) + + if tb.intro_comment: + print "Initial comment:\n\"{:s}\"".format(tb.intro_comment) + #while ((fs = tb.next_fs()) != None): + for fs in iter(ft.partial(tb.next_fs), None): + fs.print_debug(sys.stdout) + if tb.trailing_comment: + print "Trailing comment:\n\"{:s}\"".format(tb.trailing_comment) + return 0 + +def test_find(ts, argv, dr): + if len(argv) != 4: + print "try --help" + return -errno.EINVAL + + f, find, what = argv[1:] + + tb = create_table(f, False) + if find.lower() == "source": + fs = tb.find_source(what, dr) + elif find.lower() == "target": + fs = tb.find_target(what, dr) + + if not fs: + print "{:s}: not found {:s} '{:s}'".format(f, find, what) + else: + fs.print_debug(sys.stdout) + return 0 + +def test_find_fw(ts, argv): + return test_find(ts, argv, mnt.MNT_ITER_FORWARD) + +def test_find_bw(ts, argv): + return test_find(ts, argv, mnt.MNT_ITER_BACKWARD) + +def test_find_pair(ts, argv): + rc = -1 + tb = create_table(argv[1], False) + fs = tb.find_pair(argv[2], argv[3], mnt.MNT_ITER_FORWARD) + if not fs: + return rc + fs.print_debug(sys.stdout) + return 0 + +def test_is_mounted(ts, argv): + rc = -1 + tb = mnt.Tab(path="/proc/self/mountinfo") + if not tb: + print "failed to parse mountinto" + return rc + + fstab = create_table(argv[1], False) + if not fstab: + return rc + fs = () + for fs in ft.iter(tb.next_fs(), -1): + if tb.is_fs_mounted(fs): + print "{:s} already mounted on {:s}".format(fs.source, fs.target) + else: + print "{:s} not mounted on {:s}".format(fs.source, fs.target) + return 0 + +def test_find_mountpoint(ts, argv): + rc = -1 + tb = mnt.Tab("/proc/self/mountinfo") + if not tb: + return rc + fs = tb.find_mountpoint(argv[1], mnt.MNT_ITER_BACKWARD) + if not fs: + return rc + fs.print_debug(sys.stdout) + return 0 + + +tss = ( + ( "--parse", test_parse, " [--comments] parse and print tab" ), + ( "--find-forward", test_find_fw, " " ), + ( "--find-backward", test_find_bw, " " ), + ( "--find-pair", test_find_pair, " " ), + ( "--find-mountpoint", test_find_mountpoint, "" ), + ( "--copy-fs", test_copy_fs, " copy root FS from the file" ), + ( "--is-mounted", test_is_mounted, " check what from are already mounted" ), +) +sys.exit(mnt_run_test(tss, sys.argv)) diff --git a/libmount/python/test_mount_tab_update.py b/libmount/python/test_mount_tab_update.py new file mode 100755 index 000000000..12d2db022 --- /dev/null +++ b/libmount/python/test_mount_tab_update.py @@ -0,0 +1,58 @@ +#!/bin/python2 +import os +import sys +import stat +import errno +import libmount as mnt + +def usage(tss): + print "\nUsage:\n\t{:s} [testoptions]\nTests:\n".format(sys.argv[0]) + for i in tss: + print "\t{15:-s}".format(i[0]) + if i[2] != "": + print " {:s}\n".format(i[2]) + + print("\n") + return 1 + +def mnt_run_test(tss, argv): + rc = -1 + if ((len(argv) < 2) or (argv[1] == "--help") or (argv[1] == "-h")): + return usage(tss) + + #mnt_init_debug(0) + + i=() + for i in tss: + if i[0] == argv[1]: + rc = i[1](i, argv[1:]) + if rc: + print "FAILED [rc={:d}]".format(rc) + break + + if ((rc < 0) and (i == ())): + return usage(tss) + return not not rc #because !!rc is too mainstream for python + +def test_replace(ts, argv): + fs = mnt.Fs() + tb = mnt.Tab() + + if (len(argv) < 3): + return -1 + tb.enable_comments(True) + tb.parse_fstab() + + fs.source = argv[1] + fs.target = argv[2] + #TODO: resolve None + string + fs.comment = "# this is new filesystem\n" + tb.add_fs(fs) + tb.replace_file(os.environ["LIBMOUNT_FSTAB"]) + return 0 + +tss = ( + ( "--replace",test_replace, " Add a line to LIBMOUNT_FSTAB and replace the original file" ), +) + +sys.exit(mnt_run_test(tss, sys.argv)) -- cgit v1.2.3-55-g7522