Skip to content

Commit

Permalink
Merge pull request #30 from Panchorn/master
Browse files Browse the repository at this point in the history
Add optional param ignore_exception for func build_jenkins_with_param…
  • Loading branch information
Panchorn authored Apr 28, 2022
2 parents 500a51a + afb0657 commit 9524ddc
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dist: xenial
language: python
python:
- '3.7'
- '3.9'
install: pip install -r requirements.txt
script:
- pytest -vv --cov=./ --cov-report=xml
Expand Down
2 changes: 1 addition & 1 deletion JenkinsLibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class JenkinsLibrary(JenkinsFace):
| ${job_details}= | Get Jenkins Job | ${job_full_name} |
| ${job_build_details}= | Get Jenkins Job Build | ${job_full_name} | ${build_number} |
| ${build_number}= | Build Jenkins With Parameters | ${job_full_name} | ${parameters_string} |
| ${job_build_details}= | Build Jenkins With Parameters And Wait Until Job Done | ${job_full_name} | ${parameters_string} | 10 | 2 |
| ${job_build_details}= | Build Jenkins With Parameters And Wait Until Job Done | ${job_full_name} | ${parameters_string} | 10 | 2 | False |
"""

Expand Down
21 changes: 13 additions & 8 deletions JenkinsLibrary/jenkins_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ def build_jenkins_with_parameters(self, name=None, data=None):
return job_detail['nextBuildNumber']

@keyword('Build Jenkins With Parameters And Wait Until Job Done')
def build_jenkins_with_parameters_and_wait_until_job_done(self, name=None, data=None, retry=24, retry_interval=5):
def build_jenkins_with_parameters_and_wait_until_job_done(
self,
name=None,
data=None,
retry=24,
retry_interval=5,
ignore_exception=True
):
"""Build Jenkins With Parameters And Wait Until Job Done
Trigger build job jenkins and wait until build job done
Expand All @@ -148,11 +155,11 @@ def build_jenkins_with_parameters_and_wait_until_job_done(self, name=None, data=
- data: job's parameters ``str``
- retry: number of times to retry ``int`` ``default is 24``
- retry_interval: time to wait before checking job status again ``int`` ``default is 5``
- ignore_exception: flag to ignore an exception while waiting job done ``bool`` ``default is True``
Return dictionary of job information if job done ``dict``, otherwise will return ``None``
Examples:
| ${job_build_details}= | Build Jenkins With Parameters And Wait Until Job Done | ${job_full_name} | ${parameters_string} | 10 | 2 |
Examples: | ${job_build_details}= | Build Jenkins With Parameters And Wait Until Job Done | ${job_full_name} | ${parameters_string} | 10 | 2 | False |
"""
if not name:
raise Exception('Job name should not be None')
Expand All @@ -161,15 +168,13 @@ def build_jenkins_with_parameters_and_wait_until_job_done(self, name=None, data=
time.sleep(retry_interval)
try:
response = self.get_jenkins_job_build(name, next_build_no)
if response['building'] == False:
if not response['building']:
return response
except requests.exceptions.HTTPError as err:
if err.response.status_code == 404:
except Exception as err:
if ignore_exception:
pass
else:
raise err
except Exception as err:
raise err

def _send(self, req):
return self._session.send(req, **self._settings)
Expand Down
2 changes: 1 addition & 1 deletion JenkinsLibrary/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.8.6'
VERSION = '0.8.7'
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ pip install -U robotframework-jenkinslibrary
```
## Example Test Case

*** Settings *** | | | | | |
---------------------- |---------------------- |----------------- |---------------- |----------------- |----------------- |
Library | JenkinsLibrary | | | | |
*** Test Cases *** | | | | | |
create session jenkins | ${protocol} | ${host} | ${username} | ${password} | ${verify} |
${job_details}= | Get Jenkins Job | ${job_full_name} | | | |
${job_build_details}= | Get Jenkins Job Build | ${job_full_name} | ${build_number} | | |
${build_number}= | Build Jenkins With Parameters | ${job_full_name} | ${parameters_string} | | |
${job_build_details}= | Build Jenkins With Parameters And Wait Until Job Done | ${job_full_name} | ${parameters_string} | 10 | 2 |
*** Settings *** | | | | | | |
---------------------- |---------------------- |----------------- |---------------- |----------------- |----------------- |----------------- |
Library | JenkinsLibrary | | | | | |
*** Test Cases *** | | | | | | |
create session jenkins | ${protocol} | ${host} | ${username} | ${password} | ${verify} | |
${job_details}= | Get Jenkins Job | ${job_full_name} | | | | |
${job_build_details}= | Get Jenkins Job Build | ${job_full_name} | ${build_number} | | | |
${build_number}= | Build Jenkins With Parameters | ${job_full_name} | ${parameters_string} | | | |
${job_build_details}= | Build Jenkins With Parameters And Wait Until Job Done | ${job_full_name} | ${parameters_string} | 10 | 2 | False |

## Document
For more keyword detail go to the following link:
Expand Down
162 changes: 142 additions & 20 deletions docs/JenkinsLibrary.html

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
requests
robotframework
pytest
pytest-cov
codecov
requests_mock
requests==2.26.0
robotframework==3.2.2
pytest==6.2.4
pytest-cov==2.12.1
codecov==2.1.12
requests_mock==1.9.3
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,65 @@ def test_build_jenkins_with_parameters_and_wait_until_job_done_success(self, moc
'folder_name/test_job',
'data_test',
retry=5,
retry_interval=1)
retry_interval=1
)
self.assertEqual(job_build_details['nextBuildNumber'], 2)

@requests_mock.Mocker()
def test_build_jenkins_with_parameters_and_wait_until_job_done_when_http_exception_but_ignore_exception(self, mock):
mock.get(
'http://username:password@localhost:8080/job/folder_name/job/test_job/api/json',
json={
"_class": "",
"actions": [],
"description": None,
"displayName": "",
"displayNameOrNull": "",
"name": "",
"url": "http://localhost:8080/job/folder_name/job/test_job/",
"buildable": True,
"builds": [],
"color": "blue",
"firstBuild": {},
"healthReport": [],
"inQueue": False,
"keepDependencies": False,
"lastBuild": {},
"lastCompletedBuild": {},
"lastFailedBuild": {},
"lastStableBuild": {},
"lastSuccessfulBuild": {},
"lastUnstableBuild": None,
"lastUnsuccessfulBuild": {},
"nextBuildNumber": 6,
"property": [],
"queueItem": None,
"concurrentBuild": False,
"building": True
}
)

mock.post(
'http://username:password@localhost:8080/job/folder_name/job/test_job/buildWithParameters',
status_code=200
)

mock.get(
'http://username:password@localhost:8080/job/folder_name/job/test_job/6/api/json',
status_code=500
)

self.jenkins.create_session_jenkins('http', 'localhost:8080', 'username', 'password', False)

job_build_details = self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done(
'folder_name/test_job',
'data_test',
retry=1,
retry_interval=1,
ignore_exception=True
)
self.assertEqual(job_build_details, None)

def test_build_jenkins_with_parameters_and_wait_until_job_done_fail_when_name_is_none(self):
self.jenkins.create_session_jenkins('http', 'localhost:8080', 'username', 'password', False)
try:
Expand Down Expand Up @@ -159,8 +215,12 @@ def test_build_jenkins_with_parameters_and_wait_until_job_done_when_http_excepti
self.jenkins.create_session_jenkins('http', 'localhost:8080', 'username', 'password', False)

try:
self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done('folder_name/test_job',
'data_test', retry=1)
self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done(
'folder_name/test_job',
'data_test',
retry=1,
ignore_exception=False
)
except requests.exceptions.HTTPError as err:
self.assertTrue(err.response.status_code == 500)

Expand Down Expand Up @@ -212,7 +272,11 @@ def test_build_jenkins_with_parameters_and_wait_until_job_done_when_exception(se
raised = False

try:
self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done('folder_name/test_job', 'data_test')
self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done(
name='folder_name/test_job',
data='data_test',
ignore_exception=False
)
except requests.exceptions.ConnectionError:
raised = True

Expand Down

0 comments on commit 9524ddc

Please sign in to comment.