From c6b9a727788cc0355b1e18d46fcd4186b2af4dd7 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Fri, 1 Apr 2016 16:34:37 -0700 Subject: [PATCH] tests: Add sysutils test (cherry picked from commit cc69478249e9bbe82af44409d16b941e89781725) --- tests/pylorax/test_sysutils.py | 74 ++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/pylorax/test_sysutils.py diff --git a/tests/pylorax/test_sysutils.py b/tests/pylorax/test_sysutils.py new file mode 100644 index 00000000..8b33e7a0 --- /dev/null +++ b/tests/pylorax/test_sysutils.py @@ -0,0 +1,74 @@ +# +# Copyright (C) 2018 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +import unittest +import tempfile +import os + +from pylorax.sysutils import joinpaths, touch, replace, chown_, chmod_, remove, linktree + +class SysUtilsTest(unittest.TestCase): + def joinpaths_test(self): + self.assertEqual(joinpaths("foo", "bar", "baz"), "foo/bar/baz") + + with tempfile.TemporaryDirectory() as tdname: + open(os.path.join(tdname, "real-file"), "w").write("lorax test file") + os.symlink(os.path.join(tdname, "real-file"), os.path.join(tdname, "link-file")) + + self.assertEqual(joinpaths(tdname, "link-file", follow_symlinks=True), + os.path.join(tdname, "real-file")) + + def touch_test(self): + touch_file="/var/tmp/lorax-test-touch-file" + touch(touch_file) + + self.assertTrue(os.path.exists(touch_file)) + os.unlink(touch_file) + + def replace_test(self): + f = tempfile.NamedTemporaryFile(mode="w+t", delete=False) + f.write("A few words to apply @AARDVARKS@ testing\n") + f.close() + replace(f.name, "@AARDVARKS@", "ant eaters") + + self.assertEqual(open(f.name).readline(), "A few words to apply ant eaters testing\n") + os.unlink(f.name) + + @unittest.skipUnless(os.geteuid() == 0, "requires root privileges") + def chown_test(self): + with tempfile.NamedTemporaryFile() as f: + chown_(f.name, "nobody", "nobody") + + def chmod_test(self): + with tempfile.NamedTemporaryFile() as f: + chmod_(f.name, 0o777) + self.assertEqual(os.stat(f.name).st_mode, 0o100777) + + def remove_test(self): + remove_file="/var/tmp/lorax-test-remove-file" + open(remove_file, "w").write("test was here") + remove(remove_file) + self.assertFalse(os.path.exists(remove_file)) + + def linktree_test(self): + with tempfile.TemporaryDirectory() as tdname: + path = os.path.join("one", "two", "three") + os.makedirs(os.path.join(tdname, path)) + open(os.path.join(tdname, path, "lorax-link-test-file"), "w").write("test was here") + + linktree(os.path.join(tdname, "one"), os.path.join(tdname, "copy")) + + self.assertTrue(os.path.exists(os.path.join(tdname, "copy", "two", "three", "lorax-link-test-file")))