QA: Add test for “gnt-node modify”
[ganeti-local] / test / ganeti.runtime_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2010 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 """Script for testing ganeti.runtime"""
22
23 from ganeti import constants
24 from ganeti import errors
25 from ganeti import runtime
26
27 import testutils
28
29
30 class _EntStub:
31   def __init__(self, uid=None, gid=None):
32     self.pw_uid = uid
33     self.gr_gid = gid
34
35
36 def _StubGetpwnam(user):
37   users = {
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),
42     }
43   return users[user]
44
45
46 def _StubGetgrnam(group):
47   groups = {
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),
53     }
54   return groups[group]
55
56
57 def _RaisingStubGetpwnam(user):
58   raise KeyError("user not found")
59
60
61 def _RaisingStubGetgrnam(group):
62   raise KeyError("group not found")
63
64
65 class ResolverStubRaising(object):
66   def __init__(self):
67     raise errors.ConfigurationError("No entries")
68
69
70 class TestErrors(testutils.GanetiTestCase):
71   def testEverythingSuccessful(self):
72     resolver = runtime.GetentResolver(_getpwnam=_StubGetpwnam,
73                                       _getgrnam=_StubGetgrnam)
74
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)
89
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)
94
95   def testUserNotFound(self):
96     self.assertRaises(errors.ConfigurationError, runtime.GetentResolver,
97                       _getpwnam=_RaisingStubGetpwnam, _getgrnam=_StubGetgrnam)
98
99   def testGroupNotFound(self):
100     self.assertRaises(errors.ConfigurationError, runtime.GetentResolver,
101                       _getpwnam=_StubGetpwnam, _getgrnam=_RaisingStubGetgrnam)
102
103   def testUserNotFoundGetEnts(self):
104     self.assertRaises(errors.ConfigurationError, runtime.GetEnts,
105                       resolver=ResolverStubRaising)
106
107
108 if __name__ == "__main__":
109   testutils.GanetiTestProgram()