Merge branch 'stable-2.7' into stable-2.8
[ganeti-local] / test / py / ganeti.tools.ensure_dirs_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2011 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21
22 """Script for testing ganeti.tools.ensure_dirs"""
23
24 import unittest
25 import os.path
26
27 from ganeti import utils
28 from ganeti.tools import ensure_dirs
29
30 import testutils
31
32
33 class TestGetPaths(unittest.TestCase):
34   def testEntryOrder(self):
35     paths = [(path[0], path[1]) for path in ensure_dirs.GetPaths()]
36
37     # Directories for which permissions have been set
38     seen = set()
39
40     # Current directory (changes when an entry of type C{DIR} or C{QUEUE_DIR}
41     # is encountered)
42     current_dir = None
43
44     for (path, pathtype) in paths:
45       self.assertTrue(pathtype in ensure_dirs.ALL_TYPES)
46       self.assertTrue(utils.IsNormAbsPath(path),
47                       msg=("Path '%s' is not absolute and/or normalized" %
48                            path))
49
50       dirname = os.path.dirname(path)
51
52       if pathtype == ensure_dirs.DIR:
53         self.assertFalse(path in seen,
54                          msg=("Directory '%s' was seen before" % path))
55         current_dir = path
56         seen.add(path)
57
58       elif pathtype == ensure_dirs.QUEUE_DIR:
59         self.assertTrue(dirname in seen,
60                         msg=("Queue directory '%s' was not seen before" %
61                              path))
62         current_dir = path
63
64       elif pathtype == ensure_dirs.FILE:
65         self.assertFalse(current_dir is None)
66         self.assertTrue(dirname in seen,
67                         msg=("Directory '%s' of path '%s' has not been seen"
68                              " yet" % (dirname, path)))
69         self.assertTrue((utils.IsBelowDir(current_dir, path) and
70                          current_dir == dirname),
71                         msg=("File '%s' not below current directory '%s'" %
72                              (path, current_dir)))
73
74       else:
75         self.fail("Unknown path type '%s'" % (pathtype, ))
76
77
78 if __name__ == "__main__":
79   testutils.GanetiTestProgram()