blob: 46a8c7ccf3d7a7e69f19410b71fc762404b5c130 [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
Morgan Fainberge77bf872016-05-30 14:31:08 -070017from six.moves import configparser as ConfigParser
James E. Blair47958382013-01-10 17:26:02 -080018import os
19import re
Monty Taylorbc758832013-06-17 17:22:42 -040020
21import testtools
James E. Blair47958382013-01-10 17:26:02 -080022import voluptuous
Monty Taylorbc758832013-06-17 17:22:42 -040023import yaml
James E. Blair47958382013-01-10 17:26:02 -080024
25import zuul.layoutvalidator
Joshua Hesketh352264b2015-08-11 23:42:08 +100026import zuul.lib.connections
James E. Blair47958382013-01-10 17:26:02 -080027
28FIXTURE_DIR = os.path.join(os.path.dirname(__file__),
29 'fixtures')
30LAYOUT_RE = re.compile(r'^(good|bad)_.*\.yaml$')
31
32
James E. Blair6c358e72013-07-29 17:06:47 -070033class TestLayoutValidator(testtools.TestCase):
James E. Blair47958382013-01-10 17:26:02 -080034 def test_layouts(self):
35 """Test layout file validation"""
Morgan Fainberg4c6a7742016-05-27 08:42:17 -070036 print()
James E. Blair47958382013-01-10 17:26:02 -080037 errors = []
38 for fn in os.listdir(os.path.join(FIXTURE_DIR, 'layouts')):
39 m = LAYOUT_RE.match(fn)
40 if not m:
41 continue
Morgan Fainberg4c6a7742016-05-27 08:42:17 -070042 print(fn)
Joshua Hesketh352264b2015-08-11 23:42:08 +100043
44 # Load any .conf file by the same name but .conf extension.
45 config_file = ("%s.conf" %
46 os.path.join(FIXTURE_DIR, 'layouts',
47 fn.split('.yaml')[0]))
48 if not os.path.isfile(config_file):
49 config_file = os.path.join(FIXTURE_DIR, 'layouts',
50 'zuul_default.conf')
51 config = ConfigParser.ConfigParser()
52 config.read(config_file)
53 connections = zuul.lib.connections.configure_connections(config)
54
James E. Blair47958382013-01-10 17:26:02 -080055 layout = os.path.join(FIXTURE_DIR, 'layouts', fn)
56 data = yaml.load(open(layout))
57 validator = zuul.layoutvalidator.LayoutValidator()
58 if m.group(1) == 'good':
59 try:
Joshua Hesketh352264b2015-08-11 23:42:08 +100060 validator.validate(data, connections)
Christian Berendt2f166642014-05-30 00:13:23 +020061 except voluptuous.Invalid as e:
James E. Blair47958382013-01-10 17:26:02 -080062 raise Exception(
63 'Unexpected YAML syntax error in %s:\n %s' %
64 (fn, str(e)))
65 else:
66 try:
Joshua Hesketh352264b2015-08-11 23:42:08 +100067 validator.validate(data, connections)
James E. Blair47958382013-01-10 17:26:02 -080068 raise Exception("Expected a YAML syntax error in %s." %
69 fn)
Christian Berendt2f166642014-05-30 00:13:23 +020070 except voluptuous.Invalid as e:
James E. Blair47958382013-01-10 17:26:02 -080071 error = str(e)
Morgan Fainberg4c6a7742016-05-27 08:42:17 -070072 print(' ', error)
James E. Blair47958382013-01-10 17:26:02 -080073 if error in errors:
Clark Boylan2532dd62014-08-21 14:40:19 -070074 raise Exception("Error has already been tested: %s" %
James E. Blair47958382013-01-10 17:26:02 -080075 error)
76 else:
77 errors.append(error)
78 pass