From 428838650a2237af33edb054830302902a676ab7 Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Oct 19 2018 11:52:21 +0000 Subject: [PATCH 1/2] cli: adding package_manager into mock_config --- diff --git a/builder/kojid b/builder/kojid index 73e0a1d..58886aa 100755 --- a/builder/kojid +++ b/builder/kojid @@ -145,7 +145,7 @@ def main(options, session): # The load-balancing code in getNextTask() will prevent a single builder # from getting overloaded. time.sleep(options.sleeptime) - except (SystemExit,KeyboardInterrupt): + except (SystemExit, KeyboardInterrupt): logger.warn("Exiting") break logger.warn("Shutting down, please wait...") @@ -156,7 +156,7 @@ def main(options, session): class BuildRoot(object): - def __init__(self,session,options,*args,**kwargs): + def __init__(self, session, options, *args, **kwargs): self.logger = logging.getLogger("koji.build.buildroot") self.session = session self.options = options diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py index 0b66be7..2704e31 100644 --- a/cli/koji_cli/commands.py +++ b/cli/koji_cli/commands.py @@ -881,24 +881,39 @@ def anon_handle_mock_config(goptions, session, args): usage += _("\n(Specify the --help global option for a list of other help options)") parser = OptionParser(usage=usage) parser.add_option("-a", "--arch", help=_("Specify the arch")) - parser.add_option("-n", "--name", help=_("Specify the name for the buildroot")) + parser.add_option( + "-n", + "--name", + help=_("Specify the name for the buildroot")) parser.add_option("--tag", help=_("Create a mock config for a tag")) - parser.add_option("--target", help=_("Create a mock config for a build target")) - parser.add_option("--task", help=_("Duplicate the mock config of a previous task")) - parser.add_option("--latest", action="store_true", help=_("use the latest redirect url")) - parser.add_option("--buildroot", help=_("Duplicate the mock config for the specified buildroot id")) + parser.add_option( + "--target", + help=_("Create a mock config for a build target")) + parser.add_option( + "--task", + help=_("Duplicate the mock config of a previous task")) + parser.add_option( + "--latest", + action="store_true", + help=_("use the latest redirect url")) + parser.add_option( + "--buildroot", + help=_("Duplicate the mock config for the specified buildroot id")) parser.add_option("--mockdir", default="/var/lib/mock", metavar="DIR", help=_("Specify mockdir")) parser.add_option("--topdir", metavar="DIR", help=_("Specify topdir")) - parser.add_option("--topurl", metavar="URL", default=goptions.topurl, + parser.add_option("--topurl", metavar="URL", default=options.topurl, help=_("URL under which Koji files are accessible")) parser.add_option("--distribution", default="Koji Testing", help=_("Change the distribution macro")) parser.add_option("--yum-proxy", help=_("Specify a yum proxy")) - parser.add_option("-o", metavar="FILE", dest="ofile", help=_("Output to a file")) + parser.add_option( + "-o", + metavar="FILE", + dest="ofile", + help=_("Output to a file")) (options, args) = parser.parse_args(args) - activate_session(session, goptions) if args: #for historical reasons, we also accept buildroot name as first arg if not options.name: @@ -907,19 +922,30 @@ def anon_handle_mock_config(goptions, session, args): parser.error(_("Name already specified via option")) arch = None opts = {} + event_id = None for k in ('topdir', 'topurl', 'distribution', 'mockdir', 'yum_proxy'): if hasattr(options, k): opts[k] = getattr(options, k) + activate_session(session, goptions) if options.buildroot: try: br_id = int(options.buildroot) except ValueError: parser.error(_("Buildroot id must be an integer")) brootinfo = session.getBuildroot(br_id) + if not brootinfo: + parser.error(_("Buildroot: %s can not be found") % br_id) if options.latest: opts['repoid'] = 'latest' else: opts['repoid'] = brootinfo['repo_id'] + repo = session.repoInfo(brootinfo['repo_id']) + if not repo: + parser.error( + _("Could not find repo: %(repo_id)s for buildroot: %(id)s") % + brootinfo) + opts['repoid'] = repo['id'] + event_id = repo['create_event'] opts['tag_name'] = brootinfo['tag_name'] arch = brootinfo['arch'] elif options.task: @@ -929,44 +955,54 @@ def anon_handle_mock_config(goptions, session, args): parser.error(_("Task id must be an integer")) broots = session.listBuildroots(taskID=task_id) if not broots: - print(_("No buildroots for task %s (or no such task)") % options.task) - return 1 + parser.error( + _("No buildroots for task %s (or no such task)") % + options.task) if len(broots) > 1: - print(_("Multiple buildroots found: %s" % [br['id'] for br in broots])) + print(_("# Multiple buildroots found: %s" % + [br['id'] for br in broots])) + print(_("# Would use the latest one: buildroot: %s" % + broots[-1]['id'])) brootinfo = broots[-1] if options.latest: opts['repoid'] = 'latest' else: opts['repoid'] = brootinfo['repo_id'] + repo = session.repoInfo(brootinfo['repo_id']) + if not repo: + parser.error( + _("Could not find repo: %(repo_id)s for buildroot: %(id)s") % + brootinfo) + opts['repoid'] = repo['id'] + event_id = repo['create_event'] opts['tag_name'] = brootinfo['tag_name'] arch = brootinfo['arch'] if not options.name: options.name = "%s-task_%i" % (opts['tag_name'], task_id) elif options.tag: if not options.arch: - print(_("Please specify an arch")) - return 1 + parser.error(_("Please specify an arch")) tag = session.getTag(options.tag) if not tag: parser.error(_("Invalid tag: %s" % options.tag)) arch = options.arch config = session.getBuildConfig(tag['id']) if not config: - print(_("Could not get config info for tag: %(name)s") % tag) - return 1 + parser.error( + _("Could not get config info for tag: %(name)s") % + tag) opts['tag_name'] = tag['name'] if options.latest: opts['repoid'] = 'latest' else: repo = session.getRepo(config['id']) if not repo: - print(_("Could not get a repo for tag: %(name)s") % tag) - return 1 + parser.error(_("Could not get a repo for tag: %(name)s") % tag) opts['repoid'] = repo['id'] + event_id = repo['create_event'] elif options.target: if not options.arch: - print(_("Please specify an arch")) - return 1 + parser.error(_("Please specify an arch")) arch = options.arch target = session.getBuildTarget(options.target) if not target: @@ -977,12 +1013,18 @@ def anon_handle_mock_config(goptions, session, args): else: repo = session.getRepo(target['build_tag']) if not repo: - print(_("Could not get a repo for tag: %s") % opts['tag_name']) - return 1 + parser.error( + _("Could not get a repo for tag: %s") % + target['build_tag_name']) opts['repoid'] = repo['id'] + event_id = repo['create_event'] else: - parser.error(_("Please specify one of: --tag, --target, --task, --buildroot")) + parser.error( + _("Please specify one of: --tag, --target, --task, --buildroot")) assert False # pragma: no cover + taginfo = session.getBuildConfig(opts['tag_name'], event=event_id) + if 'mock.package_manager' in taginfo['extra']: + opts['package_manager'] = taginfo['extra']['mock.package_manager'] if options.name: name = options.name else: @@ -995,7 +1037,6 @@ def anon_handle_mock_config(goptions, session, args): else: print(output) - def handle_disable_host(goptions, session, args): "[admin] Mark one or more hosts as disabled" usage = _("usage: %prog disable-host [options] hostname ...") diff --git a/tests/test_cli/test_edit_tag.py b/tests/test_cli/test_edit_tag.py index 5244a63..9e28409 100644 --- a/tests/test_cli/test_edit_tag.py +++ b/tests/test_cli/test_edit_tag.py @@ -9,6 +9,7 @@ except ImportError: import unittest from koji_cli.commands import handle_edit_tag +import mock progname = os.path.basename(sys.argv[0]) or 'koji' From 7c48ab5123e15fb61b1b02a83b025fa87af760b3 Mon Sep 17 00:00:00 2001 From: Yu Ming Zhu Date: Oct 29 2018 06:34:00 +0000 Subject: [PATCH 2/2] update unit test --- diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py index 2704e31..2f177dd 100644 --- a/cli/koji_cli/commands.py +++ b/cli/koji_cli/commands.py @@ -903,7 +903,7 @@ def anon_handle_mock_config(goptions, session, args): help=_("Specify mockdir")) parser.add_option("--topdir", metavar="DIR", help=_("Specify topdir")) - parser.add_option("--topurl", metavar="URL", default=options.topurl, + parser.add_option("--topurl", metavar="URL", default=goptions.topurl, help=_("URL under which Koji files are accessible")) parser.add_option("--distribution", default="Koji Testing", help=_("Change the distribution macro")) @@ -1021,7 +1021,7 @@ def anon_handle_mock_config(goptions, session, args): else: parser.error( _("Please specify one of: --tag, --target, --task, --buildroot")) - assert False # pragma: no cover + taginfo = session.getBuildConfig(opts['tag_name'], event=event_id) if 'mock.package_manager' in taginfo['extra']: opts['package_manager'] = taginfo['extra']['mock.package_manager'] diff --git a/tests/test_cli/test_mock_config.py b/tests/test_cli/test_mock_config.py index 78fe928..1e9cdfa 100644 --- a/tests/test_cli/test_mock_config.py +++ b/tests/test_cli/test_mock_config.py @@ -1,6 +1,8 @@ from __future__ import absolute_import + import mock import six + try: import unittest2 as unittest except ImportError: @@ -11,7 +13,6 @@ from . import utils class TestMockConfig(utils.CliTestCase): - # Show long diffs in error output... maxDiff = None @@ -77,7 +78,10 @@ config_opts['macros']['%distribution'] = 'Koji Testing' # Mock out the xmlrpc server session = mock.MagicMock() session.getBuildroot.return_value = buildroot_info - + session.repoInfo.return_value = { + 'id': 101, + 'create_event': 1 + } # Mock config gen_config_mock.return_value = self.mock_output @@ -141,19 +145,28 @@ config_opts['macros']['%distribution'] = 'Koji Testing' stderr=expected) arguments = ['--task', str(task_id)] - expected = "No buildroots for task %s (or no such task)\n" % str(task_id) - self.assertEqual(1, anon_handle_mock_config(options, session, arguments)) - self.assert_console_message(stdout, expected) + expected = self.format_error_message( + "No buildroots for task %s (or no such task)" % str(task_id)) + self.assert_system_exit( + anon_handle_mock_config, + options, + session, + arguments, + stderr=expected) multi_broots = [ - {'id': 1101, 'repo_id': 101, 'tag_name': 'tag_101', 'arch': 'x86_64'}, - {'id': 1111, 'repo_id': 111, 'tag_name': 'tag_111', 'arch': 'x86_64'}, - {'id': 1121, 'repo_id': 121, 'tag_name': 'tag_121', 'arch': 'x86_64'} + {'id': 1101, 'repo_id': 101, 'tag_name': 'tag_101', + 'arch': 'x86_64'}, + {'id': 1111, 'repo_id': 111, 'tag_name': 'tag_111', + 'arch': 'x86_64'}, + {'id': 1121, 'repo_id': 121, 'tag_name': 'tag_121', + 'arch': 'x86_64'} ] session.listBuildroots.return_value = multi_broots anon_handle_mock_config(options, session, arguments) - expected = "Multiple buildroots found: %s" % [br['id'] for br in multi_broots] - self.assert_console_message(stdout, "%s\n\n" % expected) + expected = '# Multiple buildroots found: [1101, 1111, 1121]\n' \ + '# Would use the latest one: buildroot: 1121\n\n' + self.assert_console_message(stdout, expected) opts = self.common_opts.copy() opts.update({ @@ -178,9 +191,12 @@ config_opts['macros']['%distribution'] = 'Koji Testing' def test_handle_mock_config_tag_option( self, activate_session_mock, gen_config_mock, stdout, stderr): """Test anon_handle_mock_config with tag option""" - arguments = [] - tag = 'tag' - tag = {'id': 201, 'name': 'tag', 'arch': 'x86_64'} + tag = {'id': 201, + 'name': 'tag', + 'arch': 'x86_64', + 'extra': { + 'mock.package_manager': 'dnf' + }} options = mock.MagicMock() # Mock out the xmlrpc server @@ -190,9 +206,13 @@ config_opts['macros']['%distribution'] = 'Koji Testing' session.getRepo.return_value = None arguments = ['--tag', tag['name']] - expected = "Please specify an arch\n" - self.assertEqual(1, anon_handle_mock_config(options, session, arguments)) - self.assert_console_message(stdout, expected) + expected = self.format_error_message("Please specify an arch") + self.assert_system_exit( + anon_handle_mock_config, + options, + session, + arguments, + stderr=expected) arguments = ['--tag', tag['name'], '--arch', tag['arch']] expected = self.format_error_message("Invalid tag: %s" % tag['name']) @@ -205,18 +225,31 @@ config_opts['macros']['%distribution'] = 'Koji Testing' # return tag info session.getTag.return_value = tag - expected = "Could not get config info for tag: %(name)s\n" % tag - self.assertEqual(1, anon_handle_mock_config(options, session, arguments)) - self.assert_console_message(stdout, expected) + expected = self.format_error_message( + "Could not get config info for tag: %(name)s" % tag) + self.assert_system_exit( + anon_handle_mock_config, + options, + session, + arguments, + stderr=expected) # return build config - session.getBuildConfig.return_value = {'id': 301} - expected = "Could not get a repo for tag: %(name)s\n" % tag - self.assertEqual(1, anon_handle_mock_config(options, session, arguments)) - self.assert_console_message(stdout, expected) + session.getBuildConfig.return_value = {'id': 201, + 'extra': { + 'mock.package_manager': 'dnf' + }} + expected = self.format_error_message( + "Could not get a repo for tag: %(name)s" % tag) + self.assert_system_exit( + anon_handle_mock_config, + options, + session, + arguments, + stderr=expected) # return repo - session.getRepo.return_value = {'id': 101} + session.getRepo.return_value = {'id': 101, 'create_event': 1} gen_config_mock.return_value = self.mock_output anon_handle_mock_config(options, session, arguments) self.assert_console_message( @@ -230,6 +263,7 @@ config_opts['macros']['%distribution'] = 'Koji Testing' opts.update({ 'repoid': 'latest', 'tag_name': tag['name'], + 'package_manager': 'dnf' }) anon_handle_mock_config(options, session, arguments) self.assert_console_message( @@ -260,14 +294,18 @@ config_opts['macros']['%distribution'] = 'Koji Testing' session.getRepo.return_value = None arguments = ['--target', target['name']] - expected = "Please specify an arch\n" - self.assertEqual(1, anon_handle_mock_config(options, session, arguments)) - self.assert_console_message(stdout, expected) + expected = self.format_error_message("Please specify an arch") + self.assert_system_exit( + anon_handle_mock_config, + options, + session, + arguments, + stderr=expected) arguments = ['--target', target['name'], '--arch', arch] expected = self.format_error_message( - "Invalid target: %s" % target['name']) + "Invalid target: %s" % target['name']) self.assert_system_exit( anon_handle_mock_config, options, @@ -276,9 +314,14 @@ config_opts['macros']['%distribution'] = 'Koji Testing' stderr=expected) session.getBuildTarget.return_value = target - expected = "Could not get a repo for tag: %s\n" % target['build_tag_name'] - self.assertEqual(1, anon_handle_mock_config(options, session, arguments)) - self.assert_console_message(stdout, expected) + expected = self.format_error_message( + "Could not get a repo for tag: %s" % target['build_tag_name']) + self.assert_system_exit( + anon_handle_mock_config, + options, + session, + arguments, + stderr=expected) arguments = self.common_args + ['--target', target['name'], '--arch', arch, @@ -288,7 +331,7 @@ config_opts['macros']['%distribution'] = 'Koji Testing' 'repoid': 101, 'tag_name': target['build_tag_name'] }) - session.getRepo.return_value = {'id': 101} + session.getRepo.return_value = {'id': 101, 'create_event': 1} gen_config_mock.return_value = self.mock_output anon_handle_mock_config(options, session, arguments) self.assert_console_message( @@ -308,7 +351,8 @@ config_opts['macros']['%distribution'] = 'Koji Testing' self.progname, arch, **opts) @mock.patch('sys.stderr', new_callable=six.StringIO) - def test_handle_mock_config_errors(self, stderr): + @mock.patch('koji_cli.commands.activate_session') + def test_handle_mock_config_errors(self, as_mock, stderr): """Test anon_handle_mock_config general error messages""" arguments = [] options = mock.MagicMock() @@ -319,7 +363,7 @@ config_opts['macros']['%distribution'] = 'Koji Testing' # Run it and check immediate output # argument is empty expected = self.format_error_message( - "Please specify one of: --tag, --target, --task, --buildroot") + "Please specify one of: --tag, --target, --task, --buildroot") self.assert_system_exit( anon_handle_mock_config, options, @@ -330,12 +374,13 @@ config_opts['macros']['%distribution'] = 'Koji Testing' # name is specified twice case arguments = [self.progname, '--name', 'name'] expected = self.format_error_message( - "Name already specified via option") + "Name already specified via option") self.assert_system_exit( anon_handle_mock_config, options, session, arguments, + activate_session=None, stderr=expected) def test_handle_mock_config_help(self):