diff --git a/convert2rhel/backup/subscription.py b/convert2rhel/backup/subscription.py index 6b496ae62b..629825c58d 100644 --- a/convert2rhel/backup/subscription.py +++ b/convert2rhel/backup/subscription.py @@ -48,15 +48,17 @@ def enable(self): def restore(self): """Rollback subscription related changes""" + if not self.enabled: + return + loggerinst.task("Rollback: RHSM-related actions") - if self.enabled: - try: - subscription.unregister_system() - except subscription.UnregisterError as e: - loggerinst.warning(str(e)) - except OSError: - loggerinst.warning("subscription-manager not installed, skipping") + try: + subscription.unregister_system() + except subscription.UnregisterError as e: + loggerinst.warning(str(e)) + except OSError: + loggerinst.warning("subscription-manager not installed, skipping") super(RestorableSystemSubscription, self).restore() @@ -71,11 +73,15 @@ def __init__(self): self._is_attached = False def enable(self): + if self.enabled: + return + self._is_attached = subscription.auto_attach_subscription() super(RestorableAutoAttachmentSubscription, self).enable() def restore(self): if self._is_attached: + loggerinst.task("Rollback: Removing auto-attached subscription") subscription.remove_subscription() super(RestorableAutoAttachmentSubscription, self).restore() diff --git a/convert2rhel/unit_tests/backup/subscription_test.py b/convert2rhel/unit_tests/backup/subscription_test.py index 3f38a77391..d2b1dc92ba 100644 --- a/convert2rhel/unit_tests/backup/subscription_test.py +++ b/convert2rhel/unit_tests/backup/subscription_test.py @@ -144,6 +144,14 @@ def test_restore_auto_attach_not_enabled(self, auto_attach_subscription, monkeyp auto_attach_subscription.restore() assert subscription.remove_subscription.call_count == 0 + def test_already_enabled(self, auto_attach_subscription, monkeypatch): + monkeypatch.setattr(subscription, "auto_attach_subscription", mock.Mock()) + auto_attach_subscription.enabled = True + + auto_attach_subscription.enable() + + assert subscription.auto_attach_subscription.call_count == 0 + class TestRestorableDisableRepositories: @pytest.mark.parametrize(