tests: avoid returning an already used ID in randomXid()
authorJule Anger <ja@sernet.de>
Tue, 20 Oct 2020 07:42:38 +0000 (09:42 +0200)
committerJeremy Allison <jra@samba.org>
Thu, 29 Oct 2020 18:54:24 +0000 (18:54 +0000)
The error 'uidNumber xxx is already being used.' in the samba tool tests
occurs when the random.randint functions returns the same value twice and
therefore a user or group with an already used gid or uid should be created.

Avoid this error by adding a list that stores the used IDs, so that the randomXid
function can check wheter a value is already used before returning it.

Signed-off-by: Jule Anger <ja@sernet.de>
Reviewed-by: Björn Baumbach <bb@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
Autobuild-User(master): Jeremy Allison <jra@samba.org>
Autobuild-Date(master): Thu Oct 29 18:54:24 UTC 2020 on sn-devel-184

python/samba/tests/samba_tool/base.py

index 536fbfc16170ca7480a3803f66a9005cbd9aa079..00e742e7c5bcc0c97a5609a288927e86d7cccbb8 100644 (file)
@@ -125,10 +125,24 @@ class SambaToolCmdTest(samba.tests.BlackboxTestCase):
         return name
 
     def randomXid(self):
-        # pick some hopefully unused, high UID/GID range to avoid interference
+        # pick some unused, high UID/GID range to avoid interference
         # from the system the test runs on
-        xid = random.randint(4711000, 4799000)
-        return xid
+
+        # initialize a list to store used IDs
+        try:
+            self.used_xids
+        except AttributeError:
+            self.used_xids = []
+
+        # try to get an unused ID
+        failed = 0
+        while failed < 50:
+            xid = random.randint(4711000, 4799000)
+            if xid not in self.used_xids:
+                self.used_xids += [xid]
+                return xid
+            failed += 1
+        assert False, "No Xid are available"
 
     def assertWithin(self, val1, val2, delta, msg=""):
         """Assert that val1 is within delta of val2, useful for time computations"""