From 46adf9b7c805bee68dfeedfb181d9273a18e893a Mon Sep 17 00:00:00 2001 From: salimfadhley Date: Mon, 10 Jun 2013 23:59:29 +0100 Subject: [PATCH] fixes for getting job configs --- jenkinsapi/job.py | 29 +++++++++++++++++++---------- jenkinsapi_tests/systests/test_jenkins.py | 11 +++++++++-- jenkinsapi_tests/unittests/test_job.py | 3 +++ misc/jenkinsapi.sublime-project | 14 ++++++++++---- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/jenkinsapi/job.py b/jenkinsapi/job.py index cdfab6a..f61d269 100644 --- a/jenkinsapi/job.py +++ b/jenkinsapi/job.py @@ -10,7 +10,7 @@ from jenkinsapi.jenkinsbase import JenkinsBase from jenkinsapi import exceptions from jenkinsapi.mutable_jenkins_thing import MutableJenkinsThing -from jenkinsapi.exceptions import NoBuildData, NotFound, NotInQueue +from jenkinsapi.exceptions import NoBuildData, NotFound, NotInQueue, WillNotBuild log = logging.getLogger(__name__) @@ -65,7 +65,7 @@ class Job(JenkinsBase, MutableJenkinsThing): self._element_tree = ET.fromstring(self._config) return self._element_tree - def get_build_triggerurl(self, token=None, params=None): + def get_build_triggerurl(self): return "%s/build" % self.baseurl def invoke(self, securitytoken=None, block=False, skip_if_running=False, invoke_pre_check_delay=3, invoke_block_delay=15, params=None, cause=None): @@ -78,7 +78,8 @@ class Job(JenkinsBase, MutableJenkinsThing): params = params and dict(params.items()) or {} if self.is_queued(): - log.warn( "Will not request new build because %s is already queued", self.name ) + raise WillNotBuild('%s is already queued' % repr(self)) + elif self.is_running(): if skip_if_running: log.warn( "Will not request new build because %s is already running", self.name ) @@ -86,6 +87,7 @@ class Job(JenkinsBase, MutableJenkinsThing): log.warn("Will re-schedule %s even though it is already running", self.name ) original_build_no = self.get_last_buildnumber() log.info( "Attempting to start %s on %s", self.name, repr(self.get_jenkins_obj()) ) + url = self.get_build_triggerurl() if cause: @@ -98,12 +100,12 @@ class Job(JenkinsBase, MutableJenkinsThing): assert len( response.text ) > 0 if invoke_pre_check_delay > 0: - log.info("Waiting for %is to allow Jenkins to catch up" % invoke_pre_check_delay ) + log.info("Waiting for %is to allow Jenkins to catch up" , invoke_pre_check_delay ) sleep( invoke_pre_check_delay ) if block: total_wait = 0 while self.is_queued(): - log.info( "Waited %is for %s to begin..." % ( total_wait, self.name )) + log.info( "Waited %is for %s to begin...", total_wait, self.name ) sleep( invoke_block_delay ) total_wait += invoke_block_delay if self.is_running(): @@ -112,11 +114,11 @@ class Job(JenkinsBase, MutableJenkinsThing): assert self.get_last_buildnumber() > original_build_no, "Job does not appear to have run." else: if self.is_queued(): - log.info( "%s has been queued." % self.name ) + log.info( "%s has been queued.", self.name ) elif self.is_running(): - log.info( "%s is running." % self.name ) + log.info( "%s is running.", self.name ) elif original_build_no < self.get_last_buildnumber(): - log.info( "%s has completed." % self.name ) + log.info( "%s has completed.", self.name ) else: raise AssertionError("The job did not schedule.") @@ -170,6 +172,12 @@ class Job(JenkinsBase, MutableJenkinsThing): """ return reversed( sorted( self.get_build_dict().keys() ) ) + def get_next_build_number(self): + """ + Return the next build number that Jenkins will assign. + """ + return self._data.get('nextBuildNumber', 0) + def get_last_good_build( self ): """ Get the last good build @@ -238,12 +246,13 @@ class Job(JenkinsBase, MutableJenkinsThing): if build is not None: return build.is_running() except NoBuildData: - log.info("No build info available for %s, assuming not running." % str(self) ) + log.info("No build info available for %s, assuming not running.", str(self) ) return False def get_config(self): '''Returns the config.xml from the job''' - return self.jenkins.requester.get_and_confirm_status("%(baseurl)s/config.xml" % self.__dict__) + response = self.jenkins.requester.get_and_confirm_status("%(baseurl)s/config.xml" % self.__dict__) + return response.text def load_config(self): self._config = self.get_config() diff --git a/jenkinsapi_tests/systests/test_jenkins.py b/jenkinsapi_tests/systests/test_jenkins.py index a748ba0..6fca863 100644 --- a/jenkinsapi_tests/systests/test_jenkins.py +++ b/jenkinsapi_tests/systests/test_jenkins.py @@ -2,6 +2,7 @@ System tests for `jenkinsapi.jenkins` module. ''' import unittest +from jenkinsapi.build import Build from jenkinsapi_tests.test_utils.random_strings import random_string from jenkinsapi_tests.systests.base import BaseSystemTest, EMPTY_JOB_CONFIG @@ -13,11 +14,17 @@ class JobTests(BaseSystemTest): self.jenkins.create_job(job_name, EMPTY_JOB_CONFIG) self.assertJobIsPresent(job_name) + def test_get_job_config(self): + job_name = 'config_%s' % random_string() + self.jenkins.create_job(job_name, EMPTY_JOB_CONFIG) + self.assertJobIsPresent(job_name) + config = self.jenkins[job_name].get_config() + self.assertEquals(config.strip(), EMPTY_JOB_CONFIG.strip()) + def test_invoke_job(self): job_name = 'create_%s' % random_string() job = self.jenkins.create_job(job_name, EMPTY_JOB_CONFIG) - - build = job.invoke() + job.invoke() def test_get_jobs_list(self): job1_name = 'first_%s' % random_string() diff --git a/jenkinsapi_tests/unittests/test_job.py b/jenkinsapi_tests/unittests/test_job.py index e7eb4e5..2eff0ed 100644 --- a/jenkinsapi_tests/unittests/test_job.py +++ b/jenkinsapi_tests/unittests/test_job.py @@ -56,6 +56,9 @@ class TestJob(unittest.TestCase): self.j.id() self.assertEquals(self.j.name, 'foo') + def testNextBuildNumber(self): + self.assertEquals(self.j.get_next_build_number(), 4) + def test_special_urls(self): self.assertEquals(self.j.baseurl, 'http://halob:8080/job/foo') diff --git a/misc/jenkinsapi.sublime-project b/misc/jenkinsapi.sublime-project index b8d238c..0fc125c 100644 --- a/misc/jenkinsapi.sublime-project +++ b/misc/jenkinsapi.sublime-project @@ -6,17 +6,23 @@ "file_exclude_patterns": ["*.pyc"] } ], - - "settings": - { - "python_test_runner": + + "settings": + { + "python_test_runner": { "before_test": "source .env/bin/activate", "after_test": "deactivate", "test_root": "src/pythonmoo/tests", "test_delimeter": ":", "test_command": "nosetests" + }, + "pylinter": + { + "ignore":["C"], + "use_icons":true } + }, "build_systems": -- 2.7.4