Merge lp:~sergiusens/snapcraft/validation into lp:~snappy-dev/snapcraft/core
- validation
- Merge into core
Status: | Merged |
---|---|
Approved by: | Leo Arias |
Approved revision: | 151 |
Merged at revision: | 141 |
Proposed branch: | lp:~sergiusens/snapcraft/validation |
Merge into: | lp:~snappy-dev/snapcraft/core |
Prerequisite: | lp:~sergiusens/snapcraft/meta-all-yaml |
Diff against target: |
551 lines (+394/-9) 9 files modified
debian/control (+3/-1) integration-tests/data/local-plugin/snapcraft.yaml (+1/-0) schema/snapcraft.yaml (+89/-0) setup.py (+5/-2) snapcraft/common.py (+11/-0) snapcraft/dirs.py (+1/-0) snapcraft/tests/__init__.py (+1/-0) snapcraft/tests/test_yaml.py (+251/-6) snapcraft/yaml.py (+32/-0) |
To merge this branch: | bzr merge lp:~sergiusens/snapcraft/validation |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
John Lenton (community) | Approve | ||
Leo Arias (community) | Approve | ||
Review via email: mp+269120@code.launchpad.net |
Commit message
Initial json schema support
Description of the change
I'm just not sure where to add the tests here, so please advise during the review :-)
Leo Arias (elopio) wrote : | # |
Zygmunt Krynicki (zyga) wrote : | # |
One more comment below
Sergio Schvezov (sergiusens) : | # |
Leo Arias (elopio) wrote : | # |
This is great. I left some IMO comments. The only one I feel strong about is splitting the tests in scenarios or subtests.
Sergio Schvezov (sergiusens) wrote : | # |
Hey you asked me for subtests a lot and I had them added explained as in the documentation you linked to ;-)
Leo Arias (elopio) wrote : | # |
how did I not see the subtests??? Sorry about that.
So the only thing that's missing is bumping the dependency to python3 (>= 3.4),
Leo Arias (elopio) : | # |
John Lenton (chipaca) wrote : | # |
LGTM. zOMGwtfbbqschem
Leo Arias (elopio) wrote : | # |
Once this lands, the guys from erle and spreed have to be notified about the new format, right?
Snappy Tarmac (snappydevtarmac) wrote : | # |
The attempt to merge lp:~sergiusens/snapcraft/validation into lp:snapcraft failed. Below is the output from the failed tests.
wget -c http://
cp --preserve=all -R zzz /tmp/tmpo5btu74
cp --preserve=all -R src /tmp/tmpyoazun5
cp --preserve=all -R src /tmp/tmprwukoxq
.......
Connecting to 0.0.0.0:39937... connected.
HTTP request sent, awaiting response... 200 OK
Length: 22 [text/html]
Saving to: ‘test.tar’
0K 100% 4.74M=0s
2015-08-27 19:50:49 (4.74 MB/s) - ‘test.tar’ saved [22/22]
.E.....
.............E
=======
ERROR: snapcraft.
-------
Traceback (most recent call last):
File "/usr/lib/
yield
File "/usr/lib/
testMethod()
File "/usr/lib/
raise exception
ImportError: Failed to import test module: snapcraft.
Traceback (most recent call last):
File "/usr/lib/
module = self._get_
File "/usr/lib/
__import_
File "/tmp/tarmac/
from snapcraft import (
File "/tmp/tarmac/
import snapcraft.yaml
File "/tmp/tarmac/
import jsonschema
ImportError: No module named 'jsonschema'
=======
ERROR: snapcraft.
-------
Traceback (most recent call last):
File "/usr/lib/
yield
File "/usr/lib/
testMethod()
File "/usr/lib/
raise exception
ImportError: Failed to import test module: snapcraft.
Traceback (most recent call last):
File "/usr/lib/
module = self._get_
File "/usr/lib/
__import_
File "/tmp/tarmac/
import jsonschema
ImportError: No module named 'jsonschema'
-------
Ran 44 tests in 1.155s
FAILED (errors=2)
Sergio Schvezov (sergiusens) wrote : | # |
On Thu, Aug 27, 2015 at 4:47 PM, Leo Arias <email address hidden> wrote:
> Once this lands, the guys from erle and spreed have to be notified about
> the new format, right?
I've disabled daily builds for now
Preview Diff
1 | === modified file 'debian/control' | |||
2 | --- debian/control 2015-08-07 19:49:21 +0000 | |||
3 | +++ debian/control 2015-08-27 16:38:10 +0000 | |||
4 | @@ -11,9 +11,10 @@ | |||
5 | 11 | pep8, | 11 | pep8, |
6 | 12 | plainbox, | 12 | plainbox, |
7 | 13 | pyflakes, | 13 | pyflakes, |
9 | 14 | python3 (>= 3.2), | 14 | python3 (>= 3.4), |
10 | 15 | python3-apt, | 15 | python3-apt, |
11 | 16 | python3-fixtures, | 16 | python3-fixtures, |
12 | 17 | python3-jsonschema, | ||
13 | 17 | python3-setuptools, | 18 | python3-setuptools, |
14 | 18 | python3-testscenarios, | 19 | python3-testscenarios, |
15 | 19 | python3-yaml, | 20 | python3-yaml, |
16 | @@ -33,6 +34,7 @@ | |||
17 | 33 | dpkg-dev, | 34 | dpkg-dev, |
18 | 34 | git, | 35 | git, |
19 | 35 | python3-apt, | 36 | python3-apt, |
20 | 37 | python3-jsonschema, | ||
21 | 36 | python3-yaml, | 38 | python3-yaml, |
22 | 37 | mercurial, | 39 | mercurial, |
23 | 38 | sudo, | 40 | sudo, |
24 | 39 | 41 | ||
25 | === modified file 'integration-tests/data/local-plugin/snapcraft.yaml' | |||
26 | --- integration-tests/data/local-plugin/snapcraft.yaml 2015-08-27 16:38:09 +0000 | |||
27 | +++ integration-tests/data/local-plugin/snapcraft.yaml 2015-08-27 16:38:10 +0000 | |||
28 | @@ -6,3 +6,4 @@ | |||
29 | 6 | 6 | ||
30 | 7 | parts: | 7 | parts: |
31 | 8 | x-local-plugin: | 8 | x-local-plugin: |
32 | 9 | source: . | ||
33 | 9 | 10 | ||
34 | === added directory 'schema' | |||
35 | === added file 'schema/snapcraft.yaml' | |||
36 | --- schema/snapcraft.yaml 1970-01-01 00:00:00 +0000 | |||
37 | +++ schema/snapcraft.yaml 2015-08-27 16:38:10 +0000 | |||
38 | @@ -0,0 +1,89 @@ | |||
39 | 1 | $schema: http://json-schema.org/draft-04/schema# | ||
40 | 2 | |||
41 | 3 | title: snapcraft schema | ||
42 | 4 | type: object | ||
43 | 5 | properties: | ||
44 | 6 | name: | ||
45 | 7 | type: string | ||
46 | 8 | description: name of the snap package | ||
47 | 9 | pattern: "^[a-z0-9][a-z0-9+-]*$" | ||
48 | 10 | version: | ||
49 | 11 | # python's defaul yaml loading code loads 1.0 as an int | ||
50 | 12 | # type: string | ||
51 | 13 | description: package version | ||
52 | 14 | pattern: "^[a-zA-Z0-9.+~-]*$" | ||
53 | 15 | vendor: | ||
54 | 16 | type: string | ||
55 | 17 | format: email | ||
56 | 18 | summary: | ||
57 | 19 | type: string | ||
58 | 20 | description: one line summary for the package | ||
59 | 21 | maxLength: 78 | ||
60 | 22 | description: | ||
61 | 23 | type: string | ||
62 | 24 | description: long description of the package | ||
63 | 25 | type: | ||
64 | 26 | type: string | ||
65 | 27 | description: the snap type, the implicit type is 'app' | ||
66 | 28 | enum: | ||
67 | 29 | - app | ||
68 | 30 | - framework | ||
69 | 31 | frameworks: | ||
70 | 32 | type: array | ||
71 | 33 | minItems: 1 | ||
72 | 34 | uniqueItems: true | ||
73 | 35 | items: | ||
74 | 36 | - type: string | ||
75 | 37 | services: | ||
76 | 38 | type: array | ||
77 | 39 | items: | ||
78 | 40 | - type: object | ||
79 | 41 | properties: | ||
80 | 42 | name: | ||
81 | 43 | type: string | ||
82 | 44 | start: | ||
83 | 45 | type: string | ||
84 | 46 | description: command executed to start the service | ||
85 | 47 | stop: | ||
86 | 48 | type: string | ||
87 | 49 | description: command executed to stop the service | ||
88 | 50 | required: | ||
89 | 51 | - name | ||
90 | 52 | - start | ||
91 | 53 | binaries: | ||
92 | 54 | type: array | ||
93 | 55 | items: | ||
94 | 56 | - type: object | ||
95 | 57 | properties: | ||
96 | 58 | name: | ||
97 | 59 | type: string | ||
98 | 60 | exec: | ||
99 | 61 | type: string | ||
100 | 62 | description: command executed to start the service | ||
101 | 63 | required: | ||
102 | 64 | - name | ||
103 | 65 | parts: | ||
104 | 66 | type: object | ||
105 | 67 | minProperties: 1 | ||
106 | 68 | patternProperties: | ||
107 | 69 | ^[a-z0-9][a-z0-9+-]*$: | ||
108 | 70 | type: object | ||
109 | 71 | properties: | ||
110 | 72 | plugin: | ||
111 | 73 | type: string | ||
112 | 74 | description: plugin name | ||
113 | 75 | source: | ||
114 | 76 | type: string | ||
115 | 77 | description: path to the sources | ||
116 | 78 | # TODO To be enabled once the Ubuntu plugin is removed | ||
117 | 79 | # required: | ||
118 | 80 | # - plugin | ||
119 | 81 | # - source | ||
120 | 82 | addtionalProperties: false | ||
121 | 83 | required: | ||
122 | 84 | - name | ||
123 | 85 | - version | ||
124 | 86 | - vendor | ||
125 | 87 | - summary | ||
126 | 88 | - description | ||
127 | 89 | - parts | ||
128 | 0 | 90 | ||
129 | === modified file 'setup.py' | |||
130 | --- setup.py 2015-07-06 16:48:22 +0000 | |||
131 | +++ setup.py 2015-08-27 16:38:10 +0000 | |||
132 | @@ -36,6 +36,9 @@ | |||
133 | 36 | 'snapcraft.plugins'], | 36 | 'snapcraft.plugins'], |
134 | 37 | package_data={'snapcraft.plugins': ['manifest.txt']}, | 37 | package_data={'snapcraft.plugins': ['manifest.txt']}, |
135 | 38 | scripts=['bin/snapcraft'], | 38 | scripts=['bin/snapcraft'], |
137 | 39 | data_files=[('share/snapcraft/plugins', ['plugins/' + x for x in os.listdir('plugins')])], | 39 | data_files=[ |
138 | 40 | ('share/snapcraft/plugins', ['plugins/' + x for x in os.listdir('plugins')]), | ||
139 | 41 | ('share/snapcraft/schema', ['schema/' + x for x in os.listdir('schema')]), | ||
140 | 42 | ], | ||
141 | 40 | cmdclass={'test': TestCommand}, | 43 | cmdclass={'test': TestCommand}, |
143 | 41 | ) | 44 | ) |
144 | 42 | 45 | ||
145 | === modified file 'snapcraft/common.py' | |||
146 | --- snapcraft/common.py 2015-08-05 15:39:18 +0000 | |||
147 | +++ snapcraft/common.py 2015-08-27 16:38:10 +0000 | |||
148 | @@ -25,6 +25,8 @@ | |||
149 | 25 | COMMAND_ORDER = ["pull", "build", "stage", "snap"] | 25 | COMMAND_ORDER = ["pull", "build", "stage", "snap"] |
150 | 26 | _DEFAULT_PLUGINDIR = '/usr/share/snapcraft/plugins' | 26 | _DEFAULT_PLUGINDIR = '/usr/share/snapcraft/plugins' |
151 | 27 | _plugindir = _DEFAULT_PLUGINDIR | 27 | _plugindir = _DEFAULT_PLUGINDIR |
152 | 28 | _DEFAULT_SCHEMADIR = '/usr/share/snapcraft/schema' | ||
153 | 29 | _schemadir = _DEFAULT_SCHEMADIR | ||
154 | 28 | _arch = None | 30 | _arch = None |
155 | 29 | _arch_triplet = None | 31 | _arch_triplet = None |
156 | 30 | 32 | ||
157 | @@ -79,3 +81,12 @@ | |||
158 | 79 | 81 | ||
159 | 80 | def get_plugindir(): | 82 | def get_plugindir(): |
160 | 81 | return _plugindir | 83 | return _plugindir |
161 | 84 | |||
162 | 85 | |||
163 | 86 | def set_schemadir(schemadir): | ||
164 | 87 | global _schemadir | ||
165 | 88 | _schemadir = schemadir | ||
166 | 89 | |||
167 | 90 | |||
168 | 91 | def get_schemadir(): | ||
169 | 92 | return _schemadir | ||
170 | 82 | 93 | ||
171 | === modified file 'snapcraft/dirs.py' | |||
172 | --- snapcraft/dirs.py 2015-07-23 17:19:25 +0000 | |||
173 | +++ snapcraft/dirs.py 2015-08-27 16:38:10 +0000 | |||
174 | @@ -27,3 +27,4 @@ | |||
175 | 27 | # only change the default if we are running from a checkout | 27 | # only change the default if we are running from a checkout |
176 | 28 | if os.path.exists(os.path.join(topdir, "setup.py")): | 28 | if os.path.exists(os.path.join(topdir, "setup.py")): |
177 | 29 | common.set_plugindir(os.path.join(topdir, 'plugins')) | 29 | common.set_plugindir(os.path.join(topdir, 'plugins')) |
178 | 30 | common.set_schemadir(os.path.join(topdir, 'schema')) | ||
179 | 30 | 31 | ||
180 | === modified file 'snapcraft/tests/__init__.py' | |||
181 | --- snapcraft/tests/__init__.py 2015-08-04 15:06:59 +0000 | |||
182 | +++ snapcraft/tests/__init__.py 2015-08-27 16:38:10 +0000 | |||
183 | @@ -32,3 +32,4 @@ | |||
184 | 32 | # is a module variable. Make sure that it is returned to the original | 32 | # is a module variable. Make sure that it is returned to the original |
185 | 33 | # value when a test ends. | 33 | # value when a test ends. |
186 | 34 | self.addCleanup(common.set_plugindir, common.get_plugindir()) | 34 | self.addCleanup(common.set_plugindir, common.get_plugindir()) |
187 | 35 | self.addCleanup(common.set_schemadir, common.get_schemadir()) | ||
188 | 35 | 36 | ||
189 | === modified file 'snapcraft/tests/test_yaml.py' | |||
190 | --- snapcraft/tests/test_yaml.py 2015-08-05 18:40:06 +0000 | |||
191 | +++ snapcraft/tests/test_yaml.py 2015-08-27 16:38:10 +0000 | |||
192 | @@ -14,15 +14,18 @@ | |||
193 | 14 | # You should have received a copy of the GNU General Public License | 14 | # You should have received a copy of the GNU General Public License |
194 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
195 | 16 | 16 | ||
196 | 17 | import jsonschema | ||
197 | 17 | import logging | 18 | import logging |
198 | 18 | import os | 19 | import os |
199 | 19 | import tempfile | 20 | import tempfile |
200 | 20 | import unittest | 21 | import unittest |
201 | 22 | import unittest.mock | ||
202 | 21 | 23 | ||
203 | 22 | import fixtures | 24 | import fixtures |
204 | 23 | 25 | ||
205 | 26 | import snapcraft.common | ||
206 | 27 | import snapcraft.yaml | ||
207 | 24 | from snapcraft import dirs | 28 | from snapcraft import dirs |
208 | 25 | from snapcraft.yaml import Config | ||
209 | 26 | from snapcraft.tests import TestCase | 29 | from snapcraft.tests import TestCase |
210 | 27 | 30 | ||
211 | 28 | 31 | ||
212 | @@ -37,11 +40,19 @@ | |||
213 | 37 | 40 | ||
214 | 38 | @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') | 41 | @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') |
215 | 39 | def test_config_loads_plugins(self, mock_loadPlugin): | 42 | def test_config_loads_plugins(self, mock_loadPlugin): |
217 | 40 | self.make_snapcraft_yaml("""parts: | 43 | dirs.setup_dirs() |
218 | 44 | |||
219 | 45 | self.make_snapcraft_yaml("""name: test | ||
220 | 46 | version: "1" | ||
221 | 47 | vendor: me <me@me.com> | ||
222 | 48 | summary: test | ||
223 | 49 | description: test | ||
224 | 50 | |||
225 | 51 | parts: | ||
226 | 41 | ubuntu: | 52 | ubuntu: |
227 | 42 | packages: [fswebcam] | 53 | packages: [fswebcam] |
228 | 43 | """) | 54 | """) |
230 | 44 | Config() | 55 | snapcraft.yaml.Config() |
231 | 45 | mock_loadPlugin.assert_called_with("ubuntu", "ubuntu", { | 56 | mock_loadPlugin.assert_called_with("ubuntu", "ubuntu", { |
232 | 46 | "packages": ["fswebcam"], | 57 | "packages": ["fswebcam"], |
233 | 47 | }) | 58 | }) |
234 | @@ -52,7 +63,7 @@ | |||
235 | 52 | 63 | ||
236 | 53 | # no snapcraft.yaml | 64 | # no snapcraft.yaml |
237 | 54 | with self.assertRaises(SystemExit) as raised: | 65 | with self.assertRaises(SystemExit) as raised: |
239 | 55 | Config() | 66 | snapcraft.yaml.Config() |
240 | 56 | 67 | ||
241 | 57 | self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.') | 68 | self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.') |
242 | 58 | self.assertEqual( | 69 | self.assertEqual( |
243 | @@ -66,7 +77,13 @@ | |||
244 | 66 | fake_logger = fixtures.FakeLogger(level=logging.ERROR) | 77 | fake_logger = fixtures.FakeLogger(level=logging.ERROR) |
245 | 67 | self.useFixture(fake_logger) | 78 | self.useFixture(fake_logger) |
246 | 68 | 79 | ||
248 | 69 | self.make_snapcraft_yaml("""parts: | 80 | self.make_snapcraft_yaml("""name: test |
249 | 81 | version: "1" | ||
250 | 82 | vendor: me <me@me.com> | ||
251 | 83 | summary: test | ||
252 | 84 | description: test | ||
253 | 85 | |||
254 | 86 | parts: | ||
255 | 70 | p1: | 87 | p1: |
256 | 71 | plugin: ubuntu | 88 | plugin: ubuntu |
257 | 72 | after: [p2] | 89 | after: [p2] |
258 | @@ -75,7 +92,235 @@ | |||
259 | 75 | after: [p1] | 92 | after: [p1] |
260 | 76 | """) | 93 | """) |
261 | 77 | with self.assertRaises(SystemExit) as raised: | 94 | with self.assertRaises(SystemExit) as raised: |
263 | 78 | Config() | 95 | snapcraft.yaml.Config() |
264 | 79 | 96 | ||
265 | 80 | self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.') | 97 | self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.') |
266 | 81 | self.assertEqual('Circular dependency chain!\n', fake_logger.output) | 98 | self.assertEqual('Circular dependency chain!\n', fake_logger.output) |
267 | 99 | |||
268 | 100 | @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') | ||
269 | 101 | def test_invalid_yaml_missing_name(self, mock_loadPlugin): | ||
270 | 102 | dirs.setup_dirs() | ||
271 | 103 | |||
272 | 104 | fake_logger = fixtures.FakeLogger(level=logging.ERROR) | ||
273 | 105 | self.useFixture(fake_logger) | ||
274 | 106 | |||
275 | 107 | self.make_snapcraft_yaml(""" | ||
276 | 108 | version: "1" | ||
277 | 109 | vendor: me <me@me.com> | ||
278 | 110 | summary: test | ||
279 | 111 | description: nothing | ||
280 | 112 | |||
281 | 113 | parts: | ||
282 | 114 | ubuntu: | ||
283 | 115 | packages: [fswebcam] | ||
284 | 116 | """) | ||
285 | 117 | with self.assertRaises(SystemExit) as raised: | ||
286 | 118 | snapcraft.yaml.Config() | ||
287 | 119 | |||
288 | 120 | self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.') | ||
289 | 121 | self.assertEqual( | ||
290 | 122 | 'Issues while validating snapcraft.yaml: \'name\' is a required property\n', | ||
291 | 123 | fake_logger.output) | ||
292 | 124 | |||
293 | 125 | @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') | ||
294 | 126 | def test_invalid_yaml_invalid_name_as_number(self, mock_loadPlugin): | ||
295 | 127 | dirs.setup_dirs() | ||
296 | 128 | |||
297 | 129 | fake_logger = fixtures.FakeLogger(level=logging.ERROR) | ||
298 | 130 | self.useFixture(fake_logger) | ||
299 | 131 | |||
300 | 132 | self.make_snapcraft_yaml("""name: 1 | ||
301 | 133 | version: "1" | ||
302 | 134 | vendor: me <me@me.com> | ||
303 | 135 | summary: test | ||
304 | 136 | description: nothing | ||
305 | 137 | |||
306 | 138 | parts: | ||
307 | 139 | ubuntu: | ||
308 | 140 | packages: [fswebcam] | ||
309 | 141 | """) | ||
310 | 142 | with self.assertRaises(SystemExit) as raised: | ||
311 | 143 | snapcraft.yaml.Config() | ||
312 | 144 | |||
313 | 145 | self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.') | ||
314 | 146 | self.assertEqual( | ||
315 | 147 | 'Issues while validating snapcraft.yaml: 1 is not of type \'string\'\n', | ||
316 | 148 | fake_logger.output) | ||
317 | 149 | |||
318 | 150 | @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') | ||
319 | 151 | def test_invalid_yaml_invalid_name_chars(self, mock_loadPlugin): | ||
320 | 152 | dirs.setup_dirs() | ||
321 | 153 | |||
322 | 154 | fake_logger = fixtures.FakeLogger(level=logging.ERROR) | ||
323 | 155 | self.useFixture(fake_logger) | ||
324 | 156 | |||
325 | 157 | self.make_snapcraft_yaml("""name: myapp@me_1.0 | ||
326 | 158 | version: "1" | ||
327 | 159 | vendor: me <me@me.com> | ||
328 | 160 | summary: test | ||
329 | 161 | description: nothing | ||
330 | 162 | |||
331 | 163 | parts: | ||
332 | 164 | ubuntu: | ||
333 | 165 | packages: [fswebcam] | ||
334 | 166 | """) | ||
335 | 167 | with self.assertRaises(SystemExit) as raised: | ||
336 | 168 | snapcraft.yaml.Config() | ||
337 | 169 | |||
338 | 170 | self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.') | ||
339 | 171 | self.assertEqual( | ||
340 | 172 | 'Issues while validating snapcraft.yaml: \'myapp@me_1.0\' does not match \'^[a-z0-9][a-z0-9+-]*$\'\n', | ||
341 | 173 | fake_logger.output) | ||
342 | 174 | |||
343 | 175 | @unittest.mock.patch('snapcraft.yaml.Config.load_plugin') | ||
344 | 176 | def test_invalid_yaml_missing_description(self, mock_loadPlugin): | ||
345 | 177 | dirs.setup_dirs() | ||
346 | 178 | |||
347 | 179 | fake_logger = fixtures.FakeLogger(level=logging.ERROR) | ||
348 | 180 | self.useFixture(fake_logger) | ||
349 | 181 | |||
350 | 182 | self.make_snapcraft_yaml("""name: test | ||
351 | 183 | version: "1" | ||
352 | 184 | vendor: me <me@me.com> | ||
353 | 185 | summary: test | ||
354 | 186 | |||
355 | 187 | parts: | ||
356 | 188 | ubuntu: | ||
357 | 189 | packages: [fswebcam] | ||
358 | 190 | """) | ||
359 | 191 | with self.assertRaises(SystemExit) as raised: | ||
360 | 192 | snapcraft.yaml.Config() | ||
361 | 193 | |||
362 | 194 | self.assertEqual(raised.exception.code, 1, 'Wrong exit code returned.') | ||
363 | 195 | self.assertEqual( | ||
364 | 196 | 'Issues while validating snapcraft.yaml: \'description\' is a required property\n', | ||
365 | 197 | fake_logger.output) | ||
366 | 198 | |||
367 | 199 | |||
368 | 200 | class TestValidation(TestCase): | ||
369 | 201 | |||
370 | 202 | def setUp(self): | ||
371 | 203 | super().setUp() | ||
372 | 204 | dirs.setup_dirs() | ||
373 | 205 | |||
374 | 206 | self.data = { | ||
375 | 207 | 'name': 'my-package-1', | ||
376 | 208 | 'version': '1.0-snapcraft1~ppa1', | ||
377 | 209 | 'vendor': 'Me <me@me.com>', | ||
378 | 210 | 'summary': 'my summary less that 79 chars', | ||
379 | 211 | 'description': 'description which can be pretty long', | ||
380 | 212 | 'parts': { | ||
381 | 213 | 'part1': { | ||
382 | 214 | 'type': 'project', | ||
383 | 215 | }, | ||
384 | 216 | }, | ||
385 | 217 | } | ||
386 | 218 | |||
387 | 219 | def test_required_properties(self): | ||
388 | 220 | for key in self.data: | ||
389 | 221 | data = self.data.copy() | ||
390 | 222 | with self.subTest(key=key): | ||
391 | 223 | del data[key] | ||
392 | 224 | |||
393 | 225 | with self.assertRaises(jsonschema.ValidationError) as raised: | ||
394 | 226 | snapcraft.yaml._validate_snapcraft_yaml(data) | ||
395 | 227 | |||
396 | 228 | expected_message = '\'{}\' is a required property'.format(key) | ||
397 | 229 | self.assertEqual(raised.exception.message, expected_message, msg=data) | ||
398 | 230 | |||
399 | 231 | def test_invalid_names(self): | ||
400 | 232 | invalid_names = [ | ||
401 | 233 | 'package@awesome', | ||
402 | 234 | 'something.another', | ||
403 | 235 | '_hideme', | ||
404 | 236 | ] | ||
405 | 237 | |||
406 | 238 | for name in invalid_names: | ||
407 | 239 | data = self.data.copy() | ||
408 | 240 | with self.subTest(key=name): | ||
409 | 241 | data['name'] = name | ||
410 | 242 | |||
411 | 243 | with self.assertRaises(jsonschema.ValidationError) as raised: | ||
412 | 244 | snapcraft.yaml._validate_snapcraft_yaml(data) | ||
413 | 245 | |||
414 | 246 | expected_message = '\'{}\' does not match \'^[a-z0-9][a-z0-9+-]*$\''.format(name) | ||
415 | 247 | self.assertEqual(raised.exception.message, expected_message, msg=data) | ||
416 | 248 | |||
417 | 249 | def test_summary_too_long(self): | ||
418 | 250 | self.data['summary'] = 'a' * 80 | ||
419 | 251 | with self.assertRaises(jsonschema.ValidationError) as raised: | ||
420 | 252 | snapcraft.yaml._validate_snapcraft_yaml(self.data) | ||
421 | 253 | |||
422 | 254 | expected_message = '\'{}\' is too long'.format(self.data['summary']) | ||
423 | 255 | self.assertEqual(raised.exception.message, expected_message, msg=self.data) | ||
424 | 256 | |||
425 | 257 | def test_valid_types(self): | ||
426 | 258 | self.data['type'] = 'app' | ||
427 | 259 | snapcraft.yaml._validate_snapcraft_yaml(self.data) | ||
428 | 260 | |||
429 | 261 | self.data['type'] = 'framework' | ||
430 | 262 | snapcraft.yaml._validate_snapcraft_yaml(self.data) | ||
431 | 263 | |||
432 | 264 | def test_invalid_types(self): | ||
433 | 265 | invalid_types = [ | ||
434 | 266 | 'apps', | ||
435 | 267 | 'kernel', | ||
436 | 268 | 'platform', | ||
437 | 269 | 'oem', | ||
438 | 270 | 'os', | ||
439 | 271 | ] | ||
440 | 272 | |||
441 | 273 | for t in invalid_types: | ||
442 | 274 | data = self.data.copy() | ||
443 | 275 | with self.subTest(key=t): | ||
444 | 276 | data['type'] = t | ||
445 | 277 | |||
446 | 278 | with self.assertRaises(jsonschema.ValidationError) as raised: | ||
447 | 279 | snapcraft.yaml._validate_snapcraft_yaml(data) | ||
448 | 280 | |||
449 | 281 | expected_message = '\'{}\' is not one of [\'app\', \'framework\']'.format(t) | ||
450 | 282 | self.assertEqual(raised.exception.message, expected_message, msg=data) | ||
451 | 283 | |||
452 | 284 | def test_valid_services(self): | ||
453 | 285 | self.data['services'] = [ | ||
454 | 286 | { | ||
455 | 287 | 'name': 'service1', | ||
456 | 288 | 'start': 'binary1 start', | ||
457 | 289 | }, | ||
458 | 290 | { | ||
459 | 291 | 'name': 'service2', | ||
460 | 292 | 'start': 'binary2', | ||
461 | 293 | 'stop': 'binary2 --stop', | ||
462 | 294 | }, | ||
463 | 295 | { | ||
464 | 296 | 'name': 'service3', | ||
465 | 297 | } | ||
466 | 298 | ] | ||
467 | 299 | |||
468 | 300 | snapcraft.yaml._validate_snapcraft_yaml(self.data) | ||
469 | 301 | |||
470 | 302 | def test_services_required_properties(self): | ||
471 | 303 | self.data['services'] = [ | ||
472 | 304 | { | ||
473 | 305 | 'start': 'binary1 start', | ||
474 | 306 | } | ||
475 | 307 | ] | ||
476 | 308 | |||
477 | 309 | with self.assertRaises(jsonschema.ValidationError) as raised: | ||
478 | 310 | snapcraft.yaml._validate_snapcraft_yaml(self.data) | ||
479 | 311 | |||
480 | 312 | expected_message = '\'name\' is a required property' | ||
481 | 313 | self.assertEqual(raised.exception.message, expected_message, msg=self.data) | ||
482 | 314 | |||
483 | 315 | def test_schema_file_not_found(self): | ||
484 | 316 | mock_the_open = unittest.mock.mock_open() | ||
485 | 317 | mock_the_open.side_effect = FileNotFoundError() | ||
486 | 318 | |||
487 | 319 | with unittest.mock.patch('snapcraft.yaml.open', mock_the_open, create=True): | ||
488 | 320 | with self.assertRaises(snapcraft.yaml.SchemaNotFoundError) as raised: | ||
489 | 321 | snapcraft.yaml._validate_snapcraft_yaml(self.data) | ||
490 | 322 | |||
491 | 323 | expected_path = os.path.join(snapcraft.common.get_schemadir(), 'snapcraft.yaml') | ||
492 | 324 | mock_the_open.assert_called_once_with(expected_path) | ||
493 | 325 | expected_message = 'Schema is missing, could not validate snapcraft.yaml, check installation' | ||
494 | 326 | self.assertEqual(raised.exception.message, expected_message) | ||
495 | 82 | 327 | ||
496 | === modified file 'snapcraft/yaml.py' | |||
497 | --- snapcraft/yaml.py 2015-08-26 08:52:09 +0000 | |||
498 | +++ snapcraft/yaml.py 2015-08-27 16:38:10 +0000 | |||
499 | @@ -18,6 +18,8 @@ | |||
500 | 18 | import sys | 18 | import sys |
501 | 19 | 19 | ||
502 | 20 | import yaml | 20 | import yaml |
503 | 21 | import jsonschema | ||
504 | 22 | import os | ||
505 | 21 | 23 | ||
506 | 22 | import snapcraft.plugin | 24 | import snapcraft.plugin |
507 | 23 | from snapcraft import common | 25 | from snapcraft import common |
508 | @@ -26,6 +28,24 @@ | |||
509 | 26 | logger = logging.getLogger(__name__) | 28 | logger = logging.getLogger(__name__) |
510 | 27 | 29 | ||
511 | 28 | 30 | ||
512 | 31 | class SchemaNotFoundError(Exception): | ||
513 | 32 | |||
514 | 33 | def __init__(self, message): | ||
515 | 34 | self.message = message | ||
516 | 35 | |||
517 | 36 | |||
518 | 37 | def _validate_snapcraft_yaml(snapcraft_yaml): | ||
519 | 38 | schema_file = os.path.abspath(os.path.join(common.get_schemadir(), 'snapcraft.yaml')) | ||
520 | 39 | |||
521 | 40 | try: | ||
522 | 41 | with open(schema_file) as fp: | ||
523 | 42 | schema = yaml.load(fp) | ||
524 | 43 | except FileNotFoundError: | ||
525 | 44 | raise SchemaNotFoundError('Schema is missing, could not validate snapcraft.yaml, check installation') | ||
526 | 45 | |||
527 | 46 | jsonschema.validate(snapcraft_yaml, schema) | ||
528 | 47 | |||
529 | 48 | |||
530 | 29 | class Config: | 49 | class Config: |
531 | 30 | 50 | ||
532 | 31 | def __init__(self): | 51 | def __init__(self): |
533 | @@ -39,6 +59,18 @@ | |||
534 | 39 | except FileNotFoundError: | 59 | except FileNotFoundError: |
535 | 40 | logger.error("Could not find snapcraft.yaml. Are you sure you're in the right directory?\nTo start a new project, use 'snapcraft init'") | 60 | logger.error("Could not find snapcraft.yaml. Are you sure you're in the right directory?\nTo start a new project, use 'snapcraft init'") |
536 | 41 | sys.exit(1) | 61 | sys.exit(1) |
537 | 62 | |||
538 | 63 | # Make sure the loaded snapcraft yaml follows the schema | ||
539 | 64 | try: | ||
540 | 65 | _validate_snapcraft_yaml(self.data) | ||
541 | 66 | except SchemaNotFoundError as e: | ||
542 | 67 | logger.error(e.message) | ||
543 | 68 | sys.exit(1) | ||
544 | 69 | except jsonschema.ValidationError as e: | ||
545 | 70 | msg = "Issues while validating snapcraft.yaml: {}".format(e.message) | ||
546 | 71 | logger.error(msg) | ||
547 | 72 | sys.exit(1) | ||
548 | 73 | |||
549 | 42 | self.build_tools = self.data.get('build-tools', []) | 74 | self.build_tools = self.data.get('build-tools', []) |
550 | 43 | 75 | ||
551 | 44 | for part_name in self.data.get("parts", []): | 76 | for part_name in self.data.get("parts", []): |
To test, I think you should encapsulate your additions to the Config.__init__ in a validate method. Then we can pass valid and invalid yamls to it.
Some comments in the diff.