blob: 38c8e29a6a1bda7ea8998c95a058d12a2bcecf8f [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. Blair83005782015-12-11 14:46:03 -080034 def setUp(self):
35 self.skip("Disabled for early v3 development")
36
James E. Blair47958382013-01-10 17:26:02 -080037 def test_layouts(self):
38 """Test layout file validation"""
Morgan Fainberg4c6a7742016-05-27 08:42:17 -070039 print()
James E. Blair47958382013-01-10 17:26:02 -080040 errors = []
41 for fn in os.listdir(os.path.join(FIXTURE_DIR, 'layouts')):
42 m = LAYOUT_RE.match(fn)
43 if not m:
44 continue
Morgan Fainberg4c6a7742016-05-27 08:42:17 -070045 print(fn)
Joshua Hesketh352264b2015-08-11 23:42:08 +100046
47 # Load any .conf file by the same name but .conf extension.
48 config_file = ("%s.conf" %
49 os.path.join(FIXTURE_DIR, 'layouts',
50 fn.split('.yaml')[0]))
51 if not os.path.isfile(config_file):
52 config_file = os.path.join(FIXTURE_DIR, 'layouts',
53 'zuul_default.conf')
54 config = ConfigParser.ConfigParser()
55 config.read(config_file)
56 connections = zuul.lib.connections.configure_connections(config)
57
James E. Blair47958382013-01-10 17:26:02 -080058 layout = os.path.join(FIXTURE_DIR, 'layouts', fn)
59 data = yaml.load(open(layout))
60 validator = zuul.layoutvalidator.LayoutValidator()
61 if m.group(1) == 'good':
62 try:
Joshua Hesketh352264b2015-08-11 23:42:08 +100063 validator.validate(data, connections)
Christian Berendt2f166642014-05-30 00:13:23 +020064 except voluptuous.Invalid as e:
James E. Blair47958382013-01-10 17:26:02 -080065 raise Exception(
66 'Unexpected YAML syntax error in %s:\n %s' %
67 (fn, str(e)))
68 else:
69 try:
Joshua Hesketh352264b2015-08-11 23:42:08 +100070 validator.validate(data, connections)
James E. Blair47958382013-01-10 17:26:02 -080071 raise Exception("Expected a YAML syntax error in %s." %
72 fn)
Christian Berendt2f166642014-05-30 00:13:23 +020073 except voluptuous.Invalid as e:
James E. Blair47958382013-01-10 17:26:02 -080074 error = str(e)
Morgan Fainberg4c6a7742016-05-27 08:42:17 -070075 print(' ', error)
James E. Blair47958382013-01-10 17:26:02 -080076 if error in errors:
Clark Boylan2532dd62014-08-21 14:40:19 -070077 raise Exception("Error has already been tested: %s" %
James E. Blair47958382013-01-10 17:26:02 -080078 error)
79 else:
80 errors.append(error)
81 pass