Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.runtime_unittest.py @ 560ef132

History | View | Annotate | Download (5.8 kB)

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
from ganeti import ht
27

    
28
import testutils
29
import unittest
30

    
31

    
32
class _EntStub:
33
  def __init__(self, uid=None, gid=None):
34
    self.pw_uid = uid
35
    self.gr_gid = gid
36

    
37

    
38
def _StubGetpwnam(user):
39
  users = {
40
    constants.MASTERD_USER: _EntStub(uid=0),
41
    constants.CONFD_USER: _EntStub(uid=1),
42
    constants.RAPI_USER: _EntStub(uid=2),
43
    constants.NODED_USER: _EntStub(uid=3),
44
    constants.LUXID_USER: _EntStub(uid=4),
45
    }
46
  return users[user]
47

    
48

    
49
def _StubGetgrnam(group):
50
  groups = {
51
    constants.MASTERD_GROUP: _EntStub(gid=0),
52
    constants.CONFD_GROUP: _EntStub(gid=1),
53
    constants.RAPI_GROUP: _EntStub(gid=2),
54
    constants.DAEMONS_GROUP: _EntStub(gid=3),
55
    constants.ADMIN_GROUP: _EntStub(gid=4),
56
    constants.NODED_GROUP: _EntStub(gid=5),
57
    constants.LUXID_GROUP: _EntStub(gid=6),
58
    }
59
  return groups[group]
60

    
61

    
62
def _RaisingStubGetpwnam(user):
63
  raise KeyError("user not found")
64

    
65

    
66
def _RaisingStubGetgrnam(group):
67
  raise KeyError("group not found")
68

    
69

    
70
class ResolverStubRaising(object):
71
  def __init__(self):
72
    raise errors.ConfigurationError("No entries")
73

    
74

    
75
class TestErrors(unittest.TestCase):
76
  def setUp(self):
77
    self.resolver = runtime.GetentResolver(_getpwnam=_StubGetpwnam,
78
                                           _getgrnam=_StubGetgrnam)
79

    
80
  def testEverythingSuccessful(self):
81
    self.assertEqual(self.resolver.masterd_uid,
82
                     _StubGetpwnam(constants.MASTERD_USER).pw_uid)
83
    self.assertEqual(self.resolver.masterd_gid,
84
                     _StubGetgrnam(constants.MASTERD_GROUP).gr_gid)
85
    self.assertEqual(self.resolver.confd_uid,
86
                     _StubGetpwnam(constants.CONFD_USER).pw_uid)
87
    self.assertEqual(self.resolver.confd_gid,
88
                     _StubGetgrnam(constants.CONFD_GROUP).gr_gid)
89
    self.assertEqual(self.resolver.rapi_uid,
90
                     _StubGetpwnam(constants.RAPI_USER).pw_uid)
91
    self.assertEqual(self.resolver.rapi_gid,
92
                     _StubGetgrnam(constants.RAPI_GROUP).gr_gid)
93
    self.assertEqual(self.resolver.noded_uid,
94
                     _StubGetpwnam(constants.NODED_USER).pw_uid)
95

    
96
    self.assertEqual(self.resolver.daemons_gid,
97
                     _StubGetgrnam(constants.DAEMONS_GROUP).gr_gid)
98
    self.assertEqual(self.resolver.admin_gid,
99
                     _StubGetgrnam(constants.ADMIN_GROUP).gr_gid)
100

    
101
  def testUserNotFound(self):
102
    self.assertRaises(errors.ConfigurationError, runtime.GetentResolver,
103
                      _getpwnam=_RaisingStubGetpwnam, _getgrnam=_StubGetgrnam)
104

    
105
  def testGroupNotFound(self):
106
    self.assertRaises(errors.ConfigurationError, runtime.GetentResolver,
107
                      _getpwnam=_StubGetpwnam, _getgrnam=_RaisingStubGetgrnam)
108

    
109
  def testUserNotFoundGetEnts(self):
110
    self.assertRaises(errors.ConfigurationError, runtime.GetEnts,
111
                      resolver=ResolverStubRaising)
112

    
113
  def testLookupForUser(self):
114
    master_stub = _StubGetpwnam(constants.MASTERD_USER)
115
    rapi_stub = _StubGetpwnam(constants.RAPI_USER)
116
    self.assertEqual(self.resolver.LookupUid(master_stub.pw_uid),
117
                     constants.MASTERD_USER)
118
    self.assertEqual(self.resolver.LookupUid(rapi_stub.pw_uid),
119
                     constants.RAPI_USER)
120
    self.assertEqual(self.resolver.LookupUser(constants.MASTERD_USER),
121
                     master_stub.pw_uid)
122
    self.assertEqual(self.resolver.LookupUser(constants.RAPI_USER),
123
                     rapi_stub.pw_uid)
124

    
125
  def testLookupForGroup(self):
126
    master_stub = _StubGetgrnam(constants.MASTERD_GROUP)
127
    rapi_stub = _StubGetgrnam(constants.RAPI_GROUP)
128
    self.assertEqual(self.resolver.LookupGid(master_stub.gr_gid),
129
                     constants.MASTERD_GROUP)
130
    self.assertEqual(self.resolver.LookupGid(rapi_stub.gr_gid),
131
                     constants.RAPI_GROUP)
132

    
133
  def testLookupForUserNotFound(self):
134
    self.assertRaises(errors.ConfigurationError, self.resolver.LookupUid, 9999)
135
    self.assertRaises(errors.ConfigurationError,
136
                      self.resolver.LookupUser, "does-not-exist-foo")
137

    
138
  def testLookupForGroupNotFound(self):
139
    self.assertRaises(errors.ConfigurationError, self.resolver.LookupGid, 9999)
140
    self.assertRaises(errors.ConfigurationError,
141
                      self.resolver.LookupGroup, "does-not-exist-foo")
142

    
143

    
144
class TestArchInfo(unittest.TestCase):
145
  EXP_TYPES = \
146
    ht.TAnd(ht.TIsLength(2),
147
            ht.TItems([
148
              ht.TNonEmptyString,
149
              ht.TNonEmptyString,
150
              ]))
151

    
152
  def setUp(self):
153
    self.assertTrue(runtime._arch is None)
154

    
155
  def tearDown(self):
156
    runtime._arch = None
157

    
158
  def testNotInitialized(self):
159
    self.assertRaises(errors.ProgrammerError, runtime.GetArchInfo)
160

    
161
  def testInitializeMultiple(self):
162
    runtime.InitArchInfo()
163

    
164
    self.assertRaises(errors.ProgrammerError, runtime.InitArchInfo)
165

    
166
  def testNormal(self):
167
    runtime.InitArchInfo()
168

    
169
    info = runtime.GetArchInfo()
170

    
171
    self.assertTrue(self.EXP_TYPES(info),
172
                    msg=("Doesn't match expected type description: %s" %
173
                         self.EXP_TYPES))
174

    
175

    
176
if __name__ == "__main__":
177
  testutils.GanetiTestProgram()