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 | |
Morgan Fainberg | e77bf87 | 2016-05-30 14:31:08 -0700 | [diff] [blame] | 17 | from six.moves import configparser as 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 | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 34 | def setUp(self): |
| 35 | self.skip("Disabled for early v3 development") |
| 36 | |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 37 | def test_layouts(self): |
| 38 | """Test layout file validation""" |
Morgan Fainberg | 4c6a774 | 2016-05-27 08:42:17 -0700 | [diff] [blame] | 39 | print() |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 40 | 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 Fainberg | 4c6a774 | 2016-05-27 08:42:17 -0700 | [diff] [blame] | 45 | print(fn) |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 46 | |
| 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. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 58 | layout = os.path.join(FIXTURE_DIR, 'layouts', fn) |
James E. Blair | dc4721b | 2017-03-03 10:01:18 -0800 | [diff] [blame] | 59 | data = yaml.safe_load(open(layout)) |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 60 | validator = zuul.layoutvalidator.LayoutValidator() |
| 61 | if m.group(1) == 'good': |
| 62 | try: |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 63 | validator.validate(data, connections) |
Christian Berendt | 2f16664 | 2014-05-30 00:13:23 +0200 | [diff] [blame] | 64 | except voluptuous.Invalid as e: |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 65 | raise Exception( |
| 66 | 'Unexpected YAML syntax error in %s:\n %s' % |
| 67 | (fn, str(e))) |
| 68 | else: |
| 69 | try: |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 70 | validator.validate(data, connections) |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 71 | raise Exception("Expected a YAML syntax error in %s." % |
| 72 | fn) |
Christian Berendt | 2f16664 | 2014-05-30 00:13:23 +0200 | [diff] [blame] | 73 | except voluptuous.Invalid as e: |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 74 | error = str(e) |
Morgan Fainberg | 4c6a774 | 2016-05-27 08:42:17 -0700 | [diff] [blame] | 75 | print(' ', error) |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 76 | if error in errors: |
Clark Boylan | 2532dd6 | 2014-08-21 14:40:19 -0700 | [diff] [blame] | 77 | raise Exception("Error has already been tested: %s" % |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 78 | error) |
| 79 | else: |
| 80 | errors.append(error) |
| 81 | pass |