4 # Copyright (C) 2010 Google Inc.
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.
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.
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
21 """Script for testing ganeti.runtime"""
23 from ganeti import constants
24 from ganeti import errors
25 from ganeti import runtime
31 def __init__(self, uid=None, gid=None):
36 def _StubGetpwnam(user):
38 constants.MASTERD_USER: _EntStub(uid=0),
39 constants.CONFD_USER: _EntStub(uid=1),
40 constants.RAPI_USER: _EntStub(uid=2),
41 constants.NODED_USER: _EntStub(uid=3),
46 def _StubGetgrnam(group):
48 constants.MASTERD_GROUP: _EntStub(gid=0),
49 constants.CONFD_GROUP: _EntStub(gid=1),
50 constants.RAPI_GROUP: _EntStub(gid=2),
51 constants.DAEMONS_GROUP: _EntStub(gid=3),
52 constants.ADMIN_GROUP: _EntStub(gid=4),
57 def _RaisingStubGetpwnam(user):
58 raise KeyError("user not found")
61 def _RaisingStubGetgrnam(group):
62 raise KeyError("group not found")
65 class ResolverStubRaising(object):
67 raise errors.ConfigurationError("No entries")
70 class TestErrors(testutils.GanetiTestCase):
71 def testEverythingSuccessful(self):
72 resolver = runtime.GetentResolver(_getpwnam=_StubGetpwnam,
73 _getgrnam=_StubGetgrnam)
75 self.assertEqual(resolver.masterd_uid,
76 _StubGetpwnam(constants.MASTERD_USER).pw_uid)
77 self.assertEqual(resolver.masterd_gid,
78 _StubGetgrnam(constants.MASTERD_GROUP).gr_gid)
79 self.assertEqual(resolver.confd_uid,
80 _StubGetpwnam(constants.CONFD_USER).pw_uid)
81 self.assertEqual(resolver.confd_gid,
82 _StubGetgrnam(constants.CONFD_GROUP).gr_gid)
83 self.assertEqual(resolver.rapi_uid,
84 _StubGetpwnam(constants.RAPI_USER).pw_uid)
85 self.assertEqual(resolver.rapi_gid,
86 _StubGetgrnam(constants.RAPI_GROUP).gr_gid)
87 self.assertEqual(resolver.noded_uid,
88 _StubGetpwnam(constants.NODED_USER).pw_uid)
90 self.assertEqual(resolver.daemons_gid,
91 _StubGetgrnam(constants.DAEMONS_GROUP).gr_gid)
92 self.assertEqual(resolver.admin_gid,
93 _StubGetgrnam(constants.ADMIN_GROUP).gr_gid)
95 def testUserNotFound(self):
96 self.assertRaises(errors.ConfigurationError, runtime.GetentResolver,
97 _getpwnam=_RaisingStubGetpwnam, _getgrnam=_StubGetgrnam)
99 def testGroupNotFound(self):
100 self.assertRaises(errors.ConfigurationError, runtime.GetentResolver,
101 _getpwnam=_StubGetpwnam, _getgrnam=_RaisingStubGetgrnam)
103 def testUserNotFoundGetEnts(self):
104 self.assertRaises(errors.ConfigurationError, runtime.GetEnts,
105 resolver=ResolverStubRaising)
108 if __name__ == "__main__":
109 testutils.GanetiTestProgram()