From 637be55ef4f6fe9f280eb59114906f9cd8f2f511 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: May 22 2020 07:36:21 +0000
Subject: [PATCH 1/2] cli: drop unneeded activate_session


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

---

diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py
index ed96121..2385862 100644
--- a/cli/koji_cli/commands.py
+++ b/cli/koji_cli/commands.py
@@ -35,6 +35,7 @@ from koji_cli.lib import (
     download_archive,
     download_file,
     download_rpm,
+    ensure_connection,
     error,
     format_inheritance_flags,
     get_usage_str,
@@ -932,7 +933,7 @@ def anon_handle_mock_config(goptions, session, args):
     parser.add_option("--yum-proxy", help=_("Specify a yum proxy"))
     parser.add_option("-o", metavar="FILE", dest="ofile", help=_("Output to a file"))
     (options, args) = parser.parse_args(args)
-    activate_session(session, goptions)
+    ensure_connection(session)
     if args:
         # for historical reasons, we also accept buildroot name as first arg
         if not options.name:
@@ -2349,7 +2350,7 @@ def anon_handle_latest_build(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) == 0:
         parser.error(_("A tag name must be specified"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     if options.all:
         if len(args) > 1:
             parser.error(_("A package name may not be combined with --all"))
@@ -2417,7 +2418,7 @@ def anon_handle_list_api(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) != 0:
         parser.error(_("This command takes no arguments"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     for x in sorted(session._listapi(), key=lambda x: x['name']):
         if 'argdesc' in x:
             args = x['argdesc']
@@ -2463,7 +2464,7 @@ def anon_handle_list_tagged(goptions, session, args):
         parser.error(_("A tag name must be specified"))
     elif len(args) > 2:
         parser.error(_("Only one package name may be specified"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     pathinfo = koji.PathInfo()
     package = None
     if len(args) > 1:
@@ -2562,7 +2563,7 @@ def anon_handle_list_buildroot(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) != 1:
         parser.error(_("Incorrect number of arguments"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     buildrootID = int(args[0])
     opts = {}
     if options.built:
@@ -2589,7 +2590,7 @@ def anon_handle_list_untagged(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) > 1:
         parser.error(_("Only one package name may be specified"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     package = None
     if len(args) > 0:
         package = args[0]
@@ -2657,7 +2658,7 @@ def anon_handle_list_groups(goptions, session, args):
     opts = {}
     if options.incl_blocked:
         opts['incl_blocked'] = True
-    activate_session(session, goptions)
+    ensure_connection(session)
     event = koji.util.eventFromOpts(session, options)
     if event:
         opts['event'] = event['id']
@@ -2786,7 +2787,7 @@ def anon_handle_list_channels(goptions, session, args):
     parser.add_option("--quiet", action="store_true", default=goptions.quiet,
                       help=_("Do not print header information"))
     (options, args) = parser.parse_args(args)
-    activate_session(session, goptions)
+    ensure_connection(session)
     channels = session.listChannels()
     channels = sorted(channels, key=lambda x: x['name'])
     session.multicall = True
@@ -2834,7 +2835,7 @@ def anon_handle_list_hosts(goptions, session, args):
     parser.add_option("--show-channels", action="store_true", help=_("Show host's channels"))
     (options, args) = parser.parse_args(args)
     opts = {}
-    activate_session(session, goptions)
+    ensure_connection(session)
     if options.arch:
         opts['arches'] = options.arch
     if options.channel:
@@ -2915,7 +2916,7 @@ def anon_handle_list_pkgs(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) != 0:
         parser.error(_("This command takes no arguments"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     opts = {}
     if options.owner:
         user = session.getUser(options.owner)
@@ -3008,7 +3009,7 @@ def anon_handle_list_builds(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) != 0:
         parser.error(_("This command takes no arguments"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     opts = {}
     for key in ('type', 'prefix'):
         value = getattr(options, key)
@@ -3112,7 +3113,7 @@ def anon_handle_rpminfo(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) < 1:
         parser.error(_("Please specify an RPM"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     for rpm in args:
         info = session.getRPM(rpm)
         if info is None:
@@ -3184,7 +3185,7 @@ def anon_handle_buildinfo(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) < 1:
         parser.error(_("Please specify a build"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     for build in args:
         if build.isdigit():
             build = int(build)
@@ -3282,7 +3283,7 @@ def anon_handle_hostinfo(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) < 1:
         parser.error(_("Please specify a host"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     for host in args:
         if host.isdigit():
             host = int(host)
@@ -3967,7 +3968,7 @@ def anon_handle_list_targets(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) != 0:
         parser.error(_("This command takes no arguments"))
-    activate_session(session, goptions)
+    ensure_connection(session)
 
     fmt = "%(name)-30s %(build_tag_name)-30s %(dest_tag_name)-30s"
     if not options.quiet:
@@ -4037,7 +4038,7 @@ def anon_handle_list_tag_inheritance(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) != 1:
         parser.error(_("This command takes exctly one argument: a tag name or ID"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     event = koji.util.eventFromOpts(session, options)
     if event:
         event['timestr'] = time.asctime(time.localtime(event['ts']))
@@ -4088,7 +4089,7 @@ def anon_handle_list_tags(goptions, session, args):
     parser.add_option("--build", help=_("Show tags associated with a build"))
     parser.add_option("--package", help=_("Show tags associated with a package"))
     (options, args) = parser.parse_args(args)
-    activate_session(session, goptions)
+    ensure_connection(session)
 
     pkginfo = {}
     buildinfo = {}
@@ -4162,7 +4163,7 @@ def anon_handle_list_tag_history(goptions, session, args):
     if not limited and not options.all:
         parser.error(_("Please specify an option to limit the query"))
 
-    activate_session(session, goptions)
+    ensure_connection(session)
 
     hist = session.tagHistory(**kwargs)
     timeline = []
@@ -4516,7 +4517,7 @@ def anon_handle_list_history(goptions, session, args):
     if not limited and not options.all:
         parser.error(_("Please specify an option to limit the query"))
 
-    activate_session(session, goptions)
+    ensure_connection(session)
 
     if options.watch:
         if not kwargs.get('afterEvent') and not kwargs.get('after'):
@@ -4837,7 +4838,7 @@ def anon_handle_taskinfo(goptions, session, args):
     if len(args) < 1:
         parser.error(_("You must specify at least one task ID"))
 
-    activate_session(session, goptions)
+    ensure_connection(session)
 
     for arg in args:
         task_id = int(arg)
@@ -4855,7 +4856,7 @@ def anon_handle_taginfo(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) < 1:
         parser.error(_("Please specify a tag"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     event = koji.util.eventFromOpts(session, options)
     event_opts = {}
     if event:
@@ -5346,7 +5347,7 @@ def anon_handle_show_groups(goptions, session, args):
         parser.error(_("Incorrect number of arguments"))
     if options.incl_blocked and (options.comps or options.spec):
         parser.error(_("--show-blocked doesn't make sense for comps/spec output"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     tag = args[0]
     callopts = {}
     if options.incl_blocked:
@@ -5381,7 +5382,7 @@ def anon_handle_list_external_repos(goptions, session, args):
     (options, args) = parser.parse_args(args)
     if len(args) > 0:
         parser.error(_("This command takes no arguments"))
-    activate_session(session, goptions)
+    ensure_connection(session)
     opts = {}
     event = koji.util.eventFromOpts(session, options)
     if event:
@@ -6462,7 +6463,10 @@ def anon_handle_watch_task(goptions, session, args):
     if args and selection:
         parser.error(_("Selection options cannot be combined with a task list"))
 
-    activate_session(session, goptions)
+    if options.mine:
+        activate_session(session, goptions)
+    else:
+        ensure_connection(session)
     if selection:
         tasks = [task['id'] for task in _list_tasks(options, session)]
         if not tasks:
@@ -6492,9 +6496,9 @@ def anon_handle_watch_logs(goptions, session, args):
                              "this case."))
     parser.add_option("--follow", action="store_true", help=_("Follow spawned child tasks"))
     (options, args) = parser.parse_args(args)
-    activate_session(session, goptions)
 
     if options.mine:
+        activate_session(session, goptions)
         if args:
             parser.error(_("Selection options cannot be combined with a task list"))
         tasks = _list_tasks(options, session)
@@ -6503,6 +6507,7 @@ def anon_handle_watch_logs(goptions, session, args):
             print(_("You've no active tasks."))
             return
     else:
+        ensure_connection(session)
         tasks = []
         for task in args:
             try:
@@ -6744,7 +6749,7 @@ def anon_handle_download_build(options, session, args):
     elif len(args) > 1:
         parser.error(_("Only a single package N-V-R or build ID may be specified"))
 
-    activate_session(session, options)
+    ensure_connection(session)
     build = args[0]
 
     if build.isdigit():
@@ -6968,7 +6973,7 @@ def anon_handle_download_task(options, session, args):
     if len(suboptions.arches) > 0:
         suboptions.arches = ",".join(suboptions.arches).split(",")
 
-    activate_session(session, options)
+    ensure_connection(session)
 
     # get downloadable tasks
 
@@ -7392,14 +7397,14 @@ def anon_handle_list_notifications(goptions, session, args):
     if not options.mine and not options.user:
         parser.error(_("Use --user or --mine."))
 
-    activate_session(session, goptions)
-
     if options.user:
+        ensure_connection(session)
         user = session.getUser(options.user)
         if not user:
             error("User %s does not exist" % options.user)
         user_id = user['id']
     else:
+        activate_session(session, goptions)
         user_id = None
 
     mask = "%(id)6s %(tag)-25s %(package)-25s %(email)-20s %(success)-12s"
diff --git a/tests/test_cli/test_download_task.py b/tests/test_cli/test_download_task.py
index b405e60..e14926a 100644
--- a/tests/test_cli/test_download_task.py
+++ b/tests/test_cli/test_download_task.py
@@ -47,7 +47,7 @@ class TestDownloadTask(utils.CliTestCase):
         self.list_task_output_all_volumes = mock.patch('koji_cli.commands.list_task_output_all_volumes').start()
         self.ensuredir = mock.patch('koji.ensuredir').start()
         self.download_file = mock.patch('koji_cli.commands.download_file').start()
-        self.activate_session = mock.patch('koji_cli.commands.activate_session').start()
+        self.ensure_connection = mock.patch('koji_cli.commands.ensure_connection').start()
         self.stdout = mock.patch('sys.stdout', new_callable=six.StringIO).start()
         self.stderr = mock.patch('sys.stderr', new_callable=six.StringIO).start()
 
@@ -80,7 +80,7 @@ class TestDownloadTask(utils.CliTestCase):
         expected = ''
         self.assertMultiLineEqual(actual, expected)
         # Finally, assert that things were called as we expected.
-        self.activate_session.assert_called_once_with(self.session, self.options)
+        self.ensure_connection.assert_called_once_with(self.session)
         self.session.getTaskInfo.assert_called_once_with(task_id)
         self.session.getTaskChildren.assert_not_called()
         self.list_task_output_all_volumes.assert_called_once_with(self.session, task_id)
@@ -105,8 +105,7 @@ class TestDownloadTask(utils.CliTestCase):
         expected = 'No such task: #123333\n'
         self.assertMultiLineEqual(actual, expected)
         # Finally, assert that things were called as we expected.
-        self.activate_session.assert_called_once_with(self.session,
-                                                      self.options)
+        self.ensure_connection.assert_called_once_with(self.session)
         self.session.getTaskInfo.assert_called_once_with(task_id)
         self.session.getTaskChildren.assert_not_called()
 
@@ -148,7 +147,7 @@ class TestDownloadTask(utils.CliTestCase):
         expected = ''
         self.assertMultiLineEqual(actual, expected)
         # Finally, assert that things were called as we expected.
-        self.activate_session.assert_called_once_with(self.session, self.options)
+        self.ensure_connection.assert_called_once_with(self.session)
         self.session.getTaskInfo.assert_called_once_with(task_id)
         self.session.getTaskChildren.assert_called_once_with(task_id)
         self.assertEqual(self.list_task_output_all_volumes.mock_calls, [
@@ -189,7 +188,7 @@ class TestDownloadTask(utils.CliTestCase):
         expected = ''
         self.assertMultiLineEqual(actual, expected)
         # Finally, assert that things were called as we expected.
-        self.activate_session.assert_called_once_with(self.session, self.options)
+        self.ensure_connection.assert_called_once_with(self.session)
         self.session.getTaskInfo.assert_called_once_with(task_id)
         self.session.getTaskChildren.assert_not_called()
         self.list_task_output_all_volumes.assert_called_once_with(self.session, task_id)
@@ -224,7 +223,7 @@ class TestDownloadTask(utils.CliTestCase):
         expected = 'No files for download found.\n'
         self.assertMultiLineEqual(actual, expected)
         # Finally, assert that things were called as we expected.
-        self.activate_session.assert_called_once_with(self.session, self.options)
+        self.ensure_connection.assert_called_once_with(self.session)
         self.session.getTaskInfo.assert_called_once_with(task_id)
         self.session.getTaskChildren.assert_not_called()
         self.list_task_output_all_volumes.assert_called_once_with(self.session, task_id)
@@ -257,7 +256,7 @@ class TestDownloadTask(utils.CliTestCase):
         expected = 'Task 123333 has not finished yet.\n'
         self.assertMultiLineEqual(actual, expected)
         # Finally, assert that things were called as we expected.
-        self.activate_session.assert_called_once_with(self.session, self.options)
+        self.ensure_connection.assert_called_once_with(self.session)
         self.session.getTaskInfo.assert_called_once_with(task_id)
         self.session.getTaskChildren.assert_not_called()
         self.list_task_output_all_volumes.assert_called_once_with(self.session, task_id)
@@ -288,7 +287,7 @@ class TestDownloadTask(utils.CliTestCase):
         expected = 'Child task 22222 has not finished yet.\n'
         self.assertMultiLineEqual(actual, expected)
         # Finally, assert that things were called as we expected.
-        self.activate_session.assert_called_once_with(self.session, self.options)
+        self.ensure_connection.assert_called_once_with(self.session)
         self.session.getTaskInfo.assert_called_once_with(task_id)
         self.session.getTaskChildren.assert_called_once_with(task_id)
         self.list_task_output_all_volumes.assert_called_once_with(self.session, 22222)
@@ -315,7 +314,7 @@ class TestDownloadTask(utils.CliTestCase):
         expected = 'Invalid file name: somerpm..src.rpm\n'
         self.assertMultiLineEqual(actual, expected)
         # Finally, assert that things were called as we expected.
-        self.activate_session.assert_called_once_with(self.session, self.options)
+        self.ensure_connection.assert_called_once_with(self.session)
         self.session.getTaskInfo.assert_called_once_with(task_id)
         self.session.getTaskChildren.assert_not_called()
         self.list_task_output_all_volumes.assert_called_once_with(self.session, task_id)
diff --git a/tests/test_cli/test_list_api.py b/tests/test_cli/test_list_api.py
index 48dfee9..556afcf 100644
--- a/tests/test_cli/test_list_api.py
+++ b/tests/test_cli/test_list_api.py
@@ -23,10 +23,10 @@ class TestListApi(utils.CliTestCase):
 """ % (self.progname, self.progname)
 
     @mock.patch('sys.stdout', new_callable=six.StringIO)
-    @mock.patch('koji_cli.commands.activate_session')
+    @mock.patch('koji_cli.commands.ensure_connection')
     def test_anon_handle_list_api(
             self,
-            activate_session_mock,
+            ensure_connection_mock,
             stdout):
         """Test anon_handle_list_api function"""
         session = mock.MagicMock()
diff --git a/tests/test_cli/test_list_channels.py b/tests/test_cli/test_list_channels.py
index 663fb73..84a6022 100644
--- a/tests/test_cli/test_list_channels.py
+++ b/tests/test_cli/test_list_channels.py
@@ -19,8 +19,8 @@ class TestListChannels(unittest.TestCase):
         self.args = []
 
     @mock.patch('sys.stdout', new_callable=StringIO)
-    @mock.patch('koji_cli.commands.activate_session')
-    def test_list_channels(self, activate_session_mock, stdout):
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def test_list_channels(self, ensure_connection_mock, stdout):
         self.session.listChannels.return_value = [
             {'id': 1, 'name': 'default'},
             {'id': 2, 'name': 'test'},
@@ -47,4 +47,4 @@ default              3      1      0      1      6     22%
 test                 2      2      1      1      6     28%
 """
         self.assertMultiLineEqual(actual, expected)
-        activate_session_mock.assert_called_once_with(self.session, self.options)
+        ensure_connection_mock.assert_called_once_with(self.session)
diff --git a/tests/test_cli/test_list_groups.py b/tests/test_cli/test_list_groups.py
index 6d595bc..0abe89f 100644
--- a/tests/test_cli/test_list_groups.py
+++ b/tests/test_cli/test_list_groups.py
@@ -34,8 +34,10 @@ class TestListGroups(utils.CliTestCase):
 
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji_cli.commands.activate_session')
+    @mock.patch('koji_cli.commands.ensure_connection')
     def test_anon_handle_list_groups_argument_error(
             self,
+            ensure_connection_mock,
             activate_session_mock,
             stdout):
         """Test anon_handle_list_groups function"""
@@ -71,7 +73,8 @@ class TestListGroups(utils.CliTestCase):
         expected = "Querying at event %(id)i (%(timestr)s)" % event + "\n"
         self.__list_groups('build', ['--ts', '1234567'], expected)
 
-    def __list_groups(self, query_group, options, expected):
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def __list_groups(self, query_group, options, expected, ensure_connection_mock):
         _list_tags = [
             {
                 'maven_support': False,
diff --git a/tests/test_cli/test_list_notifications.py b/tests/test_cli/test_list_notifications.py
index 01e668a..a214abd 100644
--- a/tests/test_cli/test_list_notifications.py
+++ b/tests/test_cli/test_list_notifications.py
@@ -45,15 +45,15 @@ No notification blocks
 
         self.maxDiff=None
         self.assertMultiLineEqual(actual, expected)
-        activate_session_mock.assert_called_once_with(self.session, self.options)
+        activate_session_mock.assert_called_once()
         self.session.getTag.assert_has_calls([mock.call(1), mock.call(1)])
         self.session.getPackage.assert_has_calls([mock.call(11), mock.call(11)])
         self.session.getUser.assert_not_called()
         self.session.getBuildNotifications.assert_called_once_with(None)
 
     @mock.patch('sys.stdout', new_callable=StringIO)
-    @mock.patch('koji_cli.commands.activate_session')
-    def test_list_notifications_user(self, activate_session_mock, stdout):
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def test_list_notifications_user(self, ensure_connection_mock, stdout):
         self.session.getBuildNotifications.return_value = [
             {'id': 1, 'tag_id': 1, 'package_id': 11, 'email': 'email@test.com', 'success_only': True},
             {'id': 2, 'tag_id': None, 'package_id': 11, 'email': 'email@test.com', 'success_only': False},
@@ -94,7 +94,7 @@ Notification blocks
 
         self.maxDiff=None
         self.assertMultiLineEqual(actual, expected)
-        activate_session_mock.assert_called_once_with(self.session, self.options)
+        ensure_connection_mock.assert_called_once_with(self.session)
         self.session.getTag.assert_has_calls([mock.call(1), mock.call(1)])
         self.session.getPackage.assert_has_calls([mock.call(11), mock.call(11)])
         self.session.getUser.assert_called_once_with('random_name')
diff --git a/tests/test_cli/test_list_tagged.py b/tests/test_cli/test_list_tagged.py
index 0bf81b8..1b68113 100644
--- a/tests/test_cli/test_list_tagged.py
+++ b/tests/test_cli/test_list_tagged.py
@@ -64,14 +64,13 @@ class TestCliListTagged(utils.CliTestCase):
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.util.eventFromOpts', return_value={'id': 1000,
                                                          'ts': 1000000.11})
-    @mock.patch('koji_cli.commands.activate_session')
-    def test_list_tagged_builds(self, activate_session_mock,
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def test_list_tagged_builds(self, ensure_connection_mock,
                                 event_from_opts_mock, stdout):
         args = ['tag', 'pkg', '--latest', '--inherit', '--event=1000']
 
         anon_handle_list_tagged(self.options, self.session, args)
-        activate_session_mock.assert_called_once_with(self.session,
-                                                      self.options)
+        ensure_connection_mock.assert_called_once_with(self.session)
         self.session.getTag.assert_called_once_with('tag', event=1000)
         self.session.listTagged.assert_called_once_with('tag',
                                                         event=1000,
@@ -87,8 +86,8 @@ class TestCliListTagged(utils.CliTestCase):
 
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.util.eventFromOpts', return_value=None)
-    @mock.patch('koji_cli.commands.activate_session')
-    def test_list_tagged_builds_paths(self, activate_session_mock,
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def test_list_tagged_builds_paths(self, ensure_connection_mock,
                                       event_from_opts_mock, stdout):
         args = ['tag', 'pkg', '--latest', '--inherit', '--paths']
 
@@ -100,15 +99,14 @@ class TestCliListTagged(utils.CliTestCase):
 
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.util.eventFromOpts', return_value=None)
-    @mock.patch('koji_cli.commands.activate_session')
-    def test_list_tagged_rpms(self, activate_session_mock,
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def test_list_tagged_rpms(self, ensure_connection_mock,
                               event_from_opts_mock, stdout):
         args = ['tag', 'pkg', '--latest-n=3', '--rpms', '--sigs',
                 '--arch=x86_64', '--arch=noarch']
 
         anon_handle_list_tagged(self.options, self.session, args)
-        activate_session_mock.assert_called_once_with(self.session,
-                                                      self.options)
+        ensure_connection_mock.assert_called_once_with(self.session)
         self.session.getTag.assert_called_once_with('tag', event=None)
         self.session.listTaggedRPMS.assert_called_once_with('tag',
                                                             package='pkg',
@@ -124,8 +122,8 @@ class TestCliListTagged(utils.CliTestCase):
 
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.util.eventFromOpts', return_value=None)
-    @mock.patch('koji_cli.commands.activate_session')
-    def test_list_tagged_rpms_paths(self, activate_session_mock,
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def test_list_tagged_rpms_paths(self, ensure_connection_mock,
                                     event_from_opts_mock, stdout):
         args = ['tag', 'pkg', '--latest-n=3', '--rpms',
                 '--arch=x86_64', '--paths']
@@ -137,8 +135,8 @@ class TestCliListTagged(utils.CliTestCase):
 
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.util.eventFromOpts', return_value=None)
-    @mock.patch('koji_cli.commands.activate_session')
-    def test_list_tagged_sigs_paths(self, activate_session_mock,
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def test_list_tagged_sigs_paths(self, ensure_connection_mock,
                                     event_from_opts_mock, stdout):
         args = ['tag', 'pkg', '--latest-n=3', '--rpms', '--sigs',
                 '--arch=x86_64', '--paths']
@@ -148,8 +146,8 @@ class TestCliListTagged(utils.CliTestCase):
 
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.util.eventFromOpts', return_value=None)
-    @mock.patch('koji_cli.commands.activate_session')
-    def test_list_tagged_type(self, activate_session_mock,
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def test_list_tagged_type(self, ensure_connection_mock,
                               event_from_opts_mock, stdout):
         args = ['tag', 'pkg', '--latest-n=3', '--type=maven']
         self.session.listTagged.return_value = [{'id': 1,
@@ -163,8 +161,7 @@ class TestCliListTagged(utils.CliTestCase):
                                                  'maven_artifact_id': 'artifact'}]
 
         anon_handle_list_tagged(self.options, self.session, args)
-        activate_session_mock.assert_called_once_with(self.session,
-                                                      self.options)
+        ensure_connection_mock.assert_called_once_with(self.session)
         self.session.getTag.assert_called_once_with('tag', event=None)
         self.session.listTagged.assert_called_once_with('tag',
                                                         package='pkg',
@@ -179,8 +176,8 @@ class TestCliListTagged(utils.CliTestCase):
 
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.util.eventFromOpts', return_value=None)
-    @mock.patch('koji_cli.commands.activate_session')
-    def test_list_tagged_type_paths(self, activate_session_mock,
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def test_list_tagged_type_paths(self, ensure_connection_mock,
                               event_from_opts_mock, stdout):
         args = ['tag', 'pkg', '--latest-n=3', '--type=maven', '--paths']
         self.session.listTagged.return_value = [{'id': 1,
@@ -199,9 +196,10 @@ class TestCliListTagged(utils.CliTestCase):
                                     '----------------------------------------  --------------------  --------------------  --------------------  ----------------\n'
                                     '/mnt/koji/packages/packagename/version/1.el6/maven  tag                   group                 artifact              owner\n')
 
+    @mock.patch('koji_cli.commands.ensure_connection')
     @mock.patch('koji.util.eventFromOpts', return_value={'id': 1000,
                                                          'ts': 1000000.11})
-    def test_list_tagged_args(self, event_from_opts_mock):
+    def test_list_tagged_args(self, event_from_opts_mock, ensure_connection_mock):
         # Case 1, no argument
         expected = self.format_error_message(
             "A tag name must be specified")
@@ -233,7 +231,8 @@ class TestCliListTagged(utils.CliTestCase):
             self.options,
             self.session,
             ['tag', 'pkg1'],
-            stderr=expected)
+            stderr=expected,
+            activate_session=None)
 
     def test_handle_list_tagged_help(self):
         self.assert_help(
diff --git a/tests/test_cli/test_mock_config.py b/tests/test_cli/test_mock_config.py
index 028c806..5b77af2 100644
--- a/tests/test_cli/test_mock_config.py
+++ b/tests/test_cli/test_mock_config.py
@@ -61,9 +61,9 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
     @mock.patch('sys.stderr', new_callable=six.StringIO)
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.genMockConfig')
-    @mock.patch('koji_cli.commands.activate_session')
+    @mock.patch('koji_cli.commands.ensure_connection')
     def test_handle_mock_config_buildroot_option(
-            self, activate_session_mock, gen_config_mock, stdout, stderr):
+            self, ensure_connection_mock, gen_config_mock, stdout, stderr):
         """Test anon_handle_mock_config buildroot options"""
         arguments = []
         options = mock.MagicMock()
@@ -89,7 +89,8 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
             options,
             session,
             arguments,
-            stderr=expected)
+            stderr=expected,
+            activate_session=None)
 
         arguments = self.common_args + ['--buildroot', '1',
                                         '--name', self.progname]
@@ -117,9 +118,9 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
     @mock.patch('sys.stderr', new_callable=six.StringIO)
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.genMockConfig')
-    @mock.patch('koji_cli.commands.activate_session')
+    @mock.patch('koji_cli.commands.ensure_connection')
     def test_handle_mock_config_task_option(
-            self, activate_session_mock, gen_config_mock, stdout, stderr):
+            self, ensure_connection_mock, gen_config_mock, stdout, stderr):
         """Test  anon_handle_mock_config task options"""
         arguments = []
         task_id = 1001
@@ -138,7 +139,8 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
             options,
             session,
             arguments,
-            stderr=expected)
+            stderr=expected,
+            activate_session=None)
 
         arguments = ['--task', str(task_id)]
         expected = "No buildroots for task %s (or no such task)\n" % str(task_id)
@@ -176,9 +178,9 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
     @mock.patch('sys.stderr', new_callable=six.StringIO)
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.genMockConfig')
-    @mock.patch('koji_cli.commands.activate_session')
+    @mock.patch('koji_cli.commands.ensure_connection')
     def test_handle_mock_config_tag_option(
-            self, activate_session_mock, gen_config_mock, stdout, stderr):
+            self, ensure_connection_mock, gen_config_mock, stdout, stderr):
         """Test anon_handle_mock_config with tag option"""
         arguments = []
         tag = 'tag'
@@ -205,7 +207,8 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
             options,
             session,
             arguments,
-            stderr=expected)
+            stderr=expected,
+            activate_session=None)
 
         # return tag info
         session.getTag.return_value = tag
@@ -248,10 +251,10 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
     @mock.patch('sys.stderr', new_callable=six.StringIO)
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji.genMockConfig')
-    @mock.patch('koji_cli.commands.activate_session')
+    @mock.patch('koji_cli.commands.ensure_connection')
     @mock.patch('koji_cli.commands.open')
     def test_handle_mock_config_target_option(
-            self, openf, activate_session_mock, gen_config_mock, stdout, stderr):
+            self, openf, ensure_connection_mock, gen_config_mock, stdout, stderr):
         """Test anon_handle_mock_config with target option"""
         arguments = []
         arch = "x86_64"
@@ -284,7 +287,8 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
             options,
             session,
             arguments,
-            stderr=expected)
+            stderr=expected,
+            activate_session=None)
 
         session.getBuildTarget.return_value = target
         expected = "Could not get a repo for tag: %s\n" % target['build_tag_name']
@@ -321,7 +325,8 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
             self.progname, arch, **opts)
 
     @mock.patch('sys.stderr', new_callable=six.StringIO)
-    def test_handle_mock_config_errors(self, stderr):
+    @mock.patch('koji_cli.commands.ensure_connection')
+    def test_handle_mock_config_errors(self, ensure_connection_mock, stderr):
         """Test anon_handle_mock_config general error messages"""
         arguments = []
         options = mock.MagicMock()
@@ -338,7 +343,8 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
             options,
             session,
             arguments,
-            stderr=expected)
+            stderr=expected,
+            activate_session=None)
 
         # name is specified twice case
         arguments = [self.progname, '--name', 'name']
@@ -349,7 +355,8 @@ config_opts['macros']['%distribution'] = 'Koji Testing'
             options,
             session,
             arguments,
-            stderr=expected)
+            stderr=expected,
+            activate_session=None)
 
     def test_handle_mock_config_help(self):
         """Test anon_handle_mock_config help message full output"""
diff --git a/tests/test_cli/test_taskinfo.py b/tests/test_cli/test_taskinfo.py
index d16a220..f7afeb7 100644
--- a/tests/test_cli/test_taskinfo.py
+++ b/tests/test_cli/test_taskinfo.py
@@ -596,8 +596,10 @@ class TestTaskInfo(utils.CliTestCase):
 
     @mock.patch('sys.stdout', new_callable=six.StringIO)
     @mock.patch('koji_cli.commands.activate_session')
+    @mock.patch('koji_cli.commands.ensure_connection')
     def test_anon_handle_taskinfo(
             self,
+            ensure_connection_mock,
             activate_session_mock,
             stdout):
         """Test anon_handle_taskinfo function"""

From 9afe5e386662de205b5e91b635388e26f49213fd Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: May 22 2020 07:36:21 +0000
Subject: [PATCH 2/2] fix flake8


---

diff --git a/cli/koji b/cli/koji
index d4e1d93..1f79f66 100755
--- a/cli/koji
+++ b/cli/koji
@@ -121,7 +121,7 @@ def get_options():
     parser.add_option("--principal", help=_("specify a Kerberos principal to use"))
     parser.add_option("--krbservice", help=_("specify the Kerberos service name for the hub"))
     parser.add_option("--cert", help=_("specify a SSL cert to use"), metavar="FILE")
-    parser.add_option("--ca", help=SUPPRESS_HELP, metavar="FILE") # FIXME: remove in 1.24
+    parser.add_option("--ca", help=SUPPRESS_HELP, metavar="FILE")  # FIXME: remove in 1.24
     parser.add_option("--runas", help=_("run as the specified user (requires special privileges)"))
     parser.add_option("--user", help=_("specify user"))
     parser.add_option("--password", help=_("specify password"))

