Skip to content

Commit bd1de96

Browse files
committed
+ test start and stop
1 parent 159a261 commit bd1de96

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

jenkins_cli/cli.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,11 @@ def stop(self, args):
160160
job_name = self._check_job(args.job_name)
161161
info = self.jenkins.get_job_info(job_name)
162162
build_number = info['lastBuild'].get('number')
163-
stop_status = self.jenkins.stop_build(job_name, build_number)
164-
print("%s: %s" % (job_name, 'stoped' if not stop_status else stop_status))
163+
if build_number and info['lastBuild'].get('building'):
164+
stop_status = self.jenkins.stop_build(job_name, build_number)
165+
print("%s: %s" % (job_name, 'stopped' if not stop_status else stop_status))
166+
else:
167+
print("%s job is not running" % job_name)
165168

166169
def console(self, args):
167170
job_name = self._check_job(args.job_name)

tests/test_cli.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,5 +254,29 @@ def test_set_branch(self, patched_get_job_name, patched_get_job_config, patched_
254254
patched_reconfig_job.reset_mock()
255255
self.patched_print.reset_mock()
256256

257+
@mock.patch.object(jenkins.Jenkins, 'build_job', return_value=None)
258+
@mock.patch.object(jenkins.Jenkins, 'get_job_name', return_value='Job1')
259+
def test_start(self, patched_job_name, patched_build_job):
260+
self.args.job_name = ['Job1']
261+
JenkinsCli(self.args).start(self.args)
262+
patched_build_job.assert_called_once_with('Job1')
263+
self.patched_print.assert_called_once_with("%s: %s" % ('Job1', 'started'))
264+
265+
@mock.patch.object(jenkins.Jenkins, 'stop_build', return_value=None)
266+
@mock.patch.object(jenkins.Jenkins, 'get_job_info')
267+
@mock.patch.object(jenkins.Jenkins, 'get_job_name', return_value='Job1')
268+
def test_stop(self, patched_job_name, patched_job_info, patched_stop_build):
269+
self.args.job_name = 'Job1'
270+
patched_job_info.return_value = {'lastBuild': {}}
271+
JenkinsCli(self.args).stop(self.args)
272+
self.assertFalse(patched_stop_build.called)
273+
self.patched_print.assert_called_once_with("%s job is not running" % 'Job1')
274+
self.patched_print.reset_mock()
275+
276+
patched_job_info.return_value = {'lastBuild': {'building': True, 'number': 22}}
277+
JenkinsCli(self.args).stop(self.args)
278+
patched_stop_build.assert_called_once_with('Job1', 22)
279+
self.patched_print.assert_called_once_with("Job1: stopped")
280+
257281
if __name__ == '__main__':
258282
unittest.main()

0 commit comments

Comments
 (0)