Blame SOURCES/0004-Fix-plugins-unit-tests.patch

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