| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # coding=utf-8 | 2 # coding=utf-8 |
| 3 # Copyright 2015 The LUCI Authors. All rights reserved. | 3 # Copyright 2015 The LUCI Authors. All rights reserved. |
| 4 # Use of this source code is governed under the Apache License, Version 2.0 | 4 # Use of this source code is governed under the Apache License, Version 2.0 |
| 5 # that can be found in the LICENSE file. | 5 # that can be found in the LICENSE file. |
| 6 | 6 |
| 7 import base64 | 7 import base64 |
| 8 import datetime | 8 import datetime |
| 9 import json | 9 import json |
| 10 import logging | 10 import logging |
| (...skipping 1334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1345 u'service_account:none', | 1345 u'service_account:none', |
| 1346 u'user:joe@localhost', | 1346 u'user:joe@localhost', |
| 1347 ], | 1347 ], |
| 1348 u'task_id': task_id, | 1348 u'task_id': task_id, |
| 1349 u'try_number': u'1', | 1349 u'try_number': u'1', |
| 1350 u'user': u'joe@localhost', | 1350 u'user': u'joe@localhost', |
| 1351 } | 1351 } |
| 1352 out.update((unicode(k), v) for k, v in kwargs.iteritems()) | 1352 out.update((unicode(k), v) for k, v in kwargs.iteritems()) |
| 1353 return out | 1353 return out |
| 1354 | 1354 |
| 1355 def _cycle(params, expected, must_stop): | 1355 self.set_as_bot() |
| 1356 response = self.post_json('/swarming/api/v1/bot/task_update', params) | |
| 1357 self.assertEqual({u'must_stop': must_stop, u'ok': True}, response) | |
| 1358 self.assertEqual(expected, self.client_get_results(task_id)) | |
| 1359 | |
| 1360 params = _params(output=base64.b64encode('Oh ')) | 1356 params = _params(output=base64.b64encode('Oh ')) |
| 1361 expected = _expected() | 1357 response = self.post_json('/swarming/api/v1/bot/task_update', params) |
| 1362 _cycle(params, expected, False) | 1358 self.assertEqual({u'must_stop': False, u'ok': True}, response) |
| 1359 self.set_as_user() |
| 1360 self.assertEqual(_expected(), self.client_get_results(task_id)) |
| 1363 | 1361 |
| 1364 # Canceling a running task is currently not supported. | 1362 # Canceling a running task is currently not supported. |
| 1365 self.set_as_user() | |
| 1366 expected = {u'ok': False, u'was_running': True} | |
| 1367 response = self.call_api('cancel', body={'task_id': task_id}) | 1363 response = self.call_api('cancel', body={'task_id': task_id}) |
| 1368 self.assertEqual(expected, response.json) | 1364 self.assertEqual({u'ok': False, u'was_running': True}, response.json) |
| 1369 | 1365 |
| 1370 self.set_as_bot() | 1366 self.set_as_bot() |
| 1371 params = _params(output=base64.b64encode('hi'), output_chunk_start=3) | 1367 params = _params(output=base64.b64encode('hi'), output_chunk_start=3) |
| 1372 expected = _expected() | 1368 response = self.post_json('/swarming/api/v1/bot/task_update', params) |
| 1373 _cycle(params, expected, False) | 1369 self.assertEqual({u'must_stop': False, u'ok': True}, response) |
| 1370 self.set_as_user() |
| 1371 self.assertEqual(_expected(), self.client_get_results(task_id)) |
| 1374 | 1372 |
| 1375 def test_result_unknown(self): | 1373 def test_result_unknown(self): |
| 1376 """Asserts that result raises 404 for unknown task IDs.""" | 1374 """Asserts that result raises 404 for unknown task IDs.""" |
| 1377 self.call_api('result', body={'task_id': '12310'}, status=404) | 1375 self.call_api('result', body={'task_id': '12310'}, status=404) |
| 1378 | 1376 |
| 1379 def test_result_ok(self): | 1377 def test_result_ok(self): |
| 1380 """Asserts that result produces a result entity.""" | 1378 """Asserts that result produces a result entity.""" |
| 1381 self.mock(random, 'getrandbits', lambda _: 0x88) | 1379 self.mock(random, 'getrandbits', lambda _: 0x88) |
| 1382 | 1380 |
| 1383 # pending task | 1381 # pending task |
| (...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1938 self.assertEqual(expected, response.json) | 1936 self.assertEqual(expected, response.json) |
| 1939 | 1937 |
| 1940 def test_get_no_bot(self): | 1938 def test_get_no_bot(self): |
| 1941 """Asserts that get raises 404 when no bot is found.""" | 1939 """Asserts that get raises 404 when no bot is found.""" |
| 1942 self.set_as_admin() | 1940 self.set_as_admin() |
| 1943 self.call_api('get', body={'bot_id': 'not_a_bot'}, status=404) | 1941 self.call_api('get', body={'bot_id': 'not_a_bot'}, status=404) |
| 1944 | 1942 |
| 1945 def test_delete_ok(self): | 1943 def test_delete_ok(self): |
| 1946 """Assert that delete finds and deletes a bot.""" | 1944 """Assert that delete finds and deletes a bot.""" |
| 1947 self.set_as_admin() | 1945 self.set_as_admin() |
| 1948 self.mock(acl, 'is_admin', lambda *_args, **_kwargs: True) | 1946 self.mock(acl, '_is_admin', lambda *_args, **_kwargs: True) |
| 1949 now = datetime.datetime(2010, 1, 2, 3, 4, 5, 6) | 1947 now = datetime.datetime(2010, 1, 2, 3, 4, 5, 6) |
| 1950 self.mock_now(now) | 1948 self.mock_now(now) |
| 1951 state = { | 1949 state = { |
| 1952 'dict': {'random': 'values'}, | 1950 'dict': {'random': 'values'}, |
| 1953 'float': 0., | 1951 'float': 0., |
| 1954 'list': ['of', 'things'], | 1952 'list': ['of', 'things'], |
| 1955 'str': u'uni', | 1953 'str': u'uni', |
| 1956 } | 1954 } |
| 1957 bot_management.bot_event( | 1955 bot_management.bot_event( |
| 1958 event_type='bot_connected', bot_id='id1', | 1956 event_type='bot_connected', bot_id='id1', |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2189 self.call_api('terminate', body={'bot_id': 'bot1'}, status=403) | 2187 self.call_api('terminate', body={'bot_id': 'bot1'}, status=403) |
| 2190 | 2188 |
| 2191 | 2189 |
| 2192 if __name__ == '__main__': | 2190 if __name__ == '__main__': |
| 2193 if '-v' in sys.argv: | 2191 if '-v' in sys.argv: |
| 2194 unittest.TestCase.maxDiff = None | 2192 unittest.TestCase.maxDiff = None |
| 2195 logging.basicConfig(level=logging.DEBUG) | 2193 logging.basicConfig(level=logging.DEBUG) |
| 2196 else: | 2194 else: |
| 2197 logging.basicConfig(level=logging.CRITICAL) | 2195 logging.basicConfig(level=logging.CRITICAL) |
| 2198 unittest.main() | 2196 unittest.main() |
| OLD | NEW |