Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.utils.io_unittest-runasroot.py @ 335c14dc

History | View | Annotate | Download (4.6 kB)

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
import grp
29
import pwd
30
import stat
31

    
32
from ganeti import constants
33
from ganeti import utils
34
from ganeti import compat
35
from ganeti import errors
36

    
37
import testutils
38

    
39

    
40
class TestWriteFile(testutils.GanetiTestCase):
41
  def setUp(self):
42
    testutils.GanetiTestCase.setUp(self)
43
    self.tmpdir = None
44
    self.tfile = tempfile.NamedTemporaryFile()
45
    self.did_pre = False
46
    self.did_post = False
47
    self.did_write = False
48

    
49
  def tearDown(self):
50
    testutils.GanetiTestCase.tearDown(self)
51
    if self.tmpdir:
52
      shutil.rmtree(self.tmpdir)
53

    
54
  def testFileUid(self):
55
    self.tmpdir = tempfile.mkdtemp()
56
    target = utils.PathJoin(self.tmpdir, "target")
57
    tuid = os.geteuid() + 1
58
    utils.WriteFile(target, data="data", uid=tuid + 1)
59
    self.assertFileUid(target, tuid + 1)
60
    utils.WriteFile(target, data="data", uid=tuid)
61
    self.assertFileUid(target, tuid)
62
    utils.WriteFile(target, data="data", uid=tuid + 1,
63
                    keep_perms=utils.KP_IF_EXISTS)
64
    self.assertFileUid(target, tuid)
65
    utils.WriteFile(target, data="data", keep_perms=utils.KP_ALWAYS)
66
    self.assertFileUid(target, tuid)
67

    
68
  def testNewFileUid(self):
69
    self.tmpdir = tempfile.mkdtemp()
70
    target = utils.PathJoin(self.tmpdir, "target")
71
    tuid = os.geteuid() + 1
72
    utils.WriteFile(target, data="data", uid=tuid,
73
                    keep_perms=utils.KP_IF_EXISTS)
74
    self.assertFileUid(target, tuid)
75

    
76
  def testFileGid(self):
77
    self.tmpdir = tempfile.mkdtemp()
78
    target = utils.PathJoin(self.tmpdir, "target")
79
    tgid = os.getegid() + 1
80
    utils.WriteFile(target, data="data", gid=tgid + 1)
81
    self.assertFileGid(target, tgid + 1)
82
    utils.WriteFile(target, data="data", gid=tgid)
83
    self.assertFileGid(target, tgid)
84
    utils.WriteFile(target, data="data", gid=tgid + 1,
85
                    keep_perms=utils.KP_IF_EXISTS)
86
    self.assertFileGid(target, tgid)
87
    utils.WriteFile(target, data="data", keep_perms=utils.KP_ALWAYS)
88
    self.assertFileGid(target, tgid)
89

    
90
  def testNewFileGid(self):
91
    self.tmpdir = tempfile.mkdtemp()
92
    target = utils.PathJoin(self.tmpdir, "target")
93
    tgid = os.getegid() + 1
94
    utils.WriteFile(target, data="data", gid=tgid,
95
                    keep_perms=utils.KP_IF_EXISTS)
96
    self.assertFileGid(target, tgid)
97

    
98
class TestCanRead(testutils.GanetiTestCase):
99
  def setUp(self):
100
    testutils.GanetiTestCase.setUp(self)
101
    self.tmpdir = tempfile.mkdtemp()
102
    self.confdUid = pwd.getpwnam(constants.CONFD_USER).pw_uid
103
    self.masterdUid = pwd.getpwnam(constants.MASTERD_USER).pw_uid
104
    self.masterdGid = grp.getgrnam(constants.MASTERD_GROUP).gr_gid
105

    
106
  def tearDown(self):
107
    testutils.GanetiTestCase.tearDown(self)
108
    if self.tmpdir:
109
      shutil.rmtree(self.tmpdir)
110

    
111
  def testUserCanRead(self):
112
    target = utils.PathJoin(self.tmpdir, "target1")
113
    f=open(target, "w")
114
    f.close()
115
    utils.EnforcePermission(target, 0400, uid=self.confdUid,
116
                            gid=self.masterdGid)
117
    self.assertTrue(utils.CanRead(constants.CONFD_USER, target))
118
    if constants.CONFD_USER != constants.MASTERD_USER:
119
      self.assertFalse(utils.CanRead(constants.MASTERD_USER, target))
120

    
121
  def testGroupCanRead(self):
122
    target = utils.PathJoin(self.tmpdir, "target2")
123
    f=open(target, "w")
124
    f.close()
125
    utils.EnforcePermission(target, 0040, uid=self.confdUid,
126
                            gid=self.masterdGid)
127
    self.assertFalse(utils.CanRead(constants.CONFD_USER, target))
128
    if constants.CONFD_USER != constants.MASTERD_USER:
129
      self.assertTrue(utils.CanRead(constants.MASTERD_USER, target))
130

    
131
    utils.EnforcePermission(target, 0040, uid=self.masterdUid+1,
132
                            gid=self.masterdGid)
133
    self.assertTrue(utils.CanRead(constants.MASTERD_USER, target))
134

    
135

    
136
if __name__ == "__main__":
137
  testutils.GanetiTestProgram()