diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/run b/tests/run new file mode 100755 index 0000000..6726dbc --- /dev/null +++ b/tests/run @@ -0,0 +1,23 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- + +import sys + +import unittest +sys.path.insert(0,'.') +sys.path.insert(0,'..') + +from tests import version, utils +rc = 0 + +# Add to the list the module to test +mods = [version, utils] + +tests = [] +for mod in mods: + tests.append(unittest.TestLoader().loadTestsFromModule(mod)) + +result = unittest.TextTestRunner(verbosity = 1).run(unittest.TestSuite(tests)) +if result.errors: + rc = 1 +raise SystemExit(rc) diff --git a/tests/specs.py b/tests/specs.py new file mode 100644 index 0000000..2e106a8 --- /dev/null +++ b/tests/specs.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +import os +import sys +sys.path.insert(0,'.') +sys.path.insert(0,'..') +import unittest + +class SpecsTest(unittest.TestCase): + + def setUp(self): + sys.stdout.write("%s called\n" % (self,)) + sys.stdout.flush() + + def tearDown(self): + """ + tearDown is run after each test + """ + sys.stdout.write("%s ran\n" % (self,)) + sys.stdout.flush() + +if __name__ == '__main__': + unittest.main() + raise SystemExit(0) diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..ab645a5 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,123 @@ +# -*- coding: utf-8 -*- +import os +import sys +sys.path.insert(0,'.') +sys.path.insert(0,'..') +import unittest +import tempfile +from molecule.compat import get_stringtype +from molecule.utils import md5sum, copy_dir, get_random_number, \ + remove_path_sandbox, remove_path, mkdtemp, empty_dir, \ + exec_cmd_get_status_output, exec_cmd, is_exec_available, \ + valid_exec_check, get_year, exec_chroot_cmd + +class UtilsTest(unittest.TestCase): + + def setUp(self): + sys.stdout.write("%s called\n" % (self,)) + sys.stdout.flush() + + def tearDown(self): + """ + tearDown is run after each test + """ + sys.stdout.write("%s ran\n" % (self,)) + sys.stdout.flush() + + def test_md5sum(self): + + tmp_fd, tmp_path = tempfile.mkstemp(dir=os.getcwd()) + os.write(tmp_fd, "hello") + os.close(tmp_fd) + result = md5sum(tmp_path) + self.assertEqual(result, "5d41402abc4b2a76b9719d911017c592") + os.remove(tmp_path) + + def test_copy_dir(self): + tmp_dir1 = tempfile.mkdtemp(dir=os.getcwd()) + tmp_dir2 = tempfile.mkdtemp(dir=os.getcwd()) + tmp_path1 = os.path.join(tmp_dir1, "test") + tmp_path2 = os.path.join(tmp_dir2, "test") + tmp_f = open(tmp_path1, "w") + tmp_f.write("hello") + tmp_f.close() + os.rmdir(tmp_dir2) + copy_dir(tmp_dir1, tmp_dir2) + self.assertEqual(os.listdir(tmp_dir2), ['test']) + os.remove(tmp_path1) + os.rmdir(tmp_dir1) + os.remove(tmp_path2) + os.rmdir(tmp_dir2) + + def test_randint(self): + self.assert_(get_random_number() in range(0, 99999)) + + def test_sandbox_rm(self): + tmp_dir1 = tempfile.mkdtemp(dir=os.getcwd()) + tmp_dir2 = tempfile.mkdtemp(dir=os.getcwd()) + sb_dirs = [tmp_dir1] + sb_env = { + 'SANDBOX_WRITE': ':'.join(sb_dirs), + } + rc = remove_path_sandbox(tmp_dir1, sb_env) + self.assert_(rc == 0) + tmp_fd, tmp_path = tempfile.mkstemp() + with os.fdopen(tmp_fd, "w") as tmp_f: + rc = remove_path_sandbox(tmp_dir2, sb_env, stdout=tmp_fd, + stderr=tmp_fd) + os.remove(tmp_path) + self.assert_(rc != 0) + os.rmdir(tmp_dir2) + + def test_normal_rm(self): + tmp_dir1 = tempfile.mkdtemp(dir=os.getcwd()) + tmp_fd, tmp_path = tempfile.mkstemp() + os.close(tmp_fd) + rc = remove_path(tmp_path) + rc2 = remove_path(tmp_dir1) + self.assert_(rc == 0) + self.assert_(rc2 == 0) + self.assert_(not os.path.lexists(tmp_path)) + self.assert_(not os.path.lexists(tmp_dir1)) + + def test_mkdtemp(self): + tmp_dir1 = mkdtemp() + self.assert_(os.path.isdir(tmp_dir1)) + os.rmdir(tmp_dir1) + + def test_empty_dir(self): + tmp_dir1 = tempfile.mkdtemp(dir=os.getcwd()) + tmp_f = open(os.path.join(tmp_dir1, "test"), "w") + tmp_f.write("something") + tmp_f.close() + empty_dir(tmp_dir1) + self.assert_(not os.listdir(tmp_dir1)) + os.rmdir(tmp_dir1) + + def test_exec_cmd_get_status_output(self): + rc, output = exec_cmd_get_status_output(["echo", "hello"]) + self.assert_(rc == 0) + self.assertEqual(output, "hello") + + def test_exec_cmd(self): + rc = exec_cmd(["/bin/echo", "${TEST}"], env = {'TEST': "hello"}) + self.assert_(rc == 0) + + def test_is_exec_available(self): + self.assert_(is_exec_available("/bin/echo")) + + def test_valid_exec_check(self): + self.assertRaises(EnvironmentError, + valid_exec_check, "/bin/this-does-not-exist-for-sure") + + def test_get_year(self): + year = int(get_year()) + self.assert_(year in range(1970, 2138)) + + def test_exec_chroot_cmd(self): + rc = exec_chroot_cmd(["/bin/echo", "hello"], "/", env = {}) + self.assert_(rc == 0) + +if __name__ == '__main__': + unittest.main() + raise SystemExit(0) diff --git a/tests/version.py b/tests/version.py new file mode 100644 index 0000000..256d880 --- /dev/null +++ b/tests/version.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +import sys +sys.path.insert(0,'.') +sys.path.insert(0,'..') +import unittest +from molecule.compat import get_stringtype + +class VersionTest(unittest.TestCase): + + def setUp(self): + sys.stdout.write("%s called\n" % (self,)) + sys.stdout.flush() + + def tearDown(self): + """ + tearDown is run after each test + """ + sys.stdout.write("%s ran\n" % (self,)) + sys.stdout.flush() + + def test_version(self): + + from molecule.version import VERSION + self.assert_(isinstance(VERSION, get_stringtype())) + self.assert_(len(VERSION.split(".")) <= 4) + self.assert_(len(VERSION.split(".")) >= 1) + +if __name__ == '__main__': + unittest.main() + raise SystemExit(0)