From ae28279ee53b311462b1e3a71a78ef5496e4c041 Mon Sep 17 00:00:00 2001
From: Jana Cupova <root@localhost>
Date: Oct 13 2022 05:44:59 +0000
Subject: Increase unit tests


Fixes: https://pagure.io/koji/issue/3527

---

diff --git a/tests/test_cli/test_add_channel.py b/tests/test_cli/test_add_channel.py
index 48c1695..dc9d991 100644
--- a/tests/test_cli/test_add_channel.py
+++ b/tests/test_cli/test_add_channel.py
@@ -98,6 +98,22 @@ class TestAddChannel(utils.CliTestCase):
             activate_session=None)
         self.activate_session_mock.assert_not_called()
 
+    def test_handle_add_channel_other_error_msg(self):
+        expected = 'Other error message.'
+
+        self.session.addChannel.side_effect = koji.GenericError(expected)
+        arguments = ['--description', self.description, self.channel_name]
+        self.assert_system_exit(
+            handle_add_channel,
+            self.options, self.session, arguments,
+            stdout='',
+            stderr=expected + '\n',
+            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)
+
     def test_handle_add_channel_help(self):
         self.assert_help(
             handle_add_channel,
diff --git a/tests/test_cli/test_buildinfo.py b/tests/test_cli/test_buildinfo.py
index d904d28..d00eb08 100644
--- a/tests/test_cli/test_buildinfo.py
+++ b/tests/test_cli/test_buildinfo.py
@@ -139,3 +139,68 @@ Tags:
         self.session.getMavenBuild.assert_not_called()
         self.session.getWinBuild.assert_not_called()
         self.session.listRPMs.assert_not_called()
+
+    @mock.patch('sys.stdout', new_callable=StringIO)
+    def test_buildinfo_valid_win_maven_image_with_changelog(self, stdout):
+        build_id = 1
+        build_info = self.buildinfo.copy()
+        build_info['state'] = 0
+        build_info['source'] = 'test-source'
+        build_info['cg_name'] = 'cg-name-test'
+        build_info['extra'] = {'test-extra': 'result-extra'}
+        list_all_archives = [{'id': 987, 'filename': 'test-maven-file', 'group_id': '222',
+                              'artifact_id': '555', 'version': '2'},
+                             {'id': 988, 'filename': 'test-win-file', 'relpath': 'test-rel-path'},
+                             {'id': 989, 'filename': 'test-image-filename'},
+                             {'id': 990, 'filename': 'test-extra-filename', 'btype': 'test-btype'}]
+        self.session.getBuild.return_value = build_info
+        self.session.getTaskInfo.return_value = self.taskinfo
+        self.session.listTags.return_value = [{'id': 123, 'name': 'test-tag'}]
+        self.session.getMavenBuild.return_value = {
+            'build_id': 12345, 'group_id': 222, 'artifact_id': 555, 'version': 2}
+        self.session.getWinBuild.return_value = {'build_id': 12346, 'platform': 'test-platform'}
+        self.session.listArchives.side_effect = [
+            [list_all_archives[0]], [list_all_archives[1]], [list_all_archives[2]],
+            list_all_archives]
+        self.session.listRPMs.return_value = []
+        self.session.getChangelogEntries.return_value = {'author': 'Test user',
+                                                         'date': '2022-10-03 12:00:00',
+                                                         'date_ts': 1664798400,
+                                                         'text': '- change for test'},
+        expected_stdout = """BUILD: test-build-1-1 [1]
+State: BUILDING
+Reserved by: cg-name-test
+Built by: kojiadmin
+Source: test-source
+Volume: DEFAULT
+Task: 8 build (target, src)
+Finished: Thu, 04 Mar 2021 14:45:40 UTC
+Maven groupId: 222
+Maven artifactId: 555
+Maven version: 2
+Windows build platform: test-platform
+Tags: test-tag
+Extra: {'test-extra': 'result-extra'}
+Maven archives:
+/mnt/koji/packages/test-build/1/1/maven/222/555/2/test-maven-file
+Windows archives:
+/mnt/koji/packages/test-build/1/1/win/test-rel-path/test-win-file
+Image archives:
+/mnt/koji/packages/test-build/1/1/images/test-image-filename
+Test-btype Archives:
+/mnt/koji/packages/test-build/1/1/files/test-btype/test-extra-filename
+Changelog:
+* Mon Oct 03 2022 Test user
+- change for test
+
+
+"""
+        anon_handle_buildinfo(self.options, self.session, [str(build_id), '--changelog'])
+        self.assert_console_message(stdout, expected_stdout)
+        self.session.listTags.assert_called_once_with(build_id)
+        self.session.getBuild.assert_called_once_with(build_id)
+        self.session.getTaskInfo.assert_called_once_with(self.buildinfo['task_id'], request=True)
+        self.session.getMavenBuild.assert_called_once_with(self.buildinfo['id'])
+        self.session.getWinBuild.assert_called_once_with(self.buildinfo['id'])
+        self.session.listRPMs.assert_called_once_with(buildID=self.buildinfo['id'])
+        self.assertEqual(self.session.listArchives.call_count, 4)
diff --git a/tests/test_cli/test_mock_config.py b/tests/test_cli/test_mock_config.py
index bf7d968..60db69c 100644
--- a/tests/test_cli/test_mock_config.py
+++ b/tests/test_cli/test_mock_config.py
@@ -242,6 +242,12 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
                 'rpm.macro.random_macro2': 'random_macro_content2',
                 'mock.package_manager': 'yum',
                 'mock.yum.module_hotfixes': 1,
+                'mock.forcearch': True,
+                'mock.yum.best': 10,
+                'mock.bootstrap_image': 'bootstrap_image_content',
+                'mock.use_bootstrap': True,
+                'mock.module_setup_commands': 'module_setup_commands_content',
+                'mock.releasever': 'releasever_content',
             },
             'arches': 'x86_64',
         }
@@ -277,7 +283,15 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
             },
             'package_manager': 'yum',
             'module_hotfixes': 1,
+            'bootstrap_image': 'bootstrap_image_content',
+            'forcearch': 'x86_64',
+            'module_setup_commands': 'module_setup_commands_content',
+            'releasever': 'releasever_content',
+            'use_bootstrap': True,
+            'use_bootstrap_image': True,
+            'yum_best': 10,
         })
+
         del opts['topurl']
         anon_handle_mock_config(self.options, self.session, arguments)
         self.assert_console_message(stdout, "%s\n" % self.gen_config_mock.return_value)
diff --git a/tests/test_cli/test_taginfo.py b/tests/test_cli/test_taginfo.py
index 40c2bb8..1dc6113 100644
--- a/tests/test_cli/test_taginfo.py
+++ b/tests/test_cli/test_taginfo.py
@@ -10,29 +10,124 @@ from . import utils
 
 class TestTaginfo(utils.CliTestCase):
     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.error_format = """Usage: %s taginfo [options] <tag> [<tag> ...]
+(Specify the --help global option for a list of other help options)
 
-    @mock.patch('sys.stderr', new_callable=StringIO)
-    def test_taginfo_without_option(self, stderr):
-        expected = "Usage: %s taginfo [options] <tag> [<tag> ...]\n" \
-                   "(Specify the --help global option for a list of other help options)\n\n" \
-                   "%s: error: Please specify a tag\n" % (self.progname, self.progname)
-        with self.assertRaises(SystemExit) as ex:
-            anon_handle_taginfo(self.options, self.session, [])
-        self.assertExitCode(ex, 2)
-        self.assert_console_message(stderr, expected)
-
-    @mock.patch('sys.stderr', new_callable=StringIO)
-    def test_taginfo_non_exist_tag(self, stderr):
+%s: error: {message}
+""" % (self.progname, self.progname)
+
+    def test_taginfo_without_option(self):
+        self.assert_system_exit(
+            anon_handle_taginfo,
+            self.options, self.session, [],
+            stdout='',
+            stderr=self.format_error_message("Please specify a tag"),
+            exit_code=2,
+            activate_session=None)
+
+        self.session.getBuildConfig.assert_not_called()
+        self.session.getTagGroups.assert_not_called()
+        self.session.mavenEnabled.assert_not_called()
+        self.session.getBuildTargets.assert_not_called()
+        self.session.getRepo.assert_not_called()
+        self.session.getTagExternalRepos.assert_not_called()
+        self.session.getInheritanceData.assert_not_called()
+
+    def test_taginfo_non_exist_tag(self):
         tag = 'test-tag'
-        expected = "Usage: %s taginfo [options] <tag> [<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)
         self.session.getBuildConfig.return_value = None
-        with self.assertRaises(SystemExit) as cm:
-            anon_handle_taginfo(self.options, self.session, [tag])
-        self.assertExitCode(cm, 2)
-        self.assert_console_message(stderr, expected)
+        self.assert_system_exit(
+            anon_handle_taginfo,
+            self.options, self.session, [tag],
+            stdout='',
+            stderr=self.format_error_message("No such tag: %s" % tag),
+            exit_code=2,
+            activate_session=None)
+
+        self.session.getBuildConfig.assert_called_once_with('test-tag')
+        self.session.getTagGroups.assert_not_called()
+        self.session.mavenEnabled.assert_not_called()
+        self.session.getBuildTargets.assert_not_called()
+        self.session.getRepo.assert_not_called()
+        self.session.getTagExternalRepos.assert_not_called()
+        self.session.getInheritanceData.assert_not_called()
+
+    @mock.patch('sys.stdout', new_callable=StringIO)
+    def test_taginfo(self, stdout):
+        self.session.getBuildConfig.return_value = {
+            'arches': 'x86_64',
+            'config_inheritance': {'arches': None, 'extra': ['value']},
+            'extra': {'tag2distrepo.keys': '9867c58f'},
+            'id': 1111,
+            'locked': True,
+            'maven_include_all': False,
+            'maven_support': True,
+            'name': 'test-tag',
+            'perm': 1,
+            'perm_id': 'test-perm'}
+        self.session.getTagGroups.return_value = [{'name': 'group-1'}, {'name': 'group-2'}]
+        self.session.mavenEnabled.return_value = True
+        self.session.getBuildTargets.side_effect = [[
+            {'build_tag': 123,
+             'build_tag_name': 'test-build-tag-1',
+             'id': 111,
+             'name': 'test-tag-1'}],
+            [{'build_tag': 1111,
+              'build_tag_name': 'test-tag',
+              'id': 112,
+              'name': 'test-tag-2'}]]
+        self.session.getRepo.side_effect = [None, None, ]
+        self.session.getTagExternalRepos.return_value = [{'external_repo_id': 11,
+                                                          'external_repo_name': 'ext-repo',
+                                                          'tag_id': 1111,
+                                                          'tag_name': 'test-tag',
+                                                          'priority': 5,
+                                                          'merge_mode': 'simple',
+                                                          'arches': 'x86_64 i686',
+                                                          'url': 'test/url'}]
+        self.session.getInheritanceData.return_value = [{'child_id': 1,
+                                                         'intransitive': False,
+                                                         'maxdepth': 111111,
+                                                         'name': 'test-tag',
+                                                         'noconfig': False,
+                                                         'parent_id': 2,
+                                                         'pkg_filter': 'pkg-filter',
+                                                         'priority': 5,
+                                                         'tag_id': 1111}]
+        expected_stdout = """Tag: test-tag [1111]
+Arches: x86_64
+Groups: group-1, group-2
+LOCKED
+Required permission: 'test-perm'
+Maven support?: yes
+Include all Maven archives?: no
+Tag options:
+  tag2distrepo.keys : '9867c58f'
+Targets that build into this tag:
+  test-tag-1 (test-build-tag-1, no active repo)
+This tag is a buildroot for one or more targets
+Current repo: no active repo
+Targets that build from this tag:
+  test-tag-2
+External repos:
+    5 ext-repo (test/url, merge mode: simple), arches: x86_64 i686
+Inheritance:
+  5    MF.. test-tag [2]
+    maxdepth: 111111
+    package filter: pkg-filter
+"""
+        anon_handle_taginfo(self.options, self.session, ['test-tag'])
+        self.assert_console_message(stdout, expected_stdout)
+        self.session.getBuildConfig.assert_called_once_with('test-tag')
+        self.session.getTagGroups.assert_called_once_with(1111)
+        self.session.mavenEnabled.assert_called_once_with()
+        self.session.getBuildTargets.assert_has_calls([mock.call(destTagID=1111)],
+                                                      [mock.call(buildTagID=1111)])
+        self.session.getRepo.assert_has_calls([mock.call(123)], [mock.call(1111)])
+        self.session.getTagExternalRepos.assert_called_once_with(tag_info=1111)
+        self.session.getInheritanceData.assert_called_once_with(1111)

