forked from luci/luci-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_env_handlers.py
More file actions
267 lines (229 loc) · 8.48 KB
/
test_env_handlers.py
File metadata and controls
267 lines (229 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# coding: utf-8
# Copyright 2014 The LUCI Authors. All rights reserved.
# Use of this source code is governed by the Apache v2.0 license that can be
# found in the LICENSE file.
"""Base class for handlers_*_test.py"""
import base64
import json
import os
import test_env
test_env.setup_test_env()
from google.appengine.api import users
from google.appengine.ext import ndb
import endpoints
from protorpc.remote import protojson
import webtest
import handlers_endpoints
import swarming_rpcs
from components import auth
from components import auth_testing
from components import stats_framework
import gae_ts_mon
from test_support import test_case
from server import acl
from server import large
from server import stats
PINNED_PACKAGE_VERSION = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef'
class AppTestBase(test_case.TestCase):
APP_DIR = test_env.APP_DIR
def setUp(self):
super(AppTestBase, self).setUp()
self.bot_version = None
self.source_ip = '192.168.2.2'
self.testbed.init_user_stub()
self.testbed.init_search_stub()
gae_ts_mon.reset_for_unittest(disable=True)
# By default requests in tests are coming from bot with fake IP.
# WSGI app that implements auth REST API.
self.auth_app = webtest.TestApp(
auth.create_wsgi_application(debug=True),
extra_environ={
'REMOTE_ADDR': self.source_ip,
'SERVER_SOFTWARE': os.environ['SERVER_SOFTWARE'],
})
# Note that auth.ADMIN_GROUP != acl.ADMINS_GROUP.
auth.bootstrap_group(
auth.ADMIN_GROUP,
[auth.Identity(auth.IDENTITY_USER, 'super-admin@example.com')])
auth.bootstrap_group(
acl.ADMINS_GROUP,
[auth.Identity(auth.IDENTITY_USER, 'admin@example.com')])
auth.bootstrap_group(
acl.PRIVILEGED_USERS_GROUP,
[auth.Identity(auth.IDENTITY_USER, 'priv@example.com')])
auth.bootstrap_group(
acl.USERS_GROUP,
[auth.Identity(auth.IDENTITY_USER, 'user@example.com')])
auth.bootstrap_group(
acl.BOTS_GROUP,
[auth.Identity(auth.IDENTITY_BOT, self.source_ip)])
self.mock(stats_framework, 'add_entry', self._parse_line)
def _parse_line(self, line):
# pylint: disable=W0212
actual = stats._parse_line(line, stats._Snapshot(), {}, {}, {})
self.assertEqual(True, actual, line)
def set_as_anonymous(self):
"""Removes all IPs from the whitelist."""
self.testbed.setup_env(USER_EMAIL='', overwrite=True)
auth.ip_whitelist_key(auth.BOTS_IP_WHITELIST).delete()
auth_testing.reset_local_state()
auth_testing.mock_get_current_identity(self, auth.Anonymous)
def set_as_super_admin(self):
self.set_as_anonymous()
self.testbed.setup_env(USER_EMAIL='super-admin@example.com', overwrite=True)
auth_testing.reset_local_state()
auth_testing.mock_get_current_identity(
self, auth.Identity.from_bytes('user:' + os.environ['USER_EMAIL']))
def set_as_admin(self):
self.set_as_anonymous()
self.testbed.setup_env(USER_EMAIL='admin@example.com', overwrite=True)
auth_testing.reset_local_state()
auth_testing.mock_get_current_identity(
self, auth.Identity.from_bytes('user:' + os.environ['USER_EMAIL']))
def set_as_privileged_user(self):
self.set_as_anonymous()
self.testbed.setup_env(USER_EMAIL='priv@example.com', overwrite=True)
auth_testing.reset_local_state()
auth_testing.mock_get_current_identity(
self, auth.Identity.from_bytes('user:' + os.environ['USER_EMAIL']))
def set_as_user(self):
self.set_as_anonymous()
self.testbed.setup_env(USER_EMAIL='user@example.com', overwrite=True)
auth_testing.reset_local_state()
auth_testing.mock_get_current_identity(
self, auth.Identity.from_bytes('user:' + os.environ['USER_EMAIL']))
def set_as_bot(self):
self.set_as_anonymous()
auth.bootstrap_ip_whitelist(auth.BOTS_IP_WHITELIST, [self.source_ip])
auth_testing.reset_local_state()
auth_testing.mock_get_current_identity(
self, auth.Identity.from_bytes('bot:' + self.source_ip))
# Web or generic
def get_xsrf_token(self):
"""Gets the generic XSRF token for web clients."""
resp = self.auth_app.post(
'/auth/api/v1/accounts/self/xsrf_token',
headers={'X-XSRF-Token-Request': '1'}).json
return resp['xsrf_token'].encode('ascii')
def post_with_token(self, url, params, token, **kwargs):
"""Does an HTTP POST with a JSON API and a XSRF token."""
return self.app.post_json(
url, params=params, headers={'X-XSRF-Token': token}, **kwargs).json
# Bot
def get_bot_token(self, bot='bot1'):
"""Gets the XSRF token for bot after handshake."""
headers = {'X-XSRF-Token-Request': '1'}
params = {
'dimensions': {
'id': [bot],
'os': ['Amiga'],
'pool': ['default'],
},
'state': {
'running_time': 1234.0,
'sleep_streak': 0,
'started_ts': 1410990411.111,
},
'version': '123',
}
response = self.app.post_json(
'/swarming/api/v1/bot/handshake',
headers=headers,
params=params).json
token = response['xsrf_token'].encode('ascii')
self.bot_version = response['bot_version']
params['version'] = self.bot_version
return token, params
def bot_poll(self, bot='bot1'):
"""Simulates a bot that polls for task."""
token, params = self.get_bot_token(bot)
return self.post_with_token('/swarming/api/v1/bot/poll', params, token)
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_run_task(self):
token, _ = self.get_bot_token()
res = self.bot_poll()
task_id = res['manifest']['task_id']
self.bot_complete_task(token, task_id=task_id)
return task_id
# Client
def endpoint_call(self, service, name, args):
body = json.loads(protojson.encode_message(args))
return test_case.Endpoints(service).call_api(name, body=body).json
def _client_create_task(self, properties=None, **kwargs):
"""Creates an isolated command TaskRequest via the Cloud Endpoints API."""
params = {
'packages': [{
'package_name': 'rm',
'version': PINNED_PACKAGE_VERSION,
}],
'dimensions': [
{'key': 'os', 'value': 'Amiga'},
{'key': 'pool', 'value': 'default'},
],
'env': [],
'execution_timeout_secs': 3600,
'io_timeout_secs': 1200,
}
params.update(properties or {})
props = swarming_rpcs.TaskProperties(**params)
params = {
'expiration_secs': 24*60*60,
'name': 'hi',
'priority': 10,
'tags': [],
'user': 'joe@localhost',
}
params.update(kwargs)
request = swarming_rpcs.TaskRequest(properties=props, **params)
response = self.endpoint_call(
handlers_endpoints.SwarmingTasksService, 'new', request)
return response, response['task_id']
def client_create_task_isolated(self, properties=None, **kwargs):
properties = (properties or {}).copy()
properties['inputs_ref'] = {
'isolated': '0123456789012345678901234567890123456789',
'isolatedserver': 'http://localhost:1',
'namespace': 'default-gzip',
}
return self._client_create_task(properties, **kwargs)
def client_create_task_raw(self, properties=None, **kwargs):
"""Creates a raw command TaskRequest via the Cloud Endpoints API."""
properties = (properties or {}).copy()
properties['command'] = ['python', 'run_test.py']
return self._client_create_task(properties, **kwargs)
def client_get_results(self, task_id):
api = test_case.Endpoints(handlers_endpoints.SwarmingTaskService)
return api.call_api('result', body={'task_id': task_id}).json