diff options
author | Gui Chen <gui.chen@intel.com> | 2012-03-16 15:41:35 +0800 |
---|---|---|
committer | Gui Chen <gui.chen@intel.com> | 2012-03-16 15:41:35 +0800 |
commit | 6ab68adf6c68dcad07e0d4d8629e64c6f624919d (patch) | |
tree | a467936e77ccc5c690f0d4bf3648898118344f22 /tests | |
parent | c8d07c95d6051dcbe5d8cc4d930b66b6efac1324 (diff) | |
download | mic-6ab68adf6c68dcad07e0d4d8629e64c6f624919d.tar.gz mic-6ab68adf6c68dcad07e0d4d8629e64c6f624919d.tar.bz2 mic-6ab68adf6c68dcad07e0d4d8629e64c6f624919d.zip |
Add unittest case: chroot, msger, runner
Signed-off-by: Gui Chen <gui.chen@intel.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unittest/chroot_fixtures/minchroot.tar.gz | bin | 0 -> 1438891 bytes | |||
-rw-r--r-- | tests/unittest/test_chroot.py | 42 | ||||
-rw-r--r-- | tests/unittest/test_msger.py | 81 | ||||
-rw-r--r-- | tests/unittest/test_runner.py | 52 |
4 files changed, 175 insertions, 0 deletions
diff --git a/tests/unittest/chroot_fixtures/minchroot.tar.gz b/tests/unittest/chroot_fixtures/minchroot.tar.gz Binary files differnew file mode 100644 index 0000000..bccd3f7 --- /dev/null +++ b/tests/unittest/chroot_fixtures/minchroot.tar.gz diff --git a/tests/unittest/test_chroot.py b/tests/unittest/test_chroot.py new file mode 100644 index 0000000..546e3ae --- /dev/null +++ b/tests/unittest/test_chroot.py @@ -0,0 +1,42 @@ +#!/usr/bin/python + +import os +import sys +import shutil +import tarfile +import StringIO +import unittest +from mic import chroot + +TEST_CHROOT_LOC = os.path.join(os.getcwd(), 'chroot_fixtures') +TEST_CHROOT_TAR = os.path.join(TEST_CHROOT_LOC, 'minchroot.tar.gz') +TEST_CHROOT_DIR = os.path.join(TEST_CHROOT_LOC, 'minchroot') + +def suite(): + return unittest.makeSuite(ChrootTest) + +class ChrootTest(unittest.TestCase): + + def setUp(self): + tar = tarfile.open(TEST_CHROOT_TAR, "r:gz") + tar.extractall(path=TEST_CHROOT_LOC) + self.chrootdir = TEST_CHROOT_DIR + self.stdout = sys.stdout + self.stderr = sys.stderr + sys.stdout = StringIO.StringIO() + sys.stderr = StringIO.StringIO() + + def tearDown(self): + sys.stdout = self.stdout + sys.stderr = self.stderr + shutil.rmtree(TEST_CHROOT_DIR, ignore_errors=True) + + def testChroot(self): + try: + chroot.chroot(TEST_CHROOT_DIR, None, 'exit') + except Exception, e: + raise self.failureException(e) + +if __name__ == "__main__": + unittest.main() + diff --git a/tests/unittest/test_msger.py b/tests/unittest/test_msger.py new file mode 100644 index 0000000..c419a15 --- /dev/null +++ b/tests/unittest/test_msger.py @@ -0,0 +1,81 @@ +#!/usr/bin/python + +import os +import sys +import StringIO +import unittest +from mic import msger + +def suite(): + return unittest.makeSuite(MsgerTest) + +class MsgerTest(unittest.TestCase): + + def setUp(self): + self.stdout = sys.stdout + self.stderr = sys.stderr + sys.stdout = StringIO.StringIO() + sys.stderr = StringIO.StringIO() + self.loglevel = msger.LOG_LEVEL + + def tearDown(self): + msger.LOG_LEVEL = self.loglevel + sys.stdout = self.stdout + sys.stderr = self.stderr + + def testRaw(self): + excepted = "hello\n" + msger.raw("hello") + self.assertEqual(excepted, sys.stdout.getvalue()) + + def testInfo(self): + excepted = "Info: hello\n" + msger.info("hello") + self.assertEqual(excepted, sys.stdout.getvalue()) + + def testWarning(self): + excepted = "Warning: hello\n" + msger.warning("hello") + self.assertEqual(excepted, sys.stderr.getvalue()) + + def testVerbose(self): + excepted = "Verbose: hello\n" + msger.verbose("hello") + self.assertEqual("", sys.stdout.getvalue()) + msger.set_loglevel("verbose") + msger.verbose("hello") + self.assertEqual(excepted, sys.stdout.getvalue()) + + def testDebug(self): + excepted = "Debug: hello\n" + msger.debug("hello") + self.assertEqual("", sys.stdout.getvalue()) + msger.set_loglevel("debug") + msger.debug("hello") + self.assertEqual(excepted, sys.stderr.getvalue()) + + def testLogstderr(self): + excepted = "hello\n" + cwd = os.getcwd() + msger.enable_logstderr(cwd + "/__tmp_err.log") + print >>sys.stderr, "hello" + msger.disable_logstderr() + self.assertEqual(excepted, sys.stderr.getvalue()) + + def testLoglevel(self): + # test default value + self.assertEqual("normal", msger.get_loglevel()) + # test no effect value + msger.set_loglevel("zzzzzz") + self.assertEqual("normal", msger.get_loglevel()) + # test effect value + msger.set_loglevel("verbose") + self.assertEqual("verbose", msger.get_loglevel()) + msger.set_loglevel("debug") + self.assertEqual("debug", msger.get_loglevel()) + msger.set_loglevel("quiet") + self.assertEqual("quiet", msger.get_loglevel()) + +if __name__ == "__main__": + unittest.main() + diff --git a/tests/unittest/test_runner.py b/tests/unittest/test_runner.py new file mode 100644 index 0000000..5ed26cd --- /dev/null +++ b/tests/unittest/test_runner.py @@ -0,0 +1,52 @@ +#!/usr/bin/python + +import os +import sys +import StringIO +import unittest +from mic.utils import runner + +def suite(): + return unittest.makeSuite(RunnerTest) + +class RunnerTest(unittest.TestCase): + + def setUp(self): + self.stdout = sys.stdout + self.stderr = sys.stderr + sys.stdout = StringIO.StringIO() + sys.stderr = StringIO.StringIO() + + def tearDown(self): + sys.stdout = self.stdout + sys.stderr = self.stderr + + def testRuntoolCatch0(self): + (rc, out) = runner.runtool("echo hello", catch=0) + self.assertEqual(0, rc) + self.assertEqual('', out) + (rc, out) = runner.runtool("echo hello >&2", catch=0) + self.assertEqual(0, rc) + self.assertEqual('', out) + + def testRuntoolCatch1(self): + (rc, out) = runner.runtool("echo hello", catch=1) + self.assertEqual(0, rc) + self.assertEqual("hello\n", out) + + def testRuntoolCatch2(self): + (rc, out) = runner.runtool("echo hello >&2", catch=2) + self.assertEqual(0, rc) + self.assertEqual("hello\n", out) + + def testRuntoolCatch3(self): + (rc, out) = runner.runtool("echo hello", catch=3) + self.assertEqual(0, rc) + self.assertEqual("hello\n", out) + (rc, out) = runner.runtool("echo hello >&2", catch=2) + self.assertEqual(0, rc) + self.assertEqual("hello\n", out) + +if __name__ == "__main__": + unittest.main() + |