1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-10 01:18:15 +03:00

python:tests: Reuse claims created by setUp() across all tests

We now create the claims in setUp() only once, preserving them so as to
reuse them across all of the tests in this class. Then we finally delete
them all in tearDownClass().

addClassCleanup() could make this cleaner, but it’s available only in
Python 3.8 and above.

This change reduces the time taken by my machine to run these tests from
four minutes to twenty seconds.

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
Joseph Sutton 2023-08-17 10:27:44 +12:00 committed by Andrew Bartlett
parent a985873c46
commit f71893c1a3

View File

@ -67,30 +67,57 @@ VALUE_TYPES = [
class ClaimCmdTestCase(SambaToolCmdTest):
def setUp(self):
super().setUp()
self.host = "ldap://{DC_SERVER}".format(**os.environ)
self.creds = "-U{DC_USERNAME}%{DC_PASSWORD}".format(**os.environ)
self.samdb = self.getSamDB("-H", self.host, self.creds)
self.claim_types = []
self.this_test_claim_types = set()
# Generate some known claim types used by tests.
for attribute in ATTRIBUTES:
self.create_claim_type(attribute, classes=["user"])
if self._first_self is None:
cls = type(self)
cls.host = "ldap://{DC_SERVER}".format(**os.environ)
cls.creds = "-U{DC_USERNAME}%{DC_PASSWORD}".format(**os.environ)
cls.samdb = self.getSamDB("-H", self.host, self.creds)
# Generate some more with unique names not in the ATTRIBUTES list.
self.create_claim_type("accountExpires", name="expires",
classes=["user"])
self.create_claim_type("department", name="dept", classes=["user"],
protect=True)
self.create_claim_type("carLicense", name="plate", classes=["user"],
disable=True)
# Generate some known claim types used by tests.
for attribute in ATTRIBUTES:
self.create_claim_type(attribute, classes=["user"], preserve=True)
# Generate some more with unique names not in the ATTRIBUTES list.
self.create_claim_type("accountExpires", name="expires",
classes=["user"], preserve=True)
self.create_claim_type("department", name="dept", classes=["user"],
protect=True, preserve=True)
self.create_claim_type("carLicense", name="plate", classes=["user"],
disable=True, preserve=True)
cls._first_self = self
def tearDown(self):
# Remove claim types created by setUp.
for claim_type in self.claim_types:
self.delete_claim_type(claim_type, force=True)
# Remove claim types created by a single test.
first_self = self._first_self
if first_self is not None:
for claim_type in first_self.this_test_claim_types:
first_self.delete_claim_type(claim_type, force=True)
first_self.claim_types.remove(claim_type)
super().tearDown()
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._first_self = None
cls.claim_types = set()
@classmethod
def tearDownClass(cls):
# Remove claim types created by setUp, and kept for the lifetime of the
# class.
first_self = cls._first_self
if first_self is not None:
for claim_type in first_self.claim_types:
first_self.delete_claim_type(claim_type, force=True)
cls._first_self = None
super().tearDownClass()
def get_services_dn(self):
"""Returns Services DN."""
services_dn = self.samdb.get_config_basedn()
@ -113,7 +140,8 @@ class ClaimCmdTestCase(SambaToolCmdTest):
runsubcmd = _run
def create_claim_type(self, attribute, name=None, description=None,
classes=None, disable=False, protect=False):
classes=None, disable=False, protect=False,
preserve=False):
"""Create a claim type using the samba-tool command."""
# if name is specified it will override the attribute name
@ -140,7 +168,10 @@ class ClaimCmdTestCase(SambaToolCmdTest):
result, out, err = self.runcmd(*cmd)
self.assertIsNone(result, msg=err)
self.assertTrue(out.startswith("Created claim type"))
self.claim_types.append(display_name)
if preserve:
self.claim_types.add(display_name)
else:
self.this_test_claim_types.add(display_name)
return display_name
def delete_claim_type(self, name, force=False):