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

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