locking: Change locking order, move NAL after instances
[ganeti-local] / test / ganeti.backend_unittest-runasroot.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2012 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.backend (tests requiring root access)"""
23
24 import os
25 import tempfile
26 import shutil
27 import errno
28
29 from ganeti import constants
30 from ganeti import utils
31 from ganeti import compat
32 from ganeti import backend
33
34 import testutils
35
36
37 class TestWriteFile(testutils.GanetiTestCase):
38   def setUp(self):
39     self.tmpdir = tempfile.mkdtemp()
40
41   def tearDown(self):
42     shutil.rmtree(self.tmpdir)
43
44   def _PrepareTest(self):
45     tmpname = utils.PathJoin(self.tmpdir, "foobar")
46     os.mkdir(tmpname)
47     os.chmod(tmpname, 0700)
48     return tmpname
49
50   def testCorrectOwner(self):
51     tmpname = self._PrepareTest()
52
53     os.chown(tmpname, 0, 0)
54     (status, value) = backend._CommonRestrictedCmdCheck(tmpname, None)
55     self.assertTrue(status)
56     self.assertTrue(value)
57
58   def testWrongOwner(self):
59     tmpname = self._PrepareTest()
60
61     tests = [
62       (1, 0),
63       (0, 1),
64       (100, 50),
65       ]
66
67     for (uid, gid) in tests:
68       self.assertFalse(uid == os.getuid() and gid == os.getgid())
69       os.chown(tmpname, uid, gid)
70
71       (status, errmsg) = backend._CommonRestrictedCmdCheck(tmpname, None)
72       self.assertFalse(status)
73       self.assertTrue("foobar' is not owned by " in errmsg)
74
75
76 if __name__ == "__main__":
77   testutils.GanetiTestProgram()