def test_update_jobs_and_delete_old(self, builder_mock, delete_job_mock, get_jobs_mock, is_job_mock): """ Test update behaviour with --delete-old option Test update of jobs with the --delete-old option enabled, where only some jobs result in has_changed() to limit the number of times update_job is called, and have the get_jobs() method return additional jobs not in the input yaml to test that the code in cmd will call delete_job() after update_job() when '--delete-old' is set but only for the extra jobs. """ # set up some test data jobs = ['old_job001', 'old_job002'] extra_jobs = [{'name': name} for name in jobs] builder_obj = builder.Builder('http://jenkins.example.com', 'doesnot', 'matter', plugins_list={}) # get the instance created by mock and redirect some of the method # mocks to call real methods on a the above test object. b_inst = builder_mock.return_value b_inst.plugins_list = builder_obj.plugins_list b_inst.update_job.side_effect = builder_obj.update_job b_inst.delete_old_managed.side_effect = builder_obj.delete_old_managed def _get_jobs(): return builder_obj.parser.jobs + extra_jobs get_jobs_mock.side_effect = _get_jobs # override cache to ensure Jenkins.update_job called a limited number # of times self.cache_mock.return_value.has_changed.side_effect = ([True] * 2 + [False] * 2) path = os.path.join(self.fixtures_path, 'cmd-002.yaml') args = self.parser.parse_args(['update', '--delete-old', path]) with mock.patch('jenkins_jobs.builder.Jenkins.update_job') as update: with mock.patch('jenkins_jobs.builder.Jenkins.is_managed', return_value=True): cmd.execute(args, self.config) self.assertEquals( 2, update.call_count, "Expected Jenkins.update_job to be called '%d' " "times, got '%d' calls instead.\n" "Called with: %s" % (2, update.call_count, update.mock_calls)) calls = [mock.call(name) for name in jobs] self.assertEquals( 2, delete_job_mock.call_count, "Expected Jenkins.delete_job to be called '%d' " "times got '%d' calls instead.\n" "Called with: %s" % (2, delete_job_mock.call_count, delete_job_mock.mock_calls)) delete_job_mock.assert_has_calls(calls, any_order=True)
def test_delete_using_glob_params(self, delete_job_mock): """ Test handling the deletion of multiple Jenkins jobs using the glob parameters feature. """ args = self.parser.parse_args([ 'delete', '--path', os.path.join(self.fixtures_path, 'cmd-002.yaml'), '*bar*' ]) cmd.execute(args, self.config) calls = [mock.call('bar001'), mock.call('bar002')] delete_job_mock.assert_has_calls(calls, any_order=True) self.assertEquals( delete_job_mock.call_count, len(calls), "Jenkins.delete_job() was called '%s' times when " "expected '%s'" % (delete_job_mock.call_count, len(calls)))
def test_delete_using_glob_params(self, delete_job_mock): """ Test handling the deletion of multiple Jenkins jobs using the glob parameters feature. """ args = self.parser.parse_args(['delete', '--path', os.path.join(self.fixtures_path, 'cmd-002.yaml'), '*bar*']) cmd.execute(args, self.config) calls = [mock.call('bar001'), mock.call('bar002')] delete_job_mock.assert_has_calls(calls, any_order=True) self.assertEquals(delete_job_mock.call_count, len(calls), "Jenkins.delete_job() was called '%s' times when " "expected '%s'" % (delete_job_mock.call_count, len(calls)))
def test_update_jobs_and_delete_old(self, builder_mock, delete_job_mock, get_jobs_mock, is_job_mock): """ Test update behaviour with --delete-old option Test update of jobs with the --delete-old option enabled, where only some jobs result in has_changed() to limit the number of times update_job is called, and have the get_jobs() method return additional jobs not in the input yaml to test that the code in cmd will call delete_job() after update_job() when '--delete-old' is set but only for the extra jobs. """ # set up some test data jobs = ['old_job001', 'old_job002'] extra_jobs = [{'name': name} for name in jobs] builder_obj = builder.Builder('http://jenkins.example.com', 'doesnot', 'matter', plugins_list={}) # get the instance created by mock and redirect some of the method # mocks to call real methods on a the above test object. b_inst = builder_mock.return_value b_inst.plugins_list = builder_obj.plugins_list b_inst.update_jobs.side_effect = builder_obj.update_jobs b_inst.delete_old_managed.side_effect = builder_obj.delete_old_managed def _get_jobs(): return builder_obj.parser.jobs + extra_jobs get_jobs_mock.side_effect = _get_jobs # override cache to ensure Jenkins.update_job called a limited number # of times self.cache_mock.return_value.has_changed.side_effect = ( [True] * 2 + [False] * 2) path = os.path.join(self.fixtures_path, 'cmd-002.yaml') args = ['--conf', self.default_config_file, 'update', '--delete-old', path] with mock.patch('jenkins_jobs.builder.Jenkins.update_job') as update: with mock.patch('jenkins_jobs.builder.Jenkins.is_managed', return_value=True): self.execute_jenkins_jobs_with_args(args) self.assertEquals(2, update.call_count, "Expected Jenkins.update_job to be called '%d' " "times, got '%d' calls instead.\n" "Called with: %s" % (2, update.call_count, update.mock_calls)) calls = [mock.call(name) for name in jobs] self.assertEqual(2, delete_job_mock.call_count, "Expected Jenkins.delete_job to be called '%d' " "times got '%d' calls instead.\n" "Called with: %s" % (2, delete_job_mock.call_count, delete_job_mock.mock_calls)) delete_job_mock.assert_has_calls(calls, any_order=True)
def test_update_jobs_and_delete_old( self, jenkins_delete_job, jenkins_reconfig_job, jenkins_get_all_jobs, jenkins_job_exists, ): """Test update behaviour with --delete-old option. * mock out a call to jenkins.Jenkins.get_jobs() to return a known list of job names. * mock out a call to jenkins.Jenkins.reconfig_job() and jenkins.Jenkins.delete_job() to detect calls being made to determine that JJB does correctly delete the jobs it should delete when passed a specific set of inputs. * mock out a call to jenkins.Jenkins.job_exists() to always return True. """ yaml_jobs = ["bar001", "bar002", "baz001", "bam001"] extra_jobs = ["old_job001", "old_job002", "unmanaged"] path = os.path.join(self.fixtures_path, "cmd-002.yaml") args = [ "--conf", self.default_config_file, "update", "--delete-old", path ] jenkins_get_all_jobs.return_value = [{ "fullname": name } for name in yaml_jobs + extra_jobs] with mock.patch( "jenkins_jobs.builder.JenkinsManager.is_managed", side_effect=(lambda name: name != "unmanaged"), ): self.execute_jenkins_jobs_with_args(args) jenkins_reconfig_job.assert_has_calls( [mock.call(job_name, mock.ANY) for job_name in yaml_jobs], any_order=True) calls = [mock.call(name) for name in extra_jobs if name != "unmanaged"] jenkins_delete_job.assert_has_calls(calls) # to ensure only the calls we expected were made, have to check # there were no others, as no API call for assert_has_only_calls self.assertEqual(jenkins_delete_job.call_count, len(calls))
def test_update_jobs_and_delete_old( self, jenkins_delete_job, jenkins_reconfig_job, jenkins_get_jobs, jenkins_job_exists, ): """ Test update behaviour with --delete-old option * mock out a call to jenkins.Jenkins.get_jobs() to return a known list of job names. * mock out a call to jenkins.Jenkins.reconfig_job() and jenkins.Jenkins.delete_job() to detect calls being made to determine that JJB does correctly delete the jobs it should delete when passed a specific set of inputs. * mock out a call to jenkins.Jenkins.job_exists() to always return True. """ jobs = ['old_job001', 'old_job002', 'unmanaged'] extra_jobs = [{'name': name} for name in jobs] path = os.path.join(self.fixtures_path, 'cmd-002.yaml') args = [ '--conf', self.default_config_file, 'update', '--delete-old', path ] jenkins_get_jobs.return_value = extra_jobs with mock.patch('jenkins_jobs.builder.Jenkins.is_managed', side_effect=(lambda name: name != 'unmanaged')): self.execute_jenkins_jobs_with_args(args) jenkins_reconfig_job.assert_has_calls([ mock.call(job_name, mock.ANY) for job_name in ['bar001', 'bar002', 'baz001', 'bam001'] ], any_order=True) jenkins_delete_job.assert_has_calls( [mock.call(name) for name in jobs if name != 'unmanaged'])
def test_update_jobs_and_delete_old(self, jenkins_delete_job, jenkins_reconfig_job, jenkins_get_jobs, jenkins_job_exists, ): """ Test update behaviour with --delete-old option * mock out a call to jenkins.Jenkins.get_jobs() to return a known list of job names. * mock out a call to jenkins.Jenkins.reconfig_job() and jenkins.Jenkins.delete_job() to detect calls being made to determine that JJB does correctly delete the jobs it should delete when passed a specific set of inputs. * mock out a call to jenkins.Jenkins.job_exists() to always return True. """ yaml_jobs = ['bar001', 'bar002', 'baz001', 'bam001'] extra_jobs = ['old_job001', 'old_job002', 'unmanaged'] path = os.path.join(self.fixtures_path, 'cmd-002.yaml') args = ['--conf', self.default_config_file, 'update', '--delete-old', path] jenkins_get_jobs.return_value = [{'name': name} for name in yaml_jobs + extra_jobs] with mock.patch('jenkins_jobs.builder.JenkinsManager.is_managed', side_effect=(lambda name: name != 'unmanaged')): self.execute_jenkins_jobs_with_args(args) jenkins_reconfig_job.assert_has_calls( [mock.call(job_name, mock.ANY) for job_name in yaml_jobs], any_order=True ) calls = [mock.call(name) for name in extra_jobs if name != 'unmanaged'] jenkins_delete_job.assert_has_calls(calls) # to ensure only the calls we expected were made, have to check # there were no others, as no API call for assert_has_only_calls self.assertEquals(jenkins_delete_job.call_count, len(calls))
def test_delete_using_glob_params(self, delete_job_mock): """ Test handling the deletion of multiple Jenkins jobs using the glob parameters feature. """ args = [ "--conf", self.default_config_file, "delete", "--path", os.path.join(self.fixtures_path, "cmd-002.yaml"), "*bar*", ] self.execute_jenkins_jobs_with_args(args) calls = [mock.call("bar001"), mock.call("bar002")] delete_job_mock.assert_has_calls(calls, any_order=True) self.assertEqual( delete_job_mock.call_count, len(calls), "Jenkins.delete_job() was called '%s' times when " "expected '%s'" % (delete_job_mock.call_count, len(calls)), )
def test_update_jobs(self, jenkins_reconfig_job, jenkins_get_jobs, jenkins_job_exists, ): """ Test update_job is called """ path = os.path.join(self.fixtures_path, 'cmd-002.yaml') args = ['--conf', self.default_config_file, 'update', path] self.execute_jenkins_jobs_with_args(args) jenkins_reconfig_job.assert_has_calls( [mock.call(job_name, mock.ANY) for job_name in ['bar001', 'bar002', 'baz001', 'bam001']], any_order=True )
def test_update_jobs(self, jenkins_reconfig_job, jenkins_get_jobs, jenkins_job_exists): """ Test update_job is called """ path = os.path.join(self.fixtures_path, "cmd-002.yaml") args = ["--conf", self.default_config_file, "update", path] self.execute_jenkins_jobs_with_args(args) jenkins_reconfig_job.assert_has_calls( [ mock.call(job_name, mock.ANY) for job_name in ["bar001", "bar002", "baz001", "bam001"] ], any_order=True, )