Blame SOURCES/0034-Fix-plugins-unit-tests-unload-plugins-upon-their-del.patch

922fe2
From 9700b8fabd102fcf289281c3c04238da90d7b28e Mon Sep 17 00:00:00 2001
922fe2
From: Jan Kolarik <jkolarik@redhat.com>
922fe2
Date: Tue, 13 Sep 2022 14:35:10 +0200
922fe2
Subject: [PATCH] Fix plugins unit tests + unload plugins upon their deletion
922fe2
922fe2
=changelog=
922fe2
type: bugfix
922fe2
resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2134309
922fe2
---
922fe2
 dnf/plugin.py              |  8 ++++++--
922fe2
 tests/api/test_dnf_base.py | 24 +++++++++++++++++++-----
922fe2
 2 files changed, 25 insertions(+), 7 deletions(-)
922fe2
922fe2
diff --git a/dnf/plugin.py b/dnf/plugin.py
922fe2
index b083727d..d2f46ce3 100644
922fe2
--- a/dnf/plugin.py
922fe2
+++ b/dnf/plugin.py
922fe2
@@ -98,6 +98,9 @@ class Plugins(object):
922fe2
         self.plugin_cls = []
922fe2
         self.plugins = []
922fe2
 
922fe2
+    def __del__(self):
922fe2
+        self._unload()
922fe2
+
922fe2
     def _caller(self, method):
922fe2
         for plugin in self.plugins:
922fe2
             try:
922fe2
@@ -164,8 +167,9 @@ class Plugins(object):
922fe2
         self._caller('transaction')
922fe2
 
922fe2
     def _unload(self):
922fe2
-        logger.debug(_('Plugins were unloaded'))
922fe2
-        del sys.modules[DYNAMIC_PACKAGE]
922fe2
+        if DYNAMIC_PACKAGE in sys.modules:
922fe2
+            logger.log(dnf.logging.DDEBUG, 'Plugins were unloaded.')
922fe2
+            del sys.modules[DYNAMIC_PACKAGE]
922fe2
 
922fe2
     def unload_removed_plugins(self, transaction):
922fe2
         """
922fe2
diff --git a/tests/api/test_dnf_base.py b/tests/api/test_dnf_base.py
922fe2
index e84e272b..19754b07 100644
922fe2
--- a/tests/api/test_dnf_base.py
922fe2
+++ b/tests/api/test_dnf_base.py
922fe2
@@ -7,10 +7,23 @@ from __future__ import unicode_literals
922fe2
 import dnf
922fe2
 import dnf.conf
922fe2
 
922fe2
+import tests.support
922fe2
+
922fe2
 from .common import TestCase
922fe2
 from .common import TOUR_4_4
922fe2
 
922fe2
 
922fe2
+def conf_with_empty_plugins():
922fe2
+    """
922fe2
+    Use empty configuration to avoid importing plugins from default paths
922fe2
+    which would lead to crash of other tests.
922fe2
+    """
922fe2
+    conf = tests.support.FakeConf()
922fe2
+    conf.plugins = True
922fe2
+    conf.pluginpath = []
922fe2
+    return conf
922fe2
+
922fe2
+
922fe2
 class DnfBaseApiTest(TestCase):
922fe2
     def setUp(self):
922fe2
         self.base = dnf.Base(dnf.conf.Conf())
922fe2
@@ -75,13 +88,12 @@ class DnfBaseApiTest(TestCase):
922fe2
         self.assertHasType(self.base.transaction, dnf.db.group.RPMTransaction)
922fe2
 
922fe2
     def test_init_plugins(self):
922fe2
-        # Base.init_plugins(disabled_glob=(), enable_plugins=(), cli=None)
922fe2
+        # Base.init_plugins()
922fe2
         self.assertHasAttr(self.base, "init_plugins")
922fe2
 
922fe2
-        # disable plugins to avoid calling dnf.plugin.Plugins._load() multiple times
922fe2
-        # which causes the tests to crash
922fe2
-        self.base.conf.plugins = False
922fe2
-        self.base.init_plugins(disabled_glob=(), enable_plugins=(), cli=None)
922fe2
+        self.base._conf = conf_with_empty_plugins()
922fe2
+
922fe2
+        self.base.init_plugins()
922fe2
 
922fe2
     def test_pre_configure_plugins(self):
922fe2
         # Base.pre_configure_plugins()
922fe2
@@ -99,6 +111,8 @@ class DnfBaseApiTest(TestCase):
922fe2
         # Base.unload_plugins()
922fe2
         self.assertHasAttr(self.base, "unload_plugins")
922fe2
 
922fe2
+        self.base._conf = conf_with_empty_plugins()
922fe2
+
922fe2
         self.base.init_plugins()
922fe2
         self.base.unload_plugins()
922fe2
 
922fe2
-- 
922fe2
2.37.3
922fe2