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 | |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 17 | import os |
| 18 | import re |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame^] | 19 | |
| 20 | import testtools |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 21 | import voluptuous |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame^] | 22 | import yaml |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 23 | |
| 24 | import zuul.layoutvalidator |
| 25 | |
| 26 | FIXTURE_DIR = os.path.join(os.path.dirname(__file__), |
| 27 | 'fixtures') |
| 28 | LAYOUT_RE = re.compile(r'^(good|bad)_.*\.yaml$') |
| 29 | |
| 30 | |
Monty Taylor | bc75883 | 2013-06-17 17:22:42 -0400 | [diff] [blame^] | 31 | class testScheduler(testtools.TestCase): |
James E. Blair | 4795838 | 2013-01-10 17:26:02 -0800 | [diff] [blame] | 32 | 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) |
| 47 | except voluptuous.Invalid, e: |
| 48 | 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) |
| 56 | except voluptuous.Invalid, e: |
| 57 | error = str(e) |
| 58 | print ' ', error |
| 59 | if error in errors: |
| 60 | raise Exception("Error has already beed tested: %s" % |
| 61 | error) |
| 62 | else: |
| 63 | errors.append(error) |
| 64 | pass |