uidpool: test the separator= argument
[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 unittest
26
27 from ganeti import constants
28 from ganeti import uidpool
29 from ganeti import errors
30
31 import testutils
32
33
34 class TestUidPool(testutils.GanetiTestCase):
35   """Uid-pool tests"""
36
37   def setUp(self):
38     self.old_uid_min = constants.UIDPOOL_UID_MIN
39     self.old_uid_max = constants.UIDPOOL_UID_MAX
40     constants.UIDPOOL_UID_MIN = 1
41     constants.UIDPOOL_UID_MAX = 10
42
43   def tearDown(self):
44     constants.UIDPOOL_UID_MIN = self.old_uid_min
45     constants.UIDPOOL_UID_MAX = self.old_uid_max
46
47   def testParseUidPool(self):
48     self.assertEqualValues(
49         uidpool.ParseUidPool("1-100,200,"),
50         [(1, 100), (200, 200)])
51     self.assertEqualValues(
52         uidpool.ParseUidPool("1000:2000-2500", separator=":"),
53         [(1000, 1000), (2000, 2500)])
54     self.assertEqualValues(
55         uidpool.ParseUidPool("1000\n2000-2500", separator="\n"),
56         [(1000, 1000), (2000, 2500)])
57
58   def testCheckUidPool(self):
59     # UID < UIDPOOL_UID_MIN
60     self.assertRaises(errors.OpPrereqError,
61                       uidpool.CheckUidPool,
62                       [(0, 0)])
63     # UID > UIDPOOL_UID_MAX
64     self.assertRaises(errors.OpPrereqError,
65                       uidpool.CheckUidPool,
66                       [(11, 11)])
67     # lower boundary > higher boundary
68     self.assertRaises(errors.OpPrereqError,
69                       uidpool.CheckUidPool,
70                       [(2, 1)])
71
72   def testFormatUidPool(self):
73     self.assertEqualValues(
74         uidpool.FormatUidPool([(1, 100), (200, 200)]),
75         "1-100, 200")
76     self.assertEqualValues(
77         uidpool.FormatUidPool([(1, 100), (200, 200)], separator=":"),
78         "1-100:200")
79     self.assertEqualValues(
80         uidpool.FormatUidPool([(1, 100), (200, 200)], separator="\n"),
81         "1-100\n200")
82
83
84 if __name__ == '__main__':
85   testutils.GanetiTestProgram()