From daa1b5cd09e215af2964b33ed17949334d4bd212 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Sep 23 2017 12:32:21 +0000 Subject: [PATCH 1/2] Remove deprecated call to platform.dist As documented in Python documentation, platform.dist is deprecated since version 2.6 and platform.linux_distribution should be used in new code. Meanwhile, Python 2.6 is already the minimum version fedpkg works with. It makes sense to remove the call to platform.dist at current stage. Tests are added and new code for writing and running tests are also added. This patch also fixes a bug that is original mydist is referenced before assignment. Signed-off-by: Chenxiong Qi --- diff --git a/fedpkg/__init__.py b/fedpkg/__init__.py index 420eebe..cf6e4de 100644 --- a/fedpkg/__init__.py +++ b/fedpkg/__init__.py @@ -197,32 +197,16 @@ class Commands(pyrpkg.Commands): """Need to know what the runtime env is, so we can unset anything conflicting """ - try: - mydist = platform.linux_distribution() + runtime_os, runtime_version, _ = platform.linux_distribution() except: - # This is marked as eventually being deprecated. - try: - mydist = platform.dist() - except: - runtime_os = 'unknown' - runtime_version = '0' - - if mydist: - runtime_os = mydist[0] - runtime_version = mydist[1] - else: - runtime_os = 'unknown' - runtime_version = '0' + return None if runtime_os in ['redhat', 'centos']: return 'el%s' % runtime_version if runtime_os == 'Fedora': return 'fc%s' % runtime_version - # fall through, return None - return None - def check_inheritance(self, build_target, dest_tag): """Disable check inheritance diff --git a/test/fedpkg-test.conf b/test/fedpkg-test.conf index b3a4bf8..091334a 100644 --- a/test/fedpkg-test.conf +++ b/test/fedpkg-test.conf @@ -1,13 +1,14 @@ [fedpkg] -distgit_namespaced = True -lookaside = http://pkgs.example.com/repo/pkgs -lookasidehash = md5 -lookaside_cgi = https://pkgs.example.com/repo/pkgs/upload.cgi -gitbaseurl = ssh://%(user)s@pkgs.example.com/%(module)s anongiturl = git://pkgs.example.com/%(module)s +gitbaseurl = ssh://%(user)s@pkgs.example.com/%(module)s +lookaside_cgi = https://pkgs.example.com/repo/pkgs/upload.cgi +lookasidehash = sha512 +lookaside = http://pkgs.example.com/repo/pkgs branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$ kojiprofile = koji build_client = koji +distgit_namespaced = True +kerberos_realms = FEDORAPROJECT.ORG [fedpkg.bodhi] url = https://bodhi.dummy.example.com/ diff --git a/test/test_commands.py b/test/test_commands.py new file mode 100644 index 0000000..50f4e5b --- /dev/null +++ b/test/test_commands.py @@ -0,0 +1,34 @@ +# fedpkg - a Python library for RPM Packagers +# +# Copyright (C) 2017 Red Hat Inc. +# Author(s): Chenxiong qi +# +# 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 utils import CommandTestCase +from mock import patch + + +class TestDetermineRuntimeEnv(CommandTestCase): + """Test Commands._determine_runtime_env""" + + def setUp(self): + super(TestDetermineRuntimeEnv, self).setUp() + self.cmd = self.make_commands() + + @patch('platform.linux_distribution') + def test_return_fedora_disttag(self, linux_distribution): + linux_distribution.return_value = ('Fedora', '25', 'Twenty Five') + + result = self.cmd._determine_runtime_env() + self.assertEqual('fc25', result) + + @patch('platform.linux_distribution') + def test_return_None_if_cannot_os_is_unknown(self, linux_distribution): + linux_distribution.side_effect = ValueError + + self.assertEqual(None, self.cmd._determine_runtime_env()) diff --git a/test/utils.py b/test/utils.py new file mode 100644 index 0000000..5cfae0e --- /dev/null +++ b/test/utils.py @@ -0,0 +1,218 @@ +# -*- coding: utf-8 -*- +# Utilities used for running tests +# +# Copyright (C) 2017 Red Hat Inc. +# Author(s): Chenxiong Qi +# +# 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. + +import os +import subprocess +import tempfile +import unittest +import shutil + +from six.moves import configparser +from fedpkg import Commands + + +class Assertions(object): + + def get_exists_method(self, search_dir=None): + if search_dir is None: + def exists(filename): + return os.path.exists(filename) + else: + def exists(filename): + return os.path.exists(os.path.join(search_dir, filename)) + return exists + + def assertFilesExist(self, filenames, search_dir=None): + """Assert existence of files within package repository + + :param filenames: a sequence of file names within package repository to be checked. + :type filenames: list or tuple + """ + assert isinstance(filenames, (tuple, list)) + exists = self.get_exists_method(search_dir) + for filename in filenames: + self.assertTrue(exists(filename), 'Failure because {0} does not exist'.format(filename)) + + def assertFilesNotExist(self, filenames, search_dir=None): + assert isinstance(filenames, (tuple, list)) + exists = self.get_exists_method(search_dir) + for filename in filenames: + self.assertFalse(exists(filename), 'Failure because {0} exists.'.format(filename)) + + +class Utils(object): + + def run_cmd(self, cmd, allow_output=None, **kwargs): + if not allow_output: + kwargs.update({ + 'stdout': subprocess.PIPE, + 'stderr': subprocess.PIPE + }) + subprocess.call(cmd, **kwargs) + + def read_file(self, filename): + with open(filename, 'r') as f: + return f.read() + + def write_file(self, filename, content=''): + with open(filename, 'w') as f: + f.write(content) + + +class fedpkgConfig(object): + + def __init__(self, config_file=None): + if config_file: + self.config_file = config_file + else: + self.config_file = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + 'fedpkg-test.conf') + + config = configparser.RawConfigParser() + config.read([self.config_file]) + + self.anongiturl = config.get('fedpkg', 'anongiturl') + self.gitbaseurl = config.get('fedpkg', 'gitbaseurl') + self.lookaside_cgi = config.get('fedpkg', 'lookaside_cgi') + self.lookasidehash = config.get('fedpkg', 'lookasidehash') + self.lookaside = config.get('fedpkg', 'lookaside') + self.branchre = config.get('fedpkg', 'branchre') + self.kojiprofile = config.get('fedpkg', 'kojiprofile') + self.build_client = config.get('fedpkg', 'build_client') + self.distgit_namespaced = config.getboolean( + 'fedpkg', 'distgit_namespaced') + self.kerberos_realms = config.get('fedpkg', 'kerberos_realms') + + +fedpkg_test_config = fedpkgConfig() + + +class CommandTestCase(Assertions, Utils, unittest.TestCase): + + spec_file_content = '''Summary: Dummy summary +Name: docpkg +Version: 1.2 +Release: 2%{dist} +License: GPL +#Source0: +#Patch0: +Group: Applications/Productivity +BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX) +%description +Dummy docpkg for tests +%prep +%check +%build +touch README.rst +%clean +rm -rf $$RPM_BUILD_ROOT +%install +rm -rf $$RPM_BUILD_ROOT +%files +%defattr(-,root,root,-) +%doc README.rst +%changelog +* Thu Apr 21 2016 Tester - 1.2-2 +- Initial version +''' + + def setUp(self): + # create a base repo + self.repo_path = tempfile.mkdtemp(prefix='fedpkg-commands-tests-') + + self.spec_filename = 'docpkg.spec' + + # Add spec file to this repo and commit + spec_file_path = os.path.join(self.repo_path, self.spec_filename) + with open(spec_file_path, 'w') as f: + f.write(self.spec_file_content) + + git_cmds = [ + ['git', 'init'], + ['touch', 'sources', 'CHANGELOG.rst'], + ['git', 'add', spec_file_path, 'sources', 'CHANGELOG.rst'], + ['git', 'config', 'user.email', 'tester@example.com'], + ['git', 'config', 'user.name', 'tester'], + ['git', 'commit', '-m', '"initial commit"'], + ['git', 'branch', 'rhel-6.8'], + ['git', 'branch', 'rhel-7'], + ['git', 'branch', 'f26'], + ['git', 'branch', 'f27'], + ] + for cmd in git_cmds: + self.run_cmd(cmd, cwd=self.repo_path) + + # Clone the repo + self.cloned_repo_path = tempfile.mkdtemp( + prefix='fedpkg-commands-tests-cloned-') + self.run_cmd(['git', 'clone', self.repo_path, self.cloned_repo_path]) + git_cmds = [ + ['git', 'config', 'user.email', 'tester@example.com'], + ['git', 'config', 'user.name', 'tester'], + ['git', 'branch', '--track', 'rhel-7', 'origin/rhel-7'], + ['git', 'branch', '--track', 'f26', 'origin/f26'], + ['git', 'branch', '--track', 'f27', 'origin/f27'], + ] + for cmd in git_cmds: + self.run_cmd(cmd, cwd=self.cloned_repo_path) + + def tearDown(self): + shutil.rmtree(self.repo_path) + shutil.rmtree(self.cloned_repo_path) + + def make_commands(self, path=None, user=None, dist=None, target=None, + quiet=None): + """Helper method for creating Commands object for test cases + + This is where you should extend to add more features to support + additional requirements from other Commands specific test cases. + + Some tests need customize one of user, dist, target, and quiet options + when creating an instance of Commands. Keyword arguments user, dist, + target, and quiet here is for this purpose. + + :param str path: path to repository where this Commands will work on + top of + :param str user: user passed to --user option + :param str dist: dist passed to --dist option + :param str target: target passed to --target option + :param str quiet: quiet passed to --quiet option + """ + return Commands(path or self.cloned_repo_path, + fedpkg_test_config.lookaside, + fedpkg_test_config.lookasidehash, + fedpkg_test_config.lookaside_cgi, + fedpkg_test_config.gitbaseurl, + fedpkg_test_config.anongiturl, + fedpkg_test_config.branchre, + fedpkg_test_config.kojiprofile, + fedpkg_test_config.build_client, + user=user, dist=dist, target=target, quiet=quiet) + + def checkout_branch(self, repo, branch_name): + """Checkout to a local branch + + :param git.Repo repo: `git.Repo` instance represents a git repository + that current code works on top of. + :param str branch_name: name of local branch to checkout + """ + heads = [head for head in repo.heads if head.name == branch_name] + assert len(heads) > 0, \ + 'Repo must have a local branch named {} that ' \ + 'is for running tests. But now, it does not exist. Please check ' \ + 'if the repo is correct.'.format(branch_name) + + heads[0].checkout() + + def create_branch(self, repo, branch_name): + repo.git.branch(branch_name) From 4271780334c210cbf9738c52b504e723711dd839 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Sep 23 2017 14:10:18 +0000 Subject: [PATCH 2/2] Detect dist tag correctly for RHEL and CentOS Fix #141 Signed-off-by: Chenxiong Qi --- diff --git a/fedpkg/__init__.py b/fedpkg/__init__.py index cf6e4de..8451520 100644 --- a/fedpkg/__init__.py +++ b/fedpkg/__init__.py @@ -206,6 +206,9 @@ class Commands(pyrpkg.Commands): 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]) def check_inheritance(self, build_target, dest_tag): """Disable check inheritance diff --git a/test/test_commands.py b/test/test_commands.py index 50f4e5b..571d87f 100644 --- a/test/test_commands.py +++ b/test/test_commands.py @@ -32,3 +32,23 @@ class TestDetermineRuntimeEnv(CommandTestCase): linux_distribution.side_effect = ValueError self.assertEqual(None, self.cmd._determine_runtime_env()) + + @patch('platform.linux_distribution') + def test_return_for_rhel(self, linux_distribution): + linux_distribution.return_value = ('Red Hat Enterprise Linux Server', + '6.8', + 'Santiago') + + result = self.cmd._determine_runtime_env() + self.assertEqual('el6', result) + + def test_return_for_centos(self): + dists = [ + (('CentOS', '6.9', 'Final'), 'el6'), + (('CentOS Linux', '7.3.1611', 'Core'), 'el7'), + ] + + for dist, expected_dist_tag in dists: + with patch('platform.linux_distribution', return_value=dist): + result = self.cmd._determine_runtime_env() + self.assertEqual(expected_dist_tag, result)