Revision 17f25f78 test/ganeti.uidpool_unittest.py

b/test/ganeti.uidpool_unittest.py
22 22
"""Script for unittesting the uidpool module"""
23 23

  
24 24

  
25
import os
26
import tempfile
25 27
import unittest
26 28

  
27 29
from ganeti import constants
......
39 41
    self.old_uid_max = constants.UIDPOOL_UID_MAX
40 42
    constants.UIDPOOL_UID_MIN = 1
41 43
    constants.UIDPOOL_UID_MAX = 10
44
    constants.UIDPOOL_LOCKDIR = tempfile.mkdtemp()
42 45

  
43 46
  def tearDown(self):
44 47
    constants.UIDPOOL_UID_MIN = self.old_uid_min
45 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)
46 52

  
47 53
  def testParseUidPool(self):
48 54
    self.assertEqualValues(
......
80 86
        uidpool.FormatUidPool([(1, 100), (200, 200)], separator="\n"),
81 87
        "1-100\n200")
82 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

  
83 119

  
84 120
if __name__ == '__main__':
85 121
  testutils.GanetiTestProgram()

Also available in: Unified diff