forked from luci/luci-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation_test.py
executable file
·487 lines (445 loc) · 14.2 KB
/
validation_test.py
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env python
# Copyright 2015 The LUCI Authors. All rights reserved.
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.
import base64
import logging
from test_env import future
import test_env
test_env.setup_test_env()
from google.appengine.ext import ndb
from test_support import test_case
import mock
from components import config
from components import net
from components.config import validation_context
from components.config.proto import project_config_pb2
from components.config.proto import service_config_pb2
import services
import storage
import validation
class ValidationTestCase(test_case.TestCase):
def setUp(self):
super(ValidationTestCase, self).setUp()
self.services = []
self.mock(services, 'get_services_async', lambda: future(self.services))
def test_validate_project_registry(self):
cfg = '''
projects {
id: "a"
config_location {
storage_type: GITILES
url: "https://a.googlesource.com/project"
}
}
projects {
id: "b"
}
projects {
id: "a"
config_location {
storage_type: GITILES
url: "https://no-project.googlesource.com"
}
}
projects {
config_location {
storage_type: GITILES
url: "https://no-project.googlesource.com/bad_plus/+"
}
}
'''
result = validation.validate_config(
config.self_config_set(), 'projects.cfg', cfg)
self.assertEqual(
[m.text for m in result.messages],
[
'Project b: config_location: storage_type is not set',
'Project a: id is not unique',
('Project a: config_location: Invalid Gitiles repo url: '
'https://no-project.googlesource.com'),
'Project #4: id is not specified',
('Project #4: config_location: Invalid Gitiles repo url: '
'https://no-project.googlesource.com/bad_plus/+'),
'Projects are not sorted by id. First offending id: a',
]
)
def test_validate_acl_cfg(self):
cfg = '''
invalid_field: "admins"
'''
result = validation.validate_config(
config.self_config_set(), 'acl.cfg', cfg)
self.assertEqual(len(result.messages), 1)
self.assertEqual(result.messages[0].severity, logging.ERROR)
self.assertTrue('no field named "invalid_field"' in result.messages[0].text)
cfg = '''
project_access_group: "admins"
'''
result = validation.validate_config(
config.self_config_set(), 'acl.cfg', cfg)
self.assertEqual(len(result.messages), 0)
def test_validate_services_registry(self):
cfg = '''
services {
id: "a"
access: "[email protected]"
access: "user:[email protected]"
access: "group:abc"
}
services {
owners: "not an email"
config_location {
storage_type: GITILES
url: "../some"
}
metadata_url: "not an url"
access: "**&"
access: "group:**&"
access: "a:b"
}
services {
id: "b"
config_location {
storage_type: GITILES
url: "https://gitiles.host.com/project"
}
}
services {
id: "a-unsorted"
}
'''
result = validation.validate_config(
config.self_config_set(), 'services.cfg', cfg)
self.assertEqual(
[m.text for m in result.messages],
[
'Service #2: id is not specified',
('Service #2: config_location: '
'storage_type must not be set if relative url is used'),
'Service #2: invalid email: "not an email"',
'Service #2: metadata_url: hostname not specified',
'Service #2: metadata_url: scheme must be "https"',
'Service #2: access #1: invalid email: "**&"',
'Service #2: access #2: invalid group: **&',
'Service #2: access #3: Identity has invalid format: b',
'Services are not sorted by id. First offending id: a-unsorted',
]
)
def test_validate_service_dynamic_metadata_blob(self):
def expect_errors(blob, expected_messages):
ctx = config.validation.Context()
validation.validate_service_dynamic_metadata_blob(blob, ctx)
self.assertEqual(
[m.text for m in ctx.result().messages], expected_messages)
expect_errors([], ['Service dynamic metadata must be an object'])
expect_errors({}, ['Expected format version 1.0, but found "None"'])
expect_errors(
{'version': '1.0', 'validation': 'bad'},
['validation: must be an object'])
expect_errors(
{
'version': '1.0',
'validation': {
'patterns': 'bad',
}
},
[
'validation: url: not specified',
'validation: patterns must be a list',
])
expect_errors(
{
'version': '1.0',
'validation': {
'url': 'bad url',
'patterns': [
'bad',
{
},
{
'config_set': 'a:b',
'path': '/foo',
},
{
'config_set': 'regex:)(',
'path': '../b',
},
{
'config_set': 'projects/foo',
'path': 'bar.cfg',
},
]
}
},
[
'validation: url: hostname not specified',
'validation: url: scheme must be "https"',
'validation: pattern #1: must be an object',
'validation: pattern #2: config_set: Pattern must be a string',
'validation: pattern #2: path: Pattern must be a string',
'validation: pattern #3: config_set: Invalid pattern kind: a',
'validation: pattern #3: path: must not be absolute: /foo',
'validation: pattern #4: config_set: unbalanced parenthesis',
('validation: pattern #4: path: '
'must not contain ".." or "." components: ../b'),
]
)
def test_validate_schemas(self):
cfg = '''
schemas {
name: "services/config:foo"
url: "https://foo"
}
schemas {
name: "projects:foo"
url: "https://foo"
}
schemas {
name: "projects/refs:foo"
url: "https://foo"
}
# Invalid schemas.
schemas {
}
schemas {
name: "services/config:foo"
url: "https://foo"
}
schemas {
name: "no_colon"
url: "http://foo"
}
schemas {
name: "bad_prefix:foo"
url: "https://foo"
}
schemas {
name: "projects:foo/../a.cfg"
url: "https://foo"
}
'''
result = validation.validate_config(
config.self_config_set(), 'schemas.cfg', cfg)
self.assertEqual(
[m.text for m in result.messages],
[
'Schema #4: name is not specified',
'Schema #4: url: not specified',
'Schema services/config:foo: duplicate schema name',
'Schema no_colon: name must contain ":"',
'Schema no_colon: url: scheme must be "https"',
(
'Schema bad_prefix:foo: left side of ":" must be a service config '
'set, "projects" or "projects/refs"'),
(
'Schema projects:foo/../a.cfg: '
'must not contain ".." or "." components: foo/../a.cfg'),
]
)
def test_validate_project_metadata(self):
cfg = '''
name: "Chromium"
access: "group:all"
access: "[email protected]"
'''
result = validation.validate_config('projects/x', 'project.cfg', cfg)
self.assertEqual(len(result.messages), 0)
def test_validate_refs(self):
cfg = '''
refs {
name: "refs/heads/master"
}
# Invalid configs
refs {
}
refs {
name: "refs/heads/master"
config_path: "non_default"
}
refs {
name: "does_not_start_with_ref"
config_path: "../bad/path"
}
'''
result = validation.validate_config('projects/x', 'refs.cfg', cfg)
self.assertEqual(
[m.text for m in result.messages],
[
'Ref #2: name is not specified',
'Ref #3: duplicate ref: refs/heads/master',
'Ref #4: name does not start with "refs/": does_not_start_with_ref',
'Ref #4: must not contain ".." or "." components: ../bad/path'
],
)
def test_validation_by_service_async(self):
cfg = '# a config'
cfg_b64 = base64.b64encode(cfg)
self.services = [
service_config_pb2.Service(id='a'),
service_config_pb2.Service(id='b'),
service_config_pb2.Service(id='c'),
]
@ndb.tasklet
def get_metadata_async(service_id):
if service_id == 'a':
raise ndb.Return(service_config_pb2.ServiceDynamicMetadata(
validation=service_config_pb2.Validator(
patterns=[service_config_pb2.ConfigPattern(
config_set='services/foo',
path='bar.cfg',
)],
url='https://bar.verifier',
)
))
if service_id == 'b':
raise ndb.Return(service_config_pb2.ServiceDynamicMetadata(
validation=service_config_pb2.Validator(
patterns=[service_config_pb2.ConfigPattern(
config_set=r'regex:projects/[^/]+',
path=r'regex:.+\.cfg',
)],
url='https://bar2.verifier',
)))
if service_id == 'c':
raise ndb.Return(service_config_pb2.ServiceDynamicMetadata(
validation=service_config_pb2.Validator(
patterns=[service_config_pb2.ConfigPattern(
config_set=r'regex:.+',
path=r'regex:.+',
)],
url='https://ultimate.verifier',
)))
return None
self.mock(services, 'get_metadata_async', mock.Mock())
services.get_metadata_async.side_effect = get_metadata_async
@ndb.tasklet
def json_request_async(url, **kwargs):
raise ndb.Return({
'messages': [{
'text': 'OK from %s' % url,
# default severity
}],
})
self.mock(
net, 'json_request_async', mock.Mock(side_effect=json_request_async))
############################################################################
result = validation.validate_config('services/foo', 'bar.cfg', cfg)
self.assertEqual(
result.messages,
[
validation_context.Message(
text='OK from https://bar.verifier', severity=logging.INFO),
validation_context.Message(
text='OK from https://ultimate.verifier', severity=logging.INFO)
])
net.json_request_async.assert_any_call(
'https://bar.verifier',
method='POST',
payload={
'config_set': 'services/foo',
'path': 'bar.cfg',
'content': cfg_b64,
},
scopes=net.EMAIL_SCOPE,
)
net.json_request_async.assert_any_call(
'https://ultimate.verifier',
method='POST',
payload={
'config_set': 'services/foo',
'path': 'bar.cfg',
'content': cfg_b64,
},
scopes=net.EMAIL_SCOPE,
)
############################################################################
result = validation.validate_config('projects/foo', 'bar.cfg', cfg)
self.assertEqual(
result.messages,
[
validation_context.Message(
text='OK from https://bar2.verifier', severity=logging.INFO),
validation_context.Message(
text='OK from https://ultimate.verifier', severity=logging.INFO)
])
net.json_request_async.assert_any_call(
'https://bar2.verifier',
method='POST',
payload={
'config_set': 'projects/foo',
'path': 'bar.cfg',
'content': cfg_b64,
},
scopes=net.EMAIL_SCOPE,
)
net.json_request_async.assert_any_call(
'https://ultimate.verifier',
method='POST',
payload={
'config_set': 'projects/foo',
'path': 'bar.cfg',
'content': cfg_b64,
},
scopes=net.EMAIL_SCOPE,
)
############################################################################
# Error found
net.json_request_async.side_effect = None
net.json_request_async.return_value = ndb.Future()
net.json_request_async.return_value.set_result({
'messages': [{
'text': 'error',
'severity': 'ERROR'
}]
})
result = validation.validate_config('projects/baz/refs/x', 'qux.cfg', cfg)
self.assertEqual(
result.messages,
[
validation_context.Message(text='error', severity=logging.ERROR)
])
############################################################################
# Less-expected responses
res = {
'messages': [
{'severity': 'invalid severity'},
{},
[]
]
}
net.json_request_async.return_value = ndb.Future()
net.json_request_async.return_value.set_result(res)
result = validation.validate_config('projects/baz/refs/x', 'qux.cfg', cfg)
self.assertEqual(
result.messages,
[
validation_context.Message(
severity=logging.CRITICAL,
text=(
'Error during external validation: invalid response: '
'unexpected message severity: invalid severity\n'
'url: https://ultimate.verifier\n'
'config_set: projects/baz/refs/x\n'
'path: qux.cfg\n'
'response: %r' % res),
),
validation_context.Message(severity=logging.INFO, text=''),
validation_context.Message(
severity=logging.CRITICAL,
text=(
'Error during external validation: invalid response: '
'message is not a dict: []\n'
'url: https://ultimate.verifier\n'
'config_set: projects/baz/refs/x\n'
'path: qux.cfg\n'
'response: %r' % res),
),
],
)
def test_validate_json_files(self):
with self.assertRaises(ValueError):
config.validation.DEFAULT_RULE_SET.validate(
'services/luci-config', 'a.json', '[1,]')
if __name__ == '__main__':
test_env.main()