From 6a0901f6fdf744023dc90ec5141aa9ebe4d806fd Mon Sep 17 00:00:00 2001 From: Fraser Tweedale Date: Tue, 7 Jul 2020 20:53:55 +1000 Subject: [PATCH] tests: fix cleanup for CATracker With ca-del now requiring disablement first, CATracker cleanup fails because the CA is not yet disabled. Implement auto disable before delete in CATracker, with an option to suppress. This suppress option is used when testing the "disable first" option. The patch also includes a tweak to EnableTracker cleanup. In case the CA already got deleted (and in the case of CATracker, also disabled), this avoids an attempt to re-enable the already-deleted object. Part of: https://fedorahosted.org/freeipa/ticket/5011 Reviewed-By: Rob Crittenden --- ipatests/test_xmlrpc/test_ca_plugin.py | 10 +++++++++- ipatests/test_xmlrpc/tracker/base.py | 5 +++++ ipatests/test_xmlrpc/tracker/ca_plugin.py | 13 +++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/ipatests/test_xmlrpc/test_ca_plugin.py b/ipatests/test_xmlrpc/test_ca_plugin.py index b5f653c82..c94ef3010 100644 --- a/ipatests/test_xmlrpc/test_ca_plugin.py +++ b/ipatests/test_xmlrpc/test_ca_plugin.py @@ -48,7 +48,7 @@ def default_ca(request): def crud_subca(request, xmlrpc_setup): name = u'crud-subca' subject = u'CN=crud subca test,O=crud testing inc' - tracker = CATracker(name, subject) + tracker = CATracker(name, subject, auto_disable_for_delete=False) return tracker.make_fixture(request) @@ -208,3 +208,11 @@ class TestCAbasicCRUD(XMLRPC_test): self, unrecognised_subject_dn_attrs_subca): with pytest.raises(errors.ValidationError): unrecognised_subject_dn_attrs_subca.create() + + def test_pre_cleanup_disable(self, crud_subca): + # crud_subca was initialised with auto_disable_for_delete=False. But + # we already tested disablement and deletion. Then we recreated it for + # subsequent tests. Now we have finished the tests; crud_subca is + # about to be deleted. But we have to explicitly disable it first, + # otherwise deletion will will and raise a test teardown failure. + crud_subca.disable() diff --git a/ipatests/test_xmlrpc/tracker/base.py b/ipatests/test_xmlrpc/tracker/base.py index d5de41c5b..e5a05adf1 100644 --- a/ipatests/test_xmlrpc/tracker/base.py +++ b/ipatests/test_xmlrpc/tracker/base.py @@ -302,6 +302,11 @@ class EnableTracker(BaseTracker): (enabled/disabled) after the test as it was before it. """ def cleanup(): + if isinstance(self, CreationTracker): + # special case: if it already got deleted there is + # nothing to enable or disable + return + if self.original_enabled != self.enabled: if self.original_enabled: command = self.make_enable_command() diff --git a/ipatests/test_xmlrpc/tracker/ca_plugin.py b/ipatests/test_xmlrpc/tracker/ca_plugin.py index a53d8a517..275ed490a 100644 --- a/ipatests/test_xmlrpc/tracker/ca_plugin.py +++ b/ipatests/test_xmlrpc/tracker/ca_plugin.py @@ -42,7 +42,7 @@ class CATracker(Tracker, EnableTracker): update_keys = ldap_keys - {'dn'} def __init__(self, name, subject, desc=u"Test generated CA", - default_version=None): + default_version=None, auto_disable_for_delete=True): super(CATracker, self).__init__(default_version=default_version) self.attrs = {} self.ipasubjectdn = subject @@ -52,6 +52,9 @@ class CATracker(Tracker, EnableTracker): self.api.env.container_ca, self.api.env.basedn) + # Whether to run ca-disable automatically before deleting the CA. + self.auto_disable_for_delete = auto_disable_for_delete + def make_create_command(self): """Make function that creates the plugin entry object.""" return self.make_command( @@ -92,7 +95,13 @@ class CATracker(Tracker, EnableTracker): def make_delete_command(self): """Make function that deletes the plugin entry object.""" - return self.make_command('ca_del', self.name) + if self.auto_disable_for_delete: + def disable_then_delete(): + self.make_command('ca_disable', self.name)() + return self.make_command('ca_del', self.name)() + return disable_then_delete + else: + return self.make_command('ca_del', self.name) def check_delete(self, result): assert_deepequal(dict(