From 8a099b7429038f59665685c3c3ab04d8a96928a1 Mon Sep 17 00:00:00 2001 From: Khai Do Date: Fri, 13 Feb 2015 09:34:25 -0800 Subject: [PATCH] Add a jobs_count method The jobs_count method will return the total number of jobs on the jenkins server. Change-Id: I21e873646ed943d4fb4fdd61bd3b7d420fa1da55 --- jenkins/__init__.py | 7 +++++++ tests/test_jenkins.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/jenkins/__init__.py b/jenkins/__init__.py index 6939bae..1b240f0 100644 --- a/jenkins/__init__.py +++ b/jenkins/__init__.py @@ -544,6 +544,13 @@ class Jenkins(object): if self.get_job_name(name) == name: return True + def jobs_count(self): + '''Get the number of jobs on the Jenkins server + + :returns: Total number of jobs, ``int`` + ''' + return len(self.get_jobs()) + def assert_job_exists(self, name, exception_message='job[%s] does not exist'): '''Raise an exception if a job does not exist diff --git a/tests/test_jenkins.py b/tests/test_jenkins.py index 9341203..150ece5 100644 --- a/tests/test_jenkins.py +++ b/tests/test_jenkins.py @@ -650,6 +650,18 @@ class JenkinsTest(unittest.TestCase): 'Error communicating with server[http://example.com/]:' ' empty response') + @patch.object(jenkins.Jenkins, 'jenkins_open') + def test_jobs_count(self, jenkins_mock): + jobs = [ + {u'url': u'http://localhost:8080/job/guava/', u'color': u'notbuilt', u'name': u'guava'}, + {u'url': u'http://localhost:8080/job/kiwi/', u'color': u'blue', u'name': u'kiwi'}, + {u'url': u'http://localhost:8080/job/lemon/', u'color': u'red', u'name': u'lemon'} + ] + job_info_to_return = {u'jobs': jobs} + jenkins_mock.return_value = json.dumps(job_info_to_return) + j = jenkins.Jenkins('http://example.com/', 'test', 'test') + self.assertEqual(j.jobs_count(), 3) + @patch.object(jenkins.Jenkins, 'jenkins_open') def test_get_jobs(self, jenkins_mock): jobs = {