X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/93be53dacd93421b003a7219ec3a063728c720bd..48aaca91efa214b37dba94f28582be73f3c90dbd:/test/ganeti.uidpool_unittest.py diff --git a/test/ganeti.uidpool_unittest.py b/test/ganeti.uidpool_unittest.py index 638c8b2..deed3db 100755 --- a/test/ganeti.uidpool_unittest.py +++ b/test/ganeti.uidpool_unittest.py @@ -16,17 +16,20 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA -# 0.0510-1301, USA. +# 02110-1301, USA. """Script for unittesting the uidpool module""" +import os +import tempfile import unittest from ganeti import constants from ganeti import uidpool from ganeti import errors +from ganeti import pathutils import testutils @@ -39,15 +42,25 @@ class TestUidPool(testutils.GanetiTestCase): self.old_uid_max = constants.UIDPOOL_UID_MAX constants.UIDPOOL_UID_MIN = 1 constants.UIDPOOL_UID_MAX = 10 + pathutils.UIDPOOL_LOCKDIR = tempfile.mkdtemp() def tearDown(self): constants.UIDPOOL_UID_MIN = self.old_uid_min constants.UIDPOOL_UID_MAX = self.old_uid_max + for name in os.listdir(pathutils.UIDPOOL_LOCKDIR): + os.unlink(os.path.join(pathutils.UIDPOOL_LOCKDIR, name)) + os.rmdir(pathutils.UIDPOOL_LOCKDIR) def testParseUidPool(self): self.assertEqualValues( - uidpool.ParseUidPool('1-100,200,'), + uidpool.ParseUidPool("1-100,200,"), [(1, 100), (200, 200)]) + self.assertEqualValues( + uidpool.ParseUidPool("1000:2000-2500", separator=":"), + [(1000, 1000), (2000, 2500)]) + self.assertEqualValues( + uidpool.ParseUidPool("1000\n2000-2500", separator="\n"), + [(1000, 1000), (2000, 2500)]) def testCheckUidPool(self): # UID < UIDPOOL_UID_MIN @@ -66,8 +79,44 @@ class TestUidPool(testutils.GanetiTestCase): def testFormatUidPool(self): self.assertEqualValues( uidpool.FormatUidPool([(1, 100), (200, 200)]), - '1-100, 200'), - - -if __name__ == '__main__': + "1-100, 200") + self.assertEqualValues( + uidpool.FormatUidPool([(1, 100), (200, 200)], separator=":"), + "1-100:200") + self.assertEqualValues( + uidpool.FormatUidPool([(1, 100), (200, 200)], separator="\n"), + "1-100\n200") + + def testRequestUnusedUid(self): + # Check with known used user-ids + # + # Test with user-id "0" and with our own user-id, both + # of which are guaranteed to be used user-ids + for uid in 0, os.getuid(): + self.assertRaises(errors.LockError, + uidpool.RequestUnusedUid, + set([uid])) + + # Check with a single, known unused user-id + # + # We use "-1" here, which is not a valid user-id, so it's + # guaranteed that it's unused. + uid = uidpool.RequestUnusedUid(set([-1])) + self.assertEqualValues(uid.GetUid(), -1) + + # Check uid-pool exhaustion + # + # uid "-1" is locked now, so RequestUnusedUid is expected to fail + self.assertRaises(errors.LockError, + uidpool.RequestUnusedUid, + set([-1])) + + # Check unlocking + uid.Unlock() + # After unlocking, "-1" should be available again + uid = uidpool.RequestUnusedUid(set([-1])) + self.assertEqualValues(uid.GetUid(), -1) + + +if __name__ == "__main__": testutils.GanetiTestProgram()