| OLD | NEW |
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 # Copyright 2014 The LUCI Authors. All rights reserved. | 2 # Copyright 2014 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 5 | 5 |
| 6 """Base class for handlers_*_test.py""" | 6 """Base class for handlers_*_test.py""" |
| 7 | 7 |
| 8 import base64 | 8 import base64 |
| 9 import json | 9 import json |
| 10 import os | 10 import os |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 '/auth/api/v1/accounts/self/xsrf_token', | 134 '/auth/api/v1/accounts/self/xsrf_token', |
| 135 headers={'X-XSRF-Token-Request': '1'}).json | 135 headers={'X-XSRF-Token-Request': '1'}).json |
| 136 return resp['xsrf_token'].encode('ascii') | 136 return resp['xsrf_token'].encode('ascii') |
| 137 | 137 |
| 138 def post_json(self, url, params, **kwargs): | 138 def post_json(self, url, params, **kwargs): |
| 139 """Does an HTTP POST with a JSON API and return JSON response.""" | 139 """Does an HTTP POST with a JSON API and return JSON response.""" |
| 140 return self.app.post_json(url, params=params, **kwargs).json | 140 return self.app.post_json(url, params=params, **kwargs).json |
| 141 | 141 |
| 142 # Bot | 142 # Bot |
| 143 | 143 |
| 144 def do_handshake(self, bot='bot1'): | 144 def do_handshake(self, bot='bot1', status=200): |
| 145 """Performs bot handshake, returns data to be sent to bot handlers. | 145 """Performs bot handshake, returns data to be sent to bot handlers. |
| 146 | 146 |
| 147 Also populates self.bot_version. | 147 Also populates self.bot_version. |
| 148 """ | 148 """ |
| 149 params = { | 149 params = { |
| 150 'dimensions': { | 150 'dimensions': { |
| 151 'id': [bot], | 151 'id': [bot], |
| 152 'os': ['Amiga'], | 152 'os': ['Amiga'], |
| 153 'pool': ['default'], | 153 'pool': ['default'], |
| 154 }, | 154 }, |
| 155 'state': { | 155 'state': { |
| 156 'running_time': 1234.0, | 156 'running_time': 1234.0, |
| 157 'sleep_streak': 0, | 157 'sleep_streak': 0, |
| 158 'started_ts': 1410990411.111, | 158 'started_ts': 1410990411.111, |
| 159 }, | 159 }, |
| 160 'version': '123', | 160 'version': '123', |
| 161 } | 161 } |
| 162 response = self.app.post_json( | 162 response = self.app.post_json( |
| 163 '/swarming/api/v1/bot/handshake', | 163 '/swarming/api/v1/bot/handshake', |
| 164 params=params).json | 164 params=params, status=status) |
| 165 if status != 200: |
| 166 return |
| 167 response = response.json |
| 165 self.bot_version = response['bot_version'] | 168 self.bot_version = response['bot_version'] |
| 166 params['version'] = self.bot_version | 169 params['version'] = self.bot_version |
| 167 params['state']['bot_group_cfg_version'] = response['bot_group_cfg_version'] | 170 params['state']['bot_group_cfg_version'] = response['bot_group_cfg_version'] |
| 171 # A bit hackish but fine for unit testing purpose. |
| 172 if response.get('bot_config'): |
| 173 params['bot_config'] = response['bot_config'] |
| 168 return params | 174 return params |
| 169 | 175 |
| 170 def bot_poll(self, bot='bot1'): | 176 def bot_poll(self, bot='bot1'): |
| 171 """Simulates a bot that polls for task.""" | 177 """Simulates a bot that polls for task.""" |
| 172 params = self.do_handshake(bot) | 178 params = self.do_handshake(bot) |
| 173 return self.post_json('/swarming/api/v1/bot/poll', params) | 179 return self.post_json('/swarming/api/v1/bot/poll', params) |
| 174 | 180 |
| 175 def bot_complete_task(self, **kwargs): | 181 def bot_complete_task(self, **kwargs): |
| 176 # Emulate an isolated task. | 182 # Emulate an isolated task. |
| 177 params = { | 183 params = { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 | 279 |
| 274 def client_create_task_raw(self, properties=None, **kwargs): | 280 def client_create_task_raw(self, properties=None, **kwargs): |
| 275 """Creates a raw command TaskRequest via the Cloud Endpoints API.""" | 281 """Creates a raw command TaskRequest via the Cloud Endpoints API.""" |
| 276 properties = (properties or {}).copy() | 282 properties = (properties or {}).copy() |
| 277 properties['command'] = ['python', 'run_test.py'] | 283 properties['command'] = ['python', 'run_test.py'] |
| 278 return self._client_create_task(properties, **kwargs) | 284 return self._client_create_task(properties, **kwargs) |
| 279 | 285 |
| 280 def client_get_results(self, task_id): | 286 def client_get_results(self, task_id): |
| 281 return self.endpoint_call( | 287 return self.endpoint_call( |
| 282 handlers_endpoints.SwarmingTaskService, 'result', {'task_id': task_id}) | 288 handlers_endpoints.SwarmingTaskService, 'result', {'task_id': task_id}) |
| OLD | NEW |