From 597428943b1b77267243dc69ecea6fda8dfc3163 Mon Sep 17 00:00:00 2001 From: Tim Beale Date: Thu, 15 Mar 2018 12:44:30 +1300 Subject: [PATCH] tests: Move repeated code into a helper function 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 Reviewed-by: Garming Sam Signed-off-by: Tim Beale --- python/samba/tests/__init__.py | 13 +++++++++++++ source4/torture/drs/python/getncchanges.py | 9 +-------- source4/torture/drs/python/link_conflicts.py | 9 +-------- source4/torture/drs/python/repl_rodc.py | 10 ++-------- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/python/samba/tests/__init__.py b/python/samba/tests/__init__.py index bc8c185769b8..61036b5247dc 100644 --- a/python/samba/tests/__init__.py +++ b/python/samba/tests/__init__.py @@ -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 diff --git a/source4/torture/drs/python/getncchanges.py b/source4/torture/drs/python/getncchanges.py index 1b619f146921..d545fe07e584 100644 --- a/source4/torture/drs/python/getncchanges.py +++ b/source4/torture/drs/python/getncchanges.py @@ -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) diff --git a/source4/torture/drs/python/link_conflicts.py b/source4/torture/drs/python/link_conflicts.py index 6522fb610d69..31ebc30fc446 100644 --- a/source4/torture/drs/python/link_conflicts.py +++ b/source4/torture/drs/python/link_conflicts.py @@ -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) diff --git a/source4/torture/drs/python/repl_rodc.py b/source4/torture/drs/python/repl_rodc.py index 89c42f0e9d65..1d84c99b3870 100644 --- a/source4/torture/drs/python/repl_rodc.py +++ b/source4/torture/drs/python/repl_rodc.py @@ -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) -- 2.34.1