Skip to content

Commit

Permalink
[#539] Add logic (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillmakhonin authored Oct 26, 2018
1 parent 651d269 commit d2148d5
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion legion_test/legion_test/robot/jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ def run_jenkins_job(self, job_name, **parameters):
response = self._client.build_job(job_name, parameters=parameters)
print('Result of Job run: {!r}'.format(response))

def get_latest_job_number(self, job_name):
"""
Get No. of latest completed build of job
:param job_name: Jenkins job name
:type job_name: str
:return: int
"""
if not self._client:
raise Exception('Jenkins client has not been initialized')
job_info = self._client.get_job_info(job_name)
return job_info['lastBuild']['number']

def is_job_finished(self, job_name):
"""
Check if Jenkins job is finished
Expand All @@ -136,6 +149,8 @@ def is_job_finished(self, job_name):
:raises: Exception
:return: bool -- is finished or not
"""
if not self._client:
raise Exception('Jenkins client has not been initialized')
job_info = self._client.get_job_info(job_name)

if not job_info['lastBuild'] and not job_info['inQueue']:
Expand All @@ -149,9 +164,30 @@ def is_job_finished(self, job_name):

return False

def print_jenkins_build_logs(self, job_name, build_no=None):
"""
Gather and print Jenkins job logs
:param job_name: Jenkins job name
:type job_name: str
:param build_no: (Optional) No. of build or latest
:type build_no: int / None
:return: None
"""
if not self._client:
raise Exception('Jenkins client has not been initialized')

try:
if not build_no:
build_no = self.get_latest_job_number(job_name)
logs = self._client.get_build_console_output(job_name, build_no)
print("Jenkins Build #{} logs:\n{}".format(build_no, logs))
except Exception as log_gathering_exception:
print('Cannot gather build #{} logs: {}'.format(build_no, log_gathering_exception))

def wait_jenkins_job(self, job_name, timeout=0, sleep=5):
"""
Wait finish of last job build
Wait finish of last job build and print job logs
:param job_name: Jenkins job name
:type job_name: str
Expand All @@ -172,10 +208,12 @@ def wait_jenkins_job(self, job_name, timeout=0, sleep=5):
time.sleep(sleep)
while True:
if self.is_job_finished(job_name):
self.print_jenkins_build_logs(job_name)
return

elapsed = time.time() - start
if elapsed > timeout > 0:
self.print_jenkins_build_logs(job_name)
raise Exception('Timeout')

time.sleep(sleep)
Expand Down

0 comments on commit d2148d5

Please sign in to comment.