Skip to content

Commit 0344b6d

Browse files
committed
+ test_building
1 parent bd1de96 commit 0344b6d

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

jenkins_cli/cli.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import print_function
22
import os
3-
import time
3+
from time import time
44
import datetime
55
import jenkins
66
import socket
@@ -196,10 +196,13 @@ def building(self, args):
196196
for job in jobs:
197197
info = self.jenkins.get_job_info(job['name'])
198198
build_number = info['lastBuild'].get('number')
199+
eta = "unknown"
200+
display_name = job['name']
199201
if build_number:
200202
build_info = self.jenkins.get_build_info(job['name'], build_number)
201-
eta = (build_info['timestamp'] + build_info['estimatedDuration']) / 1000 - time.time()
202-
print("%s estimated time left %s" % (build_info['fullDisplayName'],
203-
datetime.timedelta(seconds=eta)))
203+
eta = (build_info['timestamp'] + build_info['estimatedDuration']) / 1000 - time()
204+
eta = datetime.timedelta(seconds=eta)
205+
display_name = build_info['fullDisplayName']
206+
print("%s estimated time left %s" % (display_name, eta))
204207
else:
205208
print("Nothing is building now")

tests/test_cli.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
from argparse import Namespace
44
from xml.etree import ElementTree
5-
from datetime import datetime
5+
from datetime import datetime, timedelta
66

77
from pyfakefs import fake_filesystem_unittest
88
import mock
@@ -22,6 +22,8 @@
2222
EMPTY_SCM_XML = """<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<project>\n <actions/>\n <description></description>\n <keepDependencies>false</keepDependencies>\n <properties/>\n <scm class="hudson.scm.NullSCM"/>\n <canRoam>true</canRoam>\n <disabled>false</disabled>\n <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>\n <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>\n <triggers/>\n <concurrentBuild>false</concurrentBuild>\n <builders/>\n <publishers/>\n <buildWrappers/>\n</project>
2323
"""
2424

25+
TS = 1456870299
26+
2527

2628
class TestCliAuth(unittest.TestCase):
2729

@@ -204,24 +206,23 @@ def test_info(self, patched_get_job_name, patched_get_job_info, patched_get_job_
204206
self.patched_print.assert_called_once_with(arg)
205207
self.patched_print.reset_mock()
206208

207-
ts = 1456870299
208209
job_info = {'lastBuild': {'fullDisplayName': 'FDN (cur)',
209210
'result': 'Done',
210-
'timestamp': ts * 1000,
211+
'timestamp': TS * 1000,
211212
'building': True},
212213
'lastSuccessfulBuild': {'fullDisplayName': 'FDN (last)'}}
213214
patched_get_job_info.return_value = job_info
214215
patched_get_job_config.return_value = GIT_SCM_XML
215216
JenkinsCli(self.args).info(self.args)
216-
arg = JenkinsCli.INFO_TEMPLATE % ('FDN (cur)', 'Done', 'FDN (last)', datetime.fromtimestamp(ts), 'Yes', 'Git', 'cli-tests')
217+
arg = JenkinsCli.INFO_TEMPLATE % ('FDN (cur)', 'Done', 'FDN (last)', datetime.fromtimestamp(TS), 'Yes', 'Git', 'cli-tests')
217218
self.patched_print.assert_called_once_with(arg)
218219
self.patched_print.reset_mock()
219220

220221
job_info['building'] = False
221222
patched_get_job_info.return_value = job_info
222223
patched_get_job_config.return_value = HG_SCM_XML
223224
JenkinsCli(self.args).info(self.args)
224-
arg = JenkinsCli.INFO_TEMPLATE % ('FDN (cur)', 'Done', 'FDN (last)', datetime.fromtimestamp(ts), 'Yes', 'Mercurial', 'v123')
225+
arg = JenkinsCli.INFO_TEMPLATE % ('FDN (cur)', 'Done', 'FDN (last)', datetime.fromtimestamp(TS), 'Yes', 'Mercurial', 'v123')
225226
self.patched_print.assert_called_once_with(arg)
226227

227228
@mock.patch.object(jenkins.Jenkins, 'reconfig_job')
@@ -278,5 +279,37 @@ def test_stop(self, patched_job_name, patched_job_info, patched_stop_build):
278279
patched_stop_build.assert_called_once_with('Job1', 22)
279280
self.patched_print.assert_called_once_with("Job1: stopped")
280281

282+
@mock.patch('jenkins_cli.cli.time', return_value=0)
283+
@mock.patch.object(jenkins.Jenkins, 'get_jobs')
284+
@mock.patch.object(jenkins.Jenkins, 'get_build_info')
285+
@mock.patch.object(jenkins.Jenkins, 'get_job_info')
286+
@mock.patch.object(jenkins.Jenkins, 'get_job_name', side_effect=lambda j: j)
287+
def test_building(self, patched_job_name, patched_job_info, patched_build_info, get_jobs_patched, patched_time):
288+
get_jobs_patched.return_value = [{'name': 'Job1', 'color': 'blue'}]
289+
JenkinsCli(self.args).building(self.args)
290+
self.assertFalse(patched_job_info.called)
291+
self.assertFalse(patched_build_info.called)
292+
self.patched_print.assert_called_once_with("Nothing is building now")
293+
self.patched_print.reset_mock()
294+
295+
get_jobs_patched.return_value = [{'name': 'Job1', 'color': 'blue_anime'},
296+
{'name': 'Job5', 'color': 'red_anime'}]
297+
patched_job_info.return_value = {'lastBuild': {}}
298+
JenkinsCli(self.args).building(self.args)
299+
self.assertFalse(patched_build_info.called)
300+
self.patched_print.assert_has_calls([mock.call("Job1 estimated time left unknown")],
301+
[mock.call("Job5 estimated time left unknown")])
302+
self.patched_print.reset_mock()
303+
304+
patched_job_info.return_value = {'lastBuild': {'number': 2}}
305+
306+
def info_side_effect(name, number):
307+
return {'timestamp': TS * 1000, 'estimatedDuration': 0, 'fullDisplayName': 'FDN ' + name}
308+
309+
patched_build_info.side_effect = info_side_effect
310+
JenkinsCli(self.args).building(self.args)
311+
self.patched_print.assert_has_calls([mock.call("FDN Job1 estimated time left %s" % timedelta(seconds=TS))],
312+
[mock.call("FDN Job5 estimated time left %s" % timedelta(seconds=TS))])
313+
281314
if __name__ == '__main__':
282315
unittest.main()

0 commit comments

Comments
 (0)