From 26189d839b2e16057694bd85f5e85d6338cc4688 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Oct 26 2016 17:44:51 +0000 Subject: PR#187 ut: cli - for handle_build / _unique_path / _upload_progress_callback / _running_in_bg ... Merges #187 https://pagure.io/koji/pull-request/187 --- diff --git a/cli/koji b/cli/koji index 16f9939..6b9cc1b 100755 --- a/cli/koji +++ b/cli/koji @@ -149,7 +149,7 @@ Try "%(progname)s help" to get all available commands Try "%(progname)s --help" for help about the options of a particular command Try "%(progname)s help " to get commands under a particular category Available categories are: %(categories)s -''' % ({'progname': progname, 'categories': categories_ordered}) +''' % ({'progname': progname, 'categories': categories_ordered}) return _(epilog_str) def get_options(): @@ -889,7 +889,7 @@ def _format_size(size): def _format_secs(t): h = t / 3600 - t = t % 3600 + t %= 3600 m = t / 60 s = t % 60 return "%02d:%02d:%02d" % (h, m, s) @@ -913,11 +913,9 @@ def _progress_callback(uploaded, total, piece, time, total_time): def _running_in_bg(): try: - if (not os.isatty(0)) or (os.getpgrp() != os.tcgetpgrp(0)): - return True + return (not os.isatty(0)) or (os.getpgrp() != os.tcgetpgrp(0)) except OSError, e: return True - return False def handle_build(options, session, args): "[build] Build a package from source" @@ -1003,8 +1001,8 @@ def handle_chain_build(options, session, args): parser = OptionParser(usage=usage) parser.add_option("--nowait", action="store_true", help=_("Don't wait on build")) - parser.add_option("--noprogress", action="store_true", - help=_("Do not display progress of the upload")) + parser.add_option("--quiet", action="store_true", + help=_("Do not print the task information"), default=options.quiet) parser.add_option("--background", action="store_true", help=_("Run the build at a lower priority")) (build_opts, args) = parser.parse_args(args) @@ -1061,14 +1059,14 @@ def handle_chain_build(options, session, args): priority = 5 task_id = session.chainBuild(src_list, target, priority=priority) - - print "Created task:", task_id - print "Task info: %s/taskinfo?taskID=%s" % (options.weburl, task_id) + if not build_opts.quiet: + print "Created task:", task_id + print "Task info: %s/taskinfo?taskID=%s" % (options.weburl, task_id) if _running_in_bg() or build_opts.nowait: return else: session.logout() - return watch_tasks(session,[task_id],quiet=options.quiet) + return watch_tasks(session, [task_id], quiet=options.quiet) def handle_maven_build(options, session, args): "[build] Build a Maven package from source" diff --git a/tests/test_cli/test_build.py b/tests/test_cli/test_build.py new file mode 100644 index 0000000..7582f80 --- /dev/null +++ b/tests/test_cli/test_build.py @@ -0,0 +1,704 @@ +import unittest + +import StringIO as stringio + +import os + +import sys + +import mock + +import loadcli + +cli = loadcli.cli + + +class TestBuild(unittest.TestCase): + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + # Mock out the options parsed in main + self.options = mock.MagicMock() + self.options.quiet = None + self.options.weburl = 'weburl' + # Mock out the xmlrpc server + self.session = mock.MagicMock() + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_from_srpm(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, activate_session_mock, + stdout): + target = 'target' + dest_tag = 'dest_tag' + target_info = {'dest_tag': dest_tag} + dest_tag_info = {'locked': False} + source = 'srpm' + task_id = 1 + args = [target, source] + opts = {} + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.build.return_value = task_id + # Run it and check immediate output + # args: target, srpm + # expected: success + rv = cli.handle_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Uploading srpm: srpm + +Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_called_once_with('cli-build') + self.assertEqual(running_in_bg_mock.call_count, 2) + self.session.uploadWrapper.assert_called_once_with(source, 'random_path', callback=cli._progress_callback) + self.session.build.assert_called_once_with('random_path/' + source, target, opts, priority=priority) + self.session.logout.assert_called() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=self.options.quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_from_scm(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + target_info = {'dest_tag': dest_tag} + dest_tag_info = {'locked': False} + source = 'http://scm' + task_id = 1 + args = [target, source] + opts = {} + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.build.return_value = task_id + # Run it and check immediate output + # args: target, http://scm + # expected: success + rv = cli.handle_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_not_called() + running_in_bg_mock.assert_called_once() + self.session.uploadWrapper.assert_not_called() + self.session.build.assert_called_once_with(source, target, opts, priority=priority) + self.session.logout.assert_called() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=self.options.quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('sys.stderr', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_no_arg( + self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, activate_session_mock, stderr, stdout): + args = [] + progname = os.path.basename(sys.argv[0]) or 'koji' + + # Run it and check immediate output + with self.assertRaises(SystemExit) as cm: + cli.handle_build(self.options, self.session, args) + actual_stdout = stdout.getvalue() + actual_stderr = stderr.getvalue() + expected_stdout = '' + expected_stderr = """Usage: %s build [options] target +(Specify the --help global option for a list of other help options) + +%s: error: Exactly two arguments (a build target and a SCM URL or srpm file) are required +""" % (progname, progname) + self.assertMultiLineEqual(actual_stdout, expected_stdout) + self.assertMultiLineEqual(actual_stderr, expected_stderr) + + # Finally, assert that things were called as we expected. + activate_session_mock.assert_not_called() + self.session.getBuildTarget.assert_not_called() + self.session.getTag.assert_not_called() + unique_path_mock.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.uploadWrapper.assert_not_called() + self.session.build.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(cm.exception.code, 2) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('sys.stderr', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_help( + self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, activate_session_mock, stderr, stdout): + args = ['--help'] + progname = os.path.basename(sys.argv[0]) or 'koji' + + # Run it and check immediate output + with self.assertRaises(SystemExit) as cm: + cli.handle_build(self.options, self.session, args) + actual_stdout = stdout.getvalue() + actual_stderr = stderr.getvalue() + expected_stdout = """Usage: %s build [options] target +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit + --skip-tag Do not attempt to tag package + --scratch Perform a scratch build + --wait Wait on the build, even if running in the background + --nowait Don't wait on build + --quiet Do not print the task information + --arch-override=ARCH_OVERRIDE + Override build arches + --repo-id=REPO_ID Use a specific repo + --noprogress Do not display progress of the upload + --background Run the build at a lower priority +""" % progname + expected_stderr = '' + self.assertMultiLineEqual(actual_stdout, expected_stdout) + self.assertMultiLineEqual(actual_stderr, expected_stderr) + + # Finally, assert that things were called as we expected. + activate_session_mock.assert_not_called() + self.session.getBuildTarget.assert_not_called() + self.session.getTag.assert_not_called() + unique_path_mock.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.uploadWrapper.assert_not_called() + self.session.build.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(cm.exception.code, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('sys.stderr', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_arch_override_denied( + self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, activate_session_mock, stderr, stdout): + target = 'target' + source = 'http://scm' + arch_override = 'somearch' + args = [target, source, '--arch-override=' + arch_override] + progname = os.path.basename(sys.argv[0]) or 'koji' + + # Run it and check immediate output + with self.assertRaises(SystemExit) as cm: + cli.handle_build(self.options, self.session, args) + actual_stdout = stdout.getvalue() + actual_stderr = stderr.getvalue() + expected_stdout = '' + expected_stderr = """Usage: %s build [options] target +(Specify the --help global option for a list of other help options) + +%s: error: --arch_override is only allowed for --scratch builds +""" % (progname, progname) + self.assertMultiLineEqual(actual_stdout, expected_stdout) + self.assertMultiLineEqual(actual_stderr, expected_stderr) + + # Finally, assert that things were called as we expected. + activate_session_mock.assert_not_called() + self.session.getBuildTarget.assert_not_called() + self.session.getTag.assert_not_called() + unique_path_mock.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.uploadWrapper.assert_not_called() + self.session.build.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(cm.exception.code, 2) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_none_tag(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stdout): + target = 'nOne' + source = 'http://scm' + task_id = 1 + repo_id = 2 + args = ['--repo-id=' + str(repo_id), target, source] + opts = {'repo_id': repo_id, 'skip_tag': True} + priority = None + + self.session.build.return_value = task_id + # Run it and check immediate output + # args: --repo-id=2, nOne, http://scm + # expected: success + rv = cli.handle_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_not_called() + self.session.getTag.assert_not_called() + unique_path_mock.assert_not_called() + running_in_bg_mock.assert_called_once() + self.session.uploadWrapper.assert_not_called() + # target==None, repo_id==2, skip_tag==True + self.session.build.assert_called_once_with(source, None, opts, priority=priority) + self.session.logout.assert_called() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=self.options.quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stderr', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_target_not_found(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stderr): + target = 'target' + target_info = None + source = 'http://scm' + args = [target, source] + + progname = os.path.basename(sys.argv[0]) or 'koji' + + self.session.getBuildTarget.return_value = target_info + # Run it and check immediate output + # args: target, http://scm + # expected: failed, target not found + with self.assertRaises(SystemExit) as cm: + cli.handle_build(self.options, self.session, args) + actual = stderr.getvalue() + expected = """Usage: %s build [options] target +(Specify the --help global option for a list of other help options) + +%s: error: Unknown build target: target +""" % (progname, progname) + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_not_called() + unique_path_mock.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.uploadWrapper.assert_not_called() + self.session.build.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(cm.exception.code, 2) + + @mock.patch('sys.stderr', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_dest_tag_not_found(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stderr): + target = 'target' + dest_tag = 'dest_tag' + dest_tag_name = 'dest_tag_name' + target_info = {'dest_tag': dest_tag, 'dest_tag_name': dest_tag_name} + dest_tag_info = None + source = 'http://scm' + args = [target, source] + + progname = os.path.basename(sys.argv[0]) or 'koji' + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + # Run it and check immediate output + # args: target, http://scm + # expected: failed, dest_tag not found + with self.assertRaises(SystemExit) as cm: + cli.handle_build(self.options, self.session, args) + actual = stderr.getvalue() + expected = """Usage: %s build [options] target +(Specify the --help global option for a list of other help options) + +%s: error: Unknown destination tag: dest_tag_name +""" % (progname, progname) + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.uploadWrapper.assert_not_called() + self.session.build.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(cm.exception.code, 2) + + @mock.patch('sys.stderr', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_dest_tag_locked(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stderr): + target = 'target' + dest_tag = 'dest_tag' + dest_tag_name = 'dest_tag_name' + target_info = {'dest_tag': dest_tag, 'dest_tag_name': dest_tag_name} + dest_tag_info = {'name': 'dest_tag_name', 'locked': True} + source = 'http://scm' + args = [target, source] + + progname = os.path.basename(sys.argv[0]) or 'koji' + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + # Run it and check immediate output + # args: target, http://scm + # expected: failed, dest_tag is locked + with self.assertRaises(SystemExit) as cm: + cli.handle_build(self.options, self.session, args) + actual = stderr.getvalue() + expected = """Usage: %s build [options] target +(Specify the --help global option for a list of other help options) + +%s: error: Destination tag dest_tag_name is locked +""" % (progname, progname) + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.uploadWrapper.assert_not_called() + self.session.build.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(cm.exception.code, 2) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_arch_override(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + target_info = {'dest_tag': dest_tag} + dest_tag_info = {'locked': False} + source = 'http://scm' + task_id = 1 + arch_override = 'somearch' + args = ['--arch-override=' + arch_override, '--scratch', target, source] + opts = {'arch_override': arch_override, 'scratch': True} + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.build.return_value = task_id + # Run it and check immediate output + # args: --arch-override=somearch, --scratch, target, http://scm + # expected: success + rv = cli.handle_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_not_called() + running_in_bg_mock.assert_called_once() + self.session.uploadWrapper.assert_not_called() + # arch-override=='somearch', scratch==True + self.session.build.assert_called_once_with(source, target, opts, priority=priority) + self.session.logout.assert_called() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=self.options.quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_background(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + target_info = {'dest_tag': dest_tag} + dest_tag_info = {'locked': False} + source = 'http://scm' + task_id = 1 + args = ['--background', target, source] + priority = 5 + opts = {} + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.build.return_value = task_id + # Run it and check immediate output + # args: --background, target, http://scm + # expected: success + rv = cli.handle_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_not_called() + running_in_bg_mock.assert_called_once() + self.session.uploadWrapper.assert_not_called() + self.session.build.assert_called_once_with(source, target, opts, priority=priority) + self.session.logout.assert_called() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=self.options.quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=True) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_running_in_bg(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + target_info = {'dest_tag': dest_tag} + dest_tag_info = {'locked': False} + source = 'srpm' + task_id = 1 + args = [target, source] + opts = {} + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.build.return_value = task_id + # Run it and check immediate output + # args: target, srpm + # expected: success + rv = cli.handle_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Uploading srpm: srpm + +Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_called_once_with('cli-build') + self.assertEqual(running_in_bg_mock.call_count, 2) + # callback==None + self.session.uploadWrapper.assert_called_once_with(source, 'random_path', callback=None) + self.session.build.assert_called_once_with('random_path/' + source, target, opts, priority=priority) + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertIsNone(rv) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_noprogress(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + target_info = {'dest_tag': dest_tag} + dest_tag_info = {'locked': False} + source = 'srpm' + task_id = 1 + args = ['--noprogress', target, source] + opts = {} + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.build.return_value = task_id + # Run it and check immediate output + # args: --noprogress, target, srpm + # expected: success + rv = cli.handle_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Uploading srpm: srpm + +Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_called_once_with('cli-build') + self.assertEqual(running_in_bg_mock.call_count, 2) + # callback==None + self.session.uploadWrapper.assert_called_once_with(source, 'random_path', callback=None) + self.session.build.assert_called_once_with('random_path/' + source, target, opts, priority=priority) + self.session.logout.assert_called_once() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=self.options.quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_quiet(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + target_info = {'dest_tag': dest_tag} + dest_tag_info = {'locked': False} + source = 'srpm' + task_id = 1 + quiet = True + args = ['--quiet', target, source] + opts = {} + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.build.return_value = task_id + # Run it and check immediate output + # args: --quiet, target, srpm + # expected: success + rv = cli.handle_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = '\n' + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_called_once_with('cli-build') + self.assertEqual(running_in_bg_mock.call_count, 2) + # callback==None + self.session.uploadWrapper.assert_called_once_with(source, 'random_path', callback=None) + self.session.build.assert_called_once_with('random_path/' + source, target, opts, priority=priority) + self.session.logout.assert_called_once() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_wait(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + target_info = {'dest_tag': dest_tag} + dest_tag_info = {'locked': False} + source = 'srpm' + task_id = 1 + quiet = None + args = ['--wait', target, source] + opts = {} + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.build.return_value = task_id + # Run it and check immediate output + # args: --wait, target, srpm + # expected: success + rv = cli.handle_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Uploading srpm: srpm + +Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_called_once_with('cli-build') + # the second one won't be executed when wait==False + self.assertEqual(running_in_bg_mock.call_count, 1) + self.session.uploadWrapper.assert_called_once_with(source, 'random_path', callback=cli._progress_callback) + self.session.build.assert_called_once_with('random_path/' + source, target, opts, priority=priority) + self.session.logout.assert_called_once() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=self.options.quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._unique_path', return_value='random_path') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_nowait(self, watch_tasks_mock, running_in_bg_mock, unique_path_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + target_info = {'dest_tag': dest_tag} + dest_tag_info = {'locked': False} + source = 'srpm' + task_id = 1 + args = ['--nowait', target, source] + opts = {} + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.build.return_value = task_id + # Run it and check immediate output + # args: --nowait, target, srpm + # expected: success + rv = cli.handle_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Uploading srpm: srpm + +Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag) + unique_path_mock.assert_called_once_with('cli-build') + # the second one won't be executed when wait==False + self.assertEqual(running_in_bg_mock.call_count, 1) + self.session.uploadWrapper.assert_called_once_with(source, 'random_path', callback=cli._progress_callback) + self.session.build.assert_called_once_with('random_path/' + source, target, opts, priority=priority) + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertIsNone(rv) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_chain_build.py b/tests/test_cli/test_chain_build.py new file mode 100644 index 0000000..188b59c --- /dev/null +++ b/tests/test_cli/test_chain_build.py @@ -0,0 +1,539 @@ +import unittest + +import StringIO as stringio + +import os + +import sys + +import mock + +import loadcli + +cli = loadcli.cli + + +class TestChainBuild(unittest.TestCase): + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + # Mock out the options parsed in main + self.options = mock.MagicMock() + self.options.quiet = None + self.options.weburl = 'weburl' + # Mock out the xmlrpc server + self.session = mock.MagicMock() + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_chain_build(self, watch_tasks_mock, running_in_bg_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + dest_tag_id = 2 + build_tag = 'build_tag' + build_tag_id = 3 + target_info = {'dest_tag': dest_tag_id, 'dest_tag_name': dest_tag, 'build_tag': build_tag_id, + 'build_tag_name': build_tag} + dest_tag_info = {'id': 2, 'name': dest_tag, 'locked': False} + tag_tree = [{'parent_id': 2}, {'parent_id': 4}, {'parent_id': 5}] + source_args = ['http://scm1', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + sources = [['http://scm1'], ['http://scm2', 'http://scm3', 'n-v-r-1'], ['n-v-r-2', 'n-v-r-3']] + task_id = 1 + args = [target] + source_args + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.getFullInheritance.return_value = tag_tree + self.session.chainBuild.return_value = task_id + # Run it and check immediate output + # args: target http://scm1 : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: success + rv = cli.handle_chain_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag_id, strict=True) + self.session.getFullInheritance.assert_called_once_with(build_tag_id) + self.session.chainBuild.assert_called_once_with(sources, target, priority=priority) + running_in_bg_mock.assert_called_once() + self.session.logout.assert_called() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=self.options.quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('sys.stderr', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_chain_build_no_arg( + self, watch_tasks_mock, running_in_bg_mock, activate_session_mock, stderr, stdout): + args = [] + progname = os.path.basename(sys.argv[0]) or 'koji' + + # Run it and check immediate output + with self.assertRaises(SystemExit) as cm: + cli.handle_chain_build(self.options, self.session, args) + actual_stdout = stdout.getvalue() + actual_stderr = stderr.getvalue() + expected_stdout = '' + expected_stderr = """Usage: %s chain-build [options] target URL [URL2 [:] URL3 [:] URL4 ...] +(Specify the --help global option for a list of other help options) + +%s: error: At least two arguments (a build target and a SCM URL) are required +""" % (progname, progname) + self.assertMultiLineEqual(actual_stdout, expected_stdout) + self.assertMultiLineEqual(actual_stderr, expected_stderr) + + # Finally, assert that things were called as we expected. + activate_session_mock.assert_not_called() + self.session.getBuildTarget.assert_not_called() + self.session.getTag.assert_not_called() + self.session.getFullInheritance.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.chainBuild.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(cm.exception.code, 2) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('sys.stderr', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_chain_build_help( + self, watch_tasks_mock, running_in_bg_mock, activate_session_mock, stderr, stdout): + args = ['--help'] + progname = os.path.basename(sys.argv[0]) or 'koji' + + # Run it and check immediate output + with self.assertRaises(SystemExit) as cm: + cli.handle_chain_build(self.options, self.session, args) + actual_stdout = stdout.getvalue() + actual_stderr = stderr.getvalue() + expected_stdout = """Usage: %s chain-build [options] target URL [URL2 [:] URL3 [:] URL4 ...] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit + --nowait Don't wait on build + --quiet Do not print the task information + --background Run the build at a lower priority +""" % progname + expected_stderr = '' + self.assertMultiLineEqual(actual_stdout, expected_stdout) + self.assertMultiLineEqual(actual_stderr, expected_stderr) + + # Finally, assert that things were called as we expected. + activate_session_mock.assert_not_called() + self.session.getBuildTarget.assert_not_called() + self.session.getTag.assert_not_called() + self.session.getFullInheritance.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.chainBuild.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(cm.exception.code, 0) + + @mock.patch('sys.stderr', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_chain_build_target_not_found(self, watch_tasks_mock, running_in_bg_mock, + activate_session_mock, stderr): + target = 'target' + target_info = None + source_args = ['http://scm1', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + args = [target] + source_args + + progname = os.path.basename(sys.argv[0]) or 'koji' + + self.session.getBuildTarget.return_value = target_info + # Run it and check immediate output + # args: target http://scm1 : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: failed, target not found + with self.assertRaises(SystemExit) as cm: + cli.handle_chain_build(self.options, self.session, args) + actual = stderr.getvalue() + expected = """Usage: %s chain-build [options] target URL [URL2 [:] URL3 [:] URL4 ...] +(Specify the --help global option for a list of other help options) + +%s: error: Unknown build target: target +""" % (progname, progname) + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_not_called() + self.session.getFullInheritance.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.chainBuild.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(cm.exception.code, 2) + + @mock.patch('sys.stderr', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_dest_tag_locked(self, watch_tasks_mock, running_in_bg_mock, + activate_session_mock, stderr): + target = 'target' + dest_tag = 'dest_tag' + dest_tag_id = 2 + build_tag = 'build_tag' + build_tag_id = 3 + target_info = {'dest_tag': dest_tag_id, 'dest_tag_name': dest_tag, 'build_tag': build_tag_id, + 'build_tag_name': build_tag} + dest_tag_info = {'id': 2, 'name': dest_tag, 'locked': True} + source_args = ['http://scm1', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + args = [target] + source_args + + progname = os.path.basename(sys.argv[0]) or 'koji' + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + # Run it and check immediate output + # args: target, target http://scm1 : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: failed, dest_tag is locked + with self.assertRaises(SystemExit) as cm: + cli.handle_chain_build(self.options, self.session, args) + actual = stderr.getvalue() + expected = """Usage: %s chain-build [options] target URL [URL2 [:] URL3 [:] URL4 ...] +(Specify the --help global option for a list of other help options) + +%s: error: Destination tag dest_tag is locked +""" % (progname, progname) + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag_id, strict=True) + self.session.getFullInheritance.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.chainBuild.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(cm.exception.code, 2) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_build_dest_tag_not_inherited_by_build_tag(self, watch_tasks_mock, running_in_bg_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + dest_tag_id = 2 + build_tag = 'build_tag' + build_tag_id = 3 + target_info = {'name': target, 'dest_tag': dest_tag_id, 'dest_tag_name': dest_tag, 'build_tag': build_tag_id, + 'build_tag_name': build_tag} + dest_tag_info = {'id': 2, 'name': dest_tag, 'locked': False} + tag_tree = [{'parent_id': 4}, {'parent_id': 5}] + source_args = ['http://scm1', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + args = [target] + source_args + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.getFullInheritance.return_value = tag_tree + # Run it and check immediate output + # args: target, target http://scm1 : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: failed, dest_tag is not in build_tag's inheritance + rv = cli.handle_chain_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Packages in destination tag dest_tag are not inherited by build tag build_tag +Target target is not usable for a chain-build +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag_id, strict=True) + self.session.getFullInheritance.assert_called_once_with(build_tag_id) + running_in_bg_mock.assert_not_called() + self.session.chainBuild.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(rv, 1) + + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_chain_build_invalidated_src(self, watch_tasks_mock, running_in_bg_mock, + activate_session_mock): + target = 'target' + dest_tag = 'dest_tag' + dest_tag_id = 2 + build_tag = 'build_tag' + build_tag_id = 3 + target_info = {'dest_tag': dest_tag_id, 'dest_tag_name': dest_tag, 'build_tag': build_tag_id, + 'build_tag_name': build_tag} + dest_tag_info = {'id': 2, 'name': dest_tag, 'locked': False} + tag_tree = [{'parent_id': 2}, {'parent_id': 4}, {'parent_id': 5}] + source_args = ['badnvr', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + args = [target] + source_args + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.getFullInheritance.return_value = tag_tree + with mock.patch('sys.stdout', new_callable=stringio.StringIO) as stdout: + # Run it and check immediate output + # args: target badnvr : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: failed, src is neither scm nor good n-v-r + rv = cli.handle_chain_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = '"badnvr" is not a SCM URL or package N-V-R\n' + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag_id, strict=True) + self.session.getFullInheritance.assert_called_once_with(build_tag_id) + self.session.chainBuild.assert_not_called() + running_in_bg_mock.assert_not_called() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertEqual(rv, 1) + + with mock.patch('sys.stdout', new_callable=stringio.StringIO) as stdout: + source_args = ['path/n-v-r', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + args = [target] + source_args + # args: target path/n-v-r : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: failed + cli.handle_chain_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = '"path/n-v-r" is not a SCM URL or package N-V-R\n' + self.assertMultiLineEqual(actual, expected) + + with mock.patch('sys.stdout', new_callable=stringio.StringIO) as stdout: + source_args = ['badn-vr', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + args = [target] + source_args + # args: target badn-vr : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: failed + cli.handle_chain_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = '"badn-vr" is not a SCM URL or package N-V-R\n' + self.assertMultiLineEqual(actual, expected) + + with mock.patch('sys.stdout', new_callable=stringio.StringIO) as stdout: + source_args = ['badn-v-r.rpm', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + args = [target] + source_args + # args: target badn-v-r.rpm : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: failed + cli.handle_chain_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = '"badn-v-r.rpm" is not a SCM URL or package N-V-R\n' + self.assertMultiLineEqual(actual, expected) + + with mock.patch('sys.stderr', new_callable=stringio.StringIO) as stderr: + source_args = ['http://scm'] + args = [target] + source_args + + progname = os.path.basename(sys.argv[0]) or 'koji' + + # args: target http://scm + # expected: failed, only one src found + with self.assertRaises(SystemExit) as cm: + cli.handle_chain_build(self.options, self.session, args) + actual = stderr.getvalue() + expected = """Usage: %s chain-build [options] target URL [URL2 [:] URL3 [:] URL4 ...] +(Specify the --help global option for a list of other help options) + +%s: error: You must specify at least one dependency between builds with : (colon) +If there are no dependencies, use the build command instead +""" % (progname, progname) + self.assertMultiLineEqual(actual, expected) + self.assertEqual(cm.exception.code, 2) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_chain_build_background(self, watch_tasks_mock, running_in_bg_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + dest_tag_id = 2 + build_tag = 'build_tag' + build_tag_id = 3 + target_info = {'dest_tag': dest_tag_id, 'dest_tag_name': dest_tag, 'build_tag': build_tag_id, + 'build_tag_name': build_tag} + dest_tag_info = {'id': 2, 'name': dest_tag, 'locked': False} + tag_tree = [{'parent_id': 2}, {'parent_id': 4}, {'parent_id': 5}] + source_args = ['http://scm1', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + sources = [['http://scm1'], ['http://scm2', 'http://scm3', 'n-v-r-1'], ['n-v-r-2', 'n-v-r-3']] + task_id = 1 + args = ['--background', target] + source_args + priority = 5 + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.getFullInheritance.return_value = tag_tree + self.session.chainBuild.return_value = task_id + # Run it and check immediate output + # args: target http://scm1 : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: success + rv = cli.handle_chain_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag_id, strict=True) + self.session.getFullInheritance.assert_called_once_with(build_tag_id) + self.session.chainBuild.assert_called_once_with(sources, target, priority=priority) + running_in_bg_mock.assert_called_once() + self.session.logout.assert_called() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=self.options.quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_chain_build_quiet(self, watch_tasks_mock, running_in_bg_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + dest_tag_id = 2 + build_tag = 'build_tag' + build_tag_id = 3 + target_info = {'dest_tag': dest_tag_id, 'dest_tag_name': dest_tag, 'build_tag': build_tag_id, + 'build_tag_name': build_tag} + dest_tag_info = {'id': 2, 'name': dest_tag, 'locked': False} + tag_tree = [{'parent_id': 2}, {'parent_id': 4}, {'parent_id': 5}] + source_args = ['http://scm1', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + sources = [['http://scm1'], ['http://scm2', 'http://scm3', 'n-v-r-1'], ['n-v-r-2', 'n-v-r-3']] + task_id = 1 + self.options.quiet = True + args = ['--quiet', target] + source_args + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.getFullInheritance.return_value = tag_tree + self.session.chainBuild.return_value = task_id + # Run it and check immediate output + # args: target http://scm1 : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: success + rv = cli.handle_chain_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = '' + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag_id, strict=True) + self.session.getFullInheritance.assert_called_once_with(build_tag_id) + self.session.chainBuild.assert_called_once_with(sources, target, priority=priority) + running_in_bg_mock.assert_called_once() + self.session.logout.assert_called() + watch_tasks_mock.assert_called_once_with(self.session, [task_id], quiet=self.options.quiet) + self.assertEqual(rv, 0) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=True) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_chain_build_running_in_bg(self, watch_tasks_mock, running_in_bg_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + dest_tag_id = 2 + build_tag = 'build_tag' + build_tag_id = 3 + target_info = {'dest_tag': dest_tag_id, 'dest_tag_name': dest_tag, 'build_tag': build_tag_id, + 'build_tag_name': build_tag} + dest_tag_info = {'id': 2, 'name': dest_tag, 'locked': False} + tag_tree = [{'parent_id': 2}, {'parent_id': 4}, {'parent_id': 5}] + source_args = ['http://scm1', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + sources = [['http://scm1'], ['http://scm2', 'http://scm3', 'n-v-r-1'], ['n-v-r-2', 'n-v-r-3']] + task_id = 1 + args = [target] + source_args + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.getFullInheritance.return_value = tag_tree + self.session.chainBuild.return_value = task_id + # Run it and check immediate output + # args: target http://scm1 : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: success + rv = cli.handle_chain_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag_id, strict=True) + self.session.getFullInheritance.assert_called_once_with(build_tag_id) + self.session.chainBuild.assert_called_once_with(sources, target, priority=priority) + running_in_bg_mock.assert_called_once() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertIsNone(rv) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + @mock.patch('koji_cli.activate_session') + @mock.patch('koji_cli._running_in_bg', return_value=False) + @mock.patch('koji_cli.watch_tasks', return_value=0) + def test_handle_chain_build_nowait(self, watch_tasks_mock, running_in_bg_mock, + activate_session_mock, stdout): + target = 'target' + dest_tag = 'dest_tag' + dest_tag_id = 2 + build_tag = 'build_tag' + build_tag_id = 3 + target_info = {'dest_tag': dest_tag_id, 'dest_tag_name': dest_tag, 'build_tag': build_tag_id, + 'build_tag_name': build_tag} + dest_tag_info = {'id': 2, 'name': dest_tag, 'locked': False} + tag_tree = [{'parent_id': 2}, {'parent_id': 4}, {'parent_id': 5}] + source_args = ['http://scm1', ':', 'http://scm2', 'http://scm3', 'n-v-r-1', ':', 'n-v-r-2', 'n-v-r-3'] + sources = [['http://scm1'], ['http://scm2', 'http://scm3', 'n-v-r-1'], ['n-v-r-2', 'n-v-r-3']] + task_id = 1 + args = ['--nowait', target] + source_args + priority = None + + self.session.getBuildTarget.return_value = target_info + self.session.getTag.return_value = dest_tag_info + self.session.getFullInheritance.return_value = tag_tree + self.session.chainBuild.return_value = task_id + # Run it and check immediate output + # args: target http://scm1 : http://scm2 http://scm3 n-v-r-1 : n-v-r-2 n-v-r-3 + # expected: success + rv = cli.handle_chain_build(self.options, self.session, args) + actual = stdout.getvalue() + expected = """Created task: 1 +Task info: weburl/taskinfo?taskID=1 +""" + self.assertMultiLineEqual(actual, expected) + # Finally, assert that things were called as we expected. + activate_session_mock.assert_called_once_with(self.session) + self.session.getBuildTarget.assert_called_once_with(target) + self.session.getTag.assert_called_once_with(dest_tag_id, strict=True) + self.session.getFullInheritance.assert_called_once_with(build_tag_id) + self.session.chainBuild.assert_called_once_with(sources, target, priority=priority) + running_in_bg_mock.assert_called_once() + self.session.logout.assert_not_called() + watch_tasks_mock.assert_not_called() + self.assertIsNone(rv) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_running_in_bg.py b/tests/test_cli/test_running_in_bg.py new file mode 100644 index 0000000..ec669e7 --- /dev/null +++ b/tests/test_cli/test_running_in_bg.py @@ -0,0 +1,45 @@ +import unittest + +import mock + +import loadcli + +cli = loadcli.cli + + +class TestRunningInBg(unittest.TestCase): + @mock.patch('koji_cli.os') + def test_running_in_bg(self, os_mock): + os_mock.isatty.return_value = False + self.assertTrue(cli._running_in_bg()) + os_mock.isatty.return_value = True + os_mock.getpgrp.return_value = 0 + os_mock.tcgetpgrp.return_value = 1 + self.assertTrue(cli._running_in_bg()) + os_mock.tcgetpgrp.return_value = 0 + self.assertFalse(cli._running_in_bg()) + + os_mock.reset_mock() + os_mock.tcgetpgrp.side_effect = OSError + self.assertTrue(cli._running_in_bg()) + os_mock.isatty.assert_called() + os_mock.getpgrp.assert_called() + os_mock.tcgetpgrp.assert_called() + + os_mock.reset_mock() + os_mock.getpgrp.side_effect = OSError + self.assertTrue(cli._running_in_bg()) + os_mock.isatty.assert_called() + os_mock.getpgrp.assert_called() + os_mock.tcgetpgrp.assert_not_called() + + os_mock.reset_mock() + os_mock.isatty.side_effect = OSError + self.assertTrue(cli._running_in_bg()) + os_mock.isatty.assert_called() + os_mock.getpgrp.assert_not_called() + os_mock.tcgetpgrp.assert_not_called() + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_unique_path.py b/tests/test_cli/test_unique_path.py new file mode 100644 index 0000000..528b882 --- /dev/null +++ b/tests/test_cli/test_unique_path.py @@ -0,0 +1,16 @@ +import unittest + +import loadcli + +cli = loadcli.cli + + +class TestUniquePath(unittest.TestCase): + + def test_unique_path(self): + for i in range(1000): + self.assertNotEqual(cli._unique_path('prefix'), cli._unique_path('prefix')) + self.assertRegexpMatches(cli._unique_path('prefix'), '^prefix/\d{10}\.\d{1,6}\.[a-zA-Z]{8}$') + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_upload_progress_callback.py b/tests/test_cli/test_upload_progress_callback.py new file mode 100644 index 0000000..f0e1fe7 --- /dev/null +++ b/tests/test_cli/test_upload_progress_callback.py @@ -0,0 +1,45 @@ +import unittest +import mock +import sys +import StringIO as stringio + +import loadcli + +cli = loadcli.cli + + +class TestUploadProgressCallBack(unittest.TestCase): + + maxDiff = None + + def test_format_size(self): + self.assertEqual(cli._format_size(2000000000), '1.86 GiB') + self.assertEqual(cli._format_size(1073741824), '1.00 GiB') + self.assertEqual(cli._format_size(3000000), '2.86 MiB') + self.assertEqual(cli._format_size(1048576), '1.00 MiB') + self.assertEqual(cli._format_size(4000), '3.91 KiB') + self.assertEqual(cli._format_size(1024), '1.00 KiB') + self.assertEqual(cli._format_size(500), '500.00 B') + + def test_format_secs(self): + self.assertEqual(cli._format_secs(0), '00:00:00') + self.assertEqual(cli._format_secs(60), '00:01:00') + self.assertEqual(cli._format_secs(3600), '01:00:00') + self.assertEqual(cli._format_secs(7283294), '2023:08:14') + self.assertEqual(cli._format_secs(1234), '00:20:34') + self.assertEqual(cli._format_secs(4321), '01:12:01') + self.assertEqual(cli._format_secs(4321.567), '01:12:01') + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + def test_progress_callback(self, stdout): + cli._progress_callback(12300, 234000, 5670, 80, 900) + cli._progress_callback(45600, 234000, 5670, 0, 900) + cli._progress_callback(234000, 234000, 5670, 80, 900) + self.assertMultiLineEqual(stdout.getvalue(), + '[= ] 05% 00:15:00 12.01 KiB 70.88 B/sec\r' + '[======= ] 19% 00:15:00 44.53 KiB - B/sec\r' + '[====================================] 100% 00:15:00 228.52 KiB 260.00 B/sec\r') + + +if __name__ == '__main__': + unittest.main()