From: salimfadhley Date: Mon, 24 Jun 2013 23:09:48 +0000 (+0100) Subject: inital somewhat working demo of invocations X-Git-Tag: v0.2.23~126^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=465d1077afe9e055d386bd4f2bb0fde8468d334e;p=tools%2Fpython-jenkinsapi.git inital somewhat working demo of invocations --- diff --git a/jenkinsapi/invocation.py b/jenkinsapi/invocation.py index 1d39c63..36c1a4c 100644 --- a/jenkinsapi/invocation.py +++ b/jenkinsapi/invocation.py @@ -1,5 +1,6 @@ import time -from jenkinsapi.exceptions import UnknownQueueItem +import datetime +from jenkinsapi.exceptions import UnknownQueueItem, TimeOut class Invocation(object): """ @@ -51,43 +52,52 @@ class Invocation(object): def get_build(self): return self.job[self.get_build_number()] - def block_until_not_queued(self): + def block_until_not_queued(self, timeout, delay): + self.__block(lambda : self.is_queued(), False, timeout, delay ) + + def block_until_completed(self, timeout, delay): + self.__block(lambda : self.is_running(), False, timeout, delay ) + + @staticmethod + def __block(fn, expectation, timeout, delay=2): + startTime = datetime.datetime.now() + endTime = startTime + datetime.timedelta(seconds=timeout) while True: - if not self.is_queued(): + if fn() == expectation: break - time.sleep(0.5) + else: + time.sleep(delay) + if datetime.datetime.now() > endTime: + raise TimeOut() - def block(self, until='completed'): + def block(self, until='completed', timeout=200, delay=2): """ Block this item until a condition is met. Setting until to 'running' blocks the item until it is running (i.e. it's no longer queued) """ assert until in ['completed', 'not_queued'], 'Unknown block condition: %s' % until - self.block_until_not_queued() + self.block_until_not_queued(timeout, delay) if until=='completed': - self.block_until_completed() + self.block_until_completed(timeout, delay) def stop(self): """ Stop this item, whether it is on the queue or blocked. """ + self.get_build().stop() def is_queued(self): """ Returns True if this item is on the queue """ - - import ipdb - ipdb.set_trace - - return True + return self.job.is_queued() def is_running(self): """ Returns True if this item is executing now """ - return True + return self.get_build().is_running() def is_queued_or_running(self): return self.is_queued() or self.is_running() @@ -97,9 +107,4 @@ class Invocation(object): If the item is queued it will return that QueueItem, otherwise it will raise an exception. """ - - def get_build(self): - """ - If the item is building it will return a Build object, otherwise it will - raise an exception. - """ + return self.queue_item diff --git a/jenkinsapi_tests/systests/job_configs.py b/jenkinsapi_tests/systests/job_configs.py index 7b6a4fb..4cea72b 100644 --- a/jenkinsapi_tests/systests/job_configs.py +++ b/jenkinsapi_tests/systests/job_configs.py @@ -22,7 +22,7 @@ EMPTY_JOB = '''\ '''.strip() -LONG_RUNNING_JOB = JOB_XML = """ +LONG_RUNNING_JOB = """ @@ -44,3 +44,26 @@ LONG_RUNNING_JOB = JOB_XML = """ """.strip() + +SHORTISH_JOB = """ + + + + + false + + + true + false + false + false + + false + + + ping -c 10 localhost + + + + +""".strip() diff --git a/jenkinsapi_tests/systests/test_invocation.py b/jenkinsapi_tests/systests/test_invocation.py index e0ce220..b68d598 100644 --- a/jenkinsapi_tests/systests/test_invocation.py +++ b/jenkinsapi_tests/systests/test_invocation.py @@ -5,8 +5,8 @@ import unittest from jenkinsapi.build import Build from jenkinsapi.invocation import Invocation from jenkinsapi_tests.systests.base import BaseSystemTest -from jenkinsapi_tests.systests.job_configs import LONG_RUNNING_JOB from jenkinsapi_tests.test_utils.random_strings import random_string +from jenkinsapi_tests.systests.job_configs import LONG_RUNNING_JOB, SHORTISH_JOB class TestInvocation(BaseSystemTest): @@ -19,17 +19,25 @@ class TestInvocation(BaseSystemTest): self.assertTrue(ii.is_queued_or_running()) self.assertEquals(ii.get_build_number(), 1) - - def test_get_build_from_invocation(self): - job_name = 'create_%s' % random_string() + def test_get_block_until_build_running(self): + job_name = 'create_%s' % random_string() job = self.jenkins.create_job(job_name, LONG_RUNNING_JOB) ii = job.invoke() bn = ii.get_build_number() self.assertIsInstance(bn, int) ii.block(until='not_queued') + self.assertTrue(ii.is_running()) b = ii.get_build() self.assertIsInstance(b, Build) + ii.stop() + self.assertFalse(ii.is_running()) + def test_get_block_until_build_complete(self): + job_name = 'create_%s' % random_string() + job = self.jenkins.create_job(job_name, SHORTISH_JOB) + ii = job.invoke() + ii.block(until='completed') + self.assertFalse(ii.is_running()) if __name__ == '__main__': unittest.main()