Add "schema validate" command 63/14363/8
authorAric Gardner <agardner@linuxfoundation.org>
Tue, 29 Jan 2019 20:25:44 +0000 (15:25 -0500)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Sun, 10 Feb 2019 16:49:41 +0000 (11:49 -0500)
This command will validate a yaml file
based on a schema file

Issue: RELENG-1685
Change-Id: I59d427ff52ff61be3df9b1413828ff13b814653a
Signed-off-by: Aric Gardner <agardner@linuxfoundation.org>
13 files changed:
.coafile
docs/commands/index.rst
docs/commands/schema.rst [new file with mode: 0644]
lftools/cli/__init__.py
lftools/cli/schema.py [new file with mode: 0644]
lftools/schema.py [new file with mode: 0644]
releasenotes/notes/schema-validate-1e5793a8dc859ecf.yaml [new file with mode: 0644]
requirements.txt
tests/fixtures/schema/ipv6-broken.yaml [new file with mode: 0644]
tests/fixtures/schema/release-broken.yaml [new file with mode: 0644]
tests/fixtures/schema/release.yaml [new file with mode: 0644]
tests/fixtures/schema/schema.yaml [new file with mode: 0644]
tests/test_schema.py [new file with mode: 0644]

index 3358cc2..a0e2259 100644 (file)
--- a/.coafile
+++ b/.coafile
@@ -38,6 +38,7 @@ known_third_party_imports =
     defusedxml,
     glob2,
     jenkins,
+    jsonschema,
     pytest,
     six,
     shade,
index 81f7ba2..5187621 100644 (file)
@@ -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 (file)
index 0000000..a97ba84
--- /dev/null
@@ -0,0 +1,13 @@
+******
+Schema
+******
+
+.. program-output:: lftools schema --help
+
+Commands
+========
+
+verify
+-------
+
+.. program-output:: lftools schema verify --help
index 9f12d65..3fd402e 100644 (file)
@@ -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 (file)
index 0000000..27298a6
--- /dev/null
@@ -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 (file)
index 0000000..0636136
--- /dev/null
@@ -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 (file)
index 0000000..39f0e79
--- /dev/null
@@ -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.
index b0657f1..7692b37 100644 (file)
@@ -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 (file)
index 0000000..cd4cbdd
--- /dev/null
@@ -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 (file)
index 0000000..cd4cbdd
--- /dev/null
@@ -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 (file)
index 0000000..a43c953
--- /dev/null
@@ -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 (file)
index 0000000..682c1d2
--- /dev/null
@@ -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 (file)
index 0000000..fe43a76
--- /dev/null
@@ -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