]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
python:tests: Reuse policies and silos created by setUp() across all tests
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Thu, 17 Aug 2023 01:29:49 +0000 (13:29 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 21 Aug 2023 23:37:29 +0000 (23:37 +0000)
We now create the claims in setUp() only once, preserving them so as to
reuse them across all of the tests using 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
samba.tests.samba_tool.domain_auth_policy from two minutes to ten
seconds.

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/tests/samba_tool/domain_auth_base.py
python/samba/tests/samba_tool/domain_auth_silo.py

index 66445e99c6a61a1087a182d42909f38ceffd1d33..54dfd90c1eb3f0f1e1d2334d3695099e2c75cf78 100644 (file)
@@ -30,39 +30,55 @@ from .base import SambaToolCmdTest
 class BaseAuthCmdTest(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)
-
-        # Generate some test data.
-        self.policies = []
-        self.create_authentication_policy(name="Single Policy")
-        self.create_authentication_policy(name="User Policy")
-        self.create_authentication_policy(name="Service Policy")
-        self.create_authentication_policy(name="Computer Policy")
-
-        self.silos = []
-        self.create_authentication_silo(name="Developers",
-                                        description="Developers, Developers",
-                                        policy="Single Policy")
-        self.create_authentication_silo(name="Managers",
-                                        description="Managers",
-                                        policy="Single Policy")
-        self.create_authentication_silo(name="QA",
-                                        description="Quality Assurance",
-                                        user_policy="User Policy",
-                                        service_policy="Service Policy",
-                                        computer_policy="Computer Policy")
-
-    def tearDown(self):
-        """Remove data created by setUp."""
-        for policy in self.policies:
-            self.delete_authentication_policy(policy, force=True)
-
-        for silo in self.silos:
-            self.delete_authentication_silo(silo, force=True)
-
-        super().tearDown()
+
+        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 test data.
+            self.create_authentication_policy(name="Single Policy")
+            self.create_authentication_policy(name="User Policy")
+            self.create_authentication_policy(name="Service Policy")
+            self.create_authentication_policy(name="Computer Policy")
+
+            self.create_authentication_silo(name="Developers",
+                                            description="Developers, Developers",
+                                            policy="Single Policy")
+            self.create_authentication_silo(name="Managers",
+                                            description="Managers",
+                                            policy="Single Policy")
+            self.create_authentication_silo(name="QA",
+                                            description="Quality Assurance",
+                                            user_policy="User Policy",
+                                            service_policy="Service Policy",
+                                            computer_policy="Computer Policy")
+
+            cls._first_self = self
+
+    @classmethod
+    def setUpClass(cls):
+        super().setUpClass()
+        cls._first_self = None
+        cls.policies = []
+        cls.silos = []
+
+    @classmethod
+    def tearDownClass(cls):
+        """Remove data created by setUp, and kept for the lifetime of the
+        class."""
+        first_self = cls._first_self
+        if first_self is not None:
+            for policy in first_self.policies:
+                first_self.delete_authentication_policy(policy, force=True)
+
+            for silo in first_self.silos:
+                first_self.delete_authentication_silo(silo, force=True)
+
+            cls._first_self = None
+
+        super().tearDownClass()
 
     def get_services_dn(self):
         """Returns Services DN."""
@@ -129,7 +145,8 @@ class BaseAuthCmdTest(SambaToolCmdTest):
         if protect:
             cmd.append("--protect")
 
-        # Run command and store name in self.silos for tearDown to clean up.
+        # Run command and store name in self.silos for tearDownClass to clean
+        # up.
         result, out, err = self.runcmd(*cmd)
         self.assertIsNone(result, msg=err)
         self.assertTrue(out.startswith("Created authentication policy"))
@@ -174,7 +191,8 @@ class BaseAuthCmdTest(SambaToolCmdTest):
         if audit:
             cmd.append("--audit")
 
-        # Run command and store name in self.silos for tearDown to clean up.
+        # Run command and store name in self.silos for tearDownClass to clean
+        # up.
         result, out, err = self.runcmd(*cmd)
         self.assertIsNone(result, msg=err)
         self.assertTrue(out.startswith("Created authentication silo"))
index b9f008f779e49936a15ecbfe07792292f956f5dc..2b18098ba0f81fe6a573a0052c88d26fe7c5f1d8 100644 (file)
@@ -458,7 +458,7 @@ class AuthSiloMemberCmdTestCase(BaseAuthCmdTest):
         # Remove organisational unit.
         self.samdb.delete(self.ou, ["tree_delete:1"])
 
-        # Remove members from silos before deleting them in super.
+        # Remove members from silos.
         for silo, members in self.members.items():
             for member in members:
                 self.remove_silo_member(silo, member)