Revision 0d20cc42

b/Makefile.am
742 742
	test/docs_unittest.py \
743 743
	test/pycurl_reset_unittest.py \
744 744
	test/tempfile_fork_unittest.py
745
if HAS_FAKEROOT
746
python_tests += test/ganeti.utils.io_unittest-runasroot.py
747
endif
745 748

  
746 749
haskell_tests = htools/test
747 750

  
b/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()
b/test/testutils.py
136 136
    actual_mode = stat.S_IMODE(st.st_mode)
137 137
    self.assertEqual(actual_mode, expected_mode)
138 138

  
139
  def assertFileUid(self, file_name, expected_uid):
140
    """Checks that the user id of a file is what we expect.
141

  
142
    @type file_name: str
143
    @param file_name: the file whose contents we should check
144
    @type expected_uid: int
145
    @param expected_uid: the user id we expect
146

  
147
    """
148
    st = os.stat(file_name)
149
    actual_uid = st.st_uid
150
    self.assertEqual(actual_uid, expected_uid)
151

  
152
  def assertFileGid(self, file_name, expected_gid):
153
    """Checks that the group id of a file is what we expect.
154

  
155
    @type file_name: str
156
    @param file_name: the file whose contents we should check
157
    @type expected_gid: int
158
    @param expected_gid: the group id we expect
159

  
160
    """
161
    st = os.stat(file_name)
162
    actual_gid = st.st_gid
163
    self.assertEqual(actual_gid, expected_gid)
164

  
139 165
  def assertEqualValues(self, first, second, msg=None):
140 166
    """Compares two values whether they're equal.
141 167

  

Also available in: Unified diff