Re: [PATCH anaconda-storage] Added the run_test.py script to easily run the test cases.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Looks good to me.

On 03/16/2009 04:56 AM, Martin Gracik wrote:
Some small changes to test cases were needed.
Added suite() function to them and changed the class names.
Also __init__.py files were added to the directories.
---
  run_test.py                           |  108 +++++++++++++++++++++++++++++++++
  tests/__init__.py                     |   29 +++++++++
  tests/storage/devicelibs/baseclass.py |   76 ++++++++++++-----------
  tests/storage/devicelibs/crypto.py    |   11 ++-
  tests/storage/devicelibs/lvm.py       |   18 +++---
  tests/storage/devicelibs/mdraid.py    |   12 ++--
  tests/storage/devicelibs/swap.py      |   11 ++-
  7 files changed, 207 insertions(+), 58 deletions(-)
  create mode 100755 run_test.py
  create mode 100644 tests/__init__.py
  create mode 100644 tests/storage/__init__.py
  create mode 100644 tests/storage/devicelibs/__init__.py

diff --git a/run_test.py b/run_test.py
new file mode 100755
index 0000000..5565a8d
--- /dev/null
+++ b/run_test.py
@@ -0,0 +1,108 @@
+#!/usr/bin/env python
+#-*- coding:utf-8 -*-
+
+import sys
+
+REQUIRED_PATHS = ["/usr/lib/anaconda",
+                  "/usr/share/system-config-date"]
+sys.path.extend(REQUIRED_PATHS)
+
+import unittest
+import tests
+import string
+from optparse import OptionParser
+
+
+def getFullTestName(test, full_test_names):
+    tests = []
+    for full_test_name in full_test_names:
+        if full_test_name.lower().find(test) != -1:
+            tests.append(full_test_name)
+
+    return tests
+
+
+if __name__ == "__main__":
+    usage = "usage: %prog [options] [test1 test2 ...]"
+    parser = OptionParser(usage)
+    parser.add_option("-l", "--list", action="store_true", default=False,
+                      help="print all available tests and exit")
+
+    (options, args) = parser.parse_args(sys.argv[1:])
+
+    print "Searching for test suites"
+    available_suites = tests.getAvailableSuites()
+    test_keys = available_suites.keys()
+    if not test_keys:
+        print "No test suites available, exiting"
+        sys.exit(1)
+
+    test_keys.sort()
+
+    if options.list:
+        print "\nAvailable tests:"
+        for test_name in test_keys:
+            print test_name
+        sys.exit(0)
+
+    tests_to_run = []
+
+    if len(args) == 0:
+        # interactive mode
+        print "Running in interactive mode"
+        print "\nAvailable tests:"
+        test_num = 0
+        for test_name in test_keys:
+            print "[%3d] %s" % (test_num, test_name)
+            test_num += 1
+        print
+
+        try:
+            input_string = raw_input("Type in the test you want to run, "
+                                     "or \"all\" to run all listed tests: ")
+        except KeyboardInterrupt as e:
+            print "\nAborted by user"
+            sys.exit(1)
+
+        for arg in input_string.split():
+            if arg.isdigit():
+                arg = int(arg)
+                try:
+                    args.append(test_keys[arg])
+                except KeyError as e:
+                    pass
+            else:
+                args.append(arg)
+
+    args = map(string.lower, args)
+    if "all" in args:
+        tests_to_run = test_keys[:]
+    else:
+        for arg in args:
+            matching_tests = getFullTestName(arg, test_keys)
+            tests_to_run.extend(filter(lambda test: test not in tests_to_run,
+                                       matching_tests))
+
+    # run the tests
+    if tests_to_run:
+        tests_to_run.sort()
+        print "Running tests: %s" % tests_to_run
+        test_suite = unittest.TestSuite([available_suites[test]
+                                         for test in tests_to_run])
+
+        try:
+            result = unittest.TextTestRunner(verbosity=2).run(test_suite)
+        except KeyboardInterrupt as e:
+            print "\nAborted by user"
+            sys.exit(1)
+
+        if result.wasSuccessful():
+            print "\nAll tests OK"
+            sys.exit(0)
+        else:
+            print "\nTests finished with %d errors and %d failures" % (len(result.errors),
+                                                                       len(result.failures))
+            sys.exit(2)
+    else:
+        print "No test suites matching your criteria found, exiting"
+        sys.exit(1)
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..959e7cf
--- /dev/null
+++ b/tests/__init__.py
@@ -0,0 +1,29 @@
+import os
+
+# this has to be imported before running anything
+import anaconda_log
+import upgrade
+
+
+def getAvailableSuites():
+    root, tests_dir = os.path.split(os.path.dirname(__file__))
+    modules = []
+
+    for root, dirs, files in os.walk(tests_dir):
+        for filename in files:
+            if filename.endswith(".py") and filename != "__init__.py":
+                basename, extension = os.path.splitext(filename)
+                modules.append(os.path.join(root, basename).replace("/", "."))
+
+    available_suites = {}
+    for module in modules:
+        imported = __import__(module, globals(), locals(), [module], -1)
+        try:
+            suite = getattr(imported, "suite")
+        except AttributeError as e:
+            continue
+
+        if callable(suite):
+            available_suites[module] = suite()
+
+    return available_suites
diff --git a/tests/storage/__init__.py b/tests/storage/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/storage/devicelibs/__init__.py b/tests/storage/devicelibs/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/storage/devicelibs/baseclass.py b/tests/storage/devicelibs/baseclass.py
index 01fe6c7..c19bfc3 100644
--- a/tests/storage/devicelibs/baseclass.py
+++ b/tests/storage/devicelibs/baseclass.py
@@ -2,11 +2,44 @@ import unittest
  import os
  import subprocess

-# need to import these first before doing anything
-import anaconda_log
-import upgrade

-class TestDevicelibs(unittest.TestCase):
+def makeLoopDev(device_name, file_name):
+    proc = subprocess.Popen(["dd", "if=/dev/zero", "of=%s" % file_name,
+                             "bs=1024", "count=102400"],
+                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    while True:
+        proc.communicate()
+        if proc.returncode is not None:
+            rc = proc.returncode
+            break
+    if rc:
+        raise OSError, "dd failed creating the file %s" % file_name
+
+    proc = subprocess.Popen(["losetup", device_name, file_name],
+                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    while True:
+        proc.communicate()
+        if proc.returncode is not None:
+            rc = proc.returncode
+            break
+    if rc:
+        raise OSError, "losetup failed setting up the loop device %s" % device_name
+
+def removeLoopDev(device_name, file_name):
+    proc = subprocess.Popen(["losetup", "-d", device_name],
+                            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    while True:
+        proc.communicate()
+        if proc.returncode is not None:
+            rc = proc.returncode
+            break
+    if rc:
+        raise OSError, "losetup failed removing the loop device %s" % device_name
+
+    os.unlink(file_name)
+
+
+class DevicelibsTestCase(unittest.TestCase):

      _LOOP_DEVICES = (("/dev/loop0", "/tmp/test-virtdev0"),
                       ("/dev/loop1", "/tmp/test-virtdev1"))
@@ -15,39 +48,8 @@ class TestDevicelibs(unittest.TestCase):

      def setUp(self):
          for dev, file in self._LOOP_DEVICES:
-            self.makeLoopDev(dev, file)
+            makeLoopDev(dev, file)

      def tearDown(self):
          for dev, file in self._LOOP_DEVICES:
-            self.removeLoopDev(dev, file)
-
-    def makeLoopDev(self, device_name, file_name):
-        proc = subprocess.Popen(["dd", "if=/dev/zero", "of=%s" % file_name, "bs=1024", "count=102400"])
-        while True:
-            proc.communicate()
-            if proc.returncode is not None:
-                rc = proc.returncode
-                break
-        if rc:
-            raise OSError, "dd failed creating the file %s" % file_name
-
-        proc = subprocess.Popen(["losetup", device_name, file_name])
-        while True:
-            proc.communicate()
-            if proc.returncode is not None:
-                rc = proc.returncode
-                break
-        if rc:
-            raise OSError, "losetup failed setting up the loop device %s" % device_name
-
-    def removeLoopDev(self, device_name, file_name):
-        proc = subprocess.Popen(["losetup", "-d", device_name])
-        while True:
-            proc.communicate()
-            if proc.returncode is not None:
-                rc = proc.returncode
-                break
-        if rc:
-            raise OSError, "losetup failed removing the loop device %s" % device_name
-
-        os.remove(file_name)
+            removeLoopDev(dev, file)
diff --git a/tests/storage/devicelibs/crypto.py b/tests/storage/devicelibs/crypto.py
index 6a76569..0f9f7bd 100644
--- a/tests/storage/devicelibs/crypto.py
+++ b/tests/storage/devicelibs/crypto.py
@@ -5,9 +5,9 @@ import storage.devicelibs.crypto as crypto
  import tempfile
  import os

-class TestCrypto(baseclass.TestDevicelibs):
+class CryptoTestCase(baseclass.DevicelibsTestCase):

-    def runTest(self):
+    def testCrypto(self):
          ##
          ## is_luks
          ##
@@ -118,6 +118,9 @@ class TestCrypto(baseclass.TestDevicelibs):
          os.unlink(new_keyfile)


+def suite():
+    return unittest.TestLoader().loadTestsFromTestCase(CryptoTestCase)
+
+
  if __name__ == "__main__":
-    suite = unittest.TestLoader().loadTestsFromTestCase(TestCrypto)
-    unittest.TextTestRunner(verbosity=2).run(suite)
+    unittest.main()
diff --git a/tests/storage/devicelibs/lvm.py b/tests/storage/devicelibs/lvm.py
index 62c52d1..e6ba1a6 100644
--- a/tests/storage/devicelibs/lvm.py
+++ b/tests/storage/devicelibs/lvm.py
@@ -2,9 +2,9 @@ import baseclass
  import unittest
  import storage.devicelibs.lvm as lvm

-class TestLVM(baseclass.TestDevicelibs):
+class LVMTestCase(baseclass.DevicelibsTestCase):

-    def testLVMStuff(self):
+    def testLVM(self):
          ##
          ## pvcreate
          ##
@@ -154,7 +154,7 @@ class TestLVM(baseclass.TestDevicelibs):
          self.assertEqual(lvm.has_lvm(), True)

          # fail
-        #TODO
+        # TODO

          ##
          ## lvremove
@@ -214,15 +214,17 @@ class TestLVM(baseclass.TestDevicelibs):
          self.assertEqual(lvm.clampSize(10, 4, True), 12L)

      #def testVGUsedSpace(self):
-        #TODO
+        # TODO
          pass

      #def testVGFreeSpace(self):
-        #TODO
+        # TODO
          pass


-if __name__ == "__main__":
-    suite = unittest.TestLoader().loadTestsFromTestCase(TestLVM)
-    unittest.TextTestRunner(verbosity=2).run(suite)
+def suite():
+    return unittest.TestLoader().loadTestsFromTestCase(LVMTestCase)
+

+if __name__ == "__main__":
+    unittest.main()
diff --git a/tests/storage/devicelibs/mdraid.py b/tests/storage/devicelibs/mdraid.py
index 6e49e55..3c0ee72 100644
--- a/tests/storage/devicelibs/mdraid.py
+++ b/tests/storage/devicelibs/mdraid.py
@@ -4,9 +4,9 @@ import storage.devicelibs.mdraid as mdraid

  import time

-class TestMDRaid(baseclass.TestDevicelibs):
+class MDRaidTestCase(baseclass.DevicelibsTestCase):

-    def testMDRaidStuff(self):
+    def testMDRaid(self):
          ##
          ## getRaidLevels
          ##
@@ -99,7 +99,9 @@ class TestMDRaid(baseclass.TestDevicelibs):
          self.assertRaises(mdraid.MDRaidError, mdraid.mddestroy, "/not/existing/device")


-if __name__ == "__main__":
-    suite = unittest.TestLoader().loadTestsFromTestCase(TestMDRaid)
-    unittest.TextTestRunner(verbosity=2).run(suite)
+def suite():
+    return unittest.TestLoader().loadTestsFromTestCase(MDRaidTestCase)
+

+if __name__ == "__main__":
+    unittest.main()
diff --git a/tests/storage/devicelibs/swap.py b/tests/storage/devicelibs/swap.py
index b1e9bd3..b99d1f6 100644
--- a/tests/storage/devicelibs/swap.py
+++ b/tests/storage/devicelibs/swap.py
@@ -2,9 +2,9 @@ import baseclass
  import unittest
  import storage.devicelibs.swap as swap

-class TestSwap(baseclass.TestDevicelibs):
+class SwapTestCase(baseclass.DevicelibsTestCase):

-    def runTest(self):
+    def testSwap(self):
          ##
          ## mkswap
          ##
@@ -58,6 +58,9 @@ class TestSwap(baseclass.TestDevicelibs):
          self.assertRaises(swap.SwapError, swap.swapoff, self._LOOP_DEV0)


+def suite():
+    return unittest.TestLoader().loadTestsFromTestCase(SwapTestCase)
+
+
  if __name__ == "__main__":
-    suite = unittest.TestLoader().loadTestsFromTestCase(TestSwap)
-    unittest.TextTestRunner(verbosity=2).run(suite)
+    unittest.main()


--
David Cantrell <dcantrell@xxxxxxxxxx>
Red Hat / Honolulu, HI

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux