|
2 | 2 | import os |
3 | 3 | from argparse import Namespace |
4 | 4 | from xml.etree import ElementTree |
5 | | -from datetime import datetime |
| 5 | +from datetime import datetime, timedelta |
6 | 6 |
|
7 | 7 | from pyfakefs import fake_filesystem_unittest |
8 | 8 | import mock |
|
22 | 22 | 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> |
23 | 23 | """ |
24 | 24 |
|
| 25 | +TS = 1456870299 |
| 26 | + |
25 | 27 |
|
26 | 28 | class TestCliAuth(unittest.TestCase): |
27 | 29 |
|
@@ -204,24 +206,23 @@ def test_info(self, patched_get_job_name, patched_get_job_info, patched_get_job_ |
204 | 206 | self.patched_print.assert_called_once_with(arg) |
205 | 207 | self.patched_print.reset_mock() |
206 | 208 |
|
207 | | - ts = 1456870299 |
208 | 209 | job_info = {'lastBuild': {'fullDisplayName': 'FDN (cur)', |
209 | 210 | 'result': 'Done', |
210 | | - 'timestamp': ts * 1000, |
| 211 | + 'timestamp': TS * 1000, |
211 | 212 | 'building': True}, |
212 | 213 | 'lastSuccessfulBuild': {'fullDisplayName': 'FDN (last)'}} |
213 | 214 | patched_get_job_info.return_value = job_info |
214 | 215 | patched_get_job_config.return_value = GIT_SCM_XML |
215 | 216 | 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') |
217 | 218 | self.patched_print.assert_called_once_with(arg) |
218 | 219 | self.patched_print.reset_mock() |
219 | 220 |
|
220 | 221 | job_info['building'] = False |
221 | 222 | patched_get_job_info.return_value = job_info |
222 | 223 | patched_get_job_config.return_value = HG_SCM_XML |
223 | 224 | 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') |
225 | 226 | self.patched_print.assert_called_once_with(arg) |
226 | 227 |
|
227 | 228 | @mock.patch.object(jenkins.Jenkins, 'reconfig_job') |
@@ -278,5 +279,37 @@ def test_stop(self, patched_job_name, patched_job_info, patched_stop_build): |
278 | 279 | patched_stop_build.assert_called_once_with('Job1', 22) |
279 | 280 | self.patched_print.assert_called_once_with("Job1: stopped") |
280 | 281 |
|
| 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 | + |
281 | 314 | if __name__ == '__main__': |
282 | 315 | unittest.main() |
0 commit comments