From 29cefef7e61d227460137443a481b2519abf994e Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Aug 13 2024 02:16:31 +0000 Subject: [PATCH 1/6] tox.ini, Jenkinsfile: drop py36, add py313 The oldest bodhi-client that can auth any more is 6.0.0, and it does not declare itself compatible with Python 3.6, so a pypi- based py36 environment has bodhi-client 5.x and we can't really test it after the next commit. So drop py36. This is unfortunate as EL 8's system Python is 3.6, but we're already effectively broken there because EPEL 8 has bodhi-client 4.x which can no longer authenticate. Add py313 as that's current. Signed-off-by: Adam Williamson --- diff --git a/Jenkinsfile b/Jenkinsfile index b4eaa28..14e4f0d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -53,7 +53,7 @@ git merge --no-ff "proposed/$params.BRANCH" -m "Merge PR" git clone https://pagure.io/rpkg.git # docker image will contain ENV: PYTHONPATH=./rpkg -podman run --rm -v .:/src:Z quay.io/exd-guild-source-tools/fedpkg-test:latest tox -e py36,py39,py312,flake8,bandit --workdir /tmp/tox ${TOX_POSARGS} +podman run --rm -v .:/src:Z quay.io/exd-guild-source-tools/fedpkg-test:latest tox -e py39,py312,py313,flake8,bandit --workdir /tmp/tox ${TOX_POSARGS} """ sh "cat job.sh" sh "ssh -o StrictHostKeyChecking=no root@$hostname mkdir $remote_dir" diff --git a/Makefile b/Makefile index a935264..2a6afd9 100644 --- a/Makefile +++ b/Makefile @@ -5,5 +5,5 @@ test: $(default_targets) tox: @python3 -m venv .env @.env/bin/pip install tox - @.env/bin/tox -e py36,py39,py312,flake --parallel=auto ${TOX_POSARGS} + @.env/bin/tox -e py39,py312,py313,flake8 --parallel=auto ${TOX_POSARGS} .PHONY: tox diff --git a/jenkins_test.dockerfile b/jenkins_test.dockerfile index 9628fc5..8455d8e 100644 --- a/jenkins_test.dockerfile +++ b/jenkins_test.dockerfile @@ -22,4 +22,4 @@ WORKDIR /src ENV PYTHONPATH "${PYTHONPATH}:./rpkg" -CMD ["tox", "-e", "py36,py39,py312,flake8,bandit"] +CMD ["tox", "-e", "py39,py312,py313,flake8,bandit"] diff --git a/tox.ini b/tox.ini index 3498249..1c05814 100644 --- a/tox.ini +++ b/tox.ini @@ -1,13 +1,13 @@ [tox] -envlist = py36,py39,py312,flake8,doc,bandit +envlist = py39,py312,py313,flake8,doc,bandit [testenv] sitepackages=false basepython= - py36: {env:TOXPYTHON:python3.6} py39: {env:TOXPYTHON:python3.9} py312: {env:TOXPYTHON:python3.12} - flake8: {env:TOXPYTHON:python3.6} + py313: {env:TOXPYTHON:python3.13} + flake8: {env:TOXPYTHON:python3.9} doc: {env:TOXPYTHON:python3} bandit: {env:TOXPYTHON:python3} @@ -20,9 +20,6 @@ commands = setenv= PYCURL_SSL_LIBRARY=openssl -[testenv:py36] -package = sdist - [testenv:flake8] skip_install = True deps = flake8 From a29b8e95074c0ca27124d64d096053c85e036aad Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Aug 13 2024 02:16:35 +0000 Subject: [PATCH 2/6] Drop support for bodhi-client <= 5 It cannot possibly work any more, since OpenID auth no longer works on our current Bodhi deployments and will not be "fixed", and bodhi-client < 6 cannot authenticate any other way. This is the last usage of python-fedora in fedpkg, so we can drop that dependency. We also drop some now-unnecessary checks and conditionals. Signed-off-by: Adam Williamson --- diff --git a/fedpkg/__init__.py b/fedpkg/__init__.py index b3e9b3b..ad9cc2f 100644 --- a/fedpkg/__init__.py +++ b/fedpkg/__init__.py @@ -19,17 +19,9 @@ from datetime import datetime, timedelta, timezone # doc/fedpkg_man_page.py uses the 'cli' import from . import cli # noqa +from .bodhi_6 import BodhiClient, UPDATE_TYPES, REQUEST_TYPES, SUGGEST_TYPES from .lookaside import FedoraLookasideCache from pyrpkg.utils import cached_property -# Use deprecated pkg_resources if importlib isn't available (python 3.6) -try: - from importlib.metadata import distribution -except ImportError: - from pkg_resources import get_distribution as distribution -try: - from packaging.version import parse as parse_version -except ImportError: - from pkg_resources import parse_version try: from distro import linux_distribution # noqa @@ -37,13 +29,6 @@ except ImportError: from platform import linux_distribution # noqa -bodhi_version = distribution('bodhi-client').version -if parse_version(bodhi_version) < parse_version("6.0.0"): - from .bodhi_5 import BodhiClient, UPDATE_TYPES, REQUEST_TYPES, SUGGEST_TYPES -else: - from .bodhi_6 import BodhiClient, UPDATE_TYPES, REQUEST_TYPES, SUGGEST_TYPES - - class Commands(pyrpkg.Commands): def __init__(self, *args, **kwargs): diff --git a/fedpkg/bodhi_5.py b/fedpkg/bodhi_5.py deleted file mode 100644 index 9ffa1df..0000000 --- a/fedpkg/bodhi_5.py +++ /dev/null @@ -1,63 +0,0 @@ -# fedpkg - a Python library for RPM Packagers -# -# Copyright (C) 2011 Red Hat Inc. -# Author(s): Jesse Keating -# -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by the -# Free Software Foundation; either version 2 of the License, or (at your -# option) any later version. See http://www.gnu.org/copyleft/gpl.html for -# the full text of the license. - -from bodhi.client.bindings import BodhiClient as _BodhiClient -from fedora.client import AuthError - - -UPDATE_TYPES = ['bugfix', 'security', 'enhancement', 'newpackage'] -REQUEST_TYPES = ['testing', 'stable'] -SUGGEST_TYPES = ['unspecified', 'reboot', 'logout'] - - -def clear_csrf_and_retry(func): - """Clear csrf token and retry - - fedpkg uses Bodhi Python binding API list_overrides first before other - save and extend APIs. That causes a readonly csrf token is received, - which will be got again when next time to construct request data to - modify updates. That is not expected and AuthError will be raised. - - So, the solution is to capture the AuthError error, clear the token and - try to modify update again by requesting another token with user's - credential. - """ - def _decorator(self, *args, **kwargs): - try: - return func(self, *args, **kwargs) - except AuthError: - self._session.cookies.clear() - self.csrf_token = None - return func(self, *args, **kwargs) - return _decorator - - -class BodhiClient(_BodhiClient): - """Customized BodhiClient for fedpkg""" - - @clear_csrf_and_retry - def save(self, *args, **kwargs): - return super(BodhiClient, self).save(*args, **kwargs) - - @clear_csrf_and_retry - def save_override(self, *args, **kwargs): - return super(BodhiClient, self).save_override(*args, **kwargs) - - @clear_csrf_and_retry - def extend_override(self, override, expiration_date): - data = dict( - nvr=override['nvr'], - notes=override['notes'], - expiration_date=expiration_date, - csrf_token=self.csrf(), - ) - return self.send_request( - 'overrides/', verb='POST', auth=True, data=data) diff --git a/pyproject.toml b/pyproject.toml index 1be99b5..ac60b0f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,6 @@ dependencies = [ "distro", "openidc-client", "python-bugzilla", - "python-fedora", "rpkg", "six", ] diff --git a/requirements.txt b/requirements.txt index cb3de2c..7e52a2c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,5 @@ argcomplete bodhi-client openidc-client python-bugzilla -python-fedora rpkg six diff --git a/test/test_cli.py b/test/test_cli.py index 3aa6737..c819614 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -26,13 +26,17 @@ try: from packaging.version import parse as parse_version except ImportError: from pkg_resources import parse_version +# Use deprecated pkg_resources if importlib isn't available (python 3.6) +try: + from importlib.metadata import distribution +except ImportError: + from pkg_resources import get_distribution as distribution import six from six.moves import StringIO from six.moves.configparser import NoOptionError, NoSectionError import fedpkg.cli -from fedpkg import bodhi_version from fedpkg.bugzilla import BugzillaClient from fedpkg.cli import check_bodhi_version from freezegun import freeze_time @@ -51,7 +55,8 @@ except ImportError: try: import bodhi - if parse_version(bodhi_version) < parse_version("5.0.0"): + bodhi_version = distribution('bodhi-client').version + if parse_version(bodhi_version) < parse_version("6.0.0"): raise ImportError("Unsupported bodhi-client") except ImportError: bodhi = None @@ -150,11 +155,10 @@ class TestUpdate(CliTestCase): with io.open(clog_file, 'w', encoding='utf-8') as f: f.write(os.linesep.join(self.fake_clog)) - if parse_version(bodhi_version) >= parse_version("6.0.0"): - self.oidcmeta_patcher = patch( - 'bodhi.client.oidcclient.OIDCClient._get_provider_metadata', _mock_metadata - ) - self.oidcmeta_patcher.start() + self.oidcmeta_patcher = patch( + 'bodhi.client.oidcclient.OIDCClient._get_provider_metadata', _mock_metadata + ) + self.oidcmeta_patcher.start() def tearDown(self): if os.path.exists('bodhi.template'): @@ -168,8 +172,7 @@ class TestUpdate(CliTestCase): self.check_bodhi_version_patcher.stop() self.run_command_patcher.stop() self.nvr_patcher.stop() - if parse_version(bodhi_version) >= parse_version("6.0.0"): - self.oidcmeta_patcher.stop() + self.oidcmeta_patcher.stop() rmdir(self.tempdir) super(TestUpdate, self).tearDown() @@ -179,9 +182,7 @@ class TestUpdate(CliTestCase): @patch('bodhi.client.bindings.BodhiClient.csrf') @patch('bodhi.client.bindings.BodhiClient.send_request') - # Do not operate OpenIDC session file with lock - @patch('fedora.client.OpenIdBaseClient._load_cookies') - def assert_bodhi_update(self, cli, _load_cookies, send_request, csrf, + def assert_bodhi_update(self, cli, send_request, csrf, update_type=None, request_type=None, notes=None, stable_karma=None, unstable_karma=None, suggest=None, severity=None): @@ -1663,11 +1664,10 @@ class TestBodhiOverride(CliTestCase): self.os_environ_patcher = patch.dict('os.environ', {'EDITOR': 'vi', 'HOME': self.tempdir}) self.os_environ_patcher.start() - if parse_version(bodhi_version) >= parse_version("6.0.0"): - self.oidcmeta_patcher = patch( - 'bodhi.client.oidcclient.OIDCClient._get_provider_metadata', _mock_metadata - ) - self.oidcmeta_patcher.start() + self.oidcmeta_patcher = patch( + 'bodhi.client.oidcclient.OIDCClient._get_provider_metadata', _mock_metadata + ) + self.oidcmeta_patcher.start() # Fake build returned from Koji for the specified build NVR in tests self.kojisession.getBuild.return_value = {'build_id': 1} @@ -1676,8 +1676,7 @@ class TestBodhiOverride(CliTestCase): self.anon_kojisession_p.stop() self.cbv_p.stop() self.os_environ_patcher.stop() - if parse_version(bodhi_version) >= parse_version("6.0.0"): - self.oidcmeta_patcher.stop() + self.oidcmeta_patcher.stop() rmdir(self.tempdir) super(TestBodhiOverride, self).tearDown() @@ -1703,10 +1702,8 @@ class TestBodhiOverride(CliTestCase): @patch('bodhi.client.bindings.BodhiClient.list_overrides') @patch('bodhi.client.bindings.BodhiClient.save_override') @patch('bodhi.client.bindings.BodhiClient.override_str') - # Do not acquire lock for local openidc cache file - @patch('fedora.client.OpenIdBaseClient._load_cookies') def test_create_for_given_build( - self, _load_cookies, override_str, save_override, list_overrides): + self, override_str, save_override, list_overrides): list_overrides.return_value = {'total': 0} expiration_date = datetime.now() + timedelta(days=7) new_override = { @@ -1817,50 +1814,6 @@ class TestBodhiOverride(CliTestCase): 'Buildroot override for %s already exists and not ' 'expired.', 'rpkg-1.54-2.fc28') - @unittest.skipIf( - parse_version(bodhi_version) >= parse_version("6.0.0"), - "Retrying is built in Bodhi 6" - ) - @patch('fedora.client.OpenIdBaseClient._load_cookies') - @patch('bodhi.client.bindings.BodhiClient.list_overrides') - @patch('bodhi.client.bindings.BodhiClient.save_override') - @patch('fedpkg.Commands.nvr', new_callable=PropertyMock) - def test_retry_create( - self, nvr, save_override, list_overrides, _load_cookies): - nvr.return_value = 'rpkg-1.54-2.fc28' - list_overrides.return_value = {'total': 0} - - # For save_override raises AuthError twice. - from fedora.client import AuthError - save_override.side_effect = AuthError - - cli_cmd = [ - 'fedpkg', '--path', self.cloned_repo_path, - 'override', 'create', - '--duration', '7', '--notes', 'build for fedpkg', - ] - - with patch('sys.argv', new=cli_cmd): - cli = self.new_cli() - - # This is raised when call save_override in the second time due to - # the AuthError is raised again. This is expected for running this - # test. - six.assertRaisesRegex(self, rpkgError, 'Cannot create override', - cli.create_buildroot_override) - - # First call to save_override should raise AuthError, and it must be - # called twice. - self.assertEqual(2, save_override.call_count) - save_override.assert_has_calls([ - call(nvr='rpkg-1.54-2.fc28', - duration=7, - notes='build for fedpkg'), - call(nvr='rpkg-1.54-2.fc28', - duration=7, - notes='build for fedpkg') - ]) - def test_invalid_duration_option(self): cli_cmds = ( ( @@ -1910,26 +1863,19 @@ class TestBodhiOverrideExtend(CliTestCase): self.os_environ_patcher = patch.dict('os.environ', {'EDITOR': 'vi', 'HOME': self.tempdir}) self.os_environ_patcher.start() - if parse_version(bodhi_version) >= parse_version("6.0.0"): - self.oidcmeta_patcher = patch( - 'bodhi.client.oidcclient.OIDCClient._get_provider_metadata', _mock_metadata - ) - self.oidcmeta_patcher.start() - - self.load_cookies_p = patch( - 'fedora.client.OpenIdBaseClient._load_cookies') - self.mock_load_cookies = self.load_cookies_p.start() + self.oidcmeta_patcher = patch( + 'bodhi.client.oidcclient.OIDCClient._get_provider_metadata', _mock_metadata + ) + self.oidcmeta_patcher.start() # Fake build returned from Koji for the specified build NVR in tests self.kojisession.getBuild.return_value = {'build_id': 1} def tearDown(self): - self.load_cookies_p.stop() self.anon_kojisession_p.stop() self.cbv_p.stop() self.os_environ_patcher.stop() - if parse_version(bodhi_version) >= parse_version("6.0.0"): - self.oidcmeta_patcher.stop() + self.oidcmeta_patcher.stop() rmdir(self.tempdir) super(TestBodhiOverrideExtend, self).tearDown() @@ -2162,75 +2108,6 @@ class TestBodhiOverrideExtend(CliTestCase): six.assertRaisesRegex(self, rpkgError, '', cli.extend_buildroot_override) - @unittest.skipIf( - parse_version(bodhi_version) >= parse_version("6.0.0"), - "Retrying is built in Bodhi 6" - ) - @patch('fedpkg.BodhiClient.list_overrides') - @patch('fedpkg.BodhiClient.csrf') - @patch('fedpkg.BodhiClient.send_request') - def test_retry_to_extend_override_by_days( - self, send_request, csrf, list_overrides): - utcnow = datetime.now(timezone.utc).replace(tzinfo=None) - override_expiration_date = utcnow + timedelta(days=7) - - from fedora.client import AuthError - send_request.side_effect = AuthError - - csrf.side_effect = ['123456', '678901'] - - build_nvr = 'somepkg-1.54-2.fc28' - build_override = { - 'expiration_date': override_expiration_date.strftime('%Y-%m-%d %H:%M:%S'), - 'nvr': build_nvr, - 'notes': 'build for other package', - 'build': {'nvr': build_nvr}, - 'submitter': {'name': 'someone'}, - 'expired_date': utcnow - timedelta(days=20) - } - - list_overrides.return_value = { - 'total': 1, - 'overrides': [build_override] - } - edited_override = build_override.copy() - expected_expiration_date = override_expiration_date + timedelta(days=2) - edited_override['expiration_date'] = \ - expected_expiration_date.strftime('%Y-%m-%d %H:%M:%S') - send_request.return_value = edited_override - - cli_cmd = [ - 'fedpkg', '--path', self.cloned_repo_path, - 'override', 'extend', '2', build_nvr - ] - - with patch('sys.argv', new=cli_cmd): - cli = self.new_cli() - - # This error is expected due to the design for this test. See also - # above explanation to test of `override create`. - six.assertRaisesRegex( - self, rpkgError, '', cli.extend_buildroot_override) - - # Ensure no microsecond is included in the expected expiration data - new_date = override_expiration_date + timedelta(days=2) - expected_expiration_date = datetime(year=new_date.year, - month=new_date.month, - day=new_date.day, - hour=new_date.hour, - minute=new_date.minute, - second=new_date.second) - - send_request.assert_has_calls([ - call('overrides/', verb='POST', auth=True, data={ - 'expiration_date': expected_expiration_date, - 'nvr': build_nvr, - 'notes': build_override['notes'], - 'csrf_token': token, - }) - for token in csrf.side_effect - ]) - @freeze_time('2018-07-22') @patch('fedpkg.BodhiClient.list_overrides') def test_raise_error_if_duration_less_than_today(self, list_overrides): From c566e5e8a6866288b9f8963665e41e1d018e4668 Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Aug 13 2024 02:16:35 +0000 Subject: [PATCH 3/6] Drop all usage of six We no longer support Python 2, so there's no need for it. This also drops some early Python 3-era compatibility things there is no longer any need for. Signed-off-by: Adam Williamson --- diff --git a/doc/fedpkg_man_page.py b/doc/fedpkg_man_page.py index 786264f..58e4d40 100755 --- a/doc/fedpkg_man_page.py +++ b/doc/fedpkg_man_page.py @@ -4,7 +4,7 @@ import os import sys -from six.moves.configparser import ConfigParser +from configparser import ConfigParser if __name__ == '__main__': module_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) diff --git a/fedpkg/__init__.py b/fedpkg/__init__.py index ad9cc2f..da4575f 100644 --- a/fedpkg/__init__.py +++ b/fedpkg/__init__.py @@ -13,7 +13,6 @@ import pyrpkg import os import git import re -import six from datetime import datetime, timedelta, timezone @@ -260,7 +259,7 @@ class Commands(pyrpkg.Commands): """Returns the latest commit message on the current branch""" try: - commitmsg = six.next(self.repo.iter_commits()).message + commitmsg = next(self.repo.iter_commits()).message except TypeError: commitmsg = "" diff --git a/fedpkg/__main__.py b/fedpkg/__main__.py index c440f08..1a2c3f8 100644 --- a/fedpkg/__main__.py +++ b/fedpkg/__main__.py @@ -15,17 +15,12 @@ import logging import os import sys -import six - import fedpkg import fedpkg.utils import pyrpkg import pyrpkg.utils -if six.PY3: # SafeConfigParser == ConfigParser, former deprecated in >= 3.2 - from six.moves.configparser import ConfigParser -else: - from six.moves.configparser import SafeConfigParser as ConfigParser +from configparser import ConfigParser cli_name = os.path.basename(sys.argv[0]) diff --git a/fedpkg/cli.py b/fedpkg/cli.py index d6c7715..d27b8b1 100644 --- a/fedpkg/cli.py +++ b/fedpkg/cli.py @@ -26,12 +26,10 @@ try: import importlib.metadata except ImportError: import pkg_resources -import six from pyrpkg import rpkgError from pyrpkg.cli import cliClient -from six.moves import configparser -from six.moves.configparser import NoOptionError, NoSectionError -from six.moves.urllib_parse import urlparse +import configparser +from urllib.parse import urlparse from fedpkg.bugzilla import BugzillaClient from fedpkg.completers import (build_arches, distgit_branches, fedpkg_packages, @@ -793,7 +791,7 @@ class fedpkgClient(cliClient): return { 'staging': self.config.getboolean(section, 'staging'), } - except (ValueError, NoOptionError, NoSectionError) as e: + except (ValueError, configparser.NoOptionError, configparser.NoSectionError) as e: self.log.error(str(e)) raise rpkgError('Could not get bodhi options. It seems configuration is changed. ' 'Please try to reinstall %s or consult developers to see what ' @@ -863,10 +861,9 @@ class fedpkgClient(cliClient): nvr = "FILL_IN_NVR_HERE" bodhi_args = { 'nvr': nvr, - 'bugs': six.u(''), - 'display_name': six.u(''), - 'descr': six.u( - 'Here is where you give an explanation of your update.'), + 'bugs': '', + 'display_name': '', + 'descr': 'Here is where you give an explanation of your update.', 'request': self.args.request, 'autokarma': str(self.args.autokarma), 'stable_karma': self.args.stable_karma, diff --git a/fedpkg/utils.py b/fedpkg/utils.py index 6f7d2be..0669c3f 100644 --- a/fedpkg/utils.py +++ b/fedpkg/utils.py @@ -18,8 +18,8 @@ import git import requests from pyrpkg import rpkgError from requests.exceptions import ConnectionError -from six.moves.configparser import NoOptionError, NoSectionError -from six.moves.urllib.parse import urlparse +from configparser import NoOptionError, NoSectionError +from urllib.parse import urlparse def query_bodhi(server_url, timeout=60): diff --git a/pyproject.toml b/pyproject.toml index ac60b0f..8cf7880 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,6 @@ dependencies = [ "openidc-client", "python-bugzilla", "rpkg", - "six", ] [project.scripts] diff --git a/requirements.txt b/requirements.txt index 7e52a2c..3c788cd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,3 @@ bodhi-client openidc-client python-bugzilla rpkg -six diff --git a/test/test_bugzilla.py b/test/test_bugzilla.py index 83b608e..6fcc8ad 100644 --- a/test/test_bugzilla.py +++ b/test/test_bugzilla.py @@ -10,8 +10,6 @@ # option) any later version. See http://www.gnu.org/copyleft/gpl.html for # the full text of the license. -import six - from fedpkg.bugzilla import BugzillaClient from pyrpkg import rpkgError from utils import unittest @@ -60,8 +58,8 @@ class TestGetReviewBug(unittest.TestCase): Bugzilla.return_value.getbug.side_effect = ValueError bzc = BugzillaClient('http://bugzilla.example.com') - six.assertRaisesRegex( - self, rpkgError, 'The Bugzilla bug could not be verified.', + self.assertRaisesRegex( + rpkgError, 'The Bugzilla bug could not be verified.', bzc.get_review_bug, 123, 'rpms', 'mypkg') @patch('bugzilla.Bugzilla') @@ -69,8 +67,8 @@ class TestGetReviewBug(unittest.TestCase): Bugzilla.return_value.getbug.return_value = Mock() bzc = BugzillaClient('http://bugzilla.example.com') - six.assertRaisesRegex( - self, rpkgError, 'not the proper type', + self.assertRaisesRegex( + rpkgError, 'not the proper type', bzc.get_review_bug, 123, 'xxx', 'mypkg') @patch('bugzilla.Bugzilla') @@ -80,6 +78,6 @@ class TestGetReviewBug(unittest.TestCase): bzc = BugzillaClient('http://bugzilla.example.com') # namespace container requires bug component is Container Review, # but fake bug has a different component. - six.assertRaisesRegex( - self, rpkgError, 'not the proper type', + self.assertRaisesRegex( + rpkgError, 'not the proper type', bzc.get_review_bug, 123, 'container', 'mypkg') diff --git a/test/test_cli.py b/test/test_cli.py index c819614..b73bb02 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -16,6 +16,7 @@ import json import os import re import sys +from configparser import NoOptionError, NoSectionError from datetime import datetime, timedelta, timezone from os import rmdir from tempfile import mkdtemp, mkstemp @@ -32,10 +33,6 @@ try: except ImportError: from pkg_resources import get_distribution as distribution -import six -from six.moves import StringIO -from six.moves.configparser import NoOptionError, NoSectionError - import fedpkg.cli from fedpkg.bugzilla import BugzillaClient from fedpkg.cli import check_bodhi_version @@ -89,19 +86,19 @@ class TestIsUpdateAborted(CliTestCase): def test_all_line_are_commented_out(self): with io.open(self.bodhi_template, 'w', encoding='utf-8') as f: - f.write(six.u('# line 1\n#line 2\n#line 3\n')) + f.write('# line 1\n#line 2\n#line 3\n') self.assertTrue(self._is_update_aborted()) def test_template_is_ok(self): with io.open(self.bodhi_template, 'w', encoding='utf-8') as f: - f.write(six.u('[fedpkg-1.34-1.fc28]\ntype=\nnotes=abc\n')) + f.write('[fedpkg-1.34-1.fc28]\ntype=\nnotes=abc\n') self.assertFalse(self._is_update_aborted()) def test_template_content_is_broken(self): with io.open(self.bodhi_template, 'w', encoding='utf-8') as f: - f.write(six.u('#[fedpkg-1.34-1.fc28]\ntype=\nnotes=abc\n')) + f.write('#[fedpkg-1.34-1.fc28]\ntype=\nnotes=abc\n') self.assertTrue(self._is_update_aborted()) @@ -142,7 +139,7 @@ class TestUpdate(CliTestCase): self.clog_patcher.start() # Logs will be read in the tests which do not specify --notes option - self.fake_clog = list(six.moves.map(six.u, [ + self.fake_clog = [ 'Add tests for command update', 'New command update - #1000', # invalid bug id format 'Fix tests - #2000, #notabug', # both invalid bug id format @@ -150,7 +147,7 @@ class TestUpdate(CliTestCase): 'fix: rh#10001', 'Fixes: rhbz#20001', ' close: fedora#30001', # test whitespace at the beginning - ])) + ] clog_file = os.path.join(self.cloned_repo_path, 'clog') with io.open(clog_file, 'w', encoding='utf-8') as f: f.write(os.linesep.join(self.fake_clog)) @@ -216,7 +213,7 @@ class TestUpdate(CliTestCase): expected_data = { 'autokarma': 'True', 'bugs': '10001,20001,30001', - 'display_name': six.u(''), + 'display_name': '', 'builds': ' {0} '.format(self.mock_nvr.return_value), 'close_bugs': True, 'request': 'testing', @@ -259,7 +256,7 @@ class TestUpdate(CliTestCase): else: self.assertTrue(self.fake_clog[0] in bodhi_template) rest_clog = os.linesep.join([ - six.u('# {0}').format(line) for line in self.fake_clog[1:] + '# {0}'.format(line) for line in self.fake_clog[1:] ]) self.assertTrue(rest_clog in bodhi_template) @@ -269,21 +266,21 @@ class TestUpdate(CliTestCase): with patch.object(cli.config, 'get', side_effect=NoOptionError('url', 'bodhi')): - six.assertRaisesRegex( - self, rpkgError, 'Could not get bodhi options.', cli.update) + self.assertRaisesRegex( + rpkgError, 'Could not get bodhi options.', cli.update) with patch.object(cli.config, 'get', side_effect=NoSectionError('bodhi')): - six.assertRaisesRegex( - self, rpkgError, 'Could not get bodhi options.', cli.update) + self.assertRaisesRegex( + rpkgError, 'Could not get bodhi options.', cli.update) @patch('os.path.isfile', return_value=False) def test_fail_if_bodhi_template_is_not_a_file(self, isfile): cli_cmd = ['fedpkg', '--path', self.cloned_repo_path, 'update'] cli = self.get_cli(cli_cmd) - six.assertRaisesRegex( - self, rpkgError, 'No bodhi update details saved', + self.assertRaisesRegex( + rpkgError, 'No bodhi update details saved', self.assert_bodhi_update, cli) self.mock_run_command.assert_called_once_with( @@ -323,8 +320,8 @@ class TestUpdate(CliTestCase): cli_cmd = ['fedpkg', '--path', self.cloned_repo_path, 'update'] cli = self.get_cli(cli_cmd) - six.assertRaisesRegex( - self, rpkgError, 'Could not generate update request', + self.assertRaisesRegex( + rpkgError, 'Could not generate update request', self.assert_bodhi_update, cli) def test_create_update_in_stage_bodhi(self): @@ -340,30 +337,30 @@ class TestUpdate(CliTestCase): def test_missing_update_type_in_template(self): cli_cmd = ['fedpkg-stage', '--path', self.cloned_repo_path, 'update'] cli = self.get_cli(cli_cmd) - six.assertRaisesRegex(self, rpkgError, 'Missing update type', - self.assert_bodhi_update, cli) + self.assertRaisesRegex(rpkgError, 'Missing update type', + self.assert_bodhi_update, cli) def test_incorrect_update_type_in_template(self): cli_cmd = ['fedpkg-stage', '--path', self.cloned_repo_path, 'update'] cli = self.get_cli(cli_cmd) - six.assertRaisesRegex(self, rpkgError, 'Incorrect update type', - self.assert_bodhi_update, cli, update_type='xxx') + self.assertRaisesRegex(rpkgError, 'Incorrect update type', + self.assert_bodhi_update, cli, update_type='xxx') def test_incorrect_request_type_in_template(self): cli_cmd = ['fedpkg-stage', '--path', self.cloned_repo_path, 'update'] cli = self.get_cli(cli_cmd) - six.assertRaisesRegex(self, rpkgError, 'Incorrect request type', - self.assert_bodhi_update, cli, - update_type='enhancement', - request_type='xxx') + self.assertRaisesRegex(rpkgError, 'Incorrect request type', + self.assert_bodhi_update, cli, + update_type='enhancement', + request_type='xxx') def test_incorrect_suggest_type_in_template(self): cli_cmd = ['fedpkg-stage', '--path', self.cloned_repo_path, 'update'] cli = self.get_cli(cli_cmd) - six.assertRaisesRegex(self, rpkgError, 'Incorrect suggest type', - self.assert_bodhi_update, cli, - update_type='enhancement', - suggest='123') + self.assertRaisesRegex(rpkgError, 'Incorrect suggest type', + self.assert_bodhi_update, cli, + update_type='enhancement', + suggest='123') def test_create_with_cli_options(self): cli_cmd = [ @@ -413,7 +410,7 @@ class TestUpdate(CliTestCase): self.assert_bodhi_update( cli, update_type='bugfix', notes='Line 1\nLine 2\nLine 3') - @patch('sys.stderr', new=six.StringIO()) + @patch('sys.stderr', new=io.StringIO()) def test_invalid_stable_karma_option(self): with self.assertRaises(SystemExit): self.get_cli([ @@ -427,7 +424,7 @@ class TestUpdate(CliTestCase): 'update', '--stable-karma', '-3' ]) - @patch('sys.stderr', new=six.StringIO()) + @patch('sys.stderr', new=io.StringIO()) def test_invalid_unstable_karma_option(self): with self.assertRaises(SystemExit): self.get_cli([ @@ -441,7 +438,7 @@ class TestUpdate(CliTestCase): 'update', '--unstable-karma', '3' ]) - @patch('sys.stderr', new=six.StringIO()) + @patch('sys.stderr', new=io.StringIO()) def test_invalid_bug(self): with self.assertRaises(SystemExit): self.get_cli([ @@ -493,7 +490,7 @@ class TestRequestRepo(CliTestCase): return self.new_cli(name=name, cfg=cfg, user_cfg=user_cfg) @patch('requests.post') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_repo(self, mock_request_post, mock_bz): """Tests a standard request-repo call""" self.mock_bug.summary = ('Review Request: testpkg - a description') @@ -531,7 +528,7 @@ class TestRequestRepo(CliTestCase): self.assertEqual(output, expected_output) @patch('requests.post') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_repo_override(self, mock_request_post, mock_bz): """Tests a request-repo call with an overridden repo name""" mock_bz.getbug.return_value = self.mock_bug @@ -569,7 +566,7 @@ class TestRequestRepo(CliTestCase): self.assertEqual(output, expected_output) @patch('requests.post') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_repo_module(self, mock_request_post, mock_bz): """Tests a request-repo call for a new module""" self.mock_bug.product = 'Fedora Modules' @@ -608,7 +605,7 @@ class TestRequestRepo(CliTestCase): self.assertEqual(output, expected_output) @patch('requests.post') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_repo_container(self, mock_request_post, mock_bz): """Tests a request-repo call for a new container""" self.mock_bug.product = 'Fedora Container Images' @@ -650,7 +647,7 @@ class TestRequestRepo(CliTestCase): self.assertEqual(output, expected_output) @patch('requests.post') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_repo_with_optional_details( self, mock_request_post, mock_bz): """Tests a request-repo call with the optional details""" @@ -690,7 +687,7 @@ class TestRequestRepo(CliTestCase): self.assertEqual(output, expected_output) @patch('requests.post') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_repo_exception(self, mock_request_post, mock_bz): """Tests a request-repo call with the exception flag""" mock_rv = Mock() @@ -935,7 +932,7 @@ class TestRequestBranch(CliTestCase): @patch('requests.get') @patch('requests.post') @patch('fedpkg.cli.get_release_branches') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_branch(self, mock_grb, mock_request_post, mock_request_get): """Tests request-branch""" mock_grb.return_value = {'fedora': ['f25', 'f26', 'f27'], @@ -981,7 +978,7 @@ class TestRequestBranch(CliTestCase): @patch('requests.get') @patch('requests.post') @patch('fedpkg.cli.get_release_branches') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_existing_branch(self, mock_grb, mock_request_post, mock_request_get): """Tests request-existing-branch""" mock_grb.return_value = {'fedora': ['f25', 'f26', 'f27'], @@ -1008,7 +1005,7 @@ class TestRequestBranch(CliTestCase): @patch('requests.get') @patch('requests.post') @patch('fedpkg.cli.get_release_branches') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_branch_override(self, mock_grb, mock_request_post, mock_request_get): """Tests request-branch with an overridden package and branch name""" mock_grb.return_value = {'fedora': ['f25', 'f26', 'f27'], @@ -1051,7 +1048,7 @@ class TestRequestBranch(CliTestCase): @patch('requests.get') @patch('requests.post') @patch('fedpkg.cli.get_release_branches') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_epel_branch_override( self, mock_grb, mock_request_post, mock_request_get ): @@ -1100,7 +1097,7 @@ class TestRequestBranch(CliTestCase): @patch('requests.get') @patch('requests.post') @patch('fedpkg.cli.get_release_branches') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_branch_module(self, mock_grb, mock_request_post, mock_request_get): """Tests request-branch for a new module branch""" mock_grb.return_value = {'fedora': ['f25', 'f26', 'f27'], @@ -1178,14 +1175,14 @@ class TestRequestBranch(CliTestCase): 'fedora-scm-requests/issue/2') self.assertEqual(output, expected_output) - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_branch_with_global_option_name_and_namespace(self): cli_cmd = ['fedpkg-stage', '--path', self.cloned_repo_path, '--name', 'nethack', '--namespace', 'container', 'request-branch', 'f27'] self.assert_request_branch_container(cli_cmd) - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_branch_with_its_own_option_repo_and_namespace(self): cli_cmd = ['fedpkg-stage', '--path', self.cloned_repo_path, 'request-branch', @@ -1196,7 +1193,7 @@ class TestRequestBranch(CliTestCase): @patch('requests.post') @patch('fedpkg.cli.get_release_branches') @patch('fedpkg.cli.verify_sls') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_branch_sls(self, mock_verify_sls, mock_grb, mock_request_post, mock_request_get): """Tests request-branch with service levels""" @@ -1299,7 +1296,7 @@ class TestRequestBranch(CliTestCase): @patch('requests.get') @patch('requests.post') @patch('fedpkg.cli.get_release_branches') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_branch_all_releases(self, mock_grb, mock_request_post, mock_request_get): """Tests request-branch with the '--all-releases' option """ mock_grb.return_value = {'fedora': ['f25', 'f26', 'f27'], @@ -1489,13 +1486,13 @@ https://pagure.stg.example.com/releng/fedora-scm-requests/issue/3""" 'supported arches, therefore, it cannot be in EPEL. If this is a ' 'mistake or you have an exception, please contact the Release ' 'Engineering team.') - with six.assertRaisesRegex(self, rpkgError, expected_error): + with self.assertRaisesRegex(rpkgError, expected_error): cli.request_branch() @patch('requests.get') @patch('requests.post') @patch('fedpkg.cli.get_release_branches') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_with_repo_option(self, mock_grb, mock_request_post, mock_request_get): """Test request branch with option --repo""" mock_grb.return_value = {'fedora': ['f25', 'f26', 'f27'], @@ -1550,7 +1547,7 @@ class TestRequestTestsRepo(CliTestCase): @patch('requests.post') @patch('requests.get') - @patch('sys.stdout', new=StringIO()) + @patch('sys.stdout', new=io.StringIO()) def test_request_tests_repo(self, mock_request_get, mock_request_post): """Tests request-tests-repo""" @@ -1626,8 +1623,8 @@ try: def test_no_2_x_version_installed(self, distribution): distribution.side_effect = importlib.metadata.PackageNotFoundError - six.assertRaisesRegex( - self, rpkgError, r'bodhi-client < 2\.0 is not supported\.', + self.assertRaisesRegex( + rpkgError, r'bodhi-client < 2\.0 is not supported\.', check_bodhi_version) except ImportError: @@ -1640,8 +1637,8 @@ except ImportError: def test_no_2_x_version_installed(self, get_distribution): get_distribution.side_effect = pkg_resources.DistributionNotFound - six.assertRaisesRegex( - self, rpkgError, r'bodhi-client < 2\.0 is not supported\.', + self.assertRaisesRegex( + rpkgError, r'bodhi-client < 2\.0 is not supported\.', check_bodhi_version) @@ -1693,8 +1690,8 @@ class TestBodhiOverride(CliTestCase): with patch('sys.argv', new=cli_cmd): cli = self.new_cli() - six.assertRaisesRegex( - self, rpkgError, 'Build {0} does not exist'.format(build_nvr), + self.assertRaisesRegex( + rpkgError, 'Build {0} does not exist'.format(build_nvr), cli.create_buildroot_override) self.kojisession.getBuild.assert_called_once_with(build_nvr) @@ -1836,7 +1833,7 @@ class TestBodhiOverride(CliTestCase): for cmd, expected_output in cli_cmds: with patch('sys.argv', new=cmd): - with patch('sys.stderr', new=StringIO()): + with patch('sys.stderr', new=io.StringIO()): with self.assertRaises(SystemExit): self.new_cli() output = sys.stderr.getvalue() @@ -1889,8 +1886,8 @@ class TestBodhiOverrideExtend(CliTestCase): with patch('sys.argv', new=cli_cmd): cli = self.new_cli() - six.assertRaisesRegex( - self, rpkgError, 'Build somepkg-1.54-2.fc28 does not exist.', + self.assertRaisesRegex( + rpkgError, 'Build somepkg-1.54-2.fc28 does not exist.', cli.extend_buildroot_override) @patch('fedpkg.BodhiClient.list_overrides') @@ -2105,8 +2102,8 @@ class TestBodhiOverrideExtend(CliTestCase): with patch('sys.argv', new=cli_cmd): cli = self.new_cli() - six.assertRaisesRegex(self, rpkgError, '', - cli.extend_buildroot_override) + self.assertRaisesRegex(rpkgError, '', + cli.extend_buildroot_override) @freeze_time('2018-07-22') @patch('fedpkg.BodhiClient.list_overrides') @@ -2134,8 +2131,8 @@ class TestBodhiOverrideExtend(CliTestCase): ] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() - six.assertRaisesRegex( - self, rpkgError, + self.assertRaisesRegex( + rpkgError, 'specified expiration date .+ should be future date', cli.extend_buildroot_override) @@ -2145,7 +2142,7 @@ class TestBodhiOverrideExtend(CliTestCase): 'override', 'extend', '2019/01/10', 'rpkg-1.10-1.fc28' ] with patch('sys.argv', new=cli_cmd): - with patch('sys.stderr', new=StringIO()): + with patch('sys.stderr', new=io.StringIO()): with self.assertRaises(SystemExit): self.new_cli() output = sys.stderr.getvalue() @@ -2173,7 +2170,7 @@ class TestReadReleasesFromLocalConfig(CliTestCase): error_msg = 'given path \'{0}\' doesn\'t exist'.format(self.cloned_repo_path) validate_path.side_effect = argparse.ArgumentTypeError(error_msg) with patch('sys.argv', new=self.fake_cmd): - with patch('sys.stderr', new=six.StringIO()): + with patch('sys.stderr', new=io.StringIO()): # argparse.ArgumentTypeError turns to SystemExit with self.assertRaises(SystemExit): self.new_cli() @@ -2197,8 +2194,8 @@ class TestReadReleasesFromLocalConfig(CliTestCase): with patch('fedpkg.cli.configparser.ConfigParser.read') as read: read.return_value = [] - six.assertRaisesRegex( - self, rpkgError, '.+ not accessible', + self.assertRaisesRegex( + rpkgError, '.+ not accessible', cli.read_releases_from_local_config, self.active_releases) def test_get_expanded_releases(self): @@ -2344,7 +2341,7 @@ class TestReleasesInfo(CliTestCase): def assert_output_releases(self, expected_output, option=[]): with patch('sys.argv', ['fedpkg', 'releases-info'] + option): cli = self.new_cli() - with patch('sys.stdout', new=six.StringIO()): + with patch('sys.stdout', new=io.StringIO()): cli.show_releases_info() output = sys.stdout.getvalue().strip() self.assertEqual(expected_output, output) diff --git a/test/test_commands.py b/test/test_commands.py index a5b5ed5..df6a2ac 100644 --- a/test/test_commands.py +++ b/test/test_commands.py @@ -9,8 +9,7 @@ # option) any later version. See http://www.gnu.org/copyleft/gpl.html for # the full text of the license. -import six -from six.moves import builtins +import builtins from pyrpkg.errors import rpkgError from utils import CommandTestCase @@ -411,8 +410,8 @@ class TestFindRawhideBranch(CommandTestCase): # As the code shows, any error will be caught koji_session.getBuildTarget.side_effect = ValueError - six.assertRaisesRegex( - self, rpkgError, 'Unable to find rawhide target', + self.assertRaisesRegex( + rpkgError, 'Unable to find rawhide target', self.cmd._findrawhidebranch) diff --git a/test/test_retire.py b/test/test_retire.py index 98d7385..9b10caf 100644 --- a/test/test_retire.py +++ b/test/test_retire.py @@ -1,13 +1,12 @@ # -*- coding: utf-8 -*- +import configparser import os import shutil import subprocess import tempfile -import six from pyrpkg.errors import rpkgError -from six.moves import configparser from utils import unittest from fedpkg.cli import fedpkgClient @@ -130,8 +129,7 @@ class RetireTestCase(unittest.TestCase): args = ['fedpkg', '--release=rawhide', 'disable-monitoring'] client = self._fake_client(args) - six.assertRaisesRegex( - self, + self.assertRaisesRegex( rpkgError, "The following error occurred while disabling monitoring: Invalid or expired token\n" "For invalid or expired tokens please set a new token", diff --git a/test/test_utils.py b/test/test_utils.py index 7a38740..04ea22a 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -10,11 +10,10 @@ # option) any later version. See http://www.gnu.org/copyleft/gpl.html for # the full text of the license. +from configparser import NoOptionError, NoSectionError import json -import six from requests.exceptions import ConnectionError -from six.moves.configparser import NoOptionError, NoSectionError from fedpkg import utils from freezegun import freeze_time @@ -123,15 +122,15 @@ class TestAssertNewTestsRepo(unittest.TestCase): def test_should_raise_error_if_connection_error_to_distgit(self, get): get.side_effect = ConnectionError - six.assertRaisesRegex( - self, rpkgError, 'The error was', + self.assertRaisesRegex( + rpkgError, 'The error was', utils.assert_new_tests_repo, 'testrepo', 'http://distgit/') def test_test_repo_exists(self, get): get.return_value = Mock(ok=True) - six.assertRaisesRegex( - self, rpkgError, 'Repository .+ already exists', + self.assertRaisesRegex( + rpkgError, 'Repository .+ already exists', utils.assert_new_tests_repo, 'testrepo', 'http://distgit/') def test_keep_quiet_if_repo_not_exist(self, get): @@ -155,23 +154,21 @@ class TestGetPagureToken(unittest.TestCase): config = Mock() config.get.side_effect = NoOptionError('token', 'fedpkg.pagure') - six.assertRaisesRegex(self, - rpkgError, "Missing option 'token' in the section 'fedpkg.pagure'", - utils.config_get_safely, - config, - 'fedpkg.pagure', 'token') + self.assertRaisesRegex(rpkgError, "Missing option 'token' in the section 'fedpkg.pagure'", + utils.config_get_safely, + config, + 'fedpkg.pagure', 'token') config.get.side_effect = NoSectionError('fedpkg.pagure') - six.assertRaisesRegex(self, rpkgError, "Missing section 'fedpkg.pagure'", - utils.config_get_safely, config, 'fedpkg.pagure', 'token') + self.assertRaisesRegex(rpkgError, "Missing section 'fedpkg.pagure'", + utils.config_get_safely, config, 'fedpkg.pagure', 'token') class TestVerifySLS(unittest.TestCase): """Test verify_sls""" def test_sl_date_format_is_invalid(self): - six.assertRaisesRegex( - self, + self.assertRaisesRegex( rpkgError, "The EOL date .+ is in an invalid format", utils.verify_sls, @@ -233,22 +230,22 @@ class TestAssertValidEPELPackage(unittest.TestCase): def test_raise_error_if_connection_error(self, get): get.side_effect = ConnectionError - six.assertRaisesRegex( - self, rpkgError, 'The error was:', + self.assertRaisesRegex( + rpkgError, 'The error was:', utils.assert_valid_epel_package, 'pkg', 'epel7') def test_raise_error_if_GET_response_not_ok(self, get): get.return_value = Mock(ok=False, status_code=404) - six.assertRaisesRegex( - self, rpkgError, 'The status code was: 404', + self.assertRaisesRegex( + rpkgError, 'The status code was: 404', utils.assert_valid_epel_package, 'pkg', 'epel7') def test_correct_url_epel7(self, get): get.return_value = Mock(ok=False, status_code=404) - six.assertRaisesRegex( - self, rpkgError, 'The status code was: 404', + self.assertRaisesRegex( + rpkgError, 'The status code was: 404', utils.assert_valid_epel_package, 'pkg', 'epel7') get.assert_called_once_with( 'https://infrastructure.fedoraproject.org/repo/json/pkg_el7.json', @@ -257,8 +254,8 @@ class TestAssertValidEPELPackage(unittest.TestCase): def test_correct_url_epel8(self, get): get.return_value = Mock(ok=False, status_code=404) - six.assertRaisesRegex( - self, rpkgError, 'The status code was: 404', + self.assertRaisesRegex( + rpkgError, 'The status code was: 404', utils.assert_valid_epel_package, 'pkg', 'epel8') get.assert_called_once_with( 'https://infrastructure.fedoraproject.org/repo/json/pkg_el8.json', @@ -267,8 +264,8 @@ class TestAssertValidEPELPackage(unittest.TestCase): def test_correct_url_epel8_next(self, get): get.return_value = Mock(ok=False, status_code=404) - six.assertRaisesRegex( - self, rpkgError, 'The status code was: 404', + self.assertRaisesRegex( + rpkgError, 'The status code was: 404', utils.assert_valid_epel_package, 'pkg', 'epel8-next') get.assert_called_once_with( 'https://infrastructure.fedoraproject.org/repo/json/pkg_el8.json', @@ -277,8 +274,8 @@ class TestAssertValidEPELPackage(unittest.TestCase): def test_correct_url_epel9(self, get): get.return_value = Mock(ok=False, status_code=404) - six.assertRaisesRegex( - self, rpkgError, 'The status code was: 404', + self.assertRaisesRegex( + rpkgError, 'The status code was: 404', utils.assert_valid_epel_package, 'pkg', 'epel9') get.assert_called_once_with( 'https://composes.stream.centos.org/production/' @@ -288,8 +285,8 @@ class TestAssertValidEPELPackage(unittest.TestCase): def test_correct_url_epel10(self, get): get.return_value = Mock(ok=False, status_code=404) - six.assertRaisesRegex( - self, rpkgError, 'The status code was: 404', + self.assertRaisesRegex( + rpkgError, 'The status code was: 404', utils.assert_valid_epel_package, 'pkg', 'epel10') get.assert_called_once_with( 'https://composes.stream.centos.org/stream-10/production' @@ -299,8 +296,8 @@ class TestAssertValidEPELPackage(unittest.TestCase): def test_correct_url_epel10_10(self, get): get.return_value = Mock(ok=False, status_code=404) - six.assertRaisesRegex( - self, rpkgError, 'The status code was: 404', + self.assertRaisesRegex( + rpkgError, 'The status code was: 404', utils.assert_valid_epel_package, 'pkg', 'epel10.10') get.assert_called_once_with( 'https://composes.stream.centos.org/stream-10/production' @@ -320,8 +317,8 @@ class TestAssertValidEPELPackage(unittest.TestCase): } } - six.assertRaisesRegex( - self, rpkgError, 'is built on all supported arches', + self.assertRaisesRegex( + rpkgError, 'is built on all supported arches', utils.assert_valid_epel_package, 'pkg1', 'el6') def test_should_not_have_epel_branch_for_el7_pkg(self, get): @@ -337,8 +334,8 @@ class TestAssertValidEPELPackage(unittest.TestCase): } } - six.assertRaisesRegex( - self, rpkgError, 'is built on all supported arches', + self.assertRaisesRegex( + rpkgError, 'is built on all supported arches', utils.assert_valid_epel_package, 'pkg1', 'epel7') def test_raise_error_if_package_has_noarch_only(self, get): @@ -353,8 +350,8 @@ class TestAssertValidEPELPackage(unittest.TestCase): } } - six.assertRaisesRegex( - self, rpkgError, 'This package is already an EL package', + self.assertRaisesRegex( + rpkgError, 'This package is already an EL package', utils.assert_valid_epel_package, 'pkg1', 'epel7') @@ -366,8 +363,8 @@ class TestNewPagureIssue(unittest.TestCase): post.side_effect = ConnectionError logger = Mock() - six.assertRaisesRegex( - self, rpkgError, 'The connection to Pagure failed', + self.assertRaisesRegex( + rpkgError, 'The connection to Pagure failed', utils.new_pagure_issue, logger, 'http://distgit/', '123456', 'new package', {'repo': 'pkg1'}, 'fedpkg') @@ -377,8 +374,8 @@ class TestNewPagureIssue(unittest.TestCase): post.return_value = rv logger = Mock() - six.assertRaisesRegex( - self, rpkgError, + self.assertRaisesRegex( + rpkgError, 'The following error occurred while creating a new issue', utils.new_pagure_issue, logger, 'http://distgit/', '123456', 'new package', {'repo': 'pkg1'}, 'fedpkg') @@ -392,8 +389,8 @@ class TestNewPagureIssue(unittest.TestCase): post.return_value = rv logger = Mock() - six.assertRaisesRegex( - self, rpkgError, + self.assertRaisesRegex( + rpkgError, '\nFor invalid or expired tokens please ' 'set a new token in your user configuration with:' '\n\n\tfedpkg set-pagure-token \n', @@ -446,16 +443,16 @@ class TestQueryBodhi(unittest.TestCase): get.side_effect = ConnectionError result = utils.query_bodhi('http://localhost/') - six.assertRaisesRegex( - self, rpkgError, 'The connection to BODHI failed', + self.assertRaisesRegex( + rpkgError, 'The connection to BODHI failed', list, result) def test_response_not_ok(self, get): get.return_value.ok = False result = utils.query_bodhi('http://localhost/') - six.assertRaisesRegex( - self, rpkgError, 'The following error occurred', + self.assertRaisesRegex( + rpkgError, 'The following error occurred', list, result) def test_read_yield_data_normally(self, get): @@ -576,6 +573,6 @@ class TestGetFedoraReleaseState(unittest.TestCase): def test_config_does_not_have_option(self): config = Mock() config.get.side_effect = NoOptionError('releases_service', 'fedpkg.bodhi') - six.assertRaisesRegex(self, rpkgError, r"Could not get release state for Fedora \(F30M\): " - "No option 'releases_service' in section: 'fedpkg.bodhi'.", - utils.get_fedora_release_state, config, 'fedpkg', 'F30M') + self.assertRaisesRegex(rpkgError, r"Could not get release state for Fedora \(F30M\): " + "No option 'releases_service' in section: 'fedpkg.bodhi'.", + utils.get_fedora_release_state, config, 'fedpkg', 'F30M') diff --git a/test/utils.py b/test/utils.py index 3899412..c98f7ec 100644 --- a/test/utils.py +++ b/test/utils.py @@ -10,14 +10,13 @@ # option) any later version. See http://www.gnu.org/copyleft/gpl.html for # the full text of the license. +import configparser import logging import os import shutil import subprocess import tempfile -from six.moves import configparser - import fedpkg.cli import pyrpkg from fedpkg import Commands From 9a1ff707d3228dd319c59ca09e70168ca2a9a3df Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Aug 16 2024 14:16:47 +0000 Subject: [PATCH 4/6] Replace and update use of deprecated 'linux_distribution' This uses distro.id and distro.version instead, and adjusts the handling appropriately. The value we get here is only ever used for comparison with Commands._disttag, which is set in _load_rpmdefines_branch. There is handling there for Fedora (tag is always 'fcNN'), various forms of EL (with possible tags like 'elN', 'elN_N' or 'elN.next'), OLPC (tag 'olpcNN', I think this is obsolete) and ELN (tag 'elnNN', I think). disttags that show up in the test suite are 'fc26', 'el6', 'el7', 'el8.next', 'el10', 'el10_10', 'fc28', 'olpc7' and 'eln104'. This handles Fedora in the obvious way. It handles RHEL, CentOS and Alma flavors of EL, assuming branch names (and hence dist tags) with major version only; I don't think we can handle minor versions, really, because e.g. RHEL or Alma 9.4 report their distro.version as '9.4', but we want the tag to be 'el9'. I think the major_minor thing is meant for EPEL 10, so *possibly* we could handle it with differing behaviour between EL >= 10 and EL < 10, but I'm really not enough of an expert to know. At least this should be no worse than before. It handles ELN by checking for the variant if the id is Fedora (ELN looks a lot like Fedora but can be distinguished by variant) then doing the counterpart to what the dist tag definition does (reading the 'eln' build macro to get the digits). The old code did not handle ELN. I don't think it's possible to handle EPEL next; the old code did not either. I don't think it's worth handling OLPC. Signed-off-by: Adam Williamson --- diff --git a/fedpkg/__init__.py b/fedpkg/__init__.py index da4575f..2ca256a 100644 --- a/fedpkg/__init__.py +++ b/fedpkg/__init__.py @@ -22,10 +22,7 @@ from .bodhi_6 import BodhiClient, UPDATE_TYPES, REQUEST_TYPES, SUGGEST_TYPES from .lookaside import FedoraLookasideCache from pyrpkg.utils import cached_property -try: - from distro import linux_distribution # noqa -except ImportError: - from platform import linux_distribution # noqa +import distro class Commands(pyrpkg.Commands): @@ -274,18 +271,14 @@ class Commands(pyrpkg.Commands): """Need to know what the runtime env is, so we can unset anything conflicting """ - try: - runtime_os, runtime_version, _ = linux_distribution() - except Exception: - return None - - if runtime_os in ['redhat', 'centos']: - return 'el%s' % runtime_version - if runtime_os == 'Fedora': - return 'fc%s' % runtime_version - if (runtime_os == 'Red Hat Enterprise Linux Server' or - runtime_os.startswith('CentOS')): - return 'el{0}'.format(runtime_version.split('.')[0]) + if distro.id() == 'rhel' or 'rhel' in distro.like().split(): + return 'el%s' % distro.major_version() + if distro.id() == 'fedora': + if distro.os_release_info()['variant'] == 'ELN': + macros = self._get_build_macros('eln-candidate') + return 'eln%s' % macros['eln'] + return 'fc%s' % distro.major_version() + return None def check_inheritance(self, build_target, dest_tag): """Disable check inheritance diff --git a/test/test_commands.py b/test/test_commands.py index df6a2ac..29ad5f7 100644 --- a/test/test_commands.py +++ b/test/test_commands.py @@ -27,40 +27,44 @@ class TestDetermineRuntimeEnv(CommandTestCase): super(TestDetermineRuntimeEnv, self).setUp() self.cmd = self.make_commands() - @patch('fedpkg.linux_distribution') - def test_return_fedora_disttag(self, linux_distribution): - linux_distribution.return_value = ('Fedora', '25', 'Twenty Five') - + @patch('distro.id', return_value='fedora') + @patch('distro.major_version', return_value='25') + def test_return_fedora_disttag(self, mock_ver, mock_id): result = self.cmd._determine_runtime_env() self.assertEqual('fc25', result) - @patch('fedpkg.linux_distribution') - def test_return_None_if_os_is_unknown(self, linux_distribution): - linux_distribution.side_effect = ValueError - + @patch('distro.id', return_value='') + def test_return_None_if_os_is_unknown(self, mock_id): self.assertEqual(None, self.cmd._determine_runtime_env()) - @patch('fedpkg.linux_distribution') - def test_return_for_rhel(self, linux_distribution): - linux_distribution.return_value = ('Red Hat Enterprise Linux Server', - '6.8', - 'Santiago') - + @patch('distro.os_release_info', return_value={'variant': 'ELN'}) + @patch('distro.id', return_value='fedora') + @patch('distro.major_version', return_value='41') + @patch('pyrpkg.Commands.kojisession', new_callable=PropertyMock) + def test_return_for_eln(self, mock_koji, mock_ver, mock_id, mock_info): + self.cmd._kojisession = Mock() + koji_session = mock_koji.return_value + koji_session.getBuildConfig.return_value = { + # minimal subset of the real koji response + 'extra': { + 'rpm.macro.eln': '128', + }, + } result = self.cmd._determine_runtime_env() - self.assertEqual('el6', result) + self.assertEqual('eln128', result) def test_return_for_el(self): dists = [ - (('CentOS', '6.9', 'Final'), 'el6'), - (('CentOS Linux', '7.3.1611', 'Core'), 'el7'), - (('redhat', '6', None), 'el6'), - (('centos', '6', None), 'el6'), + ('centos', '8', 'el8'), + ('rhel', '9', 'el9'), + ('almalinux', '10', 'el10'), ] - for dist, expected_dist_tag in dists: - with patch('fedpkg.linux_distribution', return_value=dist): - result = self.cmd._determine_runtime_env() - self.assertEqual(expected_dist_tag, result) + for did, dver, exptag in dists: + with patch('distro.id', return_value=did): + with patch('distro.major_version', return_value=dver): + result = self.cmd._determine_runtime_env() + self.assertEqual(exptag, result) class TestLoadTarget(CommandTestCase): From 280325bc785bb4a96af772b12d952f365d0aabff Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Aug 16 2024 14:16:50 +0000 Subject: [PATCH 5/6] Modernize and clean unittest imports Signed-off-by: Adam Williamson --- diff --git a/test/test_bugzilla.py b/test/test_bugzilla.py index 6fcc8ad..c0a8b02 100644 --- a/test/test_bugzilla.py +++ b/test/test_bugzilla.py @@ -10,14 +10,11 @@ # option) any later version. See http://www.gnu.org/copyleft/gpl.html for # the full text of the license. +import unittest +from unittest.mock import Mock, patch + from fedpkg.bugzilla import BugzillaClient from pyrpkg import rpkgError -from utils import unittest - -try: - from unittest.mock import Mock, patch -except ImportError: - from mock import Mock, patch class BugzillaSideEffect(object): diff --git a/test/test_cli.py b/test/test_cli.py index b73bb02..e2d82dc 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -16,10 +16,12 @@ import json import os import re import sys +import unittest from configparser import NoOptionError, NoSectionError from datetime import datetime, timedelta, timezone from os import rmdir from tempfile import mkdtemp, mkstemp +from unittest.mock import Mock, PropertyMock, call, patch import git # Use deprecated pkg_resources if packaging library isn't available (python 3.6) @@ -41,16 +43,6 @@ from pyrpkg.errors import rpkgError from utils import CliTestCase try: - import unittest2 as unittest -except ImportError: - import unittest - -try: - from unittest.mock import Mock, PropertyMock, call, patch -except ImportError: - from mock import Mock, PropertyMock, call, patch - -try: import bodhi bodhi_version = distribution('bodhi-client').version if parse_version(bodhi_version) < parse_version("6.0.0"): diff --git a/test/test_commands.py b/test/test_commands.py index 29ad5f7..330797f 100644 --- a/test/test_commands.py +++ b/test/test_commands.py @@ -10,15 +10,11 @@ # the full text of the license. import builtins +from unittest.mock import Mock, PropertyMock, call, mock_open, patch from pyrpkg.errors import rpkgError from utils import CommandTestCase -try: - from unittest.mock import Mock, PropertyMock, call, mock_open, patch -except ImportError: - from mock import Mock, PropertyMock, call, mock_open, patch - class TestDetermineRuntimeEnv(CommandTestCase): """Test Commands._determine_runtime_env""" diff --git a/test/test_retire.py b/test/test_retire.py index 9b10caf..e6f371e 100644 --- a/test/test_retire.py +++ b/test/test_retire.py @@ -5,21 +5,16 @@ import os import shutil import subprocess import tempfile +from unittest import mock, TestCase from pyrpkg.errors import rpkgError -from utils import unittest from fedpkg.cli import fedpkgClient -try: - from unittest import mock -except ImportError: - import mock - TEST_CONFIG = os.path.join(os.path.dirname(__file__), 'fedpkg-test.conf') -class RetireTestCase(unittest.TestCase): +class RetireTestCase(TestCase): def setUp(self): self.tmpdir = tempfile.mkdtemp() self.log = mock.Mock() diff --git a/test/test_utils.py b/test/test_utils.py index 04ea22a..09ddbdb 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -12,18 +12,14 @@ from configparser import NoOptionError, NoSectionError import json +import unittest +from unittest.mock import Mock, patch from requests.exceptions import ConnectionError from fedpkg import utils from freezegun import freeze_time from pyrpkg.errors import rpkgError -from utils import unittest - -try: - from unittest.mock import Mock, patch -except ImportError: - from mock import Mock, patch class TestUtils(unittest.TestCase): diff --git a/test/utils.py b/test/utils.py index c98f7ec..b4ce20c 100644 --- a/test/utils.py +++ b/test/utils.py @@ -16,16 +16,12 @@ import os import shutil import subprocess import tempfile +import unittest import fedpkg.cli import pyrpkg from fedpkg import Commands -try: - import unittest2 as unittest -except ImportError: - import unittest - class Assertions(object): From 186c987abecafbbfbc4fb3d7d012470e23cb2675 Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Aug 16 2024 14:16:50 +0000 Subject: [PATCH 6/6] Update check_bodhi_version to check for >= 6.0.0 This loses the logic for handling an exception if we're under 2.0 or bodhi-client isn't installed at all, but ehhh, that seems unnecessary. It's pretty unlikely anyone could manage to have < 2.0 at this point. Signed-off-by: Adam Williamson --- diff --git a/fedpkg/cli.py b/fedpkg/cli.py index d27b8b1..d42f848 100644 --- a/fedpkg/cli.py +++ b/fedpkg/cli.py @@ -23,9 +23,14 @@ import textwrap from datetime import datetime # Use deprecated pkg_resources if importlib isn't available (python 3.6) try: - import importlib.metadata + from importlib.metadata import distribution except ImportError: - import pkg_resources + from pkg_resources import get_distribution as distribution +# Use deprecated pkg_resources if packaging library isn't available (python 3.6) +try: + from packaging.version import parse as parse_version +except ImportError: + from pkg_resources import parse_version from pyrpkg import rpkgError from pyrpkg.cli import cliClient import configparser @@ -94,16 +99,9 @@ require_testcases=%(require_testcases)s def check_bodhi_version(): # Use deprecated pkg_resources if importlib isn't available (python 3.6) - try: - try: - importlib.metadata.distribution('bodhi_client') - except importlib.metadata.PackageNotFoundError: - raise rpkgError('bodhi-client < 2.0 is not supported.') - except NameError: - try: - pkg_resources.get_distribution('bodhi_client') - except pkg_resources.DistributionNotFound: - raise rpkgError('bodhi-client < 2.0 is not supported.') + bodhi_version = distribution('bodhi-client').version + if parse_version(bodhi_version) < parse_version("6.0.0"): + raise rpkgError('bodhi-client < 6.0.0 is not supported.') class fedpkgClient(cliClient): diff --git a/test/test_cli.py b/test/test_cli.py index e2d82dc..d201e7f 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -1604,34 +1604,15 @@ class TestRequestTestsRepo(CliTestCase): self.assertEqual(str(error), expected_error) -# Use deprecated pkg_resources if importlib isn't available (python 3.6) -try: - import importlib.metadata - - class TestCheckBodhiVersion(unittest.TestCase): - """Test check_bodhi_version""" - - @patch('importlib.metadata.distribution') - def test_no_2_x_version_installed(self, distribution): - distribution.side_effect = importlib.metadata.PackageNotFoundError - - self.assertRaisesRegex( - rpkgError, r'bodhi-client < 2\.0 is not supported\.', - check_bodhi_version) +class TestCheckBodhiVersion(unittest.TestCase): + """Test check_bodhi_version""" -except ImportError: - import pkg_resources - - class TestCheckBodhiVersion(unittest.TestCase): - """Test check_bodhi_version""" - - @patch('pkg_resources.get_distribution') - def test_no_2_x_version_installed(self, get_distribution): - get_distribution.side_effect = pkg_resources.DistributionNotFound - - self.assertRaisesRegex( - rpkgError, r'bodhi-client < 2\.0 is not supported\.', - check_bodhi_version) + @patch('fedpkg.cli.distribution') + def test_no_6_x_version_installed(self, mock_distribution): + mock_distribution.return_value.version = '5.7.5' + self.assertRaisesRegex( + rpkgError, r'bodhi-client < 6\.0\.0 is not supported\.', + check_bodhi_version) @unittest.skipUnless(bodhi, 'Skip if no supported bodhi-client is available')