summaryrefslogtreecommitdiff
path: root/tests/unittests/test_util.py
diff options
context:
space:
mode:
authorMike Milner <mike.milner@canonical.com>2012-01-17 11:47:12 -0400
committerMike Milner <mike.milner@canonical.com>2012-01-17 11:47:12 -0400
commit44ea733b7a462442fe2cc3c858001c0cd909dd1b (patch)
tree0761d92233a90b021d8076846359cf24ba36abfc /tests/unittests/test_util.py
parentdfadfb05c1d8fcb50f97952a6ba7ca662babeba4 (diff)
downloadvyos-cloud-init-44ea733b7a462442fe2cc3c858001c0cd909dd1b.tar.gz
vyos-cloud-init-44ea733b7a462442fe2cc3c858001c0cd909dd1b.zip
Added delete_dir_contents function to cloudinit.util.
Diffstat (limited to 'tests/unittests/test_util.py')
-rw-r--r--tests/unittests/test_util.py72
1 files changed, 71 insertions, 1 deletions
diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py
index 4c512990..f2b2ee3d 100644
--- a/tests/unittests/test_util.py
+++ b/tests/unittests/test_util.py
@@ -5,7 +5,7 @@ from shutil import rmtree
import os
import stat
-from cloudinit.util import mergedict, get_cfg_option_list_or_str, write_file
+from cloudinit.util import mergedict, get_cfg_option_list_or_str, write_file, delete_dir_contents
class TestMergeDict(TestCase):
def test_simple_merge(self):
@@ -173,3 +173,73 @@ class TestWriteFile(MockerTestCase):
self.mocker.replay()
write_file(path, contents)
+
+class TestDeleteDirContents(TestCase):
+ def setUp(self):
+ super(TestDeleteDirContents, self).setUp()
+ # Make a temp directoy for tests to use.
+ self.tmp = mkdtemp(prefix="unittest_")
+
+ def tearDown(self):
+ super(TestDeleteDirContents, self).tearDown()
+ # Clean up temp directory
+ rmtree(self.tmp)
+
+ def assertDirEmpty(self, dirname):
+ self.assertEqual([], os.listdir(dirname))
+
+ def test_does_not_delete_dir(self):
+ """Ensure directory itself is not deleted."""
+ delete_dir_contents(self.tmp)
+
+ self.assertTrue(os.path.isdir(self.tmp))
+ self.assertDirEmpty(self.tmp)
+
+ def test_deletes_files(self):
+ """Single file should be deleted."""
+ with open(os.path.join(self.tmp, "new_file.txt"), "wb") as f:
+ f.write("DELETE ME")
+
+ delete_dir_contents(self.tmp)
+
+ self.assertDirEmpty(self.tmp)
+
+ def test_deletes_empty_dirs(self):
+ """Empty directories should be deleted."""
+ os.mkdir(os.path.join(self.tmp, "new_dir"))
+
+ delete_dir_contents(self.tmp)
+
+ self.assertDirEmpty(self.tmp)
+
+ def test_deletes_nested_dirs(self):
+ """Nested directories should be deleted."""
+ os.mkdir(os.path.join(self.tmp, "new_dir"))
+ os.mkdir(os.path.join(self.tmp, "new_dir", "new_subdir"))
+
+ delete_dir_contents(self.tmp)
+
+ self.assertDirEmpty(self.tmp)
+
+ def test_deletes_non_empty_dirs(self):
+ """Non-empty directories should be deleted."""
+ os.mkdir(os.path.join(self.tmp, "new_dir"))
+ f_name = os.path.join(self.tmp, "new_dir", "new_file.txt")
+ with open(f_name, "wb") as f:
+ f.write("DELETE ME")
+
+ delete_dir_contents(self.tmp)
+
+ self.assertDirEmpty(self.tmp)
+
+ def test_deletes_symlinks(self):
+ """Symlinks should be deleted."""
+ file_name = os.path.join(self.tmp, "new_file.txt")
+ link_name = os.path.join(self.tmp, "new_file_link.txt")
+ with open(file_name, "wb") as f:
+ f.write("DELETE ME")
+ os.symlink(file_name, link_name)
+
+ delete_dir_contents(self.tmp)
+
+ self.assertDirEmpty(self.tmp)