#2769 cli: hostinfo with non-exist host
Merged 3 years ago by tkopecek. Opened 3 years ago by jcupova.
jcupova/koji issue2763  into  master

@@ -3325,12 +3325,14 @@ 

      if len(args) < 1:

          parser.error(_("Please specify a host"))

      ensure_connection(session, goptions)

+     error_hit = False

      for host in args:

          if host.isdigit():

              host = int(host)

          info = session.getHost(host)

          if info is None:

              warn("No such host: %s\n" % host)

+             error_hit = True

              continue

          print("Name: %(name)s" % info)

          print("ID: %(id)d" % info)
@@ -3373,6 +3375,8 @@ 

                  print("%-50s %-10s %-20s" % row)

          else:

              print("None")

+     if error_hit:

+         error()

  

  

  def _multicall_with_check(session, batch_size):

@@ -0,0 +1,72 @@ 

+ from __future__ import absolute_import

+ import mock

+ from six.moves import StringIO

+ 

+ import koji

+ from koji_cli.commands import anon_handle_hostinfo

+ from . import utils

+ 

+ 

+ class TestHostinfo(utils.CliTestCase):

+     def setUp(self):

+         self.options = mock.MagicMock()

+         self.options.debug = False

+         self.session = mock.MagicMock()

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

+         self.hostinfo = {'arches': 'x86_64',

+                          'capacity': 2.0,

+                          'comment': None,

+                          'description': None,

+                          'enabled': True,

+                          'id': 1,

+                          'name': 'kojibuilder',

+                          'ready': True,

+                          'task_load': 0.0,

+                          'user_id': 2}

+         self.last_update = '2021-03-16 06:19:14.862938+00:00'

+         self.list_channels = [{'id': 1, 'name': 'default'}, {'id': 2, 'name': 'createrepo'}]

+ 

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

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

+     def test_hostinfo_more_hosts_with_non_exit_host(self, stdout, stderr):

+         hostname = 'kojibuilder'

+         non_exist_hostname = 'testhost'

+         expected_stdout = """Name: kojibuilder

+ ID: 1

+ Arches: x86_64

+ Capacity: 2.0

+ Task Load: 0.00

+ Description:

+ Comment:

+ Enabled: yes

+ Ready: yes

+ Last Update: 2021-03-16 06:19:14

+ Channels: default createrepo

+ Active Buildroots:

+ None

+ """

+         expected_error = "No such host: %s\n\n" % non_exist_hostname

+         self.session.getHost.side_effect = [None, self.hostinfo]

+         self.session.getLastHostUpdate.return_value = self.last_update

+         self.session.listChannels.return_value = self.list_channels

+         self.session.listBuildroots.return_value = []

+         with self.assertRaises(SystemExit) as ex:

+             anon_handle_hostinfo(self.options, self.session, [non_exist_hostname, hostname])

+         self.assertExitCode(ex, 1)

+         self.assert_console_message(stdout, expected_stdout)

+         self.assert_console_message(stderr, expected_error)

+         self.assertEqual(self.session.getHost.call_count, 2)

+         self.session.getLastHostUpdate.assert_called_once_with(self.hostinfo['id'])

+         self.session.listChannels.assert_called_once_with(hostID=self.hostinfo['id'])

+         self.assertEqual(self.session.listBuildroots.call_count, 3)

+ 

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

+     def test_hostinfo_non_exist_host(self, stderr):

+         hostname = 'testhost'

+         expected = "No such host: %s\n\n" % hostname

+         self.session.getHost.return_value = None

+         self.session.getLastHostUpdate.return_value = None

+         with self.assertRaises(SystemExit) as ex:

+             anon_handle_hostinfo(self.options, self.session, [hostname])

+         self.assertExitCode(ex, 1)

+         self.assert_console_message(stderr, expected)

Commit 8a74740 fixes this pull-request

Pull-Request has been merged by tkopecek

3 years ago

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

3 years ago

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

3 years ago