Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.8 kB)

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
# 02110-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
from ganeti import pathutils
33

    
34
import testutils
35

    
36

    
37
class TestUidPool(testutils.GanetiTestCase):
38
  """Uid-pool tests"""
39

    
40
  def setUp(self):
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()
46

    
47
  def tearDown(self):
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)
53

    
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)])
64

    
65
  def testCheckUidPool(self):
66
    # UID < UIDPOOL_UID_MIN
67
    self.assertRaises(errors.OpPrereqError,
68
                      uidpool.CheckUidPool,
69
                      [(0, 0)])
70
    # UID > UIDPOOL_UID_MAX
71
    self.assertRaises(errors.OpPrereqError,
72
                      uidpool.CheckUidPool,
73
                      [(11, 11)])
74
    # lower boundary > higher boundary
75
    self.assertRaises(errors.OpPrereqError,
76
                      uidpool.CheckUidPool,
77
                      [(2, 1)])
78

    
79
  def testFormatUidPool(self):
80
    self.assertEqualValues(
81
        uidpool.FormatUidPool([(1, 100), (200, 200)]),
82
        "1-100, 200")
83
    self.assertEqualValues(
84
        uidpool.FormatUidPool([(1, 100), (200, 200)], separator=":"),
85
        "1-100:200")
86
    self.assertEqualValues(
87
        uidpool.FormatUidPool([(1, 100), (200, 200)], separator="\n"),
88
        "1-100\n200")
89

    
90
  def testRequestUnusedUid(self):
91
    # Check with known used user-ids
92
    #
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,
98
                        set([uid]))
99

    
100
    # Check with a single, known unused user-id
101
    #
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)
106

    
107
    # Check uid-pool exhaustion
108
    #
109
    # uid "-1" is locked now, so RequestUnusedUid is expected to fail
110
    self.assertRaises(errors.LockError,
111
                      uidpool.RequestUnusedUid,
112
                      set([-1]))
113

    
114
    # Check unlocking
115
    uid.Unlock()
116
    # After unlocking, "-1" should be available again
117
    uid = uidpool.RequestUnusedUid(set([-1]))
118
    self.assertEqualValues(uid.GetUid(), -1)
119

    
120

    
121
if __name__ == "__main__":
122
  testutils.GanetiTestProgram()