tests: Move repeated code into a helper function
authorTim Beale <timbeale@catalyst.net.nz>
Wed, 14 Mar 2018 23:44:30 +0000 (12:44 +1300)
committerGarming Sam <garming@samba.org>
Fri, 11 May 2018 04:01:23 +0000 (06:01 +0200)
Several tests hang all the objects they create off a unique OU.
Having a common OU makes cleanup easier, and having a unique OU (i.e.
adding some randomness) helps protect against one-off test failures
(Replication between testenvs is happening in the background.
Occasionally, when a test finishes on one testenv and moves onto the
next testenv, that testenv may have received the replicated test
objects from the first testenv, but has not received their deletion
yet).

Rather than copy-n-pasting this code yet again, split it out into a
helper function.

Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
Signed-off-by: Tim Beale <timbeale@catalyst.net.nz>
python/samba/tests/__init__.py
source4/torture/drs/python/getncchanges.py
source4/torture/drs/python/link_conflicts.py
source4/torture/drs/python/repl_rodc.py

index bc8c185769b8fe76b8bf3149316f15c3df23ae74..61036b5247dc594b0df7c4283747bceda5e4db58 100644 (file)
@@ -36,6 +36,7 @@ import re
 import samba.auth
 import samba.dcerpc.base
 from samba.compat import PY3, text_type
+from random import randint
 if not PY3:
     # Py2 only
     from samba.samdb import SamDB
@@ -475,3 +476,15 @@ def delete_force(samdb, dn, **kwargs):
     except ldb.LdbError as error:
         (num, errstr) = error.args
         assert num == ldb.ERR_NO_SUCH_OBJECT, "ldb.delete() failed: %s" % errstr
+
+def create_test_ou(samdb, name):
+    """Creates a unique OU for the test"""
+
+    # Add some randomness to the test OU. Replication between the testenvs is
+    # constantly happening in the background. Deletion of the last test's
+    # objects can be slow to replicate out. So the OU created by a previous
+    # testenv may still exist at the point that tests start on another testenv.
+    rand = randint(1, 10000000)
+    dn = "OU=%s%d,%s" %(name, rand, samdb.get_default_basedn())
+    samdb.add({ "dn": dn, "objectclass": "organizationalUnit"})
+    return dn
index 1b619f146921087954b77e34a8cee13368024be6..d545fe07e584b0f20d6e8446fa7ed32e6b72d4db 100644 (file)
@@ -46,15 +46,8 @@ class DrsReplicaSyncIntegrityTestCase(drs_base.DrsBaseTestCase):
         # the vampire_dc), so we point this test directly at that DC
         self.set_test_ldb_dc(self.ldb_dc2)
 
-        # add some randomness to the test OU. (Deletion of the last test's
-        # objects can be slow to replicate out. So the OU created by a previous
-        # testenv may still exist at this point).
-        rand = random.randint(1, 10000000)
+        self.ou = samba.tests.create_test_ou(self.test_ldb_dc, "getncchanges")
         self.base_dn = self.test_ldb_dc.get_default_basedn()
-        self.ou = "OU=getncchanges%d_test,%s" %(rand, self.base_dn)
-        self.test_ldb_dc.add({
-            "dn": self.ou,
-            "objectclass": "organizationalUnit"})
 
         self.default_conn = DcConnection(self, self.ldb_dc2, self.dnsname_dc2)
         self.set_dc_connection(self.default_conn)
index 6522fb610d69be60e7f47b59dc80c334645dbb54..31ebc30fc446e062da6c2ada2edb334fef3b634f 100644 (file)
@@ -46,15 +46,8 @@ class DrsReplicaLinkConflictTestCase(drs_base.DrsBaseTestCase):
     def setUp(self):
         super(DrsReplicaLinkConflictTestCase, self).setUp()
 
-        # add some randomness to the test OU. (Deletion of the last test's
-        # objects can be slow to replicate out. So the OU created by a previous
-        # testenv may still exist at this point).
-        rand = random.randint(1, 10000000)
+        self.ou = samba.tests.create_test_ou(self.ldb_dc1, "test_link_conflict")
         self.base_dn = self.ldb_dc1.get_default_basedn()
-        self.ou = "OU=test_link_conflict%d,%s" %(rand, self.base_dn)
-        self.ldb_dc1.add({
-            "dn": self.ou,
-            "objectclass": "organizationalUnit"})
 
         (self.drs, self.drs_handle) = self._ds_bind(self.dnsname_dc1)
         (self.drs2, self.drs2_handle) = self._ds_bind(self.dnsname_dc2)
index 89c42f0e9d659ddd83c7ed70873a337a299cd6c0..1d84c99b38707718dcbe8f3c8ca46eb5209a08a7 100644 (file)
@@ -92,17 +92,11 @@ class DrsRodcTestCase(drs_base.DrsBaseTestCase):
         super(DrsRodcTestCase, self).setUp()
         self.base_dn = self.ldb_dc1.get_default_basedn()
 
-        rand = random.randint(1, 10000000)
-
-        self.ou = "OU=test_drs_rodc%s,%s" % (rand, self.base_dn)
-        self.ldb_dc1.add({
-            "dn": self.ou,
-            "objectclass": "organizationalUnit"
-        })
+        self.ou = samba.tests.create_test_ou(self.ldb_dc1, "test_drs_rodc")
         self.allowed_group = "CN=Allowed RODC Password Replication Group,CN=Users,%s" % self.base_dn
 
         self.site = self.ldb_dc1.server_site_name()
-        self.rodc_name = "TESTRODCDRS%s" % rand
+        self.rodc_name = "TESTRODCDRS%s" % random.randint(1, 10000000)
         self.rodc_pass = "password12#"
         self.computer_dn = "CN=%s,OU=Domain Controllers,%s" % (self.rodc_name, self.base_dn)