'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.'

@mock.patch('jenkins_jobs.builder.jenkins.Jenkins.job_exists')
@mock.patch('jenkins_jobs.builder.jenkins.Jenkins.get_jobs')
@mock.patch('jenkins_jobs.builder.jenkins.Jenkins.reconfig_job')
@mock.patch('jenkins_jobs.builder.jenkins.Jenkins.delete_job')
def test_update_jobs_and_delete_old(self, jenkins_delete_job, jenkins_reconfig_job, jenkins_get_jobs, jenkins_job_exists):

    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)
    self.assertEquals(jenkins_delete_job.call_count, len(calls))
