Commit e37f47d3 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Improve test for tools.ensure_dirs

- Add more checks, some of them are deliberately redundant
- Descriptive error messages
- Add comment describing order to “tools.ensure_dirs”
- Avoid copying a list in an assertion in “tools.ensure_dirs”
Signed-off-by: default avatarMichael Hanselmann <>
Reviewed-by: default avatarGuido Trotter <>
parent 164947cc
......@@ -102,7 +102,7 @@ def ProcessPath(path):
if pathtype in (DIR, QUEUE_DIR):
# No additional parameters
assert len(path[5:]) == 0
assert len(path) == 5
if pathtype == DIR:
utils.MakeDirWithPerm(pathname, mode, uid, gid)
elif pathtype == QUEUE_DIR:
......@@ -127,6 +127,9 @@ def GetPaths():
cleaner_log_dir = os.path.join(pathutils.LOG_DIR, "cleaner")
master_cleaner_log_dir = os.path.join(pathutils.LOG_DIR, "master-cleaner")
# A note on the ordering: The parent directory (type C{DIR}) must always be
# listed before files (type C{FILE}) in that directory. Once the directory is
# set, only files directly in that directory can be listed.
paths = [
(pathutils.DATA_DIR, DIR, 0755, getent.masterd_uid, getent.masterd_gid),
......@@ -24,32 +24,55 @@
import unittest
import os.path
from ganeti import utils
from import ensure_dirs
import testutils
class TestEnsureDirsFunctions(unittest.TestCase):
def testPaths(self):
class TestGetPaths(unittest.TestCase):
def testEntryOrder(self):
paths = [(path[0], path[1]) for path in ensure_dirs.GetPaths()]
seen = []
last_dirname = ""
for path, pathtype in paths:
# Directories for which permissions have been set
seen = set()
# Current directory (changes when an entry of type C{DIR} or C{QUEUE_DIR}
# is encountered)
current_dir = None
for (path, pathtype) in paths:
self.assertTrue(pathtype in ensure_dirs.ALL_TYPES)
msg=("Path '%s' is not absolute and/or normalized" %
dirname = os.path.dirname(path)
if dirname != last_dirname or pathtype == ensure_dirs.DIR:
if pathtype == ensure_dirs.FILE:
self.assertFalse(dirname in seen,
msg="path %s; dirname %s seen in %s" % (path,
last_dirname = dirname
elif pathtype == ensure_dirs.DIR:
self.assertFalse(path in seen)
last_dirname = path
if pathtype == ensure_dirs.DIR:
self.assertFalse(path in seen,
msg=("Directory '%s' was seen before" % path))
current_dir = path
elif pathtype == ensure_dirs.QUEUE_DIR:
self.assertTrue(dirname in seen,
msg=("Queue directory '%s' was not seen before" %
current_dir = path
elif pathtype == ensure_dirs.FILE:
self.assertFalse(current_dir is None)
self.assertTrue(dirname in seen,
msg=("Directory '%s' of path '%s' has not been seen"
" yet" % (dirname, path)))
self.assertTrue((utils.IsBelowDir(current_dir, path) and
current_dir == dirname),
msg=("File '%s' not below current directory '%s'" %
(path, current_dir)))
else:"Unknown path type '%s'" % (pathtype, ))
if __name__ == "__main__":
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment