[Piglit] [PATCH 42/49] unittests: replace json_tests with a schema and validator

Dylan Baker dylan at pnwbakers.com
Fri Jul 29 18:39:28 UTC 2016


This module is really a want for a schema and enforcement for that
schema, since there is in fact schema for JSON and several python
libraries to enforce said schema, it would be much better to use use one
of those, since that also makes the output format reproducible and
portable.

This adds the schema and the proper test, which currently this test
xfails because bugs.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 tox.ini                                           |   1 +
 unittests/framework/backends/schema/piglit-8.json | 120 ++++++++++++++++
 unittests/framework/backends/shared.py            |   4 +-
 unittests/framework/backends/test_json.py         |  16 +++
 unittests/json_tests.py                           | 164 ----------------------
 5 files changed, 140 insertions(+), 165 deletions(-)
 create mode 100644 unittests/framework/backends/schema/piglit-8.json
 delete mode 100644 unittests/json_tests.py

diff --git a/tox.ini b/tox.ini
index 5ea976a..f8836ed 100644
--- a/tox.ini
+++ b/tox.ini
@@ -30,6 +30,7 @@ deps =
     six==1.5.2
     wrapt
     {accel,noaccel}: nose 
+    {accel,noaccel}: jsonschema
 commands = 
     {accel,noaccel}: nosetests unittests -e generators -e framework -e suites []
     {accel,noaccel}: py.test -rw unittests/framework unittests/suites
diff --git a/unittests/framework/backends/schema/piglit-8.json b/unittests/framework/backends/schema/piglit-8.json
new file mode 100644
index 0000000..b2a8622
--- /dev/null
+++ b/unittests/framework/backends/schema/piglit-8.json
@@ -0,0 +1,120 @@
+{
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "title": "TestrunResult",
+    "description": "The collection of all results",
+    "type": "object",
+    "properties": {
+        "__type__": { "type": "string" },
+        "clinfo": { "type": ["string", "null"] },
+        "glxinfo": { "type": ["string", "null"] },
+        "lspci": { "type": ["string", "null"] },
+        "wglinfo": { "type": ["string", "null"] },
+        "name": { "type": "string" },
+        "results_version": { "type": "number" },
+        "uname": { "type": [ "string", "null" ] },
+        "time_elapsed": { "$ref": "#/definitions/timeAttribute" },
+        "options": {
+            "descrption": "The options that were invoked with this run. These are implementation specific and not required.",
+            "type": "object",
+            "properties": {
+                "exclude_tests": { 
+                    "type": "array",
+                    "items": { "type": "string" },
+                    "uniqueItems": true
+                },
+                "include_filter": { 
+                    "type": "array",
+                    "items": { "type": "string" }
+                },
+                "exclude_filter": { 
+                    "type": "array",
+                    "items": { "type": "string" }
+                },
+                "sync": { "type": "boolean" },
+                "valgrind": { "type": "boolean" },
+                "monitored": { "type": "boolean" },
+                "dmesg": { "type": "boolean" },
+                "execute": { "type": "boolean" },
+                "concurrent": { "enum": ["none", "all", "some"] },
+                "platform": { "type": "string" },
+                "log_level": { "type": "string" },
+                "env": {
+                    "description": "Environment variables that must be specified",
+                    "type": "object",
+                    "additionalProperties": { "type": "string" }
+                },
+                "profile": {
+                    "type": "array",
+                    "items": { "type": "string" }
+                }
+            },
+            "additionalProperties": false
+        },
+        "totals": {
+            "type": "object",
+            "description": "A calculation of the group totals.",
+            "additionalProperties": {
+                "type": "object",
+                "properties": {
+                    "crash": { "type": "number" },
+                    "dmesg-fail": { "type": "number" },
+                    "dmesg-warn": { "type": "number" },
+                    "fail": { "type": "number" },
+                    "incomplete": { "type": "number" },
+                    "notrun": { "type": "number" },
+                    "pass": { "type": "number" },
+                    "skip": { "type": "number" },
+                    "timeout": { "type": "number" },
+                    "warn": { "type": "number" }
+                },
+                "additionalProperties": false,
+                "required": [ "crash", "dmesg-fail", "dmesg-warn", "fail", "incomplete", "notrun", "pass", "skip", "timeout", "warn" ]
+            }
+        },
+        "tests": {
+            "type": "object",
+            "additionalProperties": {
+                "type": "object",
+                "properties": {
+                    "__type__": { "type": "string" },
+                    "err": { "type": "string" },
+                    "exception": { "type": ["string", "null"] },
+                    "result": {
+                        "type": "string",
+                        "enum": [ "pass", "fail", "crash", "warn", "incomplete", "notrun", "skip", "dmesg-warn", "dmesg-fail" ]
+                    },
+                    "environment": { "type": "string" },
+                    "command": { "type": "string" },
+                    "traceback": { "type": ["string", "null"] },
+                    "out": { "type": "string" },
+                    "dmesg": { "type": "string" },
+                    "pid": { "type": [ "number", "null"] },
+                    "returncode": { "type": [ "number", "null" ] },
+                    "time": { "$ref": "#/definitions/timeAttribute" },
+                    "subtests": {
+                        "type": "object",
+                        "properties": { "__type__": { "type": "string" } },
+                        "additionalProperties": { "type": "string" },
+                        "required": [ "__type__" ]
+                    }
+                },
+                "additionalProperties": false
+            }
+        }
+    },
+    "additionalProperties": false,
+    "required": [ "__type__", "clinfo", "glxinfo", "lspci", "wglinfo", "name", "results_version", "uname", "time_elapsed", "totals", "tests" ],
+    "definitions": {
+        "timeAttribute": {
+            "type": "object",
+            "description": "An element containing a start and end time",
+            "properties": {
+                "__type__": { "type": "string" },
+                "start": { "type": "number" },
+                "end": { "type": "number" }
+            },
+            "additionalProperties": false,
+            "required": [ "__type__", "start", "end" ]
+        }
+    }
+}
diff --git a/unittests/framework/backends/shared.py b/unittests/framework/backends/shared.py
index e9bc5e4..eeffe76 100644
--- a/unittests/framework/backends/shared.py
+++ b/unittests/framework/backends/shared.py
@@ -25,12 +25,14 @@ from __future__ import (
     absolute_import, division, print_function, unicode_literals
 )
 
+from framework.options import OPTIONS
+
 
 INITIAL_METADATA = {
     'name': 'name',
     'test_count': 0,
     'env': {},
-    'options': {},
+    'options': dict(OPTIONS),
 }
 
 # This is current JSON data, in raw form with only the minimum required
diff --git a/unittests/framework/backends/test_json.py b/unittests/framework/backends/test_json.py
index ab91705..f29a046 100644
--- a/unittests/framework/backends/test_json.py
+++ b/unittests/framework/backends/test_json.py
@@ -33,6 +33,7 @@ try:
 except ImportError:
     from unittests import mock
 
+import jsonschema
 import pytest
 import six
 
@@ -45,6 +46,9 @@ from . import shared
 
 # pylint: disable=no-self-use,protected-access
 
+SCHEMA = os.path.join(os.path.dirname(__file__), 'schema',
+                      'piglit-{}.json'.format(backends.json.CURRENT_JSON_VERSION))
+
 
 @pytest.yield_fixture(scope='module', autouse=True)
 def mock_compression():
@@ -127,6 +131,18 @@ class TestJSONBackend(object):
             with tmpdir.join('results.json').open('r') as f:
                 json.load(f)
 
+        # Currently our concurrent flag is set incorrectly
+        @pytest.mark.xfail
+        def test_results_are_valid(self, tmpdir):
+            """Test that the values produced are valid."""
+            with tmpdir.join('results.json').open('r') as f:
+                json_ = json.load(f)
+
+            with open(SCHEMA, 'r') as f:
+                schema = json.load(f)
+
+            jsonschema.validate(json_, schema)
+
 
 class TestUpdateResults(object):
     """Test for the _update_results function."""
diff --git a/unittests/json_tests.py b/unittests/json_tests.py
deleted file mode 100644
index 7c7451e..0000000
--- a/unittests/json_tests.py
+++ /dev/null
@@ -1,164 +0,0 @@
-# Copyright (c) 2014 Intel Corporation
-
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-# SOFTWARE.
-
-"""Tests for the json outpt format.
-
-This module drills the ouput of the json backend with a series of tests
-designed to catch changes in the json output. This is a rather volatile set of
-tests and they will change with each version of the json output.
-
-"""
-
-from __future__ import (
-    absolute_import, division, print_function, unicode_literals
-)
-import os
-
-import nose.tools as nt
-try:
-    import simplejson as json
-except ImportError:
-    import json
-
-from . import utils
-from framework import results
-from framework.backends.json import JSONBackend
-from framework.programs.run import _create_metadata
-
-# pylint: disable=invalid-name
-
-
-def setup_module():
-    utils.piglit.set_compression('none')
-
-
-def teardown_module():
-    utils.piglit.unset_compression()
-
-
-# Helpers
-class Namespace(object):
-    """Simple namespace object for replicating and argparse namespace."""
-    pass
-
-
-# Tests
-# pylint: disable=too-many-public-methods
-class TestJsonOutput(utils.nose.StaticDirectory):
-    """Class for testing JSON output."""
-    @classmethod
-    def setup_class(cls):
-        super(TestJsonOutput, cls).setup_class()
-
-        args = Namespace()
-        # pylint: disable=attribute-defined-outside-init
-        args.test_profile = ['fake.py']
-        args.platform = 'gbm'
-        args.log_level = 'verbose'
-
-        backend = JSONBackend(cls.tdir, file_fsync=True)
-        backend.initialize(_create_metadata(args, 'test'))
-        with backend.write_test('result') as t:
-            t(results.TestResult('pass'))
-        backend.finalize({'time_elapsed': results.TimeAttribute(end=1.2)})
-        with open(os.path.join(cls.tdir, 'results.json'), 'r') as f:
-            cls.json = json.load(f)
-
-    def test_root_results_version(self):
-        """JSON: result_version is a root key."""
-        nt.assert_in('results_version', self.json)
-
-    def test_root_name(self):
-        """JSON: name is a root key."""
-        nt.assert_in('name', self.json)
-
-    def test_root_options(self):
-        """JSON: options is a root key."""
-        nt.assert_in('options', self.json)
-
-    def test_root_tests(self):
-        """JSON: tests is a root key."""
-        nt.assert_in('tests', self.json)
-
-    @utils.nose.Skip.platform('linux')
-    @utils.nose.Skip.binary('lspci')
-    def test_root_lspci(self):
-        """JSON: lspci is a root key."""
-        nt.assert_in('lspci', self.json)
-
-    @utils.nose.Skip.platform('linux')
-    @utils.nose.Skip.binary('uname')
-    def test_root_uname(self):
-        """JSON: uname is a root key."""
-        nt.assert_in('uname', self.json)
-
-    @utils.nose.Skip.platform('linux')
-    @utils.nose.Skip.binary('glxinfo')
-    def test_root_glxinfo(self):
-        """JSON: glxinfo is a root key."""
-        nt.assert_in('glxinfo', self.json)
-
-    def test_root_time_elapsed(self):
-        """JSON: time_elapsed is a root key."""
-        nt.assert_in('time_elapsed', self.json)
-
-    def test_options_profile(self):
-        """JSON: profile is an options key."""
-        nt.assert_in('profile', self.json['options'])
-
-    def test_options_dmesg(self):
-        """JSON: dmesg is an options key."""
-        nt.assert_in('dmesg', self.json['options'])
-
-    def test_options_execute(self):
-        """JSON: execute is an options key."""
-        nt.assert_in('execute', self.json['options'])
-
-    def test_options_log_level(self):
-        """JSON: log_level is an options key."""
-        nt.assert_in('log_level', self.json['options'])
-
-    def test_options_platform(self):
-        """JSON: platform is an options key."""
-        nt.assert_in('platform', self.json['options'])
-
-    def test_options_sync(self):
-        """JSON: sync is an options key."""
-        nt.assert_in('sync', self.json['options'])
-
-    def test_options_valgrind(self):
-        """JSON: valgrind is an options key."""
-        nt.assert_in('valgrind', self.json['options'])
-
-    def test_options_concurrent(self):
-        """JSON: concurrent is an options key."""
-        nt.assert_in('concurrent', self.json['options'])
-
-    def test_options_filter(self):
-        """JSON: include_filter is an options key."""
-        nt.assert_in('include_filter', self.json['options'])
-
-    def test_options_exclude_tests(self):
-        """JSON: exclude_tests is an options key."""
-        nt.assert_in('exclude_tests', self.json['options'])
-
-    def test_options_exclude_filter(self):
-        """JSON: exclude_filter is an options key."""
-        nt.assert_in('exclude_filter', self.json['options'])
-- 
2.9.0



More information about the Piglit mailing list