Speed up job existence tests by fetching less info

Most methods are asserting that a job exists by calling get_job_info().
Unfortunately that might trigger on the server side a parse of the full
job history to gather the lastSuccessful, lastFailure build numbers.
Since Jenkins is heavily file based, the process is rather long on jobs
having thousands and thousands of build.

Since we only care about checking whether the job exists, introduce
job_exists() which is light on the server side.  To throw exception,
this also introduce the wrapper assert_job_exists() which can futher
customize the exception messages.

Updates all constructs such as:

    if not self.job_exists(job_name):
	raise JenkinsException(<some message> % (job_name))

To:

    self.assert_job_exists(to_name, <some message>)

That speed up operations tremendously whenever the targetted jobs are
very long.

Added coverage test.

Change-Id: I946dbb7a08a8bb172a369af3b2aedc07e3cea9e6
This commit is contained in:
Antoine Musso
2014-04-22 16:25:59 +02:00
parent 5d7ecd43e0
commit c5ccb8560c
2 changed files with 42 additions and 14 deletions

View File

@@ -132,6 +132,27 @@ class JenkinsTest(unittest.TestCase):
jenkins_mock.call_args[0][0].get_full_url(),
'http://example.com/job/TestJob')
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_assert_job_exists__job_missing(self, jenkins_mock):
jenkins_mock.side_effect = [
None,
]
j = jenkins.Jenkins('http://example.com/', 'test', 'test')
with self.assertRaises(jenkins.JenkinsException) as context_manager:
j.assert_job_exists('NonExistent')
self.assertEqual(
str(context_manager.exception),
'job[NonExistent] does not exist')
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_assert_job_exists__job_exists(self, jenkins_mock):
jenkins_mock.side_effect = [
json.dumps({'name': 'ExistingJob'}),
]
j = jenkins.Jenkins('http://example.com/', 'test', 'test')
j.assert_job_exists('ExistingJob')
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_create_job(self, jenkins_mock):
"""