root / test / ganeti.uidpool_unittest.py @ 6e7f0cd9
History | View | Annotate | Download (3.8 kB)
1 | 93be53da | Balazs Lecz | #!/usr/bin/python
|
---|---|---|---|
2 | 93be53da | Balazs Lecz | #
|
3 | 93be53da | Balazs Lecz | |
4 | 93be53da | Balazs Lecz | # Copyright (C) 2010 Google Inc.
|
5 | 93be53da | Balazs Lecz | #
|
6 | 93be53da | Balazs Lecz | # This program is free software; you can redistribute it and/or modify
|
7 | 93be53da | Balazs Lecz | # it under the terms of the GNU General Public License as published by
|
8 | 93be53da | Balazs Lecz | # the Free Software Foundation; either version 2 of the License, or
|
9 | 93be53da | Balazs Lecz | # (at your option) any later version.
|
10 | 93be53da | Balazs Lecz | #
|
11 | 93be53da | Balazs Lecz | # This program is distributed in the hope that it will be useful, but
|
12 | 93be53da | Balazs Lecz | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 93be53da | Balazs Lecz | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 93be53da | Balazs Lecz | # General Public License for more details.
|
15 | 93be53da | Balazs Lecz | #
|
16 | 93be53da | Balazs Lecz | # You should have received a copy of the GNU General Public License
|
17 | 93be53da | Balazs Lecz | # along with this program; if not, write to the Free Software
|
18 | 93be53da | Balazs Lecz | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 93be53da | Balazs Lecz | # 0.0510-1301, USA.
|
20 | 93be53da | Balazs Lecz | |
21 | 93be53da | Balazs Lecz | |
22 | 93be53da | Balazs Lecz | """Script for unittesting the uidpool module"""
|
23 | 93be53da | Balazs Lecz | |
24 | 93be53da | Balazs Lecz | |
25 | 17f25f78 | Balazs Lecz | import os |
26 | 17f25f78 | Balazs Lecz | import tempfile |
27 | 93be53da | Balazs Lecz | import unittest |
28 | 93be53da | Balazs Lecz | |
29 | 93be53da | Balazs Lecz | from ganeti import constants |
30 | 93be53da | Balazs Lecz | from ganeti import uidpool |
31 | 93be53da | Balazs Lecz | from ganeti import errors |
32 | 93be53da | Balazs Lecz | |
33 | 93be53da | Balazs Lecz | import testutils |
34 | 93be53da | Balazs Lecz | |
35 | 93be53da | Balazs Lecz | |
36 | 93be53da | Balazs Lecz | class TestUidPool(testutils.GanetiTestCase): |
37 | 93be53da | Balazs Lecz | """Uid-pool tests"""
|
38 | 93be53da | Balazs Lecz | |
39 | 93be53da | Balazs Lecz | def setUp(self): |
40 | 93be53da | Balazs Lecz | self.old_uid_min = constants.UIDPOOL_UID_MIN
|
41 | 93be53da | Balazs Lecz | self.old_uid_max = constants.UIDPOOL_UID_MAX
|
42 | 93be53da | Balazs Lecz | constants.UIDPOOL_UID_MIN = 1
|
43 | 93be53da | Balazs Lecz | constants.UIDPOOL_UID_MAX = 10
|
44 | 17f25f78 | Balazs Lecz | constants.UIDPOOL_LOCKDIR = tempfile.mkdtemp() |
45 | 93be53da | Balazs Lecz | |
46 | 93be53da | Balazs Lecz | def tearDown(self): |
47 | 93be53da | Balazs Lecz | constants.UIDPOOL_UID_MIN = self.old_uid_min
|
48 | 93be53da | Balazs Lecz | constants.UIDPOOL_UID_MAX = self.old_uid_max
|
49 | 17f25f78 | Balazs Lecz | for name in os.listdir(constants.UIDPOOL_LOCKDIR): |
50 | 17f25f78 | Balazs Lecz | os.unlink(os.path.join(constants.UIDPOOL_LOCKDIR, name)) |
51 | 17f25f78 | Balazs Lecz | os.rmdir(constants.UIDPOOL_LOCKDIR) |
52 | 93be53da | Balazs Lecz | |
53 | 93be53da | Balazs Lecz | def testParseUidPool(self): |
54 | 93be53da | Balazs Lecz | self.assertEqualValues(
|
55 | 41b10cf6 | Guido Trotter | uidpool.ParseUidPool("1-100,200,"),
|
56 | 93be53da | Balazs Lecz | [(1, 100), (200, 200)]) |
57 | 41b10cf6 | Guido Trotter | self.assertEqualValues(
|
58 | 41b10cf6 | Guido Trotter | uidpool.ParseUidPool("1000:2000-2500", separator=":"), |
59 | 41b10cf6 | Guido Trotter | [(1000, 1000), (2000, 2500)]) |
60 | 41b10cf6 | Guido Trotter | self.assertEqualValues(
|
61 | 41b10cf6 | Guido Trotter | uidpool.ParseUidPool("1000\n2000-2500", separator="\n"), |
62 | 41b10cf6 | Guido Trotter | [(1000, 1000), (2000, 2500)]) |
63 | 93be53da | Balazs Lecz | |
64 | 93be53da | Balazs Lecz | def testCheckUidPool(self): |
65 | 93be53da | Balazs Lecz | # UID < UIDPOOL_UID_MIN
|
66 | 93be53da | Balazs Lecz | self.assertRaises(errors.OpPrereqError,
|
67 | 93be53da | Balazs Lecz | uidpool.CheckUidPool, |
68 | 93be53da | Balazs Lecz | [(0, 0)]) |
69 | 93be53da | Balazs Lecz | # UID > UIDPOOL_UID_MAX
|
70 | 93be53da | Balazs Lecz | self.assertRaises(errors.OpPrereqError,
|
71 | 93be53da | Balazs Lecz | uidpool.CheckUidPool, |
72 | 93be53da | Balazs Lecz | [(11, 11)]) |
73 | 93be53da | Balazs Lecz | # lower boundary > higher boundary
|
74 | 93be53da | Balazs Lecz | self.assertRaises(errors.OpPrereqError,
|
75 | 93be53da | Balazs Lecz | uidpool.CheckUidPool, |
76 | 93be53da | Balazs Lecz | [(2, 1)]) |
77 | 93be53da | Balazs Lecz | |
78 | 93be53da | Balazs Lecz | def testFormatUidPool(self): |
79 | 93be53da | Balazs Lecz | self.assertEqualValues(
|
80 | 93be53da | Balazs Lecz | uidpool.FormatUidPool([(1, 100), (200, 200)]), |
81 | 41b10cf6 | Guido Trotter | "1-100, 200")
|
82 | 41b10cf6 | Guido Trotter | self.assertEqualValues(
|
83 | 41b10cf6 | Guido Trotter | uidpool.FormatUidPool([(1, 100), (200, 200)], separator=":"), |
84 | 41b10cf6 | Guido Trotter | "1-100:200")
|
85 | 41b10cf6 | Guido Trotter | self.assertEqualValues(
|
86 | 41b10cf6 | Guido Trotter | uidpool.FormatUidPool([(1, 100), (200, 200)], separator="\n"), |
87 | 41b10cf6 | Guido Trotter | "1-100\n200")
|
88 | 93be53da | Balazs Lecz | |
89 | 17f25f78 | Balazs Lecz | def testRequestUnusedUid(self): |
90 | 17f25f78 | Balazs Lecz | # Check with known used user-ids
|
91 | 17f25f78 | Balazs Lecz | #
|
92 | 17f25f78 | Balazs Lecz | # Test with user-id "0" and with our own user-id, both
|
93 | 17f25f78 | Balazs Lecz | # of which are guaranteed to be used user-ids
|
94 | 17f25f78 | Balazs Lecz | for uid in 0, os.getuid(): |
95 | 17f25f78 | Balazs Lecz | self.assertRaises(errors.LockError,
|
96 | 17f25f78 | Balazs Lecz | uidpool.RequestUnusedUid, |
97 | 17f25f78 | Balazs Lecz | set([uid]))
|
98 | 17f25f78 | Balazs Lecz | |
99 | 17f25f78 | Balazs Lecz | # Check with a single, known unused user-id
|
100 | 17f25f78 | Balazs Lecz | #
|
101 | 17f25f78 | Balazs Lecz | # We use "-1" here, which is not a valid user-id, so it's
|
102 | 17f25f78 | Balazs Lecz | # guaranteed that it's unused.
|
103 | 17f25f78 | Balazs Lecz | uid = uidpool.RequestUnusedUid(set([-1])) |
104 | 17f25f78 | Balazs Lecz | self.assertEqualValues(uid.GetUid(), -1) |
105 | 17f25f78 | Balazs Lecz | |
106 | 17f25f78 | Balazs Lecz | # Check uid-pool exhaustion
|
107 | 17f25f78 | Balazs Lecz | #
|
108 | 17f25f78 | Balazs Lecz | # uid "-1" is locked now, so RequestUnusedUid is expected to fail
|
109 | 17f25f78 | Balazs Lecz | self.assertRaises(errors.LockError,
|
110 | 17f25f78 | Balazs Lecz | uidpool.RequestUnusedUid, |
111 | 17f25f78 | Balazs Lecz | set([-1])) |
112 | 17f25f78 | Balazs Lecz | |
113 | 17f25f78 | Balazs Lecz | # Check unlocking
|
114 | 17f25f78 | Balazs Lecz | uid.Unlock() |
115 | 17f25f78 | Balazs Lecz | # After unlocking, "-1" should be available again
|
116 | 17f25f78 | Balazs Lecz | uid = uidpool.RequestUnusedUid(set([-1])) |
117 | 17f25f78 | Balazs Lecz | self.assertEqualValues(uid.GetUid(), -1) |
118 | 17f25f78 | Balazs Lecz | |
119 | 93be53da | Balazs Lecz | |
120 | 93be53da | Balazs Lecz | if __name__ == '__main__': |
121 | 93be53da | Balazs Lecz | testutils.GanetiTestProgram() |