| OLD | NEW |
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 # Copyright 2014 The Swarming Authors. All rights reserved. | 2 # Copyright 2014 The Swarming Authors. All rights reserved. |
| 3 # Use of this source code is governed by the Apache v2.0 license that can be | 3 # Use of this source code is governed by the Apache v2.0 license that can be |
| 4 # found in the LICENSE file. | 4 # 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 def _parse_line(self, line): | 71 def _parse_line(self, line): |
| 72 # pylint: disable=W0212 | 72 # pylint: disable=W0212 |
| 73 actual = stats._parse_line(line, stats._Snapshot(), {}, {}, {}) | 73 actual = stats._parse_line(line, stats._Snapshot(), {}, {}, {}) |
| 74 self.assertEqual(True, actual, line) | 74 self.assertEqual(True, actual, line) |
| 75 | 75 |
| 76 def set_as_anonymous(self): | 76 def set_as_anonymous(self): |
| 77 """Removes all IPs from the whitelist.""" | 77 """Removes all IPs from the whitelist.""" |
| 78 self.testbed.setup_env(USER_EMAIL='', overwrite=True) | 78 self.testbed.setup_env(USER_EMAIL='', overwrite=True) |
| 79 auth.ip_whitelist_key(auth.BOTS_IP_WHITELIST).delete() | 79 auth.ip_whitelist_key(auth.BOTS_IP_WHITELIST).delete() |
| 80 auth_testing.reset_local_state() | 80 auth_testing.reset_local_state() |
| 81 auth_testing.mock_get_current_identity(self, auth.Anonymous) |
| 81 | 82 |
| 82 def set_as_super_admin(self): | 83 def set_as_super_admin(self): |
| 83 self.set_as_anonymous() | 84 self.set_as_anonymous() |
| 84 self.testbed.setup_env(USER_EMAIL='super-admin@example.com', overwrite=True) | 85 self.testbed.setup_env(USER_EMAIL='super-admin@example.com', overwrite=True) |
| 86 auth_testing.reset_local_state() |
| 87 auth_testing.mock_get_current_identity( |
| 88 self, auth.Identity.from_bytes('user:' + os.environ['USER_EMAIL'])) |
| 85 | 89 |
| 86 def set_as_admin(self): | 90 def set_as_admin(self): |
| 87 self.set_as_anonymous() | 91 self.set_as_anonymous() |
| 88 self.testbed.setup_env(USER_EMAIL='admin@example.com', overwrite=True) | 92 self.testbed.setup_env(USER_EMAIL='admin@example.com', overwrite=True) |
| 93 auth_testing.reset_local_state() |
| 94 auth_testing.mock_get_current_identity( |
| 95 self, auth.Identity.from_bytes('user:' + os.environ['USER_EMAIL'])) |
| 89 | 96 |
| 90 def set_as_privileged_user(self): | 97 def set_as_privileged_user(self): |
| 91 self.set_as_anonymous() | 98 self.set_as_anonymous() |
| 92 self.testbed.setup_env(USER_EMAIL='priv@example.com', overwrite=True) | 99 self.testbed.setup_env(USER_EMAIL='priv@example.com', overwrite=True) |
| 100 auth_testing.reset_local_state() |
| 101 auth_testing.mock_get_current_identity( |
| 102 self, auth.Identity.from_bytes('user:' + os.environ['USER_EMAIL'])) |
| 93 | 103 |
| 94 def set_as_user(self): | 104 def set_as_user(self): |
| 95 self.set_as_anonymous() | 105 self.set_as_anonymous() |
| 96 self.testbed.setup_env(USER_EMAIL='user@example.com', overwrite=True) | 106 self.testbed.setup_env(USER_EMAIL='user@example.com', overwrite=True) |
| 107 auth_testing.reset_local_state() |
| 108 auth_testing.mock_get_current_identity( |
| 109 self, auth.Identity.from_bytes('user:' + os.environ['USER_EMAIL'])) |
| 97 | 110 |
| 98 def set_as_bot(self): | 111 def set_as_bot(self): |
| 99 self.set_as_anonymous() | 112 self.set_as_anonymous() |
| 100 auth.bootstrap_ip_whitelist(auth.BOTS_IP_WHITELIST, [self.source_ip]) | 113 auth.bootstrap_ip_whitelist(auth.BOTS_IP_WHITELIST, [self.source_ip]) |
| 114 auth_testing.reset_local_state() |
| 115 auth_testing.mock_get_current_identity( |
| 116 self, auth.Identity.from_bytes('bot:' + self.source_ip)) |
| 101 | 117 |
| 102 # Web or generic | 118 # Web or generic |
| 103 | 119 |
| 104 def get_xsrf_token(self): | 120 def get_xsrf_token(self): |
| 105 """Gets the generic XSRF token for web clients.""" | 121 """Gets the generic XSRF token for web clients.""" |
| 106 resp = self.auth_app.post( | 122 resp = self.auth_app.post( |
| 107 '/auth/api/v1/accounts/self/xsrf_token', | 123 '/auth/api/v1/accounts/self/xsrf_token', |
| 108 headers={'X-XSRF-Token-Request': '1'}).json | 124 headers={'X-XSRF-Token-Request': '1'}).json |
| 109 return resp['xsrf_token'].encode('ascii') | 125 return resp['xsrf_token'].encode('ascii') |
| 110 | 126 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 177 |
| 162 def bot_run_task(self): | 178 def bot_run_task(self): |
| 163 token, _ = self.get_bot_token() | 179 token, _ = self.get_bot_token() |
| 164 res = self.bot_poll() | 180 res = self.bot_poll() |
| 165 task_id = res['manifest']['task_id'] | 181 task_id = res['manifest']['task_id'] |
| 166 self.bot_complete_task(token, task_id=task_id) | 182 self.bot_complete_task(token, task_id=task_id) |
| 167 return task_id | 183 return task_id |
| 168 | 184 |
| 169 # Client | 185 # Client |
| 170 | 186 |
| 171 def get_client_token(self): | 187 def endpoint_call(self, service, name, args): |
| 172 """Gets the XSRF token for client after handshake.""" | 188 body = json.loads(protojson.encode_message(args)) |
| 173 headers = {'X-XSRF-Token-Request': '1'} | 189 return test_case.Endpoints(service).call_api(name, body=body).json |
| 174 params = {} | |
| 175 response = self.app.post_json( | |
| 176 '/swarming/api/v1/client/handshake', | |
| 177 headers=headers, | |
| 178 params=params).json | |
| 179 return response['xsrf_token'].encode('ascii') | |
| 180 | 190 |
| 181 def client_create_task_isolated(self, properties=None, **kwargs): | 191 def _client_create_task(self, properties=None, **kwargs): |
| 182 """Creates a TaskRequest via the Cloud Endpoints API.""" | 192 """Creates an isolated command TaskRequest via the Cloud Endpoints API.""" |
| 183 params = { | 193 params = { |
| 184 'dimensions': [ | 194 'dimensions': [ |
| 185 {'key': 'os', 'value': 'Amiga'}, | 195 {'key': 'os', 'value': 'Amiga'}, |
| 186 ], | 196 ], |
| 187 'env': [], | 197 'env': [], |
| 188 'execution_timeout_secs': 3600, | 198 'execution_timeout_secs': 3600, |
| 189 'io_timeout_secs': 1200, | 199 'io_timeout_secs': 1200, |
| 190 'inputs_ref': { | |
| 191 'isolated': '0123456789012345678901234567890123456789', | |
| 192 'isolatedserver': 'http://localhost:1', | |
| 193 'namespace': 'default-gzip', | |
| 194 }, | |
| 195 } | 200 } |
| 196 params.update(properties or {}) | 201 params.update(properties or {}) |
| 197 props = swarming_rpcs.TaskProperties(**params) | 202 props = swarming_rpcs.TaskProperties(**params) |
| 198 | 203 |
| 199 params = { | 204 params = { |
| 200 'expiration_secs': 24*60*60, | 205 'expiration_secs': 24*60*60, |
| 201 'name': 'hi', | 206 'name': 'hi', |
| 202 'priority': 10, | 207 'priority': 10, |
| 203 'tags': [], | 208 'tags': [], |
| 204 'user': 'joe@localhost', | 209 'user': 'joe@localhost', |
| 205 } | 210 } |
| 206 params.update(kwargs) | 211 params.update(kwargs) |
| 207 request = swarming_rpcs.TaskRequest(properties=props, **params) | 212 request = swarming_rpcs.TaskRequest(properties=props, **params) |
| 208 api = test_case.Endpoints(handlers_endpoints.SwarmingTasksService) | 213 response = self.endpoint_call( |
| 209 # Poor's man authentication. | 214 handlers_endpoints.SwarmingTasksService, 'new', request) |
| 210 old_get_current_user = endpoints.get_current_user | |
| 211 endpoints.get_current_user = lambda: users.User( | |
| 212 'admin@example.com', 'localhost') | |
| 213 try: | |
| 214 response = api.call_api( | |
| 215 'new', body=json.loads(protojson.encode_message(request))) | |
| 216 finally: | |
| 217 endpoints.get_current_user = old_get_current_user | |
| 218 response = response.json | |
| 219 return response, response['task_id'] | 215 return response, response['task_id'] |
| 220 | 216 |
| 217 def client_create_task_isolated(self, properties=None, **kwargs): |
| 218 properties = (properties or {}).copy() |
| 219 properties['inputs_ref'] = { |
| 220 'isolated': '0123456789012345678901234567890123456789', |
| 221 'isolatedserver': 'http://localhost:1', |
| 222 'namespace': 'default-gzip', |
| 223 } |
| 224 return self._client_create_task(properties, **kwargs) |
| 225 |
| 221 def client_create_task_raw(self, properties=None, **kwargs): | 226 def client_create_task_raw(self, properties=None, **kwargs): |
| 222 """Creates a TaskRequest via the client API.""" | 227 """Creates a raw command TaskRequest via the Cloud Endpoints API.""" |
| 223 token = self.get_client_token() | 228 properties = (properties or {}).copy() |
| 224 params = { | 229 properties['command'] = ['python', 'run_test.py'] |
| 225 'name': 'hi', | 230 return self._client_create_task(properties, **kwargs) |
| 226 'priority': 10, | |
| 227 'properties': { | |
| 228 'commands': [['python', 'run_test.py']], | |
| 229 'data': [], | |
| 230 'dimensions': {'os': 'Amiga'}, | |
| 231 'env': {}, | |
| 232 'execution_timeout_secs': 3600, | |
| 233 'io_timeout_secs': 1200, | |
| 234 }, | |
| 235 'scheduling_expiration_secs': 24*60*60, | |
| 236 'tags': [], | |
| 237 'user': 'joe@localhost', | |
| 238 } | |
| 239 params.update(kwargs) | |
| 240 params['properties'].update(properties or {}) | |
| 241 response = self.post_with_token( | |
| 242 '/swarming/api/v1/client/request', params, token) | |
| 243 return response, response['task_id'] | |
| 244 | 231 |
| 245 def client_get_results(self, task_id): | 232 def client_get_results(self, task_id): |
| 246 return self.app.get( | 233 api = test_case.Endpoints(handlers_endpoints.SwarmingTaskService) |
| 247 '/swarming/api/v1/client/task/%s' % task_id).json | 234 return api.call_api('result', body={'task_id': task_id}).json |
| OLD | NEW |