def bot_complete_task(self, token, **kwargs): # Emulate an isolated task. params = { 'cost_usd': 0.1, 'duration': 0.1, 'bot_overhead': 0.1, 'exit_code': 0, 'id': 'bot1', 'isolated_stats': { 'download': { 'duration': 1., 'initial_number_items': 10, 'initial_size': 100000, 'items_cold': [20], 'items_hot': [30], }, 'upload': { 'duration': 2., 'items_cold': [40], 'items_hot': [50], }, }, 'output': base64.b64encode(u'rÉsult string'.encode('utf-8')), 'output_chunk_start': 0, 'task_id': None, } for k in ('download', 'upload'): for j in ('items_cold', 'items_hot'): params['isolated_stats'][k][j] = base64.b64encode( large.pack(params['isolated_stats'][k][j])) params.update(kwargs) response = self.post_with_token( '/swarming/api/v1/bot/task_update', params, token) self.assertEqual({u'ok': True}, response)
def bot_complete_task(self, **kwargs): # Emulate an isolated task. params = { 'cost_usd': 0.1, 'duration': 0.1, 'bot_overhead': 0.1, 'exit_code': 0, 'id': 'bot1', 'isolated_stats': { 'download': { 'duration': 1., 'initial_number_items': 10, 'initial_size': 100000, 'items_cold': [20], 'items_hot': [30, 40], }, 'upload': { 'duration': 2., 'items_cold': [1, 2, 40], 'items_hot': [1, 2, 3, 50], }, }, 'output': base64.b64encode(u'rÉsult string'.encode('utf-8')), 'output_chunk_start': 0, 'task_id': None, } for k in ('download', 'upload'): for j in ('items_cold', 'items_hot'): params['isolated_stats'][k][j] = base64.b64encode( large.pack(params['isolated_stats'][k][j])) params.update(kwargs) return self.post_json('/swarming/api/v1/bot/task_update', params)
def test_integration(self): # Creates a TaskRequest, along its TaskResultSummary and TaskToRun. Have a # bot reap the task, and complete the task. Ensure the resulting # TaskResultSummary and TaskRunResult are properly updated. request = _gen_request() result_summary = task_result.new_result_summary(request) to_run = task_to_run.new_task_to_run(request, 1, 0) result_summary.modified_ts = utils.utcnow() ndb.transaction(lambda: ndb.put_multi([result_summary, to_run])) expected = self._gen_summary(modified_ts=self.now) self.assertEqual(expected, result_summary.to_dict()) # Nothing changed 2 secs later except latency. self.mock_now(self.now, 2) self.assertEqual(expected, result_summary.to_dict()) # Task is reaped after 2 seconds (4 secs total). reap_ts = self.now + datetime.timedelta(seconds=4) self.mock_now(reap_ts) to_run.queue_number = None to_run.put() run_result = task_result.new_run_result(request, to_run, u'localhost', u'abc', {}) run_result.started_ts = utils.utcnow() run_result.modified_ts = run_result.started_ts ndb.transaction( lambda: result_summary.set_from_run_result(run_result, request)) ndb.transaction(lambda: ndb.put_multi((result_summary, run_result))) expected = self._gen_summary(bot_dimensions={}, bot_version=u'abc', bot_id=u'localhost', costs_usd=[0.], modified_ts=reap_ts, state=task_result.State.RUNNING, started_ts=reap_ts, try_number=1) self.assertEqual(expected, result_summary.key.get().to_dict()) # Task completed after 2 seconds (6 secs total), the task has been running # for 2 seconds. complete_ts = self.now + datetime.timedelta(seconds=6) self.mock_now(complete_ts) run_result.completed_ts = complete_ts run_result.duration = 0.1 run_result.exit_code = 0 run_result.state = task_result.State.COMPLETED run_result.modified_ts = utils.utcnow() task_result.PerformanceStats( key=task_pack.run_result_key_to_performance_stats_key( run_result.key), bot_overhead=0.1, isolated_download=task_result.OperationStats( duration=0.05, initial_number_items=10, initial_size=10000, items_cold=large.pack([1, 2]), items_hot=large.pack([3, 4, 5])), isolated_upload=task_result.OperationStats(duration=0.01, items_cold=large.pack( [10]))).put() ndb.transaction( lambda: ndb.put_multi(run_result.append_output('foo', 0))) ndb.transaction( lambda: result_summary.set_from_run_result(run_result, request)) ndb.transaction(lambda: ndb.put_multi((result_summary, run_result))) expected = self._gen_summary(bot_dimensions={}, bot_version=u'abc', bot_id=u'localhost', completed_ts=complete_ts, costs_usd=[0.], duration=0.1, exit_code=0, modified_ts=complete_ts, state=task_result.State.COMPLETED, started_ts=reap_ts, try_number=1) self.assertEqual(expected, result_summary.key.get().to_dict()) expected = { 'bot_overhead': 0.1, 'isolated_download': { 'duration': 0.05, 'initial_number_items': 10, 'initial_size': 10000, 'items_cold': large.pack([1, 2]), 'items_hot': large.pack([3, 4, 5]), 'num_items_cold': 2, 'total_bytes_items_cold': 3, 'num_items_hot': 3, 'total_bytes_items_hot': 12, }, 'isolated_upload': { 'duration': 0.01, 'initial_number_items': None, 'initial_size': None, 'items_cold': large.pack([10]), 'items_hot': None, 'num_items_cold': 1, 'total_bytes_items_cold': 10, 'num_items_hot': None, 'total_bytes_items_hot': None, }, 'package_installation': { 'duration': None, 'initial_number_items': None, 'initial_size': None, 'items_cold': None, 'items_hot': None, 'num_items_cold': None, 'total_bytes_items_cold': None, 'num_items_hot': None, 'total_bytes_items_hot': None, }, } self.assertEqual(expected, result_summary.performance_stats.to_dict()) self.assertEqual('foo', result_summary.get_output()) self.assertEqual(datetime.timedelta(seconds=2), result_summary.duration_as_seen_by_server) self.assertEqual(datetime.timedelta(seconds=0.1), result_summary.duration_now(utils.utcnow())) self.assertEqual(datetime.timedelta(seconds=4), result_summary.pending) self.assertEqual(datetime.timedelta(seconds=4), result_summary.pending_now(utils.utcnow())) self.assertEqual(task_pack.pack_result_summary_key(result_summary.key), result_summary.task_id) self.assertEqual(complete_ts, result_summary.ended_ts) self.assertEqual(task_pack.pack_run_result_key(run_result.key), run_result.task_id) self.assertEqual(complete_ts, run_result.ended_ts)