Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 13 | import os |
| 14 | import unittest |
| 15 | |
| 16 | from gabbi import runner |
| 17 | from gabbi import suitemaker |
| 18 | from gabbi import utils |
Pavlo Shchelokovskyy | 959fc69 | 2020-10-05 12:05:15 +0300 | [diff] [blame] | 19 | from oslo_config import cfg |
Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 20 | from oslo_log import log as logging |
| 21 | |
| 22 | LOG = logging.getLogger(__name__) |
Pavlo Shchelokovskyy | 959fc69 | 2020-10-05 12:05:15 +0300 | [diff] [blame] | 23 | CONF = cfg.CONF |
Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def run_test(test_class_instance, test_dir, filename): |
| 27 | d = utils.load_yaml(yaml_file=os.path.join(test_dir, filename)) |
Pavlo Shchelokovskyy | 959fc69 | 2020-10-05 12:05:15 +0300 | [diff] [blame] | 28 | cert_validate = not CONF.telemetry.disable_ssl_certificate_validation |
| 29 | if 'defaults' in d: |
| 30 | d['defaults']['cert_validate'] = cert_validate |
| 31 | else: |
| 32 | d['defaults'] = {'cert_validate': cert_validate} |
Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 33 | test_suite = suitemaker.test_suite_from_dict( |
| 34 | loader=unittest.defaultTestLoader, |
| 35 | test_base_name="gabbi", |
| 36 | suite_dict=d, |
| 37 | test_directory=test_dir, |
| 38 | host='example.com', port=None, |
| 39 | fixture_module=None, |
| 40 | intercept=None, |
| 41 | handlers=runner.initialize_handlers([]), |
| 42 | test_loader_name="tempest") |
| 43 | |
| 44 | # NOTE(sileht): We hide stdout/stderr and reraise the failure |
| 45 | # manually, tempest will print it ittest_class. |
| 46 | with open(os.devnull, 'w') as stream: |
| 47 | result = unittest.TextTestRunner( |
| 48 | stream=stream, verbosity=0, failfast=True, |
| 49 | ).run(test_suite) |
| 50 | |
Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 51 | if not result.wasSuccessful(): |
Andreas Jaeger | 7716ce0 | 2020-05-03 11:11:49 +0200 | [diff] [blame] | 52 | failures = (result.errors + result.failures |
| 53 | + result.unexpectedSuccesses) |
Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 54 | if failures: |
| 55 | test, bt = failures[0] |
| 56 | name = test.test_data.get('name', test.id()) |
| 57 | msg = 'From test "%s" :\n%s' % (name, bt) |
| 58 | test_class_instance.fail(msg) |
| 59 | |
| 60 | test_class_instance.assertTrue(result.wasSuccessful()) |
| 61 | |
| 62 | |
| 63 | def test_maker(test_dir, filename, name): |
| 64 | def test(self): |
| 65 | self._prep_test(filename) |
| 66 | run_test(self, test_dir, filename) |
| 67 | test.__name__ = name |
| 68 | return test |
| 69 | |
| 70 | |
| 71 | def generate_tests(test_class, test_dir): |
| 72 | # Create one scenario per yaml file |
| 73 | filenames = os.listdir(test_dir) |
| 74 | if not filenames: |
| 75 | raise RuntimeError("%s is empty" % test_dir) |
| 76 | for filename in filenames: |
| 77 | if not filename.endswith('.yaml'): |
| 78 | continue |
| 79 | name = "test_%s" % filename[:-5].lower().replace("-", "_") |
| 80 | setattr(test_class, name, |
| 81 | test_maker(test_dir, filename, name)) |