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
32 from ganeti import pathutils
37 class TestUidPool(testutils.GanetiTestCase):
41 self.old_uid_min = constants.UIDPOOL_UID_MIN
42 self.old_uid_max = constants.UIDPOOL_UID_MAX
43 constants.UIDPOOL_UID_MIN = 1
44 constants.UIDPOOL_UID_MAX = 10
45 pathutils.UIDPOOL_LOCKDIR = tempfile.mkdtemp()
48 constants.UIDPOOL_UID_MIN = self.old_uid_min
49 constants.UIDPOOL_UID_MAX = self.old_uid_max
50 for name in os.listdir(pathutils.UIDPOOL_LOCKDIR):
51 os.unlink(os.path.join(pathutils.UIDPOOL_LOCKDIR, name))
52 os.rmdir(pathutils.UIDPOOL_LOCKDIR)
54 def testParseUidPool(self):
55 self.assertEqualValues(
56 uidpool.ParseUidPool("1-100,200,"),
57 [(1, 100), (200, 200)])
58 self.assertEqualValues(
59 uidpool.ParseUidPool("1000:2000-2500", separator=":"),
60 [(1000, 1000), (2000, 2500)])
61 self.assertEqualValues(
62 uidpool.ParseUidPool("1000\n2000-2500", separator="\n"),
63 [(1000, 1000), (2000, 2500)])
65 def testCheckUidPool(self):
66 # UID < UIDPOOL_UID_MIN
67 self.assertRaises(errors.OpPrereqError,
70 # UID > UIDPOOL_UID_MAX
71 self.assertRaises(errors.OpPrereqError,
74 # lower boundary > higher boundary
75 self.assertRaises(errors.OpPrereqError,
79 def testFormatUidPool(self):
80 self.assertEqualValues(
81 uidpool.FormatUidPool([(1, 100), (200, 200)]),
83 self.assertEqualValues(
84 uidpool.FormatUidPool([(1, 100), (200, 200)], separator=":"),
86 self.assertEqualValues(
87 uidpool.FormatUidPool([(1, 100), (200, 200)], separator="\n"),
90 def testRequestUnusedUid(self):
91 # Check with known used user-ids
93 # Test with user-id "0" and with our own user-id, both
94 # of which are guaranteed to be used user-ids
95 for uid in 0, os.getuid():
96 self.assertRaises(errors.LockError,
97 uidpool.RequestUnusedUid,
100 # Check with a single, known unused user-id
102 # We use "-1" here, which is not a valid user-id, so it's
103 # guaranteed that it's unused.
104 uid = uidpool.RequestUnusedUid(set([-1]))
105 self.assertEqualValues(uid.GetUid(), -1)
107 # Check uid-pool exhaustion
109 # uid "-1" is locked now, so RequestUnusedUid is expected to fail
110 self.assertRaises(errors.LockError,
111 uidpool.RequestUnusedUid,
116 # After unlocking, "-1" should be available again
117 uid = uidpool.RequestUnusedUid(set([-1]))
118 self.assertEqualValues(uid.GetUid(), -1)
121 if __name__ == '__main__':
122 testutils.GanetiTestProgram()