#2722 cli: use multicall for cancel command
Merged a year ago by tkopecek. Opened a year ago by tkopecek.
tkopecek/koji issue2607  into  master

file modified
+24 -13
@@ -6322,19 +6322,30 @@ 

                  blist.append(arg)

              except koji.GenericError:

                  parser.error(_("please specify only task ids (integer) or builds (n-v-r)"))

-     if tlist:

-         opts = {}

-         remote_fn = session.cancelTask

-         if options.justone:

-             opts['recurse'] = False

-         elif options.full:

-             remote_fn = session.cancelTaskFull

-             if options.force:

-                 opts['strict'] = False

-         for task_id in tlist:

-             remote_fn(task_id, **opts)

-     for build in blist:

-         session.cancelBuild(build)

+ 

+     results = []

+     with session.multicall(strict=False, batch=100) as m:

+         if tlist:

+             opts = {}

+             remote_fn = m.cancelTask

+             if options.justone:

+                 opts['recurse'] = False

+             elif options.full:

+                 remote_fn = m.cancelTaskFull

+                 if options.force:

+                     opts['strict'] = False

+             for task_id in tlist:

+                 results.append(remote_fn(task_id, **opts))

+         for build in blist:

+             results.append(m.cancelBuild(build))

+ 

+     err = False

+     for r in results:

+         if isinstance(r.result, dict):

+             warn(r.result['faultString'])

+             err = True

+     if err:

+         return 1

  

  

  def handle_set_task_priority(goptions, session, args):

@@ -0,0 +1,118 @@ 

+ from __future__ import absolute_import

+ import mock

+ import unittest

+ 

+ import koji

+ from koji_cli.commands import handle_cancel

+ from . import utils

+ 

+ class TestCancel(utils.CliTestCase):

+     maxDiff = None

+ 

+     def setUp(self):

+         self.options = mock.MagicMock()

+         self.options.quiet = False

+         self.session = mock.MagicMock()

+         self.session.multicall.return_value.__enter__.return_value = self.session

+ 

+         self.error_format = """Usage: %s cancel [options] <task_id|build> [<task_id|build> ...]

+ (Specify the --help global option for a list of other help options)

+ 

+ %s: error: {message}

+ """ % (self.progname, self.progname)

+ 

+ 

+     @mock.patch('koji_cli.commands.activate_session')

+     def test_anon_cancel(self, activate_session_mock):

+         args = ['123']

+         activate_session_mock.side_effect = koji.GenericError

+ 

+         with self.assertRaises(koji.GenericError):

+             handle_cancel(self.options, self.session, args)

+ 

+         activate_session_mock.assert_called_once_with(self.session, self.options)

+         self.session.cancelTask.assert_not_called()

+         self.session.cancelTaskFull.assert_not_called()

+         self.session.cancelBuild.assert_not_called()

+ 

+     @mock.patch('koji_cli.commands.activate_session')

+     def test_cancel_tasks(self, activate_session_mock):

+         # integers are always treated like task IDs, not build IDs

+         args = ['123', '234']

+ 

+         handle_cancel(self.options, self.session, args)

+ 

+         activate_session_mock.assert_called_once_with(self.session, self.options)

+         self.session.cancelTask.assert_has_calls([mock.call(123), mock.call(234)])

+         self.session.cancelTaskFull.assert_not_called()

+         self.session.cancelBuild.assert_not_called()

+ 

+     def test_cancel_wrong_nvr(self):

+         args = ['nvr_cant_be_parsed']

+         expected = self.format_error_message(

+             "please specify only task ids (integer) or builds (n-v-r)")

+         self.assert_system_exit(

+             handle_cancel,

+             self.options,

+             self.session,

+             args,

+             stdout='',

+             stderr=expected)

+ 

+         self.session.cancelTask.assert_not_called()

+         self.session.cancelTaskFull.assert_not_called()

+         self.session.cancelBuild.assert_not_called()

+ 

+     @mock.patch('koji_cli.commands.activate_session')

+     def test_cancel_builds(self, activate_session_mock):

+         args = ['name-version-release']

+ 

+         handle_cancel(self.options, self.session, args)

+ 

+         activate_session_mock.assert_called_once_with(self.session, self.options)

+         self.session.cancelTask.assert_not_called()

+         self.session.cancelTaskFull.assert_not_called()

+         self.session.cancelBuild.assert_called_once_with(args[0])

+ 

+     @mock.patch('koji_cli.commands.activate_session')

+     def test_cancel_builds_unused_options(self, activate_session_mock):

+         # it is good for nothing here

+         args = ['name-version-release', '--full', '--justone', '--force']

+         handle_cancel(self.options, self.session, args)

+ 

+         activate_session_mock.assert_called_once_with(self.session, self.options)

+         self.session.cancelTask.assert_not_called()

+         self.session.cancelTaskFull.assert_not_called()

+         self.session.cancelBuild.assert_called_once_with(args[0])

+ 

+     @mock.patch('koji_cli.commands.activate_session')

+     def test_cancel_tasks_full(self, activate_session_mock):

+         args = ['123', '--full']

+ 

+         handle_cancel(self.options, self.session, args)

+ 

+         activate_session_mock.assert_called_once_with(self.session, self.options)

+         self.session.cancelTask.assert_not_called()

+         self.session.cancelTaskFull.assert_called_once_with(123)

+         self.session.cancelBuild.assert_not_called()

+ 

+     @mock.patch('koji_cli.commands.activate_session')

+     def test_cancel_tasks_justone(self, activate_session_mock):

+         args = ['123', '--justone']

+ 

+         handle_cancel(self.options, self.session, args)

+ 

+         activate_session_mock.assert_called_once_with(self.session, self.options)

+         self.session.cancelTask.assert_called_once_with(123, recurse=False)

+         self.session.cancelTaskFull.assert_not_called()

+         self.session.cancelBuild.assert_not_called()

+ 

+     @mock.patch('koji_cli.commands.activate_session')

+     def test_cancel_tasks_force(self, activate_session_mock):

+         args = ['123', '--force', '--full']

+ 

+         handle_cancel(self.options, self.session, args)

+ 

+         activate_session_mock.assert_called_once_with(self.session, self.options)

+         self.session.cancelTaskFull.assert_called_once_with(123, strict=False)

+         self.session.cancelBuild.assert_not_called()

Metadata Update from @tkopecek:
- Pull-request tagged with: testing-ready

a year ago

Is there any reason to cancel tasks without batch, but cancel builds with batch=100?

And maybe better to handle the result like _multicall_with_check to print failures.

rebased onto b8d0c5e5c46be0d2e5468bf463f71931307b4ca0

a year ago

oh, it is completely wrong - I'm not using that multicall object at all.

Metadata Update from @tkopecek:
- Pull-request untagged with: testing-ready

a year ago

rebased onto 18b8d4472bbe4999f6ce9b41723a6f3b64544fea

a year ago

As it changes behaviour a bit (prinitng warnings + error code after trying all calls), I've moved it back to 1.25

Metadata Update from @tkopecek:
- Pull-request tagged with: testing-ready

a year ago

rebased onto a47f35a

a year ago

Metadata Update from @jcupova:
- Pull-request tagged with: testing-done

a year ago

Commit 21ec251 fixes this pull-request

Pull-Request has been merged by tkopecek

a year ago