Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.tools.ensure_dirs_unittest.py @ 7578ab0a

History | View | Annotate | Download (5.1 kB)

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 errno
25
import stat
26
import unittest
27
import os.path
28

    
29
from ganeti.tools import ensure_dirs
30

    
31
import testutils
32

    
33

    
34
class TestEnsureDirsFunctions(unittest.TestCase):
35
  def _NoopMkdir(self, _):
36
    self.mkdir_called = True
37

    
38
  @staticmethod
39
  def _MockStatResult(mode, pre_fn=lambda: 0):
40
    def _fn(path):
41
      pre_fn()
42
      return {stat.ST_MODE: mode}
43
    return _fn
44

    
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)
50

    
51
  @staticmethod
52
  def _RaiseNoEntError():
53
    noent_error = EnvironmentError()
54
    noent_error.errno = errno.ENOENT
55
    raise noent_error
56

    
57
  @staticmethod
58
  def _OtherStatRaise():
59
    raise EnvironmentError()
60

    
61
  def _ChmodWrapper(self, pre_fn=lambda: 0):
62
    def _fn(path, mode):
63
      self.chmod_called = True
64
      pre_fn()
65
    return _fn
66

    
67
  def _NoopChown(self, path, uid, gid):
68
    self.chown_called = True
69

    
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)
77

    
78
    self.assertRaises(ensure_dirs.EnsureError, ensure_dirs.EnsureDir,
79
                      "/ganeti-qa-non-test", 0700, 0, 0,
80
                      _stat_fn=not_dir_stat)
81
    self.assertRaises(ensure_dirs.EnsureError, ensure_dirs.EnsureDir,
82
                      "/ganeti-qa-non-test", 0700, 0, 0,
83
                      _stat_fn=other_stat_raise)
84
    self.mkdir_called = False
85
    ensure_dirs.EnsureDir("/ganeti-qa-non-test", 0700, 0, 0,
86
                          _stat_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
                          _stat_fn=is_dir_stat, _ensure_fn=self._VerifyEnsure)
92
    self.assertFalse(self.mkdir_called)
93

    
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)
119

    
120
  def testPaths(self):
121
    paths = [(path[0], path[1]) for path in ensure_dirs.GetPaths()]
122

    
123
    seen = []
124
    last_dirname = ""
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,
132
                                                                   dirname,
133
                                                                   seen))
134
          last_dirname = dirname
135
          seen.append(dirname)
136
        elif pathtype == ensure_dirs.DIR:
137
          self.assertFalse(path in seen)
138
          last_dirname = path
139
          seen.append(path)
140

    
141

    
142
if __name__ == "__main__":
143
  testutils.GanetiTestProgram()