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

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