-
Notifications
You must be signed in to change notification settings - Fork 36
/
test_parser.py
623 lines (507 loc) · 20.1 KB
/
test_parser.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# -*- coding: utf-8 -*-
# Any copyright is dedicated to the Public Domain.
# http://creativecommons.org/publicdomain/zero/1.0/
from pathlib import Path
import re
import sys
import textwrap
import pytest
from jsonschema import _utils
from glean_parser import metrics
from glean_parser import parser
import util
ROOT = Path(__file__).parent
def test_parser():
"""Test the basics of parsing a single file."""
all_metrics = parser.parse_objects(
[ROOT / "data" / "core.yaml", ROOT / "data" / "pings.yaml"],
config={"allow_reserved": True},
)
errs = list(all_metrics)
assert len(errs) == 0
for category_key, category_val in all_metrics.value.items():
if category_key == "pings":
continue
for metric_key, metric_val in category_val.items():
assert isinstance(metric_val, metrics.Metric)
assert isinstance(metric_val.lifetime, metrics.Lifetime)
if getattr(metric_val, "labels", None) is not None:
assert isinstance(metric_val.labels, set)
pings = all_metrics.value["pings"]
assert pings["custom-ping"].name == "custom-ping"
assert pings["custom-ping"].include_client_id is False
assert pings["really-custom-ping"].name == "really-custom-ping"
assert pings["really-custom-ping"].include_client_id is True
def test_parser_invalid():
"""Test the basics of parsing a single file."""
all_metrics = parser.parse_objects(ROOT / "data" / "invalid.yamlx")
errors = list(all_metrics)
assert len(errors) == 1
assert "could not determine a constructor for the tag" in errors[0]
def test_parser_schema_violation():
"""1507792"""
all_metrics = parser.parse_objects(ROOT / "data" / "schema-violation.yaml")
errors = list(all_metrics)
found_errors = set(
re.sub(r"\s", "", str(error).split("\n", 1)[1].strip()) for error in errors
)
expected_errors = [
"""
```
gleantest:
test_event:
type: event
```
Missing required properties: bugs, data_reviews, description, expires,
notification_emails
Documentation for this node:
Describes a single metric.
See https://mozilla.github.io/glean_parser/metrics-yaml.html
""",
"""
```
gleantest.lifetime:
test_counter_inv_lt:
lifetime: user2
```
'user2' is not one of ['ping', 'user', 'application']
Documentation for this node:
Definesthelifetimeofthe metric. It must be one of the following values:
- `ping` (default): The metric is reset each time it is sent in a ping.
- `user`: The metric contains a property that is part of the user's
profile and is never reset.
- `application`: The metric contains a property that is related to the
application, and is reset only at application restarts.
""",
"""
```
gleantest.foo:
a: b
```
'b' is not of type 'object'
Documentation for this node:
Describes a single metric.
See https://mozilla.github.io/glean_parser/metrics-yaml.html
""",
"""
```
gleantest.with.way.too.long.category.name
...
```
'gleantest.with.way.too.long.category.name' is not valid under any of
the given schemas
'gleantest.with.way.too.long.category.name' is too long
'gleantest.with.way.too.long.category.name' is not one of
['$schema']
""",
"""
```
gleantest.short.category:very_long_metric_name_this_is_too_long_s_well
```
'very_long_metric_name_this_is_too_long_s_well' is not valid under any
of the given schemas
'very_long_metric_name_this_is_too_long_s_well' is too long
""",
]
# The validator reports a different error based on the python version, so
# we need to provide two copies for this to work.
if sys.version_info < (3, 7):
expected_errors.append(
"""
```
gleantest.event:
event_too_many_extras:
extra_keys:
key_1:
description: Sample extra key
key_2:
description: Sample extra key
key_3:
description: Sample extra key
key_4:
description: Sample extra key
key_5:
description: Sample extra key
key_6:
description: Sample extra key
key_7:
description: Sample extra key
key_8:
description: Sample extra key
key_9:
description: Sample extra key
key_10:
description: Sample extra key
key_11:
description: Sample extra key
```
OrderedDict([('key_1', OrderedDict([('description', 'Sample extra
key')])), ('key_2', OrderedDict([('description', 'Sample extra
key')])), ('key_3', OrderedDict([('description', 'Sample extra
key')])), ('key_4', OrderedDict([('description', 'Sample extra
key')])), ('key_5', OrderedDict([('description', 'Sample extra
key')])), ('key_6', OrderedDict([('description', 'Sample extra
key')])), ('key_7', OrderedDict([('description', 'Sample extra
key')])), ('key_8', OrderedDict([('description', 'Sample extra
key')])), ('key_9', OrderedDict([('description', 'Sample extra
key')])), ('key_10', OrderedDict([('description', 'Sample extra
key')])), ('key_11', OrderedDict([('description', 'Sample extra
key')]))]) has too many properties
Documentation for this node:
The acceptable keys on the "extra" object sent with events. This is an
object mapping the key to an object containing metadata about the key.
A maximum of 10 extra keys is allowed.
This metadata object has the following keys:
- `description`: **Required.** A description of the key.
Valid when `type`_ is `event`.
"""
)
else:
expected_errors.append(
"""
```
gleantest.event:
event_too_many_extras:
extra_keys:
key_1:
description: Sample extra key
key_10:
description: Sample extra key
key_11:
description: Sample extra key
key_2:
description: Sample extra key
key_3:
description: Sample extra key
key_4:
description: Sample extra key
key_5:
description: Sample extra key
key_6:
description: Sample extra key
key_7:
description: Sample extra key
key_8:
description: Sample extra key
key_9:
description: Sample extra key
```
{'key_1': {'description': 'Sample extra key'}, 'key_2':
{'description': 'Sample extra key'}, 'key_3': {'description': 'Sample
extra key'}, 'key_4': {'description': 'Sample extra key'}, 'key_5':
{'description': 'Sample extra key'}, 'key_6': {'description': 'Sample
extra key'}, 'key_7': {'description': 'Sample extra key'}, 'key_8':
{'description': 'Sample extra key'}, 'key_9': {'description': 'Sample
extra key'}, 'key_10': {'description': 'Sample extra key'}, 'key_11':
{'description': 'Sample extra key'}} has too many properties
Documentation for this node:
The acceptable keys on the "extra" object sent with events. This is an
object mapping the key to an object containing metadata about the key.
A maximum of 10 extra keys is allowed.
This metadata object has the following keys:
- `description`: **Required.** A description of the key.
Valid when `type`_ is `event`.
"""
)
expected_errors = set(
re.sub(r"\s", "", _utils.indent(textwrap.dedent(x)).strip())
for x in expected_errors
)
assert sorted(list(found_errors)) == sorted(list(expected_errors))
def test_parser_empty():
"""1507792: Get a good error message if the metrics.yaml file is empty."""
all_metrics = parser.parse_objects(ROOT / "data" / "empty.yaml")
errors = list(all_metrics)
assert len(errors) == 1
assert "file can not be empty" in errors[0]
def test_invalid_schema():
all_metrics = parser.parse_objects([{"$schema": "This is wrong"}])
errors = list(all_metrics)
print(errors)
assert any("key must be one of" in str(e) for e in errors)
def test_merge_metrics():
"""Merge multiple metrics.yaml files"""
contents = [
{"category1": {"metric1": {}, "metric2": {}}, "category2": {"metric3": {}}},
{"category1": {"metric4": {}}, "category3": {"metric5": {}}},
]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
list(all_metrics)
all_metrics = all_metrics.value
assert set(all_metrics["category1"].keys()) == set(
["metric1", "metric2", "metric4"]
)
assert set(all_metrics["category2"].keys()) == set(["metric3"])
assert set(all_metrics["category3"].keys()) == set(["metric5"])
def test_merge_metrics_clash():
"""Merge multiple metrics.yaml files with conflicting metric names."""
contents = [{"category1": {"metric1": {}}}, {"category1": {"metric1": {}}}]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 1
assert "Duplicate metric name" in errors[0]
def test_snake_case_enforcement():
"""Expect exception if names aren't in snake case."""
contents = [
{"categoryWithCamelCase": {"metric": {}}},
{"category": {"metricWithCamelCase": {}}},
]
for content in contents:
util.add_required(content)
errors = list(parser._load_file(content))
assert len(errors) == 1
def test_multiple_errors():
"""Make sure that if there are multiple errors, we get all of them."""
contents = [{"camelCaseName": {"metric": {"type": "unknown"}}}]
contents = [util.add_required(x) for x in contents]
metrics = parser.parse_objects(contents)
errors = list(metrics)
assert len(errors) == 2
def test_event_must_be_ping_lifetime():
contents = [{"category": {"metric": {"type": "event", "lifetime": "user"}}}]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 1
assert "Event metrics must have ping lifetime" in errors[0]
def test_parser_reserved():
contents = [{"glean.baseline": {"metric": {"type": "string"}}}]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 1
assert "For category 'glean.baseline'" in errors[0]
all_metrics = parser.parse_objects(contents, {"allow_reserved": True})
errors = list(all_metrics)
assert len(errors) == 0
def invalid_in_category(name):
return [{name: {"metric": {"type": "string"}}}]
def invalid_in_name(name):
return [{"baseline": {name: {"type": "string"}}}]
def invalid_in_label(name):
return [{"baseline": {"metric": {"type": "string", "labels": [name]}}}]
@pytest.mark.parametrize(
"location", [invalid_in_category, invalid_in_name, invalid_in_label]
)
@pytest.mark.parametrize(
"name",
[
"name/with_slash",
"name#with_pound",
"this_name_is_too_long_and_shouldnt_be_used",
"",
],
)
def test_invalid_names(location, name):
contents = location(name)
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 1
assert name in errors[0]
def test_duplicate_send_in_pings():
"""Test the basics of parsing a single file."""
all_metrics = parser.parse_objects(
[ROOT / "data" / "duplicate_send_in_ping.yaml"], config={"allow_reserved": True}
)
errs = list(all_metrics)
assert len(errs) == 0
metric = all_metrics.value["telemetry"]["test"]
assert metric.send_in_pings == ["core", "metrics"]
def test_geckoview_only_on_valid_metrics():
for metric in [
"timing_distribution",
"custom_distributiuon",
"memory_distribution",
]:
contents = [
{"category1": {"metric1": {"type": metric, "gecko_datapoint": "FOO"}}}
]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errs = list(all_metrics)
contents = [{"category1": {"metric1": {"type": "event", "gecko_datapoint": "FOO"}}}]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errs = list(all_metrics)
assert len(errs) == 1
assert "is only allowed for" in str(errs[0])
def test_timing_distribution_unit_default():
contents = [{"category1": {"metric1": {"type": "timing_distribution"}}}]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errs = list(all_metrics)
assert len(errs) == 0
assert (
all_metrics.value["category1"]["metric1"].time_unit
== metrics.TimeUnit.nanosecond
)
def test_all_pings_reserved():
# send_in_pings: [all-pings] is only allowed for internal metrics
contents = [
{"category": {"metric": {"type": "string", "send_in_pings": ["all-pings"]}}}
]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 1
assert "On instance category.metric" in errors[0]
assert "Only internal metrics" in errors[0]
all_metrics = parser.parse_objects(contents, {"allow_reserved": True})
errors = list(all_metrics)
assert len(errors) == 0
# A custom ping called "all-pings" is not allowed
contents = [{"all-pings": {"include_client_id": True}}]
contents = [util.add_required_ping(x) for x in contents]
all_pings = parser.parse_objects(contents)
errors = list(all_pings)
assert len(errors) == 1
assert "is not allowed for 'all-pings'"
def test_custom_distribution():
# Test that custom_distribution isn't allowed on non-Gecko metric
contents = [
{
"category": {
"metric": {
"type": "custom_distribution",
"range_min": 0,
"range_max": 60000,
"bucket_count": 100,
"histogram_type": "exponential",
}
}
}
]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 1
assert "is only allowed for Gecko" in errors[0]
# Test that custom_distribution has required parameters
contents = [
{
"category": {
"metric": {
"type": "custom_distribution",
"gecko_datapoint": "FROM_GECKO",
}
}
}
]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 1
assert "`custom_distribution` is missing required parameters" in errors[0]
# Test maximum bucket_count is enforced
contents = [
{
"category": {
"metric": {
"type": "custom_distribution",
"gecko_datapoint": "FROM_GECKO",
"range_max": 60000,
"bucket_count": 101,
"histogram_type": "exponential",
}
}
}
]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 1
assert "101 is greater than" in errors[0]
# Test that correct usage
contents = [
{
"category": {
"metric": {
"type": "custom_distribution",
"gecko_datapoint": "FROM_GECKO",
"range_max": 60000,
"bucket_count": 100,
"histogram_type": "exponential",
}
}
}
]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 0
distribution = all_metrics.value["category"]["metric"]
assert distribution.range_min == 1
assert distribution.range_max == 60000
assert distribution.bucket_count == 100
assert distribution.histogram_type == metrics.HistogramType.exponential
def test_memory_distribution():
# Test that we get an error for a missing unit
contents = [{"category": {"metric": {"type": "memory_distribution"}}}]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 1
assert (
"`memory_distribution` is missing required parameter `memory_unit`" in errors[0]
)
# Test that memory_distribution works
contents = [
{
"category": {
"metric": {"type": "memory_distribution", "memory_unit": "megabyte"}
}
}
]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 0
assert len(all_metrics.value) == 1
all_metrics.value["category"]["metric"].memory_unit == metrics.MemoryUnit.megabyte
def test_quantity():
# Test that we get an error for a missing unit and gecko_datapoint
contents = [{"category": {"metric": {"type": "quantity"}}}]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 2
assert any(
"`quantity` is missing required parameter `unit`" in err for err in errors
)
assert any("is only allowed for Gecko" in err for err in errors)
# Test that quantity works
contents = [
{
"category": {
"metric": {
"type": "quantity",
"unit": "pixel",
"gecko_datapoint": "FOO",
}
}
}
]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents)
errors = list(all_metrics)
assert len(errors) == 0
assert len(all_metrics.value) == 1
all_metrics.value["category"]["metric"].unit == "pixel"
def test_do_not_disable_expired():
# Test that we get an error for a missing unit and gecko_datapoint
contents = [{"category": {"metric": {"type": "boolean", "expires": "1900-01-01"}}}]
contents = [util.add_required(x) for x in contents]
all_metrics = parser.parse_objects(contents, {"do_not_disable_expired": True})
errors = list(all_metrics)
assert len(errors) == 0
metrics = all_metrics.value
assert metrics["category"]["metric"].disabled is False
def test_send_in_pings_restrictions():
"""Test that invalid ping names are disallowed in `send_in_pings`."""
all_metrics = parser.parse_objects(ROOT / "data" / "invalid-ping-names.yaml")
errors = list(all_metrics)
assert len(errors) == 1
assert "'invalid_ping_name' does not match" in errors[0]