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
22 """Script for unittesting the uidpool module"""
29 from ganeti import constants
30 from ganeti import uidpool
31 from ganeti import errors
36 class TestUidPool(testutils.GanetiTestCase):
40 self.old_uid_min = constants.UIDPOOL_UID_MIN
41 self.old_uid_max = constants.UIDPOOL_UID_MAX
42 constants.UIDPOOL_UID_MIN = 1
43 constants.UIDPOOL_UID_MAX = 10
44 constants.UIDPOOL_LOCKDIR = tempfile.mkdtemp()
47 constants.UIDPOOL_UID_MIN = self.old_uid_min
48 constants.UIDPOOL_UID_MAX = self.old_uid_max
49 for name in os.listdir(constants.UIDPOOL_LOCKDIR):
50 os.unlink(os.path.join(constants.UIDPOOL_LOCKDIR, name))
51 os.rmdir(constants.UIDPOOL_LOCKDIR)
53 def testParseUidPool(self):
54 self.assertEqualValues(
55 uidpool.ParseUidPool("1-100,200,"),
56 [(1, 100), (200, 200)])
57 self.assertEqualValues(
58 uidpool.ParseUidPool("1000:2000-2500", separator=":"),
59 [(1000, 1000), (2000, 2500)])
60 self.assertEqualValues(
61 uidpool.ParseUidPool("1000\n2000-2500", separator="\n"),
62 [(1000, 1000), (2000, 2500)])
64 def testCheckUidPool(self):
65 # UID < UIDPOOL_UID_MIN
66 self.assertRaises(errors.OpPrereqError,
69 # UID > UIDPOOL_UID_MAX
70 self.assertRaises(errors.OpPrereqError,
73 # lower boundary > higher boundary
74 self.assertRaises(errors.OpPrereqError,
78 def testFormatUidPool(self):
79 self.assertEqualValues(
80 uidpool.FormatUidPool([(1, 100), (200, 200)]),
82 self.assertEqualValues(
83 uidpool.FormatUidPool([(1, 100), (200, 200)], separator=":"),
85 self.assertEqualValues(
86 uidpool.FormatUidPool([(1, 100), (200, 200)], separator="\n"),
89 def testRequestUnusedUid(self):
90 # Check with known used user-ids
92 # Test with user-id "0" and with our own user-id, both
93 # of which are guaranteed to be used user-ids
94 for uid in 0, os.getuid():
95 self.assertRaises(errors.LockError,
96 uidpool.RequestUnusedUid,
99 # Check with a single, known unused user-id
101 # We use "-1" here, which is not a valid user-id, so it's
102 # guaranteed that it's unused.
103 uid = uidpool.RequestUnusedUid(set([-1]))
104 self.assertEqualValues(uid.GetUid(), -1)
106 # Check uid-pool exhaustion
108 # uid "-1" is locked now, so RequestUnusedUid is expected to fail
109 self.assertRaises(errors.LockError,
110 uidpool.RequestUnusedUid,
115 # After unlocking, "-1" should be available again
116 uid = uidpool.RequestUnusedUid(set([-1]))
117 self.assertEqualValues(uid.GetUid(), -1)
120 if __name__ == '__main__':
121 testutils.GanetiTestProgram()