#3115 Add and update CLI unit tests
Merged 6 months ago by tkopecek. Opened 6 months ago by jcupova.
jcupova/koji add-cli-unit-tests-2  into  master

Add and upfate CLI unit tests
Jana Cupova • 6 months ago  
@@ -19,87 +19,84 @@ 

          self.channel_id = 1

          self.options = mock.MagicMock()

          self.session = mock.MagicMock()

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

+         self.error_format = """Usage: %s add-channel [options] <channel_name>

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

+ 

+ %s: error: {message}

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

  

      @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_channel(self, activate_session_mock, stdout):

+     def test_handle_add_channel(self, stdout):

          self.session.addChannel.return_value = self.channel_id

          rv = handle_add_channel(self.options, self.session,

                                  ['--description', self.description, self.channel_name])

          actual = stdout.getvalue()

          expected = '%s added: id %s\n' % (self.channel_name, self.channel_id)

          self.assertMultiLineEqual(actual, expected)

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

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

          self.session.addChannel.assert_called_once_with(self.channel_name,

                                                          description=self.description)

          self.assertNotEqual(rv, 1)

  

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_channel_exist(self, activate_session_mock, stderr):

+     def test_handle_add_channel_exist(self):

          expected_api = 'channel %s already exists (id=%s)' % (self.channel_name, self.channel_id)

          expected = 'channel %s already exists\n' % self.channel_name

  

          self.session.addChannel.side_effect = koji.GenericError(expected_api)

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_channel(self.options, self.session,

-                                ['--description', self.description, self.channel_name])

-         self.assertExitCode(ex, 1)

-         actual = stderr.getvalue()

-         self.assertMultiLineEqual(actual, expected)

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

+         arguments = ['--description', self.description, self.channel_name]

+         self.assert_system_exit(

+             handle_add_channel,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=expected,

+             exit_code=1,

+             activate_session=None)

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

          self.session.addChannel.assert_called_once_with(self.channel_name,

                                                          description=self.description)

  

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_channel_older_hub(self, activate_session_mock, stderr):

+     def test_handle_add_channel_older_hub(self):

          expected_api = 'Invalid method: addChannel'

          expected = 'addChannel is available on hub from Koji 1.26 version, your version ' \

                     'is 1.25.1\n'

          self.session.getKojiVersion.return_value = '1.25.1'

  

          self.session.addChannel.side_effect = koji.GenericError(expected_api)

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_channel(self.options, self.session,

-                                ['--description', self.description, self.channel_name])

-         self.assertExitCode(ex, 1)

-         actual = stderr.getvalue()

-         self.assertMultiLineEqual(actual, expected)

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

+         arguments = ['--description', self.description, self.channel_name]

+         self.assert_system_exit(

+             handle_add_channel,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=expected,

+             exit_code=1,

+             activate_session=None)

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

          self.session.addChannel.assert_called_once_with(self.channel_name,

                                                          description=self.description)

  

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_channel_without_args(self, activate_session_mock, stderr):

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_channel(self.options, self.session, [])

-         self.assertExitCode(ex, 2)

-         actual = stderr.getvalue()

-         expected_stderr = """Usage: %s add-channel [options] <channel_name>

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

- 

- %s: error: Please specify one channel name

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

-         self.assertMultiLineEqual(actual, expected_stderr)

-         activate_session_mock.assert_not_called()

- 

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_channel_more_args(self, activate_session_mock, stderr):

+     def test_handle_add_channel_without_args(self):

+         arguments = []

+         self.assert_system_exit(

+             handle_add_channel,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message('Please specify one channel name'),

+             exit_code=2,

+             activate_session=None)

+         self.activate_session_mock.assert_not_called()

+ 

+     def test_handle_add_channel_more_args(self):

          channel_2 = 'channel-2'

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_channel(self.options, self.session, [self.channel_name, channel_2])

-         self.assertExitCode(ex, 2)

-         actual = stderr.getvalue()

-         expected_stderr = """Usage: %s add-channel [options] <channel_name>

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

- 

- %s: error: Please specify one channel name

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

-         self.assertMultiLineEqual(actual, expected_stderr)

-         activate_session_mock.assert_not_called()

+         arguments = [self.channel_name, channel_2]

+         self.assert_system_exit(

+             handle_add_channel,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message('Please specify one channel name'),

+             exit_code=2,

+             activate_session=None)

+         self.activate_session_mock.assert_not_called()

  

      def test_handle_add_channel_help(self):

          self.assert_help(

@@ -7,12 +7,14 @@ 

  from koji_cli.commands import handle_add_group

  from . import utils

  

+ 

  class TestAddGroup(utils.CliTestCase):

  

      # Show long diffs in error output...

      maxDiff = None

  

      def setUp(self):

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

          self.error_format = """Usage: %s add-group <tag> <group>

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

  
@@ -20,8 +22,7 @@ 

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

  

      @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_group(self, activate_session_mock, stdout):

+     def test_handle_add_group(self, stdout):

          tag = 'tag'

          group = 'group'

          arguments = [tag, group]
@@ -41,16 +42,14 @@ 

          self.assertMultiLineEqual(actual, expected)

  

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.hasPerm.assert_called_once_with('admin')

          session.getTag.assert_called_once_with(tag)

          session.getTagGroups.assert_called_once_with(tag, inherit=False)

          session.groupListAdd.assert_called_once_with(tag, group)

          self.assertNotEqual(rv, 1)

  

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_group_dupl(self, activate_session_mock, stderr):

+     def test_handle_add_group_dupl(self):

          tag = 'tag'

          group = 'group'

          arguments = [tag, group]
@@ -64,28 +63,22 @@ 

              {'name': 'group', 'group_id': 'groupId'}]

  

          # Run it and check immediate output

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_group(options, session, arguments)

-         self.assertExitCode(ex, 1)

-         actual = stderr.getvalue()

-         expected = 'Group group already exists for tag tag\n'

-         self.assertMultiLineEqual(actual, expected)

+         self.assert_system_exit(

+             handle_add_group,

+             options, session, arguments,

+             stdout='',

+             stderr='Group group already exists for tag tag\n',

+             exit_code=1,

+             activate_session=None)

  

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.hasPerm.assert_called_once_with('admin')

          session.getTag.assert_called_once_with(tag)

          session.getTagGroups.assert_called_once_with(tag, inherit=False)

          session.groupListAdd.assert_not_called()

  

-     @mock.patch('sys.stdout', new_callable=six.StringIO)

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_group_help(

-             self,

-             activate_session_mock,

-             stderr,

-             stdout):

+     def test_handle_add_group_help(self):

          arguments = []

          options = mock.MagicMock()

  
@@ -102,15 +95,13 @@ 

              activate_session=None)

  

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_not_called()

+         self.activate_session_mock.assert_not_called()

          session.hasPerm.assert_not_called()

          session.getTag.assert_not_called()

          session.getTagGroups.assert_not_called()

          session.groupListAdd.assert_not_called()

  

-     @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_group_no_perm(self, activate_session_mock, stdout):

+     def test_handle_add_group_no_perm(self):

          tag = 'tag'

          group = 'group'

          arguments = [tag, group]
@@ -130,16 +121,14 @@ 

              exit_code=2)

  

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.hasPerm.assert_has_calls([mock.call('admin'),

                                            mock.call('tag')])

          session.getTag.assert_not_called()

          session.getTagGroups.assert_not_called()

          session.groupListAdd.assert_not_called()

  

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_group_no_tag(self, activate_session_mock, stderr):

+     def test_handle_add_group_no_tag(self):

          tag = 'tag'

          group = 'group'

          arguments = [tag, group]
@@ -151,15 +140,16 @@ 

          session.getTag.return_value = None

  

          # Run it and check immediate output

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_group(options, session, arguments)

-         self.assertExitCode(ex, 1)

-         actual = stderr.getvalue()

-         expected = 'No such tag: tag\n'

-         self.assertMultiLineEqual(actual, expected)

+         self.assert_system_exit(

+             handle_add_group,

+             options, session, arguments,

+             stdout='',

+             stderr='No such tag: tag\n',

+             exit_code=1,

+             activate_session=None)

  

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.hasPerm.assert_called_once_with('admin')

          session.getTag.assert_called_once_with(tag)

          session.getTagGroups.assert_not_called()

file modified
+32 -44
@@ -1,8 +1,5 @@ 

  from __future__ import absolute_import

  

- import os

- import sys

- 

  import mock

  import six

  
@@ -13,12 +10,17 @@ 

  

  class TestAddHost(utils.CliTestCase):

  

-     # Show long diffs in error output...

-     maxDiff = None

+     def setUp(self):

+         self.maxDiff = None

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

+         self.error_format = """Usage: %s add-host [options] <hostname> <arch> [<arch> ...]

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

+ 

+ %s: error: {message}

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

  

      @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_host(self, activate_session_mock, stdout):

+     def test_handle_add_host(self, stdout):

          host = 'host'

          host_id = 1

          arches = ['arch1', 'arch2']
@@ -41,15 +43,13 @@ 

          expected = 'host added: id 1\n'

          self.assertMultiLineEqual(actual, expected)

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.getHost.assert_called_once_with(host)

          session.addHost.assert_called_once_with(host, arches, **kwargs)

          self.assertNotEqual(rv, 1)

  

      @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_host_no_krb_principal(

-             self, activate_session_mock, stdout):

+     def test_handle_add_host_no_krb_principal(self, stdout):

          host = 'host'

          host_id = 1

          arches = ['arch1', 'arch2']
@@ -68,14 +68,12 @@ 

          expected = 'host added: id 1\n'

          self.assertMultiLineEqual(actual, expected)

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.getHost.assert_called_once_with(host)

          session.addHost.assert_called_once_with(host, arches, force=False)

          self.assertNotEqual(rv, 1)

  

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_host_dupl(self, activate_session_mock, stderr):

+     def test_handle_add_host_dupl(self):

          host = 'host'

          host_id = 1

          arches = ['arch1', 'arch2']
@@ -90,45 +88,36 @@ 

          # Run it and check immediate output

          # args: host, arch1, arch2, --krb-principal=krb

          # expected: failed, host already exists

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_host(options, session, arguments)

-         self.assertExitCode(ex, 1)

-         actual = stderr.getvalue()

-         expected = 'host is already in the database\n'

-         self.assertMultiLineEqual(actual, expected)

+         self.assert_system_exit(

+             handle_add_host,

+             options, session, arguments,

+             stdout='',

+             stderr='host is already in the database\n',

+             exit_code=1,

+             activate_session=None)

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.getHost.assert_called_once_with(host)

          session.addHost.assert_not_called()

  

-     @mock.patch('sys.stdout', new_callable=six.StringIO)

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_host_without_args(self, activate_session_mock, stderr, stdout):

+     def test_handle_add_host_without_args(self):

          arguments = []

          options = mock.MagicMock()

-         progname = os.path.basename(sys.argv[0]) or 'koji'

  

          # Mock out the xmlrpc server

          session = mock.MagicMock()

  

          # Run it and check immediate output

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_host(options, session, arguments)

-         self.assertExitCode(ex, 2)

-         actual_stdout = stdout.getvalue()

-         actual_stderr = stderr.getvalue()

-         expected_stdout = ''

-         expected_stderr = """Usage: %s add-host [options] <hostname> <arch> [<arch> ...]

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

- 

- %s: error: Please specify a hostname and at least one arch

- """ % (progname, progname)

-         self.assertMultiLineEqual(actual_stdout, expected_stdout)

-         self.assertMultiLineEqual(actual_stderr, expected_stderr)

+         self.assert_system_exit(

+             handle_add_host,

+             options, session, arguments,

+             stdout='',

+             stderr=self.format_error_message('Please specify a hostname and at least one arch'),

+             exit_code=2,

+             activate_session=None)

  

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_not_called()

+         self.activate_session_mock.assert_not_called()

          session.hasHost.assert_not_called()

          session.addHost.assert_not_called()

  
@@ -147,8 +136,7 @@ 

  """ % self.progname)

  

      @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_host_failed(self, activate_session_mock, stderr):

+     def test_handle_add_host_failed(self, stderr):

          host = 'host'

          arches = ['arch1', 'arch2']

          krb_principal = '--krb-principal=krb'
@@ -171,6 +159,6 @@ 

          expected = ''

          self.assertMultiLineEqual(actual, expected)

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.getHost.assert_called_once_with(host)

          session.addHost.assert_called_once_with(host, arches, **kwargs)

@@ -1,9 +1,7 @@ 

  from __future__ import absolute_import

  

  import mock

- import os

  import six

- import sys

  import unittest

  

  from koji_cli.commands import handle_add_host_to_channel
@@ -12,12 +10,17 @@ 

  

  class TestAddHostToChannel(utils.CliTestCase):

  

-     # Show long diffs in error output...

-     maxDiff = None

+     def setUp(self):

+         self.maxDiff = None

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

+         self.error_format = """Usage: %s add-host-to-channel [options] <hostname> <channel>

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

+ 

+ %s: error: {message}

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

  

      @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_host_to_channel(self, activate_session_mock, stdout):

+     def test_handle_add_host_to_channel(self, stdout):

          host = 'host'

          host_info = mock.ANY

          channel = 'channel'
@@ -38,16 +41,14 @@ 

          expected = ''

          self.assertMultiLineEqual(actual, expected)

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.getChannel.assert_called_once_with(channel)

          session.getHost.assert_called_once_with(host)

          session.addHostToChannel.assert_called_once_with(host, channel)

          self.assertNotEqual(rv, 1)

  

      @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_host_to_channel_list(

-             self, activate_session_mock, stdout):

+     def test_handle_add_host_to_channel_list(self, stdout):

          list_arg = '--list'

          args = [list_arg]

          channel_infos = [{'name': 'channel1'}, {'name': 'channel2'}]
@@ -65,7 +66,7 @@ 

          expected = 'channel1\nchannel2\n'

          self.assertMultiLineEqual(actual, expected)

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.listChannels.assert_called_once()

          session.getChannel.assert_not_called()

          session.getHost.assert_not_called()
@@ -73,9 +74,7 @@ 

          self.assertNotEqual(rv, 1)

  

      @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_host_to_channel_new_and_force(

-             self, activate_session_mock, stdout):

+     def test_handle_add_host_to_channel_new_and_force(self, stdout):

          host = 'host'

          host_info = mock.ANY

          channel = 'channel'
@@ -96,20 +95,17 @@ 

          expected = ''

          self.assertMultiLineEqual(actual, expected)

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.getChannel.assert_not_called()

          session.getHost.assert_called_once_with(host)

          session.addHostToChannel.assert_called_once_with(host, channel, create=True, force=True)

          self.assertNotEqual(rv, 1)

  

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_host_to_channel_no_channel(

-             self, activate_session_mock, stderr):

+     def test_handle_add_host_to_channel_no_channel(self):

          host = 'host'

          channel = 'channel'

          channel_info = None

-         args = [host, channel]

+         arguments = [host, channel]

          options = mock.MagicMock()

  

          # Mock out the xmlrpc server
@@ -119,27 +115,25 @@ 

          # Run it and check immediate output

          # args: host, channel

          # expected: failed, channel not found

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_host_to_channel(options, session, args)

-         self.assertExitCode(ex, 1)

-         actual = stderr.getvalue()

-         expected = 'No such channel: channel\n'

-         self.assertMultiLineEqual(actual, expected)

+         self.assert_system_exit(

+             handle_add_host_to_channel,

+             options, session, arguments,

+             stdout='',

+             stderr='No such channel: channel\n',

+             exit_code=1,

+             activate_session=None)

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.getChannel.assert_called_once_with(channel)

          session.getHost.assert_not_called()

          session.addHostToChannel.assert_not_called()

  

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_add_host_to_channel_no_host(

-             self, activate_session_mock, stderr):

+     def test_handle_add_host_to_channel_no_host(self):

          host = 'host'

          host_info = None

          channel = 'channel'

          channel_info = mock.ANY

-         args = [host, channel]

+         arguments = [host, channel]

          options = mock.MagicMock()

  

          # Mock out the xmlrpc server
@@ -150,14 +144,15 @@ 

          # Run it and check immediate output

          # args: host, channel

          # expected: success

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_host_to_channel(options, session, args)

-         self.assertExitCode(ex, 1)

-         actual = stderr.getvalue()

-         expected = 'No such host: host\n'

-         self.assertMultiLineEqual(actual, expected)

+         self.assert_system_exit(

+             handle_add_host_to_channel,

+             options, session, arguments,

+             stdout='',

+             stderr='No such host: host\n',

+             exit_code=1,

+             activate_session=None)

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

+         self.activate_session_mock.assert_called_once_with(session, options)

          session.getChannel.assert_called_once_with(channel)

          session.getHost.assert_called_once_with(host)

          session.addHostToChannel.assert_not_called()
@@ -167,9 +162,8 @@ 

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

      def test_handle_add_host_to_channel_help(

              self, activate_session_mock, stderr, stdout):

-         args = []

+         arguments = []

          options = mock.MagicMock()

-         progname = os.path.basename(sys.argv[0]) or 'koji'

  

          # Mock out the xmlrpc server

          session = mock.MagicMock()
@@ -177,19 +171,13 @@ 

          # Run it and check immediate output

          # args: _empty_

          # expected: failed, help msg shows

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_host_to_channel(options, session, args)

-         self.assertExitCode(ex, 2)

-         actual_stdout = stdout.getvalue()

-         actual_stderr = stderr.getvalue()

-         expected_stdout = ''

-         expected_stderr = """Usage: %s add-host-to-channel [options] <hostname> <channel>

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

- 

- %s: error: Please specify a hostname and a channel

- """ % (progname, progname)

-         self.assertMultiLineEqual(actual_stdout, expected_stdout)

-         self.assertMultiLineEqual(actual_stderr, expected_stderr)

+         self.assert_system_exit(

+             handle_add_host_to_channel,

+             options, session, arguments,

+             stdout='',

+             stderr=self.format_error_message("Please specify a hostname and a channel"),

+             exit_code=2,

+             activate_session=None)

  

          # Finally, assert that things were called as we expected.

          activate_session_mock.assert_not_called()

@@ -6,6 +6,7 @@ 

  from koji_cli.commands import handle_add_notification

  from . import utils

  

+ 

  class TestAddNotification(utils.CliTestCase):

      def setUp(self):

          self.options = mock.MagicMock()
@@ -13,25 +14,29 @@ 

          self.options.debug = False

          self.session = mock.MagicMock()

          self.session.getAPIVersion.return_value = koji.API_VERSION

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

+         self.error_format = """Usage: %s add-notification [options]

+ (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_handle_add_notification(self, activate_session_mock):

+     def test_handle_add_notification(self):

          self.session.getPackageID.return_value = 1234

          self.session.getTagID.return_value = 4321

          self.session.getLoggedInUser.return_value = {'id': 678}

  

-         handle_add_notification(self.options, self.session, ['--package', 'pkg_a', '--tag', 'tag_a', '--success-only'])

+         handle_add_notification(self.options, self.session,

+                                 ['--package', 'pkg_a', '--tag', 'tag_a', '--success-only'])

  

          self.session.getPackageID.assert_called_once_with('pkg_a')

          self.session.getTagID.assert_called_once_with('tag_a', strict=True)

          self.session.getLoggedInUser.assert_called_once_with()

          self.session.getUser.assert_not_called()

          self.session.createNotification.assert_called_once_with(678, 1234, 4321, True)

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

  

- 

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

-     def test_handle_add_notification_no_pkg(self, activate_session_mock):

+     def test_handle_add_notification_no_pkg(self,):

          self.session.getTagID.return_value = 4321

          self.session.getLoggedInUser.return_value = {'id': 678}

  
@@ -42,10 +47,9 @@ 

          self.session.getLoggedInUser.assert_called_once_with()

          self.session.getUser.assert_not_called()

          self.session.createNotification.assert_called_once_with(678, None, 4321, True)

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

  

- 

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

-     def test_handle_add_notification_no_tag(self, activate_session_mock):

+     def test_handle_add_notification_no_tag(self):

          self.session.getPackageID.return_value = 1234

          self.session.getLoggedInUser.return_value = {'id': 678}

  
@@ -56,87 +60,104 @@ 

          self.session.getLoggedInUser.assert_called_once_with()

          self.session.getUser.assert_not_called()

          self.session.createNotification.assert_called_once_with(678, 1234, None, False)

- 

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

  

      @mock.patch('sys.exit')

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_handle_add_notification_no_pkg_no_tag(self, sys_stderr, sys_exit):

+     def test_handle_add_notification_no_pkg_no_tag(self, sys_exit):

          sys_exit.side_effect = SystemExit()

+         arguments = ['--success-only']

  

-         with self.assertRaises(SystemExit):

-             handle_add_notification(self.options, self.session, ['--success-only'])

+         self.assert_system_exit(

+             handle_add_notification,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message(

+                 "Command need at least one from --tag or --package options."),

+             exit_code=None,

+             activate_session=None)

  

          self.session.getPackageID.assert_not_called()

          self.session.getTagID.assert_not_called()

          self.session.getLoggedInUser.assert_not_called()

          self.session.getUser.assert_not_called()

          self.session.createNotification.assert_not_called()

- 

+         self.activate_session_mock.assert_not_called()

  

      @mock.patch('sys.exit')

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_handle_add_notification_user_no_admin(self, sys_stderr, sys_exit):

+     def test_handle_add_notification_user_no_admin(self, sys_exit):

          sys_exit.side_effect = SystemExit()

          self.session.hasPerm.return_value = False

+         arguments = ['--user', 'username', '--tag', 'tag_a']

  

-         with self.assertRaises(SystemExit):

-             handle_add_notification(self.options, self.session, ['--user', 'username', '--tag', 'tag_a'])

+         self.assert_system_exit(

+             handle_add_notification,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message('--user requires admin permission'),

+             exit_code=None,

+             activate_session=None)

  

          self.session.getPackageID.assert_not_called()

          self.session.getTagID.assert_not_called()

          self.session.getLoggedInUser.assert_not_called()

          self.session.getUser.assert_not_called()

          self.session.createNotification.assert_not_called()

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

  

- 

-     @mock.patch('sys.exit')

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_handle_add_notification_user_admin(self, sys_stderr, sys_exit):

+     def test_handle_add_notification_user_admin(self):

          self.session.hasPerm.return_value = True

          self.session.getPackageID.return_value = 1234

          self.session.getUser.return_value = {'id': 789}

  

-         handle_add_notification(self.options, self.session, ['--package', 'pkg_a', '--user', 'username'])

+         handle_add_notification(self.options, self.session,

+                                 ['--package', 'pkg_a', '--user', 'username'])

  

          self.session.getPackageID.assert_called_once_with('pkg_a')

          self.session.getTagID.assert_not_called()

          self.session.getLoggedInUser.assert_not_called()

          self.session.getUser.assert_called_once_with('username')

          self.session.createNotification.assert_called_once_with(789, 1234, None, False)

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

  

- 

-     @mock.patch('sys.exit')

      @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_handle_add_notification_args(self, sys_stderr, sys_exit):

+     def test_handle_add_notification_args(self, sys_exit):

          sys_exit.side_effect = SystemExit()

- 

-         with self.assertRaises(SystemExit):

-             handle_add_notification(self.options, self.session, ['bogus'])

+         arguments = ['bogus']

+         self.assert_system_exit(

+             handle_add_notification,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message("This command takes no arguments"),

+             exit_code=2,

+             activate_session=None)

  

          self.session.createNotification.assert_not_called()

+         self.activate_session_mock.assert_not_called()

  

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_handle_add_notification_non_exist_tag(self, stderr):

+     def test_handle_add_notification_non_exist_tag(self):

          tag = 'tag_a'

-         expected = "Usage: %s add-notification [options]\n" \

-                    "(Specify the --help global option for a list of other help options)\n\n" \

-                    "%s: error: No such tag: %s\n" % (self.progname, self.progname, tag)

+         arguments = ['--tag', tag]

  

          self.session.getTagID.side_effect = koji.GenericError

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_notification(self.options, self.session, ['--tag', tag])

-         self.assertExitCode(ex, 2)

-         self.assert_console_message(stderr, expected)

- 

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_handle_add_notification_non_exist_pkg(self, stderr):

+         self.assert_system_exit(

+             handle_add_notification,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message('No such tag: %s' % tag),

+             exit_code=2,

+             activate_session=None)

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

+ 

+     def test_handle_add_notification_non_exist_pkg(self):

          pkg = 'pkg_a'

-         expected = "Usage: %s add-notification [options]\n" \

-                    "(Specify the --help global option for a list of other help options)\n\n" \

-                    "%s: error: No such package: %s\n" % (self.progname, self.progname, pkg)

+         arguments = ['--package', pkg]

  

          self.session.getPackageID.return_value = None

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_notification(self.options, self.session, ['--package', pkg])

-         self.assertExitCode(ex, 2)

-         self.assert_console_message(stderr, expected)

+         self.assert_system_exit(

+             handle_add_notification,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message('No such package: %s' % pkg),

+             exit_code=2,

+             activate_session=None)

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

file modified
+21 -24
@@ -1,6 +1,5 @@ 

  from __future__ import absolute_import

  import mock

- import six

  import unittest

  

  from koji_cli.commands import handle_add_tag
@@ -9,45 +8,42 @@ 

  

  class TestAddTag(utils.CliTestCase):

  

-     # Show long diffs in error output...

-     maxDiff = None

- 

      def setUp(self):

+         self.maxDiff = None

+         self.options = mock.MagicMock()

+         self.options.quiet = True

+         self.options.debug = False

+         self.session = mock.MagicMock()

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

          self.error_format = """Usage: %s add-tag [options] <name>

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

  

  %s: error: {message}

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

  

-     @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_tag(

-             self,

-             activate_session_mock,

-             stdout):

+     def test_handle_add_tag(self):

          """Test handle_add_tag function"""

-         session = mock.MagicMock()

-         options = mock.MagicMock()

- 

          # Case 1. no argument error

-         expected = self.format_error_message(

-             "Please specify a name for the tag")

          self.assert_system_exit(

              handle_add_tag,

-             options,

-             session,

-             [],

-             stderr=expected,

+             self.options, self.session, [],

+             stderr=self.format_error_message("Please specify a name for the tag"),

+             exit_code=2,

              activate_session=None)

+         self.activate_session_mock.assert_not_called()

+         self.activate_session_mock.reset_mock()

  

          # Case 2. not admin account

-         session.hasPerm.return_value = None

+         self.session.hasPerm.return_value = None

          self.assert_system_exit(

              handle_add_tag,

-             options, session, ['test-tag'],

+             self.options, self.session, ['test-tag'],

              stdout='',

              stderr=self.format_error_message("This action requires tag or admin privileges"),

+             exit_code=2,

          )

+         self.activate_session_mock.assert_not_called()

+         self.activate_session_mock.reset_mock()

  

          # Case 3. options test

          arguments = ['test-tag',
@@ -70,9 +66,10 @@ 

              }

          }

  

-         session.hasPerm.return_value = True

-         handle_add_tag(options, session, arguments)

-         session.createTag.assert_called_with('test-tag', **opts)

+         self.session.hasPerm.return_value = True

+         handle_add_tag(self.options, self.session, arguments)

+         self.session.createTag.assert_called_with('test-tag', **opts)

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

  

      def test_handle_add_tag_help(self):

          self.assert_help(

@@ -1,6 +1,5 @@ 

  from __future__ import absolute_import

  

- from six.moves import StringIO

  import mock

  

  import koji
@@ -14,34 +13,43 @@ 

          self.options.debug = False

          self.session = mock.MagicMock()

          self.session.getAPIVersion.return_value = koji.API_VERSION

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

+         self.error_format = """Usage: %s add-tag-inheritance [options] <tag> <parent-tag>

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

  

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_add_tag_inheritance_without_option(self, stderr):

-         expected = "Usage: %s add-tag-inheritance [options] <tag> <parent-tag>\n" \

-                    "(Specify the --help global option for a list of other help options)\n\n" \

-                    "%s: error: This command takes exctly two argument: " \

-                    "a tag name or ID and that tag's new parent name " \

-                    "or ID\n" % (self.progname, self.progname)

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_tag_inheritance(self.options, self.session, [])

-         self.assertExitCode(ex, 2)

-         self.assert_console_message(stderr, expected)

+ %s: error: {message}

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

  

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_add_tag_inheritance_non_exist_tag(self, stderr):

+     def test_add_tag_inheritance_without_option(self):

+         arguments = []

+         expected = self.format_error_message(

+             "This command takes exctly two argument: a tag name or ID and that tag's new "

+             "parent name or ID")

+         self.assert_system_exit(

+             handle_add_tag_inheritance,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=expected,

+             exit_code=2,

+             activate_session=None)

+         self.activate_session_mock.assert_not_called()

+ 

+     def test_add_tag_inheritance_non_exist_tag(self):

          tag = 'test-tag'

          parent_tag = 'parent-test-tag'

-         expected = "Usage: %s add-tag-inheritance [options] <tag> <parent-tag>\n" \

-                    "(Specify the --help global option for a list of other help options)\n\n" \

-                    "%s: error: No such tag: %s\n" % (self.progname, self.progname, tag)

+         arguments = [tag, parent_tag]

          self.session.getTag.return_value = None

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_tag_inheritance(self.options, self.session, [tag, parent_tag])

-         self.assertExitCode(ex, 2)

-         self.assert_console_message(stderr, expected)

  

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_add_tag_inheritance_non_exist_parent_tag(self, stderr):

+         self.assert_system_exit(

+             handle_add_tag_inheritance,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message("No such tag: %s" % tag),

+             exit_code=2,

+             activate_session=None)

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

+ 

+     def test_add_tag_inheritance_non_exist_parent_tag(self):

          side_effect_result = [{'arches': 'x86_64',

                                 'extra': {},

                                 'id': 1,
@@ -54,11 +62,13 @@ 

                                None]

          tag = 'test-tag'

          parent_tag = 'parent-test-tag'

-         expected = "Usage: %s add-tag-inheritance [options] <tag> <parent-tag>\n" \

-                    "(Specify the --help global option for a list of other help options)\n\n" \

-                    "%s: error: No such tag: %s\n" % (self.progname, self.progname, parent_tag)

+         arguments = [tag, parent_tag]

          self.session.getTag.side_effect = side_effect_result

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_tag_inheritance(self.options, self.session, [tag, parent_tag])

-         self.assertExitCode(ex, 2)

-         self.assert_console_message(stderr, expected)

+         self.assert_system_exit(

+             handle_add_tag_inheritance,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message("No such tag: %s" % parent_tag),

+             exit_code=2,

+             activate_session=None)

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

@@ -1,7 +1,6 @@ 

  from __future__ import absolute_import

  

  import mock

- from six.moves import StringIO

  

  import koji

  from koji_cli.commands import handle_add_target
@@ -14,32 +13,41 @@ 

          self.options.debug = False

          self.session = mock.MagicMock()

          self.session.getAPIVersion.return_value = koji.API_VERSION

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

+         self.error_format = """Usage: %s add-target <name> <build tag> <dest tag>

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

+ 

+ %s: error: {message}

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

  

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_add_target_without_option(self, stderr):

-         expected = "Usage: %s add-target <name> <build tag> <dest tag>\n" \

-                    "(Specify the --help global option for a list of other help options)\n\n" \

-                    "%s: error: Please specify a target name, a build tag, " \

-                    "and destination tag\n" % (self.progname, self.progname)

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_target(self.options, self.session, [])

-         self.assertExitCode(ex, 2)

-         self.assert_console_message(stderr, expected)

- 

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_add_target_non_exist_tag(self, stderr):

+     def test_add_target_without_option(self,):

+         expected = self.format_error_message(

+             "Please specify a target name, a build tag, and destination tag")

+         self.assert_system_exit(

+             handle_add_target,

+             self.options, self.session, [],

+             stdout='',

+             stderr=expected,

+             exit_code=2,

+             activate_session=None)

+         self.activate_session_mock.assert_not_called()

+ 

+     def test_add_target_non_exist_tag(self):

          target = 'test-target'

          tag = 'test-tag'

          dest_tag = 'test-dest-tag'

-         expected = "No such tag: %s\n" % tag

+         arguments = [target, tag, dest_tag]

          self.session.getTag.return_value = None

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_target(self.options, self.session, [target, tag, dest_tag])

-         self.assertExitCode(ex, 1)

-         self.assert_console_message(stderr, expected)

- 

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_add_target_tag_without_arch(self, stderr):

+         self.assert_system_exit(

+             handle_add_target,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr="No such tag: %s\n" % tag,

+             exit_code=1,

+             activate_session=None)

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

+ 

+     def test_add_target_tag_without_arch(self,):

          tag_info = {'arches': None,

                      'extra': {},

                      'id': 1,
@@ -52,15 +60,18 @@ 

          target = 'test-target'

          tag = 'test-tag'

          dest_tag = 'test-dest-tag'

-         expected = "Build tag has no arches: %s\n" % tag

          self.session.getTag.return_value = tag_info

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_target(self.options, self.session, [target, tag, dest_tag])

-         self.assertExitCode(ex, 1)

-         self.assert_console_message(stderr, expected)

- 

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_add_target_non_exist_dest_tag(self, stderr):

+         arguments = [target, tag, dest_tag]

+         self.assert_system_exit(

+             handle_add_target,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr="Build tag has no arches: %s\n" % tag,

+             exit_code=1,

+             activate_session=None)

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

+ 

+     def test_add_target_non_exist_dest_tag(self):

          side_effect_result = [{'arches': 'x86_64',

                                 'extra': {},

                                 'id': 1,
@@ -77,23 +88,27 @@ 

          target = 'test-target'

          tag = 'test-tag'

          dest_tag = 'test-dest-tag'

-         expected = "No such destination tag: %s\n" % dest_tag

          self.session.getTag.side_effect = side_effect_result

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_target(self.options, self.session, [target, tag, dest_tag])

-         self.assertExitCode(ex, 1)

-         self.assert_console_message(stderr, expected)

- 

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_add_target_more_option(self, stderr):

-         args = ['test-target', 'tag', 'test-dest-tag', 'tag-2']

-         expected = "Usage: %s add-target <name> <build tag> <dest tag>\n" \

-                    "(Specify the --help global option for a list of other help options)\n\n" \

-                    "%s: error: Incorrect number of arguments\n" % (self.progname, self.progname)

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_target(self.options, self.session, args)

-         self.assertExitCode(ex, 2)

-         self.assert_console_message(stderr, expected)

+         arguments = [target, tag, dest_tag]

+         self.assert_system_exit(

+             handle_add_target,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr="No such destination tag: %s\n" % dest_tag,

+             exit_code=1,

+             activate_session=None)

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

+ 

+     def test_add_target_more_option(self):

+         arguments = ['test-target', 'tag', 'test-dest-tag', 'tag-2']

+         self.assert_system_exit(

+             handle_add_target,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message("Incorrect number of arguments"),

+             exit_code=2,

+             activate_session=None)

+         self.activate_session_mock.assert_not_called()

  

      def test_add_target_valid(self):

          side_effect_result = [{'arches': 'x86_64',
@@ -126,25 +141,25 @@ 

          self.assertEqual(rv, None)

          self.session.createBuildTarget.assert_called_once_with(target, tag, target)

          self.session.getTag.assert_called_with(target)

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

  

-     @mock.patch('sys.stderr', new_callable=StringIO)

-     def test_add_target_without_perms(self, stderr):

+     def test_add_target_without_perms(self):

          side_effect_result = [False, False]

  

          target = 'test-target'

          tag = 'test-tag'

          self.session.hasPerm.side_effect = side_effect_result

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_target(self.options, self.session, [target, tag])

-         self.assertExitCode(ex, 2)

-         expected_msg = """Usage: %s add-target <name> <build tag> <dest tag>

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

- 

- %s: error: This action requires target or admin privileges

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

-         self.assert_console_message(stderr, expected_msg)

+         arguments = [target, tag]

+         self.assert_system_exit(

+             handle_add_target,

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message("This action requires target or admin privileges"),

+             exit_code=2,

+             activate_session=None)

          self.session.createBuildTarget.assert_not_called()

          self.session.getTag.assert_not_called()

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

  

      def test_add_target_help(self):

          self.assert_help(

file modified
+25 -20
@@ -2,6 +2,7 @@ 

  import mock

  import six

  import unittest

+ import koji

  

  from koji_cli.commands import handle_add_user

  from . import utils
@@ -13,6 +14,11 @@ 

      maxDiff = None

  

      def setUp(self):

+         self.options = mock.MagicMock()

+         self.options.debug = False

+         self.session = mock.MagicMock()

+         self.session.getAPIVersion.return_value = koji.API_VERSION

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

          self.error_format = """Usage: %s add-user <username> [options]

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

  
@@ -20,14 +26,8 @@ 

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

  

      @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_user(

-             self,

-             activate_session_mock,

-             stdout):

+     def test_handle_add_user(self, stdout):

          """Test handle_add_user function"""

-         session = mock.MagicMock()

-         options = mock.MagicMock()

          username = 'user'

          user_id = 1001

          principal = 'krb-pricipal'
@@ -37,44 +37,49 @@ 

              "You must specify the username of the user to add")

          self.assert_system_exit(

              handle_add_user,

-             options,

-             session,

-             [],

+             self.options, self.session, [],

+             stdout='',

              stderr=expected,

+             exit_code=2,

              activate_session=None)

+         self.activate_session_mock.assert_not_called()

+         self.activate_session_mock.reset_mock()

  

          # Case 2. Too many argument error

          expected = self.format_error_message(

              "This command only accepts one argument (username)")

          self.assert_system_exit(

              handle_add_user,

-             options,

-             session,

-             ['user-1', 'user-2', 'user-3'],

+             self.options, self.session, ['user-1', 'user-2', 'user-3'],

+             stdout='',

              stderr=expected,

+             exit_code=2,

              activate_session=None)

+         self.activate_session_mock.assert_not_called()

+         self.activate_session_mock.reset_mock()

  

          # Case 3. Add user test

          expected = "Added user %s (%i)" % (username, user_id) + "\n"

          arguments = [username, '--principal', principal]

-         session.createUser.return_value = user_id

-         handle_add_user(options, session, arguments)

-         session.createUser.assert_called_with(

+         self.session.createUser.return_value = user_id

+         handle_add_user(self.options, self.session, arguments)

+         self.session.createUser.assert_called_with(

              username,

              status=0,

              krb_principal=principal)

          self.assert_console_message(stdout, expected)

-         activate_session_mock.assert_called_with(session, options)

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

+         self.activate_session_mock.reset_mock()

  

          # Case 3. Add blocked user

          arguments = [username, '--principal', principal, '--disable']

-         handle_add_user(options, session, arguments)

-         session.createUser.assert_called_with(

+         handle_add_user(self.options, self.session, arguments)

+         self.session.createUser.assert_called_with(

              username,

              status=1,  # 0: normal, 1: disabled

              krb_principal=principal)

          self.assert_console_message(stdout, expected)

-         activate_session_mock.assert_called_with(session, options)

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

  

      def test_handle_add_user_help(self):

          self.assert_help(

@@ -2,6 +2,7 @@ 

  import mock

  import six

  import unittest

+ import koji

  

  from koji_cli.commands import handle_add_volume

  from . import utils
@@ -9,60 +10,59 @@ 

  

  class TestAddVolume(utils.CliTestCase):

  

-     # Show long diffs in error output...

-     maxDiff = None

- 

      def setUp(self):

+         # Show long diffs in error output...

+         self.maxDiff = None

+         self.options = mock.MagicMock()

+         self.options.debug = False

+         self.session = mock.MagicMock()

+         self.session.getAPIVersion.return_value = koji.API_VERSION

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

          self.error_format = """Usage: %s add-volume <volume-name>

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

  

  %s: error: {message}

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

  

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

      @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_add_volume(

-             self,

-             activate_session_mock,

-             stdout,

-             stderr):

+     def test_handle_add_volume(self, stdout):

          """Test handle_add_volume function"""

-         session = mock.MagicMock()

-         options = mock.MagicMock()

          vol_name = 'vol-test-01'

          vol_info = {'id': 1, 'name': vol_name}

  

          # Case 1. argument error

-         expected = self.format_error_message(

-             "Command requires exactly one volume-name.")

          for arg in [[], ['test-1', 'test-2']]:

              self.assert_system_exit(

                  handle_add_volume,

-                 options,

-                 session,

-                 arg,

-                 stderr=expected,

+                 self.options, self.session, arg,

+                 stdout='',

+                 stderr=self.format_error_message("Command requires exactly one volume-name."),

+                 exit_code=2,

                  activate_session=None)

+         self.activate_session_mock.assert_not_called()

+         self.activate_session_mock.reset_mock()

  

          # Case 2. volume already exists

-         expected = "Volume %s already exists" % vol_name + "\n"

-         session.getVolume.return_value = vol_info

-         with self.assertRaises(SystemExit) as ex:

-             handle_add_volume(options, session, [vol_name])

-         self.assertExitCode(ex, 1)

-         self.assert_console_message(stderr, expected)

-         session.getVolume.assert_called_with(vol_name)

-         activate_session_mock.assert_not_called()

+         self.session.getVolume.return_value = vol_info

+         self.assert_system_exit(

+             handle_add_volume,

+             self.options, self.session, [vol_name],

+             stdout='',

+             stderr="Volume %s already exists" % vol_name + "\n",

+             exit_code=1,

+             activate_session=None)

+         self.session.getVolume.assert_called_with(vol_name)

+         self.activate_session_mock.assert_not_called()

+         self.activate_session_mock.reset_mock()

  

          # Case 3. Add volume

          expected = "Added volume %(name)s with id %(id)i" % vol_info + "\n"

-         session.getVolume.return_value = {}

-         session.addVolume.return_value = vol_info

-         handle_add_volume(options, session, [vol_name])

+         self.session.getVolume.return_value = {}

+         self.session.addVolume.return_value = vol_info

+         handle_add_volume(self.options, self.session, [vol_name])

          self.assert_console_message(stdout, expected)

-         session.addVolume(vol_name)

-         activate_session_mock.assert_called_with(session, options)

+         self.session.addVolume(vol_name)

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

  

      def test_handle_add_volume_help(self):

          self.assert_help(

@@ -10,10 +10,14 @@ 

  

  class TestAssignTask(utils.CliTestCase):

  

-     # Show long diffs in error output...

-     maxDiff = None

- 

      def setUp(self):

+         # Show long diffs in error output...

+         self.maxDiff = None

+         self.options = mock.MagicMock()

+         self.options.debug = False

+         self.session = mock.MagicMock()

+         self.session.getAPIVersion.return_value = koji.API_VERSION

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

          self.error_format = """Usage: %s assign-task <task_id> <hostname>

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

  
@@ -21,44 +25,40 @@ 

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

  

      @mock.patch('sys.stdout', new_callable=six.StringIO)

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

-     def test_handle_assign_task(

-             self, activate_session_mock, stdout):

+     def test_handle_assign_task(self, stdout):

          hostname = "host"

          task_id = "1"

          arguments = [task_id, hostname]

-         options = mock.MagicMock()

- 

-         # Mock out the xmlrpc server

-         session = mock.MagicMock()

  

-         session.getTaskInfo.return_value = None

+         self.session.getTaskInfo.return_value = None

          with six.assertRaisesRegex(self, koji.GenericError,

                                     "No such task: %s" % task_id):

-             handle_assign_task(options, session, arguments)

+             handle_assign_task(self.options, self.session, arguments)

  

-         session.getTaskInfo.return_value = "task_info"

-         session.getHost.return_value = None

+         self.session.getTaskInfo.return_value = "task_info"

+         self.session.getHost.return_value = None

          with six.assertRaisesRegex(self, koji.GenericError,

                                     "No such host: %s" % hostname):

-             handle_assign_task(options, session, arguments)

+             handle_assign_task(self.options, self.session, arguments)

  

          arguments.append("--force")

-         session.getHost.return_value = hostname

-         session.hasPerm.return_value = False

+         self.session.getHost.return_value = hostname

+         self.session.hasPerm.return_value = False

          self.assert_system_exit(

              handle_assign_task,

-             options, session, arguments,

-             stderr=self.format_error_message("This action requires admin privileges")

+             self.options, self.session, arguments,

+             stdout='',

+             stderr=self.format_error_message("This action requires admin privileges"),

+             exit_code=2

          )

  

          # Clean stdout buffer

          stdout.truncate(0)

          stdout.seek(0)

  

-         session.hasPerm.return_value = True

-         session.assignTask.return_value = True

-         handle_assign_task(options, session, arguments)

+         self.session.hasPerm.return_value = True

+         self.session.assignTask.return_value = True

+         handle_assign_task(self.options, self.session, arguments)

          actual = stdout.getvalue()

          expected = 'assigned task %s to host %s\n' % \

                     (task_id, hostname)
@@ -68,32 +68,23 @@ 

          stdout.truncate(0)

          stdout.seek(0)

  

-         session.assignTask.return_value = False

-         handle_assign_task(options, session, arguments)

+         self.session.assignTask.return_value = False

+         handle_assign_task(self.options, self.session, arguments)

          actual = stdout.getvalue()

          expected = 'failed to assign task %s to host %s\n' % \

                     (task_id, hostname)

          self.assertMultiLineEqual(actual, expected)

  

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_with(session, options)

-         session.assignTask.assert_called_with(int(task_id), hostname, True)

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

+         self.session.assignTask.assert_called_with(int(task_id), hostname, True)

  

-     @mock.patch('sys.stdout', new_callable=six.StringIO)

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_assign_task_help(

-             self, activate_session_mock, stderr, stdout):

+     def test_handle_assign_task_help(self):

          arguments = []

-         options = mock.MagicMock()

- 

-         # Mock out the xmlrpc server

-         session = mock.MagicMock()

- 

          # Run it and check immediate output

          self.assert_system_exit(

              handle_assign_task,

-             options, session, arguments,

+             self.options, self.session, arguments,

              stdout='',

              stderr=self.format_error_message('please specify a task id and a hostname'),

              activate_session=None,
@@ -101,9 +92,9 @@ 

          )

  

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_not_called()

-         session.hasHost.assert_not_called()

-         session.addHost.assert_not_called()

+         self.activate_session_mock.assert_not_called()

+         self.session.hasHost.assert_not_called()

+         self.session.addHost.assert_not_called()

  

  

  if __name__ == '__main__':

@@ -2,6 +2,7 @@ 

  

  import mock

  import six

+ import koji

  

  from koji_cli.commands import handle_block_group

  from . import utils
@@ -12,123 +13,108 @@ 

      maxDiff = None

  

      def setUp(self):

+         self.maxDiff = None

+         self.options = mock.MagicMock()

+         self.options.debug = False

+         self.session = mock.MagicMock()

+         self.session.getAPIVersion.return_value = koji.API_VERSION

+         self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()

          self.error_format = """Usage: %s block-group <tag> <group>

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

  

  %s: error: {message}

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

  

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_block_group_nonexistent_tag(self, activate_session_mock, stderr):

+     def test_handle_block_group_nonexistent_tag(self):

          tag = 'nonexistent-tag'

          group = 'group'

          arguments = [tag, group]

-         options = mock.MagicMock()

- 

-         # Mock out the xmlrpc server

-         session = mock.MagicMock()

-         session.hasPerm.return_value = True

-         session.getTag.return_value = None

+         self.session.hasPerm.return_value = True

+         self.session.getTag.return_value = None

  

          # Run it and check immediate output

-         with self.assertRaises(SystemExit) as ex:

-             handle_block_group(options, session, arguments)

-         self.assertExitCode(ex, 1)

-         actual = stderr.getvalue()

-         expected = 'No such tag: %s\n' % tag

-         self.assertMultiLineEqual(actual, expected)

+         self.assert_system_exit(

+             handle_block_group,

+             self.options, self.session, arguments,

+             stderr='No such tag: %s\n' % tag,

+             stdout='',

+             activate_session=None,

+             exit_code=1)

  

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)

-         session.hasPerm.assert_called_once_with('admin')

-         session.getTag.assert_called_once_with(tag)

-         session.getTagGroups.assert_not_called()

-         session.groupListBlock.assert_not_called()

- 

-     @mock.patch('sys.stderr', new_callable=six.StringIO)

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

-     def test_handle_block_group_nonexistent_group(self, activate_session_mock, stderr):

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

+         self.session.hasPerm.assert_called_once_with('admin')

+         self.session.getTag.assert_called_once_with(tag)

+         self.session.getTagGroups.assert_not_called()

+         self.session.groupListBlock.assert_not_called()

+ 

+     def test_handle_block_group_nonexistent_group(self):

          tag = 'tag'

          group = 'group'

          arguments = [tag, group]

-         options = mock.MagicMock()

- 

-         # Mock out the xmlrpc server

-         session = mock.MagicMock()

-         session.hasPerm.return_value = True

-         session.getTag.return_value = tag

-         session.getTagGroups.return_value = []

+         self.session.hasPerm.return_value = True

+         self.session.getTag.return_value = tag

+         self.session.getTagGroups.return_value = []

  

          # Run it and check immediate output

-         with self.assertRaises(SystemExit) as ex:

-             handle_block_group(options, session, arguments)

-         self.assertExitCode(ex, 1)

-         actual = stderr.getvalue()

-         expected = "Group %s doesn't exist within tag %s\n" % (group, tag)

-         self.assertMultiLineEqual(actual, expected)

+         self.assert_system_exit(

+             handle_block_group,

+             self.options, self.session, arguments,

+             stderr="Group %s doesn't exist within tag %s\n" % (group, tag),

+             stdout='',

+             activate_session=None,

+             exit_code=1)

  

          # Finally, assert that things were called as we expected.

-         activate_session_mock.assert_called_once_with(session, options)