Blob Blame Raw
From 7f8731a89c3154abe845700e900f139eec381e04 Mon Sep 17 00:00:00 2001
From: Chenxiong Qi <cqi@redhat.com>
Date: Thu, 24 Aug 2017 22:12:14 +0800
Subject: [PATCH] Fix kojiprofile selection in cliClient.container_build_koji

kojiprofile or deprecated kojiconfig should be selected according to
whether current client is compatible with kojiconfig or not. Tests are
added for command container-build with koji.

Also fix the default value of koji_config_type and add tests to ensure
_compat_kojiconfig is set correctly.

Signed-off-by: Chenxiong Qi <cqi@redhat.com>
---
 pyrpkg/__init__.py                             |   2 +-
 pyrpkg/cli.py                                  |  27 ++---
 tests/fixtures/rpkg-deprecated-kojiconfig.conf |  11 ++
 tests/test_cli.py                              | 139 +++++++++++++++++++++++++
 4 files changed, 165 insertions(+), 14 deletions(-)
 create mode 100644 tests/fixtures/rpkg-deprecated-kojiconfig.conf

diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py
index 1c80d2e..d5f04ba 100644
--- a/pyrpkg/__init__.py
+++ b/pyrpkg/__init__.py
@@ -66,7 +66,7 @@ class Commands(object):
     def __init__(self, path, lookaside, lookasidehash, lookaside_cgi,
                  gitbaseurl, anongiturl, branchre, kojiconfig,
                  build_client,
-                 koji_config_type='kojiconfig', user=None,
+                 koji_config_type='config', user=None,
                  dist=None, target=None, quiet=False,
                  distgit_namespaced=False, realms=None, lookaside_namespaced=False):
         """Init the object and some configuration details."""
diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py
index 35d9878..90874fc 100644
--- a/pyrpkg/cli.py
+++ b/pyrpkg/cli.py
@@ -1198,21 +1198,22 @@ see API KEY section of copr-cli(1) man page.
                   "Using %(option)s from [%(root.section)s]"
         err_args = {"plugin.section": section_name, "root.section": self.name}
 
-        if self.config.has_option(section_name, "kojiconfig"):
-            kojiconfig = self.config.get(section_name, "kojiconfig")
-            kojiprofile = None
-        else:
-            err_args["option"] = "kojiconfig"
-            self.log.debug(err_msg % err_args)
-            kojiprofile = self.config.get(self.name, "kojiconfig")
+        kojiconfig = kojiprofile = None
 
-        if self.config.has_option(section_name, "kojiprofile"):
-            kojiconfig = None
-            kojiprofile = self.config.get(section_name, "kojiprofile")
+        if self.cmd._compat_kojiconfig:
+            if self.config.has_option(section_name, "kojiconfig"):
+                kojiconfig = self.config.get(section_name, "kojiconfig")
+            else:
+                err_args["option"] = "kojiconfig"
+                self.log.debug(err_msg % err_args)
+                kojiconfig = self.config.get(self.name, "kojiconfig")
         else:
-            err_args["option"] = "kojiprofile"
-            self.log.debug(err_msg % err_args)
-            kojiprofile = self.config.get(self.name, "kojiprofile")
+            if self.config.has_option(section_name, "kojiprofile"):
+                kojiprofile = self.config.get(section_name, "kojiprofile")
+            else:
+                err_args["option"] = "kojiprofile"
+                self.log.debug(err_msg % err_args)
+                kojiprofile = self.config.get(self.name, "kojiprofile")
 
         if self.config.has_option(section_name, "build_client"):
             build_client = self.config.get(section_name, "build_client")
diff --git a/tests/fixtures/rpkg-deprecated-kojiconfig.conf b/tests/fixtures/rpkg-deprecated-kojiconfig.conf
new file mode 100644
index 0000000..adcd1da
--- /dev/null
+++ b/tests/fixtures/rpkg-deprecated-kojiconfig.conf
@@ -0,0 +1,11 @@
+[rpkg]
+lookaside = http://localhost/repo/pkgs
+lookasidehash = md5
+lookaside_cgi = https://localhost/repo/pkgs/upload.cgi
+gitbaseurl = ssh://%(user)s@localhost/%(module)s
+anongiturl = git://localhost/%(module)s
+branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$
+kojiconfig = /path/to/koji.conf
+build_client = koji
+clone_config =
+  bz.default-component %(module)s
diff --git a/tests/test_cli.py b/tests/test_cli.py
index f4b44ec..1372411 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -16,6 +16,7 @@ from six.moves import StringIO
 import git
 import pyrpkg.cli
 
+import utils
 from mock import patch
 from utils import CommandTestCase
 from pyrpkg import rpkgError
@@ -111,6 +112,144 @@ class TestModuleNameOption(CliTestCase):
         self.assertEqual(cmd.ns_module_name, 'user/project/foo')
 
 
+class TestKojiConfigBackwardCompatibility(CliTestCase):
+    """Test backward compatibility of kojiconfig and kojiprofile
+
+    Remove this test case after deprecated kojiconfig is removed eventually.
+    """
+
+    @patch('pyrpkg.Commands._deprecated_read_koji_config')
+    @patch('pyrpkg.koji.read_config')
+    def test_use_deprecated_kojiconfig(self,
+                                       read_config,
+                                       _deprecated_read_koji_config):
+        cli_cmd = ['rpkg', '--path', self.cloned_repo_path, 'build']
+
+        cfg_file = os.path.join(os.path.dirname(__file__),
+                                'fixtures',
+                                'rpkg-deprecated-kojiconfig.conf')
+
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli(cfg_file)
+
+        cli.cmd.read_koji_config()
+
+        self.assertFalse(hasattr(cli.cmd, 'kojiprofile'))
+        self.assertEqual(utils.kojiconfig, cli.cmd.kojiconfig)
+        self.assertTrue(cli.cmd._compat_kojiconfig)
+
+        read_config.assert_not_called()
+        _deprecated_read_koji_config.assert_called_once()
+
+    @patch('pyrpkg.Commands._deprecated_read_koji_config')
+    @patch('pyrpkg.koji.read_config')
+    def test_use_kojiprofile(self, read_config, _deprecated_read_koji_config):
+        cli_cmd = ['rpkg', '--path', self.cloned_repo_path, 'build']
+
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli()
+
+        cli.cmd.read_koji_config()
+
+        self.assertFalse(hasattr(cli.cmd, 'kojiconfig'))
+        self.assertEqual(utils.kojiprofile, cli.cmd.kojiprofile)
+        self.assertFalse(cli.cmd._compat_kojiconfig)
+
+        read_config.assert_called_once_with(utils.kojiprofile)
+        _deprecated_read_koji_config.assert_not_called()
+
+
+class TestContainerBuildWithKoji(CliTestCase):
+    """Test container_build with koji"""
+
+    def setUp(self):
+        super(TestContainerBuildWithKoji, self).setUp()
+        self.checkout_branch(git.Repo(self.cloned_repo_path), 'eng-rhel-7')
+
+    @patch('pyrpkg.Commands.container_build_koji')
+    def test_using_kojiprofile(self, container_build_koji):
+        cli_cmd = ['rpkg', '--path', self.cloned_repo_path,
+                   'container-build', '--build-with', 'koji']
+
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli()
+            cli.container_build()
+
+        container_build_koji.assert_called_once_with(
+            False,
+            opts={
+                'scratch': False,
+                'quiet': False,
+                'yum_repourls': None,
+                'git_branch': 'eng-rhel-7',
+            },
+            kojiconfig=None,
+            kojiprofile='koji',
+            build_client=utils.build_client,
+            koji_task_watcher=cli._watch_koji_tasks,
+            nowait=False
+        )
+
+    @patch('pyrpkg.Commands.container_build_koji')
+    def test_override_target(self, container_build_koji):
+        cli_cmd = ['rpkg', '--path', self.cloned_repo_path, 'container-build',
+                   '--target', 'f25-docker-candidate', '--build-with', 'koji']
+
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli()
+            cli.container_build()
+
+        self.assertEqual('f25-docker-candidate', cli.cmd._target)
+        container_build_koji.assert_called_once_with(
+            True,
+            opts={
+                'scratch': False,
+                'quiet': False,
+                'yum_repourls': None,
+                'git_branch': 'eng-rhel-7',
+            },
+            kojiconfig=None,
+            kojiprofile='koji',
+            build_client=utils.build_client,
+            koji_task_watcher=cli._watch_koji_tasks,
+            nowait=False
+        )
+
+    @patch('pyrpkg.Commands.container_build_koji')
+    def test_using_deprecated_kojiconfig(self, container_build_koji):
+        """test_build_using_deprecated_kojiconfig
+
+        This is for ensuring container_build works with deprecated kojiconfig.
+        This test can be delete after kojiconfig is removed eventually.
+        """
+        cli_cmd = ['rpkg', '--path', self.cloned_repo_path,
+                   '--module-name', 'mycontainer',
+                   'container-build', '--build-with', 'koji']
+
+        cfg_file = os.path.join(os.path.dirname(__file__),
+                                'fixtures',
+                                'rpkg-deprecated-kojiconfig.conf')
+
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli(cfg_file)
+            cli.container_build()
+
+        container_build_koji.assert_called_once_with(
+            False,
+            opts={
+                'scratch': False,
+                'quiet': False,
+                'yum_repourls': None,
+                'git_branch': 'eng-rhel-7',
+            },
+            kojiconfig='/path/to/koji.conf',
+            kojiprofile=None,
+            build_client=utils.build_client,
+            koji_task_watcher=cli._watch_koji_tasks,
+            nowait=False
+        )
+
+
 class TestClog(CliTestCase):
 
     def setUp(self):
-- 
2.9.5