James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 1 | #!/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 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame^] | 17 | import ConfigParser |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 18 | import os |
| 19 | import re |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 20 | |
| 21 | import testtools |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 22 | import voluptuous |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame] | 23 | import yaml |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 24 | |
| 25 | import zuul.layoutvalidator |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame^] | 26 | import zuul.lib.connections |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 27 | |
| 28 | FIXTURE_DIR = os.path.join(os.path.dirname(__file__), |
| 29 | 'fixtures') |
| 30 | LAYOUT_RE = re.compile(r'^(good|bad)_.*\.yaml$') |
| 31 | |
| 32 | |
James E. Blair | 6c358e7 | 2013-07-29 17:06:47 -0700 | [diff] [blame] | 33 | class TestLayoutValidator(testtools.TestCase): |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 34 | def test_layouts(self): |
| 35 | """Test layout file validation""" |
| 36 | print |
| 37 | 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 |
| 42 | print fn |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame^] | 43 | |
| 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. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 55 | 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 Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame^] | 60 | validator.validate(data, connections) |
Christian Berendt | 2f16664 | 2014-05-30 00:13:23 +0200 | [diff] [blame] | 61 | except voluptuous.Invalid as e: |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 62 | raise Exception( |
| 63 | 'Unexpected YAML syntax error in %s:\n %s' % |
| 64 | (fn, str(e))) |
| 65 | else: |
| 66 | try: |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame^] | 67 | validator.validate(data, connections) |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 68 | raise Exception("Expected a YAML syntax error in %s." % |
| 69 | fn) |
Christian Berendt | 2f16664 | 2014-05-30 00:13:23 +0200 | [diff] [blame] | 70 | except voluptuous.Invalid as e: |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 71 | error = str(e) |
| 72 | print ' ', error |
| 73 | if error in errors: |
Clark Boylan | 2532dd6 | 2014-08-21 14:40:19 -0700 | [diff] [blame] | 74 | raise Exception("Error has already been tested: %s" % |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 75 | error) |
| 76 | else: |
| 77 | errors.append(error) |
| 78 | pass |