4 # Copyright (C) 2011 Google Inc.
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.
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.
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
22 """Script for testing ganeti.tools.ensure_dirs"""
29 from ganeti.tools import ensure_dirs
34 class TestEnsureDirsFunctions(unittest.TestCase):
35 def _NoopMkdir(self, _):
36 self.mkdir_called = True
39 def _MockStatResult(mode, pre_fn=lambda: 0):
42 return {stat.ST_MODE: mode}
45 def _VerifyEnsure(self, path, mode, uid=-1, gid=-1):
46 self.assertEqual(path, "/ganeti-qa-non-test")
47 self.assertEqual(mode, 0700)
48 self.assertEqual(uid, 0)
49 self.assertEqual(gid, 0)
52 def _RaiseNoEntError():
53 noent_error = EnvironmentError()
54 noent_error.errno = errno.ENOENT
58 def _OtherStatRaise():
59 raise EnvironmentError()
61 def _ChmodWrapper(self, pre_fn=lambda: 0):
63 self.chmod_called = True
67 def _NoopChown(self, path, uid, gid):
68 self.chown_called = True
70 def testEnsureDir(self):
71 is_dir_stat = self._MockStatResult(stat.S_IFDIR)
72 not_dir_stat = self._MockStatResult(0)
73 non_exist_stat = self._MockStatResult(stat.S_IFDIR,
74 pre_fn=self._RaiseNoEntError)
75 other_stat_raise = self._MockStatResult(stat.S_IFDIR,
76 pre_fn=self._OtherStatRaise)
78 self.assertRaises(ensure_dirs.EnsureError, ensure_dirs.EnsureDir,
79 "/ganeti-qa-non-test", 0700, 0, 0,
80 _lstat_fn=not_dir_stat)
81 self.assertRaises(ensure_dirs.EnsureError, ensure_dirs.EnsureDir,
82 "/ganeti-qa-non-test", 0700, 0, 0,
83 _lstat_fn=other_stat_raise)
84 self.mkdir_called = False
85 ensure_dirs.EnsureDir("/ganeti-qa-non-test", 0700, 0, 0,
86 _lstat_fn=non_exist_stat, _mkdir_fn=self._NoopMkdir,
87 _ensure_fn=self._VerifyEnsure)
88 self.assertTrue(self.mkdir_called)
89 self.mkdir_called = False
90 ensure_dirs.EnsureDir("/ganeti-qa-non-test", 0700, 0, 0,
91 _lstat_fn=is_dir_stat, _ensure_fn=self._VerifyEnsure)
92 self.assertFalse(self.mkdir_called)
94 def testEnsurePermission(self):
95 noent_chmod_fn = self._ChmodWrapper(pre_fn=self._RaiseNoEntError)
96 self.assertRaises(ensure_dirs.EnsureError, ensure_dirs.EnsurePermission,
97 "/ganeti-qa-non-test", 0600,
98 _chmod_fn=noent_chmod_fn)
99 self.chmod_called = False
100 ensure_dirs.EnsurePermission("/ganeti-qa-non-test", 0600, must_exist=False,
101 _chmod_fn=noent_chmod_fn)
102 self.assertTrue(self.chmod_called)
103 self.assertRaises(ensure_dirs.EnsureError, ensure_dirs.EnsurePermission,
104 "/ganeti-qa-non-test", 0600, must_exist=False,
105 _chmod_fn=self._ChmodWrapper(pre_fn=self._OtherStatRaise))
106 self.chmod_called = False
107 self.chown_called = False
108 ensure_dirs.EnsurePermission("/ganeti-qa-non-test", 0600,
109 _chmod_fn=self._ChmodWrapper(),
110 _chown_fn=self._NoopChown)
111 self.assertTrue(self.chmod_called)
112 self.assertFalse(self.chown_called)
113 self.chmod_called = False
114 ensure_dirs.EnsurePermission("/ganeti-qa-non-test", 0600, uid=1, gid=1,
115 _chmod_fn=self._ChmodWrapper(),
116 _chown_fn=self._NoopChown)
117 self.assertTrue(self.chmod_called)
118 self.assertTrue(self.chown_called)
121 paths = [(path[0], path[1]) for path in ensure_dirs.GetPaths()]
125 for path, pathtype in paths:
126 self.assertTrue(pathtype in ensure_dirs.ALL_TYPES)
127 dirname = os.path.dirname(path)
128 if dirname != last_dirname or pathtype == ensure_dirs.DIR:
129 if pathtype == ensure_dirs.FILE:
130 self.assertFalse(dirname in seen,
131 msg="path %s; dirname %s seen in %s" % (path,
134 last_dirname = dirname
136 elif pathtype == ensure_dirs.DIR:
137 self.assertFalse(path in seen)
142 if __name__ == "__main__":
143 testutils.GanetiTestProgram()