From 9ee057a4ccf3170deeb4626aa2387085d2ec6d5b Mon Sep 17 00:00:00 2001
From: Jan Kolarik <jkolarik@redhat.com>
Date: Tue, 13 Sep 2022 14:35:10 +0200
Subject: [PATCH] Fix plugins unit tests + unload plugins upon their deletion

Conflict:NA
Reference:https://github.com/rpm-software-management/dnf/commit/7ef317db99efeddb66905134292b0fe05e2ed58c
---
 dnf/plugin.py              |  8 ++++++--
 tests/api/test_dnf_base.py | 24 +++++++++++++++++++-----
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/dnf/plugin.py b/dnf/plugin.py
index b083727deb..d2f46ce340 100644
--- a/dnf/plugin.py
+++ b/dnf/plugin.py
@@ -98,6 +98,9 @@ def __init__(self):
         self.plugin_cls = []
         self.plugins = []
 
+    def __del__(self):
+        self._unload()
+
     def _caller(self, method):
         for plugin in self.plugins:
             try:
@@ -164,8 +167,9 @@ def run_transaction(self):
         self._caller('transaction')
 
     def _unload(self):
-        logger.debug(_('Plugins were unloaded'))
-        del sys.modules[DYNAMIC_PACKAGE]
+        if DYNAMIC_PACKAGE in sys.modules:
+            logger.log(dnf.logging.DDEBUG, 'Plugins were unloaded.')
+            del sys.modules[DYNAMIC_PACKAGE]
 
     def unload_removed_plugins(self, transaction):
         """
diff --git a/tests/api/test_dnf_base.py b/tests/api/test_dnf_base.py
index e84e272b77..19754b072e 100644
--- a/tests/api/test_dnf_base.py
+++ b/tests/api/test_dnf_base.py
@@ -7,10 +7,23 @@
 import dnf
 import dnf.conf
 
+import tests.support
+
 from .common import TestCase
 from .common import TOUR_4_4
 
 
+def conf_with_empty_plugins():
+    """
+    Use empty configuration to avoid importing plugins from default paths
+    which would lead to crash of other tests.
+    """
+    conf = tests.support.FakeConf()
+    conf.plugins = True
+    conf.pluginpath = []
+    return conf
+
+
 class DnfBaseApiTest(TestCase):
     def setUp(self):
         self.base = dnf.Base(dnf.conf.Conf())
@@ -75,13 +88,12 @@ def test_transaction(self):
         self.assertHasType(self.base.transaction, dnf.db.group.RPMTransaction)
 
     def test_init_plugins(self):
-        # Base.init_plugins(disabled_glob=(), enable_plugins=(), cli=None)
+        # Base.init_plugins()
         self.assertHasAttr(self.base, "init_plugins")
 
-        # disable plugins to avoid calling dnf.plugin.Plugins._load() multiple times
-        # which causes the tests to crash
-        self.base.conf.plugins = False
-        self.base.init_plugins(disabled_glob=(), enable_plugins=(), cli=None)
+        self.base._conf = conf_with_empty_plugins()
+
+        self.base.init_plugins()
 
     def test_pre_configure_plugins(self):
         # Base.pre_configure_plugins()
@@ -99,6 +111,8 @@ def test_unload_plugins(self):
         # Base.unload_plugins()
         self.assertHasAttr(self.base, "unload_plugins")
 
+        self.base._conf = conf_with_empty_plugins()
+
         self.base.init_plugins()
         self.base.unload_plugins()
 