-
Notifications
You must be signed in to change notification settings - Fork 48
Fix 1504200: Add ping validation feature to glean_parser #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| """ | ||
| Validates the contents of a glean ping against the schema. | ||
| """ | ||
|
|
||
| import functools | ||
| import io | ||
| import json | ||
| from pathlib import Path | ||
| import sys | ||
|
|
||
| import jsonschema | ||
|
|
||
| from . import util | ||
|
|
||
|
|
||
| ROOT_DIR = Path(__file__).parent | ||
| SCHEMAS_DIR = ROOT_DIR / 'schemas' | ||
|
|
||
|
|
||
| PING_SCHEMA_DEFAULT_URL = ( | ||
| 'https://raw.githubusercontent.com/mozilla-services/' | ||
| 'mozilla-pipeline-schemas/dev/schemas/glean/baseline/' | ||
| 'baseline.1.schema.json' | ||
| ) | ||
|
|
||
|
|
||
| @functools.lru_cache(maxsize=1) | ||
| def _get_ping_schema(schema_url): | ||
| contents = util.fetch_remote_url( | ||
| schema_url, | ||
| cache=(schema_url != PING_SCHEMA_DEFAULT_URL) | ||
| ) | ||
| return json.loads(contents) | ||
|
|
||
|
|
||
| def _validate_ping(ins, outs, schema_url): | ||
| schema = _get_ping_schema(schema_url) | ||
|
|
||
| resolver = util.get_null_resolver(schema) | ||
|
|
||
| document = json.load(ins) | ||
|
|
||
| validator_class = jsonschema.validators.validator_for(schema) | ||
| validator = validator_class(schema, resolver=resolver) | ||
|
|
||
| has_error = 0 | ||
| for error in validator.iter_errors(document): | ||
| outs.write(str(error)) | ||
| has_error = 1 | ||
|
|
||
| return has_error | ||
|
|
||
|
|
||
| def validate_ping(ins, outs=None, schema_url=PING_SCHEMA_DEFAULT_URL): | ||
| """ | ||
| Validates the contents of a glean ping. | ||
|
|
||
| :param ins: Input stream or file path to the ping contents to validate | ||
| :param outs: Output stream to write errors to. (Defaults to stdout) | ||
| :param schema_url: HTTP URL or local filesystem path to glean ping schema. | ||
| Defaults to the current version of the schema in | ||
| mozilla-pipeline-schemas. | ||
| :rtype: int 1 if any errors occurred, otherwise 0. | ||
| """ | ||
| if outs is None: | ||
| outs = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') | ||
|
|
||
| if isinstance(ins, (str, bytes, Path)): | ||
| with open(ins, 'r') as fd: | ||
| return _validate_ping(fd, outs, schema_url=schema_url) | ||
| else: | ||
| return _validate_ping(ins, outs, schema_url=schema_url) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # Any copyright is dedicated to the Public Domain. | ||
| # http://creativecommons.org/publicdomain/zero/1.0/ | ||
|
|
||
| import io | ||
| import json | ||
|
|
||
| from glean_parser import validate_ping | ||
|
|
||
|
|
||
| def test_validate_ping(): | ||
| content = { | ||
| "experiments": { | ||
| "experiment2": { | ||
| "branch": "branch_b", | ||
| "extra": { | ||
| "key": "value" | ||
| } | ||
| } | ||
| }, | ||
| "metrics": { | ||
| "string": { | ||
| "telemetry.string_metric": "foo" | ||
| } | ||
| }, | ||
| "ping_info": { | ||
| "ping_type": "metrics", | ||
| "telemetry_sdk_build": "0.32.0", | ||
| "seq": 0, | ||
| "app_build": "test-placeholder", | ||
| "client_id": "900b6d8c-34d2-44d4-926d-83bde790474f", | ||
| "start_time": "2018-11-19T16:19-05:00", | ||
| "end_time": "2018-11-19T16:19-05:00" | ||
| } | ||
| } | ||
|
|
||
| input = io.StringIO(json.dumps(content)) | ||
| output = io.StringIO() | ||
|
|
||
| schema_url = ( | ||
| 'https://raw.githubusercontent.com/mozilla-services/' | ||
| 'mozilla-pipeline-schemas/3a15121c582ef0cffe430da024a5bf11b7c48740/' | ||
| 'schemas/glean/baseline/baseline.1.schema.json' | ||
| ) | ||
|
|
||
| assert validate_ping.validate_ping( | ||
| input, output, schema_url=schema_url | ||
| ) == 0 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're moving the schema off this repo, let's keep this shorter: let's use a small JSON and provide a sample schema stub.