Merge branch 'stable-2.6-esi' into stable-2.6-ippool-hotplug-esi
[ganeti-local] / test / ganeti.utils.io_unittest-runasroot.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2006, 2007, 2010, 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.utils.io (tests that require 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 errors
33
34 import testutils
35
36
37 class TestWriteFile(testutils.GanetiTestCase):
38   def setUp(self):
39     testutils.GanetiTestCase.setUp(self)
40     self.tmpdir = None
41     self.tfile = tempfile.NamedTemporaryFile()
42     self.did_pre = False
43     self.did_post = False
44     self.did_write = False
45
46   def tearDown(self):
47     testutils.GanetiTestCase.tearDown(self)
48     if self.tmpdir:
49       shutil.rmtree(self.tmpdir)
50
51   def testFileUid(self):
52     self.tmpdir = tempfile.mkdtemp()
53     target = utils.PathJoin(self.tmpdir, "target")
54     tuid = os.geteuid() + 1
55     utils.WriteFile(target, data="data", uid=tuid + 1)
56     self.assertFileUid(target, tuid + 1)
57     utils.WriteFile(target, data="data", uid=tuid)
58     self.assertFileUid(target, tuid)
59     utils.WriteFile(target, data="data", uid=tuid + 1,
60                     keep_perms=utils.KP_IF_EXISTS)
61     self.assertFileUid(target, tuid)
62     utils.WriteFile(target, data="data", keep_perms=utils.KP_ALWAYS)
63     self.assertFileUid(target, tuid)
64
65   def testNewFileUid(self):
66     self.tmpdir = tempfile.mkdtemp()
67     target = utils.PathJoin(self.tmpdir, "target")
68     tuid = os.geteuid() + 1
69     utils.WriteFile(target, data="data", uid=tuid,
70                     keep_perms=utils.KP_IF_EXISTS)
71     self.assertFileUid(target, tuid)
72
73   def testFileGid(self):
74     self.tmpdir = tempfile.mkdtemp()
75     target = utils.PathJoin(self.tmpdir, "target")
76     tgid = os.getegid() + 1
77     utils.WriteFile(target, data="data", gid=tgid + 1)
78     self.assertFileGid(target, tgid + 1)
79     utils.WriteFile(target, data="data", gid=tgid)
80     self.assertFileGid(target, tgid)
81     utils.WriteFile(target, data="data", gid=tgid + 1,
82                     keep_perms=utils.KP_IF_EXISTS)
83     self.assertFileGid(target, tgid)
84     utils.WriteFile(target, data="data", keep_perms=utils.KP_ALWAYS)
85     self.assertFileGid(target, tgid)
86
87   def testNewFileGid(self):
88     self.tmpdir = tempfile.mkdtemp()
89     target = utils.PathJoin(self.tmpdir, "target")
90     tgid = os.getegid() + 1
91     utils.WriteFile(target, data="data", gid=tgid,
92                     keep_perms=utils.KP_IF_EXISTS)
93     self.assertFileGid(target, tgid)
94
95
96 if __name__ == "__main__":
97   testutils.GanetiTestProgram()