blob: 5a8fc4622a016aa99e0b7bb299833a96ba36bab2 [file] [log] [blame]
James E. Blair47958382013-01-10 17:26:02 -08001#!/usr/bin/env python
2
3# Copyright 2013 OpenStack Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blair47958382013-01-10 17:26:02 -080017import os
18import re
Monty Taylorbc758832013-06-17 17:22:42 -040019
20import testtools
James E. Blair47958382013-01-10 17:26:02 -080021import voluptuous
Monty Taylorbc758832013-06-17 17:22:42 -040022import yaml
James E. Blair47958382013-01-10 17:26:02 -080023
24import zuul.layoutvalidator
25
26FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
27 'fixtures')
28LAYOUT_RE = re.compile(r'^(good|bad)_.*\.yaml$')
29
30
James E. Blair6c358e72013-07-29 17:06:47 -070031class TestLayoutValidator(testtools.TestCase):
James E. Blair47958382013-01-10 17:26:02 -080032 def test_layouts(self):
33 """Test layout file validation"""
34 print
35 errors = []
36 for fn in os.listdir(os.path.join(FIXTURE_DIR, 'layouts')):
37 m = LAYOUT_RE.match(fn)
38 if not m:
39 continue
40 print fn
41 layout = os.path.join(FIXTURE_DIR, 'layouts', fn)
42 data = yaml.load(open(layout))
43 validator = zuul.layoutvalidator.LayoutValidator()
44 if m.group(1) == 'good':
45 try:
46 validator.validate(data)
Christian Berendt2f166642014-05-30 00:13:23 +020047 except voluptuous.Invalid as e:
James E. Blair47958382013-01-10 17:26:02 -080048 raise Exception(
49 'Unexpected YAML syntax error in %s:\n %s' %
50 (fn, str(e)))
51 else:
52 try:
53 validator.validate(data)
54 raise Exception("Expected a YAML syntax error in %s." %
55 fn)
Christian Berendt2f166642014-05-30 00:13:23 +020056 except voluptuous.Invalid as e:
James E. Blair47958382013-01-10 17:26:02 -080057 error = str(e)
58 print ' ', error
59 if error in errors:
Clark Boylan2532dd62014-08-21 14:40:19 -070060 raise Exception("Error has already been tested: %s" %
James E. Blair47958382013-01-10 17:26:02 -080061 error)
62 else:
63 errors.append(error)
64 pass