Merge ~jslarraz/review-tools:unify-schema-validation into review-tools:master

Proposed by Jorge Sancho Larraz
Status: Merged
Merged at revision: ba8dfd328fbc9fff29ac89237cd8a5681741070d
Proposed branch: ~jslarraz/review-tools:unify-schema-validation
Merge into: review-tools:master
Diff against target: 193 lines (+41/-49)
5 files modified
reviewtools/schemas/schema_validator.py (+27/-0)
reviewtools/sr_lint.py (+3/-20)
reviewtools/tests/schemas/test_schema_against_store.py (+3/-9)
reviewtools/tests/schemas/test_schema_base.py (+7/-19)
reviewtools/tests/schemas/test_schema_snap.py (+1/-1)
Reviewer Review Type Date Requested Status
Alex Murray Approve
Review via email: mp+466311@code.launchpad.net

Commit message

rt/{sr_lint,tests/schema}: unify how jsonschema is validated

To post a comment you must log in.
Revision history for this message
Alex Murray (alexmurray) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reviewtools/schemas/schema_validator.py b/reviewtools/schemas/schema_validator.py
2new file mode 100644
3index 0000000..d9803c3
4--- /dev/null
5+++ b/reviewtools/schemas/schema_validator.py
6@@ -0,0 +1,27 @@
7+import os
8+import json
9+import jsonschema
10+from pkg_resources import resource_filename
11+
12+
13+def validate(yaml, schema_name):
14+ # prefer local copy if it exists, otherwise, use one shipped in the
15+ # package
16+ schema_fn = "./reviewtools/schemas/%s" % schema_name
17+ if not os.path.exists(schema_fn):
18+ schema_fn = resource_filename("reviewtools", "schemas/%s" % schema_name)
19+ if not os.path.exists(schema_fn):
20+ return "Cannot find the schema file: %s" % schema_fn
21+
22+ with open(schema_fn) as fd:
23+ schema = json.loads(fd.read())
24+
25+ resolver = jsonschema.RefResolver(
26+ "file://%s/" % os.path.abspath(os.path.dirname(__file__)), None
27+ )
28+
29+ try:
30+ jsonschema.validate(yaml, schema, resolver=resolver)
31+ return None
32+ except jsonschema.ValidationError as e:
33+ return e
34diff --git a/reviewtools/sr_lint.py b/reviewtools/sr_lint.py
35index 5c7d53f..40990ad 100644
36--- a/reviewtools/sr_lint.py
37+++ b/reviewtools/sr_lint.py
38@@ -15,10 +15,10 @@
39 # along with this program. If not, see <http://www.gnu.org/licenses/>.
40
41 from __future__ import print_function
42+from reviewtools.schemas.schema_validator import validate
43 from reviewtools.sr_common import SnapReview
44 from reviewtools.common import (
45 find_external_symlinks,
46- error,
47 )
48 from reviewtools.overrides import (
49 redflagged_snap_types_overrides,
50@@ -32,9 +32,6 @@ import os
51 import re
52 import shlex
53 import unicodedata
54-import jsonschema
55-import json
56-from pkg_resources import resource_filename
57
58
59 class SnapReviewLint(SnapReview):
60@@ -79,25 +76,11 @@ class SnapReviewLint(SnapReview):
61
62 def check_schema(self):
63 """Check snap.yaml against the schema"""
64- t = "info"
65 n = self._get_check_name("snap_schema")
66- s = "OK"
67 manual_review = False
68
69- # prefer local copy if it exists, otherwise, use one shipped in the
70- # package
71- schema_fn = "./reviewtools/schemas/snap.json"
72- if not os.path.exists(schema_fn):
73- schema_fn = resource_filename("reviewtools", "schemas/snap.json")
74- if not os.path.exists(schema_fn):
75- error("could not find '%s'" % schema_fn)
76-
77- with open(schema_fn) as fd:
78- schema = json.loads(fd.read())
79-
80- try:
81- jsonschema.validate(self.snap_yaml, schema)
82- except jsonschema.ValidationError as e:
83+ e = validate(self.snap_yaml, "snap.json")
84+ if e is not None:
85 e.absolute_path.appendleft("$")
86 t = "error"
87 s = "Error found in snap.yaml while validating %s: %s" % (
88diff --git a/reviewtools/tests/schemas/test_schema_against_store.py b/reviewtools/tests/schemas/test_schema_against_store.py
89index 16259e2..d6fd208 100644
90--- a/reviewtools/tests/schemas/test_schema_against_store.py
91+++ b/reviewtools/tests/schemas/test_schema_against_store.py
92@@ -3,10 +3,9 @@ import json
93 import yaml
94 import os
95 import sys
96-import jsonschema
97 import unittest
98+from reviewtools.schemas.schema_validator import validate
99
100-SNAP_SCHEMA = "reviewtools/schemas/snap.json"
101 SNAP_YAML_CACHE = os.path.expanduser("~/.cache/review-tools")
102
103
104@@ -22,10 +21,6 @@ def test_store():
105 except OSError:
106 pass
107
108- # Load schema
109- with open(SNAP_SCHEMA) as fd:
110- snap_schema = json.loads(fd.read())
111-
112 # Get list of snaps in the store
113 with open("/var/cache/snapd/names", "r") as fd:
114 snaps = fd.readlines()
115@@ -60,9 +55,8 @@ def test_store():
116 continue
117
118 # Validate the snap.yaml against the schema
119- try:
120- jsonschema.validate(snap_yaml, snap_schema)
121- except jsonschema.ValidationError as e:
122+ e = validate(snap_yaml, "snap.json")
123+ if e is not None:
124 print(
125 "Error to validate %s for snap %s: %s" % (e.json_path, snap, e.message)
126 )
127diff --git a/reviewtools/tests/schemas/test_schema_base.py b/reviewtools/tests/schemas/test_schema_base.py
128index b41f5dd..d6c4536 100644
129--- a/reviewtools/tests/schemas/test_schema_base.py
130+++ b/reviewtools/tests/schemas/test_schema_base.py
131@@ -1,30 +1,18 @@
132 import json
133-import jsonschema
134 import copy
135 import unittest
136+from reviewtools.schemas.schema_validator import validate
137
138
139 class TestSchemaBase(unittest.TestCase):
140 yaml = {}
141- schema_file = "reviewtools/schemas/****.json"
142+ schema_name = "****.json"
143
144 def setUp(self):
145- # Load the relevant schema (defined in subclass)
146- with open(self.schema_file) as fd:
147- self.schema = json.loads(fd.read())
148-
149 # Validate base yaml against the schema
150- error = self._validate(self.yaml, self.schema)
151+ error = validate(self.yaml, self.schema_name)
152 self.assertEqual(None, error)
153
154- def _validate(self, yaml, schema):
155- # Validate yaml against the schema
156- try:
157- jsonschema.validate(yaml, schema)
158- return None
159- except jsonschema.ValidationError as e:
160- return e.message
161-
162 def _test_value(self, key, value, expected_error):
163 # Prepare yaml, make a copy so we use the pristine yaml on each subtest
164 yaml = copy.deepcopy(self.yaml)
165@@ -34,11 +22,11 @@ class TestSchemaBase(unittest.TestCase):
166 else:
167 yaml[key] = value
168
169- # Validate the schema
170- error = self._validate(yaml, self.schema)
171+ # Validate yaml against the schema
172+ e = validate(yaml, self.schema_name)
173
174 # Compare the errors against the expected ones
175 if expected_error is None:
176- self.assertIsNone(error)
177+ self.assertIsNone(e.message)
178 else:
179- self.assertIn(expected_error, error)
180+ self.assertIn(expected_error, e.message)
181diff --git a/reviewtools/tests/schemas/test_schema_snap.py b/reviewtools/tests/schemas/test_schema_snap.py
182index bcc55da..23a7098 100644
183--- a/reviewtools/tests/schemas/test_schema_snap.py
184+++ b/reviewtools/tests/schemas/test_schema_snap.py
185@@ -2,7 +2,7 @@ from reviewtools.tests.schemas.test_schema_base import TestSchemaBase
186
187
188 class TestSchemaSnap(TestSchemaBase):
189- schema_file = "reviewtools/schemas/snap.json"
190+ schema_name = "snap.json"
191
192 def setUp(self):
193 self.yaml = {

Subscribers

People subscribed via source and target branches