import time
-from jenkinsapi.exceptions import UnknownQueueItem
+import datetime
+from jenkinsapi.exceptions import UnknownQueueItem, TimeOut
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()
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
</project>
'''.strip()
-LONG_RUNNING_JOB = JOB_XML = """
+LONG_RUNNING_JOB = """
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<publishers/>
<buildWrappers/>
</project>""".strip()
+
+SHORTISH_JOB = """
+<?xml version='1.0' encoding='UTF-8'?>
+<project>
+ <actions/>
+ <description></description>
+ <keepDependencies>false</keepDependencies>
+ <properties/>
+ <scm class="hudson.scm.NullSCM"/>
+ <canRoam>true</canRoam>
+ <disabled>false</disabled>
+ <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
+ <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
+ <triggers class="vector"/>
+ <concurrentBuild>false</concurrentBuild>
+ <builders>
+ <hudson.tasks.Shell>
+ <command>ping -c 10 localhost</command>
+ </hudson.tasks.Shell>
+ </builders>
+ <publishers/>
+ <buildWrappers/>
+</project>""".strip()
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):
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()