From e87d74066af3b552333aa28d4180e11b32e465b9 Mon Sep 17 00:00:00 2001 From: Rob van der Linde Date: Wed, 25 Oct 2023 16:02:31 +1300 Subject: [PATCH] python: tests: addCleanup is always before create operation This way if it raises during a create, it will still end up running the cleanup. Signed-off-by: Rob van der Linde Reviewed-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- .../tests/samba_tool/domain_auth_policy.py | 51 +++++++++++-------- .../tests/samba_tool/domain_auth_silo.py | 21 +++++--- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/python/samba/tests/samba_tool/domain_auth_policy.py b/python/samba/tests/samba_tool/domain_auth_policy.py index ad5d2437e05..1d7079c78c4 100644 --- a/python/samba/tests/samba_tool/domain_auth_policy.py +++ b/python/samba/tests/samba_tool/domain_auth_policy.py @@ -94,7 +94,6 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): """Test creating a new authentication policy.""" self.addCleanup(self.delete_authentication_policy, name="createTest", force=True) - result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name", "createTest") self.assertIsNone(result, msg=err) @@ -108,7 +107,6 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): """Test creating a new authentication policy with description set.""" self.addCleanup(self.delete_authentication_policy, name="descriptionTest", force=True) - result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name", "descriptionTest", "--description", "Custom Description") @@ -126,7 +124,6 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): """ self.addCleanup(self.delete_authentication_policy, name="userTGTLifetime", force=True) - result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name", "userTGTLifetime", "--user-tgt-lifetime-mins", "60") @@ -160,7 +157,6 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): """ self.addCleanup(self.delete_authentication_policy, name="serviceTGTLifetime", force=True) - result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name", "serviceTGTLifetime", "--service-tgt-lifetime-mins", "60") @@ -194,7 +190,6 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): """ self.addCleanup(self.delete_authentication_policy, name="computerTGTLifetime", force=True) - result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name", "computerTGTLifetime", "--computer-tgt-lifetime-mins", "60") @@ -227,7 +222,6 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): self.addCleanup(self.delete_authentication_policy, name="validSDDLPolicy", force=True) - result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name", "validSDDLPolicy", "--user-allowed-to-authenticate-from", @@ -266,6 +260,8 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): def test_create__audit(self): """Test create authentication policy with --audit flag.""" + self.addCleanup(self.delete_authentication_policy, + name="auditPolicy", force=True) result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name", "auditPolicy", "--audit") @@ -277,6 +273,8 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): def test_create__enforce(self): """Test create authentication policy with --enforce flag.""" + self.addCleanup(self.delete_authentication_policy, + name="enforcePolicy", force=True) result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name", "enforcePolicy", "--enforce") @@ -314,11 +312,12 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): def test_modify__description(self): """Test modifying an authentication policy description.""" - # Create a policy to modify for this test. name = "modifyDescription" - self.runcmd("domain", "auth", "policy", "create", "--name", name) + + # Create a policy to modify for this test. self.addCleanup(self.delete_authentication_policy, name=name, force=True) + self.runcmd("domain", "auth", "policy", "create", "--name", name) # Change the policy description. result, out, err = self.runcmd("domain", "auth", "policy", "modify", @@ -332,11 +331,12 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): def test_modify__strong_ntlm_policy(self): """Test modify strong ntlm policy on the authentication policy.""" - # Create a policy to modify for this test. name = "modifyStrongNTLMPolicy" - self.runcmd("domain", "auth", "policy", "create", "--name", name) + + # Create a policy to modify for this test. self.addCleanup(self.delete_authentication_policy, name=name, force=True) + self.runcmd("domain", "auth", "policy", "create", "--name", name) result, out, err = self.runcmd("domain", "auth", "policy", "modify", "--name", name, @@ -362,11 +362,12 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): This includes checking the upper and lower bounds. """ - # Create a policy to modify for this test. name = "modifyUserTGTLifetime" - self.runcmd("domain", "auth", "policy", "create", "--name", name) + + # Create a policy to modify for this test. self.addCleanup(self.delete_authentication_policy, name=name, force=True) + self.runcmd("domain", "auth", "policy", "create", "--name", name) result, out, err = self.runcmd("domain", "auth", "policy", "modify", "--name", name, @@ -398,11 +399,12 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): This includes checking the upper and lower bounds. """ - # Create a policy to modify for this test. name = "modifyServiceTGTLifetime" - self.runcmd("domain", "auth", "policy", "create", "--name", name) + + # Create a policy to modify for this test. self.addCleanup(self.delete_authentication_policy, name=name, force=True) + self.runcmd("domain", "auth", "policy", "create", "--name", name) result, out, err = self.runcmd("domain", "auth", "policy", "modify", "--name", name, @@ -434,11 +436,12 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): This includes checking the upper and lower bounds. """ - # Create a policy to modify for this test. name = "modifyComputerTGTLifetime" - self.runcmd("domain", "auth", "policy", "create", "--name", name) + + # Create a policy to modify for this test. self.addCleanup(self.delete_authentication_policy, name=name, force=True) + self.runcmd("domain", "auth", "policy", "create", "--name", name) result, out, err = self.runcmd("domain", "auth", "policy", "modify", "--name", name, @@ -482,11 +485,12 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): def test_modify__audit_enforce(self): """Test modify authentication policy using --audit and --enforce.""" - # Create a policy to modify for this test. name = "modifyEnforce" - self.runcmd("domain", "auth", "policy", "create", "--name", name) + + # Create a policy to modify for this test. self.addCleanup(self.delete_authentication_policy, name=name, force=True) + self.runcmd("domain", "auth", "policy", "create", "--name", name) # Change to audit, the default is --enforce. result, out, err = self.runcmd("domain", "auth", "policy", "modify", @@ -509,11 +513,12 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): def test_modify__protect_unprotect(self): """Test modify authentication policy using --protect and --unprotect.""" - # Create a policy to modify for this test. name = "modifyProtect" - self.runcmd("domain", "auth", "policy", "create", "--name", name) + + # Create a policy to modify for this test. self.addCleanup(self.delete_authentication_policy, name=name, force=True) + self.runcmd("domain", "auth", "policy", "create", "--name", name) utils = SDUtils(self.samdb) result, out, err = self.runcmd("domain", "auth", "policy", "modify", @@ -625,6 +630,8 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): def test_delete__force_fails(self): """Test deleting an authentication policy with --force, but it fails.""" # Create protected authentication policy. + self.addCleanup(self.delete_authentication_policy, + name="deleteForceFail", force=True) result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name=deleteForceFail", "--protect") @@ -645,6 +652,8 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): def test_delete__fails(self): """Test deleting an authentication policy, but it fails.""" # Create regular authentication policy. + self.addCleanup(self.delete_authentication_policy, + name="regularPolicy", force=True) result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name=regularPolicy") self.assertIsNone(result, msg=err) @@ -665,6 +674,8 @@ class AuthPolicyCmdTestCase(BaseAuthCmdTest): def test_delete__protected_fails(self): """Test deleting an authentication policy, but it fails.""" # Create protected authentication policy. + self.addCleanup(self.delete_authentication_policy, + name="protectedPolicy", force=True) result, out, err = self.runcmd("domain", "auth", "policy", "create", "--name=protectedPolicy", "--protect") diff --git a/python/samba/tests/samba_tool/domain_auth_silo.py b/python/samba/tests/samba_tool/domain_auth_silo.py index ec5042a6e11..d41bd36b270 100644 --- a/python/samba/tests/samba_tool/domain_auth_silo.py +++ b/python/samba/tests/samba_tool/domain_auth_silo.py @@ -227,11 +227,12 @@ class AuthSiloCmdTestCase(BaseAuthCmdTest): def test_modify__description(self): """Test modify authentication silo changing the description field.""" - # Create a silo to modify for this test. name = "modifyDescription" - self.runcmd("domain", "auth", "silo", "create", "--name", name) + + # Create a silo to modify for this test. self.addCleanup(self.delete_authentication_silo, name=name, force=True) + self.runcmd("domain", "auth", "silo", "create", "--name", name) result, out, err = self.runcmd("domain", "auth", "silo", "modify", "--name", name, @@ -244,11 +245,12 @@ class AuthSiloCmdTestCase(BaseAuthCmdTest): def test_modify__audit_enforce(self): """Test modify authentication silo setting --audit and --enforce.""" - # Create a silo to modify for this test. name = "modifyEnforce" - self.runcmd("domain", "auth", "silo", "create", "--name", name) + + # Create a silo to modify for this test. self.addCleanup(self.delete_authentication_silo, name=name, force=True) + self.runcmd("domain", "auth", "silo", "create", "--name", name) result, out, err = self.runcmd("domain", "auth", "silo", "modify", "--name", name, @@ -270,11 +272,12 @@ class AuthSiloCmdTestCase(BaseAuthCmdTest): def test_modify__protect_unprotect(self): """Test modify un-protecting and protecting an authentication silo.""" - # Create a silo to modify for this test. name = "modifyProtect" - self.runcmd("domain", "auth", "silo", "create", "--name", name) + + # Create a silo to modify for this test. self.addCleanup(self.delete_authentication_silo, name=name, force=True) + self.runcmd("domain", "auth", "silo", "create", "--name", name) utils = SDUtils(self.samdb) result, out, err = self.runcmd("domain", "auth", "silo", "modify", @@ -402,6 +405,8 @@ class AuthSiloCmdTestCase(BaseAuthCmdTest): def test_delete__force_fails(self): """Test deleting an authentication silo with --force, but it fails.""" # Create protected authentication silo. + self.addCleanup(self.delete_authentication_silo, + name="deleteForceFail", force=True) result, out, err = self.runcmd("domain", "auth", "silo", "create", "--name=deleteForceFail", "--user-authentication-policy", "User Policy", @@ -423,6 +428,8 @@ class AuthSiloCmdTestCase(BaseAuthCmdTest): def test_delete__fails(self): """Test deleting an authentication silo, but it fails.""" # Create regular authentication silo. + self.addCleanup(self.delete_authentication_silo, + name="regularSilo", force=True) result, out, err = self.runcmd("domain", "auth", "silo", "create", "--name=regularSilo", "--user-authentication-policy", "User Policy") @@ -444,6 +451,8 @@ class AuthSiloCmdTestCase(BaseAuthCmdTest): def test_delete__protected_fails(self): """Test deleting an authentication silo, but it fails.""" # Create protected authentication silo. + self.addCleanup(self.delete_authentication_silo, + name="protectedSilo", force=True) result, out, err = self.runcmd("domain", "auth", "silo", "create", "--name=protectedSilo", "--user-authentication-policy", "User Policy", -- 2.34.1