jenkins_build: Support stop a running Jenkins build (#2850)

* Support stop a running Jenkins build. Meanwhile enrich document content and test cases.

* Fix the inconsistencies regarding the function name.

* Submit the changelog and fix a PEP8 issue.

* Remedy whitespace related PEP8 issues.

* Implement the idempotent test case for the stop build function.

* Make sure it returns proper changed status when we stop a build repeatedly.

* Fix incorrect usages on comparison with True or False and incorrect usages on validating the changed status.

* In this mocking situation, adjust the mock return value and test case to perform unit testing.

* Implement JenkinsMockIdempotent() to mock return value in idempotent test cases.

* Fix issues reported by CI.

* Refactor the code to avoid CI exception and remove get_build_status() from mock function as they should not be there.

* Update plugins/modules/web_infrastructure/jenkins_build.py

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Tong He 2021-06-23 17:29:50 -04:00 committed by GitHub
commit 860b2b89a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 126 additions and 13 deletions

View file

@ -50,18 +50,42 @@ class JenkinsMock():
def get_build_info(self, name, build_number):
return {
"building": True,
"result": "SUCCESS"
}
def get_build_status(self):
pass
def build_job(self, *args):
return None
def delete_build(self, name, build_number):
return None
def stop_build(self, name, build_number):
return None
class JenkinsMockIdempotent():
def get_job_info(self, name):
return {
"nextBuildNumber": 1235
}
def get_build_info(self, name, build_number):
return {
"building": False,
"result": "ABORTED"
}
def build_job(self, *args):
return None
def delete_build(self, name, build_number):
return None
def stop_build(self, name, build_number):
return None
class TestJenkinsBuild(unittest.TestCase):
@ -79,6 +103,16 @@ class TestJenkinsBuild(unittest.TestCase):
set_module_args({})
jenkins_build.main()
@patch('ansible_collections.community.general.plugins.modules.web_infrastructure.jenkins_build.test_dependencies')
def test_module_fail_when_missing_build_number(self, test_deps):
test_deps.return_value = None
with self.assertRaises(AnsibleFailJson):
set_module_args({
"name": "required-if",
"state": "stopped"
})
jenkins_build.main()
@patch('ansible_collections.community.general.plugins.modules.web_infrastructure.jenkins_build.test_dependencies')
@patch('ansible_collections.community.general.plugins.modules.web_infrastructure.jenkins_build.JenkinsBuild.get_jenkins_connection')
def test_module_create_build(self, jenkins_connection, test_deps):
@ -93,6 +127,42 @@ class TestJenkinsBuild(unittest.TestCase):
})
jenkins_build.main()
@patch('ansible_collections.community.general.plugins.modules.web_infrastructure.jenkins_build.test_dependencies')
@patch('ansible_collections.community.general.plugins.modules.web_infrastructure.jenkins_build.JenkinsBuild.get_jenkins_connection')
def test_module_stop_build(self, jenkins_connection, test_deps):
test_deps.return_value = None
jenkins_connection.return_value = JenkinsMock()
with self.assertRaises(AnsibleExitJson) as return_json:
set_module_args({
"name": "host-check",
"build_number": "1234",
"state": "stopped",
"user": "abc",
"token": "xyz"
})
jenkins_build.main()
self.assertTrue(return_json.exception.args[0]['changed'])
@patch('ansible_collections.community.general.plugins.modules.web_infrastructure.jenkins_build.test_dependencies')
@patch('ansible_collections.community.general.plugins.modules.web_infrastructure.jenkins_build.JenkinsBuild.get_jenkins_connection')
def test_module_stop_build_again(self, jenkins_connection, test_deps):
test_deps.return_value = None
jenkins_connection.return_value = JenkinsMockIdempotent()
with self.assertRaises(AnsibleExitJson) as return_json:
set_module_args({
"name": "host-check",
"build_number": "1234",
"state": "stopped",
"user": "abc",
"password": "xyz"
})
jenkins_build.main()
self.assertFalse(return_json.exception.args[0]['changed'])
@patch('ansible_collections.community.general.plugins.modules.web_infrastructure.jenkins_build.test_dependencies')
@patch('ansible_collections.community.general.plugins.modules.web_infrastructure.jenkins_build.JenkinsBuild.get_jenkins_connection')
def test_module_delete_build(self, jenkins_connection, test_deps):