Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.uidpool_unittest.py @ 560ef132

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 fd7b69c0 Michael Hanselmann
# 02110-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 a56625a2 Michael Hanselmann
from ganeti import pathutils
33 93be53da Balazs Lecz
34 93be53da Balazs Lecz
import testutils
35 93be53da Balazs Lecz
36 93be53da Balazs Lecz
37 93be53da Balazs Lecz
class TestUidPool(testutils.GanetiTestCase):
38 93be53da Balazs Lecz
  """Uid-pool tests"""
39 93be53da Balazs Lecz
40 93be53da Balazs Lecz
  def setUp(self):
41 93be53da Balazs Lecz
    self.old_uid_min = constants.UIDPOOL_UID_MIN
42 93be53da Balazs Lecz
    self.old_uid_max = constants.UIDPOOL_UID_MAX
43 93be53da Balazs Lecz
    constants.UIDPOOL_UID_MIN = 1
44 93be53da Balazs Lecz
    constants.UIDPOOL_UID_MAX = 10
45 a56625a2 Michael Hanselmann
    pathutils.UIDPOOL_LOCKDIR = tempfile.mkdtemp()
46 93be53da Balazs Lecz
47 93be53da Balazs Lecz
  def tearDown(self):
48 93be53da Balazs Lecz
    constants.UIDPOOL_UID_MIN = self.old_uid_min
49 93be53da Balazs Lecz
    constants.UIDPOOL_UID_MAX = self.old_uid_max
50 a56625a2 Michael Hanselmann
    for name in os.listdir(pathutils.UIDPOOL_LOCKDIR):
51 a56625a2 Michael Hanselmann
      os.unlink(os.path.join(pathutils.UIDPOOL_LOCKDIR, name))
52 a56625a2 Michael Hanselmann
    os.rmdir(pathutils.UIDPOOL_LOCKDIR)
53 93be53da Balazs Lecz
54 93be53da Balazs Lecz
  def testParseUidPool(self):
55 93be53da Balazs Lecz
    self.assertEqualValues(
56 41b10cf6 Guido Trotter
        uidpool.ParseUidPool("1-100,200,"),
57 93be53da Balazs Lecz
        [(1, 100), (200, 200)])
58 41b10cf6 Guido Trotter
    self.assertEqualValues(
59 41b10cf6 Guido Trotter
        uidpool.ParseUidPool("1000:2000-2500", separator=":"),
60 41b10cf6 Guido Trotter
        [(1000, 1000), (2000, 2500)])
61 41b10cf6 Guido Trotter
    self.assertEqualValues(
62 41b10cf6 Guido Trotter
        uidpool.ParseUidPool("1000\n2000-2500", separator="\n"),
63 41b10cf6 Guido Trotter
        [(1000, 1000), (2000, 2500)])
64 93be53da Balazs Lecz
65 93be53da Balazs Lecz
  def testCheckUidPool(self):
66 93be53da Balazs Lecz
    # UID < UIDPOOL_UID_MIN
67 93be53da Balazs Lecz
    self.assertRaises(errors.OpPrereqError,
68 93be53da Balazs Lecz
                      uidpool.CheckUidPool,
69 93be53da Balazs Lecz
                      [(0, 0)])
70 93be53da Balazs Lecz
    # UID > UIDPOOL_UID_MAX
71 93be53da Balazs Lecz
    self.assertRaises(errors.OpPrereqError,
72 93be53da Balazs Lecz
                      uidpool.CheckUidPool,
73 93be53da Balazs Lecz
                      [(11, 11)])
74 93be53da Balazs Lecz
    # lower boundary > higher boundary
75 93be53da Balazs Lecz
    self.assertRaises(errors.OpPrereqError,
76 93be53da Balazs Lecz
                      uidpool.CheckUidPool,
77 93be53da Balazs Lecz
                      [(2, 1)])
78 93be53da Balazs Lecz
79 93be53da Balazs Lecz
  def testFormatUidPool(self):
80 93be53da Balazs Lecz
    self.assertEqualValues(
81 93be53da Balazs Lecz
        uidpool.FormatUidPool([(1, 100), (200, 200)]),
82 41b10cf6 Guido Trotter
        "1-100, 200")
83 41b10cf6 Guido Trotter
    self.assertEqualValues(
84 41b10cf6 Guido Trotter
        uidpool.FormatUidPool([(1, 100), (200, 200)], separator=":"),
85 41b10cf6 Guido Trotter
        "1-100:200")
86 41b10cf6 Guido Trotter
    self.assertEqualValues(
87 41b10cf6 Guido Trotter
        uidpool.FormatUidPool([(1, 100), (200, 200)], separator="\n"),
88 41b10cf6 Guido Trotter
        "1-100\n200")
89 93be53da Balazs Lecz
90 17f25f78 Balazs Lecz
  def testRequestUnusedUid(self):
91 17f25f78 Balazs Lecz
    # Check with known used user-ids
92 17f25f78 Balazs Lecz
    #
93 17f25f78 Balazs Lecz
    # Test with user-id "0" and with our own user-id, both
94 17f25f78 Balazs Lecz
    # of which are guaranteed to be used user-ids
95 17f25f78 Balazs Lecz
    for uid in 0, os.getuid():
96 17f25f78 Balazs Lecz
      self.assertRaises(errors.LockError,
97 17f25f78 Balazs Lecz
                        uidpool.RequestUnusedUid,
98 17f25f78 Balazs Lecz
                        set([uid]))
99 17f25f78 Balazs Lecz
100 17f25f78 Balazs Lecz
    # Check with a single, known unused user-id
101 17f25f78 Balazs Lecz
    #
102 17f25f78 Balazs Lecz
    # We use "-1" here, which is not a valid user-id, so it's
103 17f25f78 Balazs Lecz
    # guaranteed that it's unused.
104 17f25f78 Balazs Lecz
    uid = uidpool.RequestUnusedUid(set([-1]))
105 17f25f78 Balazs Lecz
    self.assertEqualValues(uid.GetUid(), -1)
106 17f25f78 Balazs Lecz
107 17f25f78 Balazs Lecz
    # Check uid-pool exhaustion
108 17f25f78 Balazs Lecz
    #
109 17f25f78 Balazs Lecz
    # uid "-1" is locked now, so RequestUnusedUid is expected to fail
110 17f25f78 Balazs Lecz
    self.assertRaises(errors.LockError,
111 17f25f78 Balazs Lecz
                      uidpool.RequestUnusedUid,
112 17f25f78 Balazs Lecz
                      set([-1]))
113 17f25f78 Balazs Lecz
114 17f25f78 Balazs Lecz
    # Check unlocking
115 17f25f78 Balazs Lecz
    uid.Unlock()
116 17f25f78 Balazs Lecz
    # After unlocking, "-1" should be available again
117 17f25f78 Balazs Lecz
    uid = uidpool.RequestUnusedUid(set([-1]))
118 17f25f78 Balazs Lecz
    self.assertEqualValues(uid.GetUid(), -1)
119 17f25f78 Balazs Lecz
120 93be53da Balazs Lecz
121 2f96c43c Michael Hanselmann
if __name__ == "__main__":
122 93be53da Balazs Lecz
  testutils.GanetiTestProgram()