!156 [sync] PR-152: fix test cases failed in tests/test_plugin.py

From: @openeuler-sync-bot 
Reviewed-by: @anonymous_z 
Signed-off-by: @anonymous_z
This commit is contained in:
openeuler-ci-bot 2022-12-26 06:40:30 +00:00 committed by Gitee
commit d39284b8e5
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 100 additions and 4 deletions

View File

@ -0,0 +1,93 @@
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()

View File

@ -3,7 +3,7 @@
Name: dnf
Version: 4.14.0
Release: 6
Release: 7
Summary: A software package manager that manages packages on Linux distributions.
License: GPLv2+ and GPLv2 and GPL
URL: https://github.com/rpm-software-management/dnf
@ -23,6 +23,8 @@ Patch6000: dnf-4.10.0-sw.patch
Patch6001: 0001-Add-loongarch-architecture-support.patch
%endif
Patch6002: backport-fix-plugins-unit-tests-unload-plugins-upon-their-deletion.patch
BuildArch: noarch
BuildRequires: cmake gettext systemd bash-completion python3-sphinx
Requires: python3-%{name} = %{version}-%{release} libreport-filesystem
@ -101,11 +103,9 @@ mkdir build-py3
#It not have root permissions in obs
#tests/api/test_dnf_base.py test_do_transaction
#tests/api/test_dnf_logging.py test_dnf_logger
sed -i '154,160d' tests/api/test_dnf_base.py
sed -i '168,174d' tests/api/test_dnf_base.py
sed -i '51,55d' tests/api/test_dnf_logging.py
rm -rf tests/test_plugin.py
%build
pushd build-py3
%cmake .. -DPYTHON_DESIRED:FILEPATH=%{__python3}
@ -248,6 +248,9 @@ popd
%{_mandir}/man8/%{name}-automatic.8*
%changelog
* Tue Dec 20 2022 chenhaixing <chenhaixing@huawei.com> - 4.14.0-7
- DESC:fix test cases failed in tests/test_plugin.py
* Thu Dec 15 2022 chenhaixing <chenhaixing@huawei.com> - 4.14.0-6
- DESC:remove tests