Migration and failover: add iallocator and target_node slots
[ganeti-local] / test / ganeti.uidpool_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 # 0.0510-1301, USA.
20
21
22 """Script for unittesting the uidpool module"""
23
24
25 import os
26 import tempfile
27 import unittest
28
29 from ganeti import constants
30 from ganeti import uidpool
31 from ganeti import errors
32
33 import testutils
34
35
36 class TestUidPool(testutils.GanetiTestCase):
37   """Uid-pool tests"""
38
39   def setUp(self):
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()
45
46   def tearDown(self):
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)
52
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)])
63
64   def testCheckUidPool(self):
65     # UID < UIDPOOL_UID_MIN
66     self.assertRaises(errors.OpPrereqError,
67                       uidpool.CheckUidPool,
68                       [(0, 0)])
69     # UID > UIDPOOL_UID_MAX
70     self.assertRaises(errors.OpPrereqError,
71                       uidpool.CheckUidPool,
72                       [(11, 11)])
73     # lower boundary > higher boundary
74     self.assertRaises(errors.OpPrereqError,
75                       uidpool.CheckUidPool,
76                       [(2, 1)])
77
78   def testFormatUidPool(self):
79     self.assertEqualValues(
80         uidpool.FormatUidPool([(1, 100), (200, 200)]),
81         "1-100, 200")
82     self.assertEqualValues(
83         uidpool.FormatUidPool([(1, 100), (200, 200)], separator=":"),
84         "1-100:200")
85     self.assertEqualValues(
86         uidpool.FormatUidPool([(1, 100), (200, 200)], separator="\n"),
87         "1-100\n200")
88
89   def testRequestUnusedUid(self):
90     # Check with known used user-ids
91     #
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,
97                         set([uid]))
98
99     # Check with a single, known unused user-id
100     #
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)
105
106     # Check uid-pool exhaustion
107     #
108     # uid "-1" is locked now, so RequestUnusedUid is expected to fail
109     self.assertRaises(errors.LockError,
110                       uidpool.RequestUnusedUid,
111                       set([-1]))
112
113     # Check unlocking
114     uid.Unlock()
115     # After unlocking, "-1" should be available again
116     uid = uidpool.RequestUnusedUid(set([-1]))
117     self.assertEqualValues(uid.GetUid(), -1)
118
119
120 if __name__ == '__main__':
121   testutils.GanetiTestProgram()