From dea7a04afa406f856b168b90e9d2ae5b75e7ef97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20Stehl=C3=A9?= Date: Fri, 17 Dec 2021 18:39:16 +0100 Subject: [PATCH] Add schemas to validate config files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add two schemas to validate the check-sr-results.yaml and the format-sr-results.yaml configuration files. - Add a script to perform validation. - Run the validation in `make check'. - Document in the README. Signed-off-by: Vincent Stehlé --- Makefile | 4 ++ README.md | 14 +++++- schemas/check-sr-results-schema.yaml | 70 +++++++++++++++++++++++++++ schemas/format-sr-results-schema.yaml | 63 ++++++++++++++++++++++++ validate.py | 38 +++++++++++++++ 5 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 schemas/check-sr-results-schema.yaml create mode 100644 schemas/format-sr-results-schema.yaml create mode 100755 validate.py diff --git a/Makefile b/Makefile index 0388306..c737808 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,10 @@ doc: README.pdf check: yamllint . flake8 + ./validate.py --schema schemas/check-sr-results-schema.yaml \ + check-sr-results.yaml + ./validate.py --schema schemas/format-sr-results-schema.yaml \ + format-sr-results.yaml clean: -rm -f README.pdf diff --git a/README.md b/README.md index 3ee350a..39cec13 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ parent directory is scanned and all matching entries are considered. If the file or dir entry with the pattern is not marked `optional', there must be at least one match. +The `schemas/check-sr-results-schema.yaml` file describes this configuration +file format and can be used with the `validate.py` script to validate the +configuration. This is run during [Sanity checks]. + ## SystemReady results formatter The `format-sr-results.py` script produces a report from a SystemReady @@ -120,6 +124,10 @@ starting from matching line". * If `last-line` is empty, extraction stops when reaching an empty line. +The `schemas/format-sr-results-schema.yaml` file describes this configuration +file format and can be used with the `validate.py` script to validate the +configuration. This is run during [Sanity checks]. + ### Internal data format The internal python data format corresponds closely to a flattened view of the @@ -157,4 +165,8 @@ To perform some sanity checks in this repository, run: $ make check ``` -This will run a number of checkers and reports errors. See `make help`. +This will run a number of checkers and reports errors. This validates the +configuration files of the [SystemReady results checker] and of the [SystemReady +results formatter] using the `validate.py` script. + +See `make help`. diff --git a/schemas/check-sr-results-schema.yaml b/schemas/check-sr-results-schema.yaml new file mode 100644 index 0000000..b93cbd0 --- /dev/null +++ b/schemas/check-sr-results-schema.yaml @@ -0,0 +1,70 @@ +--- +$schema: http://json-schema.org/draft-07/schema# +title: SystemReady IR results check rules schema +description: | + SystemReady IR results are expected to conform to a certain format, + described in https://gitlab.arm.com/systemready/systemready-template. + + The check-sr-results.py scripts verifies requirements on those results, + using the check-sr-results.yaml configuration file. + + This schema describes requirements on the configuration file. It can be used + by the valid.py script. + + See the README for details. +definitions: + array-of-strings: + type: array + minItems: 1 + items: + type: string + tree: + type: array + minItems: 1 + uniqueItems: true + items: + type: object + oneOf: + - '$ref': '#/definitions/file' + - '$ref': '#/definitions/dir' + file: + type: object + properties: + file: + type: string + must-contain: + '$ref': '#/definitions/array-of-strings' + warn-if-contains: + '$ref': '#/definitions/array-of-strings' + can-be-empty: + const: null + optional: + const: null + required: + - file + # must-contain is optional + # warn-if-contains is optional + # can-be-empty is optional + # optional is optional + dir: + type: object + properties: + dir: + type: string + tree: + '$ref': '#/definitions/tree' + optional: + const: null + required: + - dir + # tree is optional + # optional is optional +type: object +properties: + check-sr-results-configuration: + const: null + tree: + '$ref': '#/definitions/tree' +required: + - check-sr-results-configuration + - tree diff --git a/schemas/format-sr-results-schema.yaml b/schemas/format-sr-results-schema.yaml new file mode 100644 index 0000000..992c72d --- /dev/null +++ b/schemas/format-sr-results-schema.yaml @@ -0,0 +1,63 @@ +--- +$schema: http://json-schema.org/draft-07/schema# +title: SystemReady IR results formatting rules schema +description: | + SystemReady IR results are expected to conform to a certain format, + described in https://gitlab.arm.com/systemready/systemready-template. + + The format-sr-results.py scripts formats results, using the + format-sr-results.yaml configuration file. + + This schema describes requirements on the configuration file. It can be used + by the valid.py script. + + See the README for details. +definitions: + subs: + type: array + minItems: 1 + uniqueItems: true + items: + '$ref': '#/definitions/entry' + entry: + type: object + properties: + heading: + type: string + extract: + type: object + properties: + filename: + type: string + find: + type: string + first-line: + type: number + last-line: + oneOf: + - const: null + - type: number + - type: string + required: + - filename + - find + # first-line is optional + # last-line is optional + paragraph: + type: string + subs: + '$ref': '#/definitions/subs' + required: + - heading + # extract is optional + - paragraph + # subs is optional +type: object +properties: + format-sr-results-configuration: + const: null + subs: + '$ref': '#/definitions/subs' +required: + - format-sr-results-configuration + - subs diff --git a/validate.py b/validate.py new file mode 100755 index 0000000..8e7eb66 --- /dev/null +++ b/validate.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +import argparse +import sys +import yaml +import jsonschema + +try: + from packaging import version +except ImportError: + print('No packaging. You should install python3-packaging...') + +# Not all yaml versions have a Loader argument. +if 'packaging.version' in sys.modules and \ + version.parse(yaml.__version__) >= version.parse('5.1'): + yaml_load_args = {'Loader': yaml.FullLoader} +else: + yaml_load_args = {} + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Validate a YAML file with a schema.') + parser.add_argument( + '--schema', required=True, help='Schema file to use for validation') + parser.add_argument('yamlfile', help='YAML file to validate') + args = parser.parse_args() + + # Load YAML file. + with open(args.yamlfile, 'r') as f: + data = yaml.load(f, **yaml_load_args) + + # Load YAML jsonschema. + with open(args.schema, 'r') as f: + schema = yaml.load(f, **yaml_load_args) + + fc = jsonschema.FormatChecker() + jsonschema.validate(instance=data, schema=schema, format_checker=fc) + # If we arrive here, data is valid. -- GitLab