From 899bc6c946030b0f3ddbe591e1dc6dd0af99eafb Mon Sep 17 00:00:00 2001 From: Khai Do Date: Fri, 9 Oct 2015 13:03:45 -0700 Subject: [PATCH] fix for python 3, decode job output Change Ie952617a34c0719e forced utf-8 format but forgot to decode the xml when passing to update_job. This broke jjb for python 3: File "/Users/khaido/PycharmProjects/jenkins-job-builder /jenkins_jobs/builder.py", line 134, in update_job self.jenkins.create_job(job_name, xml) File "/Users/khaido/PycharmProjects/jenkins-job-builder/.tox /py34/lib/python3.4/site-packages/jenkins/__init__.py", line 852, in create_job config_xml.encode('utf-8'), DEFAULT_HEADERS)) AttributeError: 'function' object has no attribute 'encode' Change-Id: Iceda46214bf4709ccd8141ef654cf3ec81e8af06 --- jenkins_jobs/builder.py | 2 +- tests/cmd/subcommands/test_update.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/jenkins_jobs/builder.py b/jenkins_jobs/builder.py index c48d280f6..0da3b57af 100644 --- a/jenkins_jobs/builder.py +++ b/jenkins_jobs/builder.py @@ -352,7 +352,7 @@ class Builder(object): self.cache.set(job.name, old_md5) if self.cache.has_changed(job.name, md5) or self.ignore_cache: - self.jenkins.update_job(job.name, job.output()) + self.jenkins.update_job(job.name, job.output().decode('utf-8')) updated_jobs += 1 self.cache.set(job.name, md5) else: diff --git a/tests/cmd/subcommands/test_update.py b/tests/cmd/subcommands/test_update.py index 18b573752..f5ee0c226 100644 --- a/tests/cmd/subcommands/test_update.py +++ b/tests/cmd/subcommands/test_update.py @@ -15,6 +15,7 @@ # under the License. import os +import six from jenkins_jobs import cmd from jenkins_jobs import builder @@ -39,6 +40,26 @@ class UpdateTests(CmdTestsBase): cmd.execute(args, self.config) update_job_mock.assert_called_with([path], []) + @mock.patch('jenkins_jobs.builder.Jenkins.is_job', return_value=True) + @mock.patch('jenkins_jobs.builder.Jenkins.get_jobs') + @mock.patch('jenkins_jobs.builder.Jenkins.get_job_md5') + @mock.patch('jenkins_jobs.builder.Jenkins.update_job') + def test_update_jobs_decode_job_output(self, update_job_mock, + get_job_md5_mock, get_jobs_mock, + is_job_mock): + """ + Test that job xml output has been decoded before attempting to update + """ + # don't care about the value returned here + update_job_mock.return_value = ([], 0) + + path = os.path.join(self.fixtures_path, 'cmd-002.yaml') + args = self.parser.parse_args(['update', path]) + + cmd.execute(args, self.config) + self.assertTrue(isinstance(update_job_mock.call_args[0][1], + six.text_type)) + @mock.patch('jenkins_jobs.builder.Jenkins.is_job', return_value=True) @mock.patch('jenkins_jobs.builder.Jenkins.get_jobs') @mock.patch('jenkins_jobs.builder.Builder.delete_job')