From: Aric Gardner Date: Tue, 29 Jan 2019 20:25:44 +0000 (-0500) Subject: Add "schema validate" command X-Git-Tag: v0.20.0~8 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F63%2F14363%2F8;p=releng%2Flftools.git Add "schema validate" command This command will validate a yaml file based on a schema file Issue: RELENG-1685 Change-Id: I59d427ff52ff61be3df9b1413828ff13b814653a Signed-off-by: Aric Gardner --- diff --git a/.coafile b/.coafile index 3358cc2f..a0e2259f 100644 --- a/.coafile +++ b/.coafile @@ -38,6 +38,7 @@ known_third_party_imports = defusedxml, glob2, jenkins, + jsonschema, pytest, six, shade, diff --git a/docs/commands/index.rst b/docs/commands/index.rst index 81f7ba22..5187621a 100644 --- a/docs/commands/index.rst +++ b/docs/commands/index.rst @@ -17,6 +17,7 @@ It supports the following commands: license nexus openstack + schema sign version diff --git a/docs/commands/schema.rst b/docs/commands/schema.rst new file mode 100644 index 00000000..a97ba847 --- /dev/null +++ b/docs/commands/schema.rst @@ -0,0 +1,13 @@ +****** +Schema +****** + +.. program-output:: lftools schema --help + +Commands +======== + +verify +------- + +.. program-output:: lftools schema verify --help diff --git a/lftools/cli/__init__.py b/lftools/cli/__init__.py index 9f12d652..3fd402e4 100644 --- a/lftools/cli/__init__.py +++ b/lftools/cli/__init__.py @@ -27,6 +27,7 @@ from lftools.cli.infofile import infofile from lftools.cli.jenkins import jenkins_cli from lftools.cli.license import license from lftools.cli.nexus import nexus +from lftools.cli.schema import schema from lftools.cli.sign import sign from lftools.cli.version import version @@ -82,6 +83,7 @@ cli.add_command(infofile) cli.add_command(jenkins_cli, name='jenkins') cli.add_command(license) cli.add_command(nexus) +cli.add_command(schema) cli.add_command(sign) cli.add_command(version) diff --git a/lftools/cli/schema.py b/lftools/cli/schema.py new file mode 100644 index 00000000..27298a67 --- /dev/null +++ b/lftools/cli/schema.py @@ -0,0 +1,40 @@ +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2019 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""Verify YAML Schema.""" + +from __future__ import print_function + +import click + +from lftools.schema import check_schema_file + + +@click.group() +@click.pass_context +def schema(ctx): + """Verify YAML Schema.""" + pass + + +@click.command(name='verify') +@click.argument('yamlfile') +@click.argument('schemafile') +@click.pass_context +def verify_schema(ctx, yamlfile, schemafile): + """Verify YAML Schema. + + YAMLFILE: Release YAML file to be validated. + + SCHEMAFILE: SCHEMA file to validate against. + """ + check_schema_file(yamlfile, schemafile) + + +schema.add_command(verify_schema) diff --git a/lftools/schema.py b/lftools/schema.py new file mode 100644 index 00000000..0636136e --- /dev/null +++ b/lftools/schema.py @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2019 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""Verify YAML Schema.""" + +from __future__ import print_function + +import logging + +import jsonschema +import yaml + + +def check_schema_file(yamlfile, schemafile): + """Verify YAML Schema. + + YAMLFILE: Release YAML file to be validated. + + SCHEMAFILE: SCHEMA file to validate against. + """ + with open(yamlfile) as _: + yaml_file = yaml.safe_load(_) + + with open(schemafile) as _: + schema_file = yaml.safe_load(_) + + # Load the schema + validation = jsonschema.Draft4Validator( + schema_file, + format_checker=jsonschema.FormatChecker() + ) + + validation.iter_errors(yaml_file) + # Look for errors + errors = 0 + for error in validation.iter_errors(yaml_file): + errors += 1 + logging.error(error) + if errors > 0: + raise RuntimeError("{:d} issues invalidate the release schema".format(errors)) diff --git a/releasenotes/notes/schema-validate-1e5793a8dc859ecf.yaml b/releasenotes/notes/schema-validate-1e5793a8dc859ecf.yaml new file mode 100644 index 00000000..39f0e79f --- /dev/null +++ b/releasenotes/notes/schema-validate-1e5793a8dc859ecf.yaml @@ -0,0 +1,16 @@ +--- +features: + - | + Verify YAML Schema. + + Usage: Usage: lftools schema verify [OPTIONS] YAMLFILE SCHEMAFILE + + .. code-block:: none + + Commands: + verify a yaml file based on a schema file. + + .. code-block:: none + + Options: + --help Show this message and exit. diff --git a/requirements.txt b/requirements.txt index b0657f1f..7692b37b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ click glob2 # Needed for Python < 3.5 recursive glob support deb-pkg-tools~=5.2 defusedxml # Needed due to tox complains on parseString not safe +jsonschema~=2.6.0 pyyaml requests~=2.18.0 rpmfile~=0.1.4 diff --git a/tests/fixtures/schema/ipv6-broken.yaml b/tests/fixtures/schema/ipv6-broken.yaml new file mode 100644 index 00000000..cd4cbdd9 --- /dev/null +++ b/tests/fixtures/schema/ipv6-broken.yaml @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: Apache-2.0 +############################################################################## +# Copyright (c) 2019 The Linux Foundation and others. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +--- +project: ipv6 +project-type: feature +release-model: stable +foo: bar + +releases: + - version: opnfv-7.0.0 + location: + ipv6: c0e6ae3d1d443f96f0ab50e417f9ad740669b890 + - version: opnfv-7.1.0 + location: + ipv6: 7978c6172432174ea6027922524b476783516f56 + +branches: + - name: stable/gambia + location: + ipv6: e36f753cf6e9ab0c02a400b6bac5c79b48268d44 + +release-notes: https://opnfv-ipv6.readthedocs.io/en/stable-gambia/release/release-notes/ diff --git a/tests/fixtures/schema/release-broken.yaml b/tests/fixtures/schema/release-broken.yaml new file mode 100644 index 00000000..cd4cbdd9 --- /dev/null +++ b/tests/fixtures/schema/release-broken.yaml @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: Apache-2.0 +############################################################################## +# Copyright (c) 2019 The Linux Foundation and others. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +--- +project: ipv6 +project-type: feature +release-model: stable +foo: bar + +releases: + - version: opnfv-7.0.0 + location: + ipv6: c0e6ae3d1d443f96f0ab50e417f9ad740669b890 + - version: opnfv-7.1.0 + location: + ipv6: 7978c6172432174ea6027922524b476783516f56 + +branches: + - name: stable/gambia + location: + ipv6: e36f753cf6e9ab0c02a400b6bac5c79b48268d44 + +release-notes: https://opnfv-ipv6.readthedocs.io/en/stable-gambia/release/release-notes/ diff --git a/tests/fixtures/schema/release.yaml b/tests/fixtures/schema/release.yaml new file mode 100644 index 00000000..a43c9535 --- /dev/null +++ b/tests/fixtures/schema/release.yaml @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: Apache-2.0 +############################################################################## +# Copyright (c) 2019 The Linux Foundation and others. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +--- +project: ipv6 +project-type: feature +release-model: stable + +releases: + - version: opnfv-7.0.0 + location: + ipv6: c0e6ae3d1d443f96f0ab50e417f9ad740669b890 + - version: opnfv-7.1.0 + location: + ipv6: 7978c6172432174ea6027922524b476783516f56 + +branches: + - name: stable/gambia + location: + ipv6: e36f753cf6e9ab0c02a400b6bac5c79b48268d44 + +release-notes: https://opnfv-ipv6.readthedocs.io/en/stable-gambia/release/release-notes/ diff --git a/tests/fixtures/schema/schema.yaml b/tests/fixtures/schema/schema.yaml new file mode 100644 index 00000000..682c1d25 --- /dev/null +++ b/tests/fixtures/schema/schema.yaml @@ -0,0 +1,56 @@ +############################################################################## +# Copyright (c) 2019 Linux Foundation and others. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +--- +$schema: 'http://json-schema.org/schema#' +$id: 'https://github.com/opnfv/releng/blob/master/releases/schema.yaml' + +additionalProperties: false + +required: + - 'project' + - 'project-type' + +properties: + project: + type: 'string' + release-model: + type: 'string' + enum: ['stable', 'non-release'] + project-type: + type: 'string' + enum: ['installer', 'testing', 'feature', 'tools', 'infra'] + upstream: + type: 'string' + releases: + type: 'array' + items: + type: 'object' + properties: + version: + type: 'string' + # Matches semantic versioning (X.Y.Z) + pattern: '^opnfv-([0-9]+\.){2}[0-9]+$' + location: + type: 'object' + required: ['version', 'location'] + additionalProperties: false + branches: + type: 'array' + items: + type: 'object' + properties: + name: + type: 'string' + pattern: '^stable/[a-z]+$' + location: + type: 'object' + required: ['name', 'location'] + additionalProperties: false + release-notes: + type: 'string' + format: 'uri' diff --git a/tests/test_schema.py b/tests/test_schema.py new file mode 100644 index 00000000..fe43a762 --- /dev/null +++ b/tests/test_schema.py @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2019 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""Test license command.""" + +import os + +import pytest + +from lftools import cli + +FIXTURE_DIR = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + 'fixtures', + ) + + +@pytest.mark.datafiles( + os.path.join(FIXTURE_DIR, 'schema'), + ) +def test_check_license(cli_runner, datafiles): + """Test check_schema() command.""" + os.chdir(str(datafiles)) + + # Check that schema passes when schema and yaml are valid. + result = cli_runner.invoke(cli.cli, ['schema', 'verify', 'release.yaml', 'schema.yaml'], obj={}) + # noqa: B101 . + assert result.exit_code == 0 + + # Check that schema fails when schema and yaml are invalid. + result = cli_runner.invoke(cli.cli, ['schema', 'verify', 'release-broken.yaml', 'schema.yaml'], obj={}) + # noqa: B101 . + assert result.exit_code == 1