blob: 09041609777e0a2d5ae5ca54f7968ff1d7c0cf10 [file] [log] [blame]
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +02001# 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 Abaakoukb64d2b02018-09-03 11:50:49 +020013import os
14import unittest
15
16from gabbi import runner
17from gabbi import suitemaker
18from gabbi import utils
Pavlo Shchelokovskyy959fc692020-10-05 12:05:15 +030019from oslo_config import cfg
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +020020from oslo_log import log as logging
21
22LOG = logging.getLogger(__name__)
Pavlo Shchelokovskyy959fc692020-10-05 12:05:15 +030023CONF = cfg.CONF
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +020024
25
26def run_test(test_class_instance, test_dir, filename):
27 d = utils.load_yaml(yaml_file=os.path.join(test_dir, filename))
Pavlo Shchelokovskyy959fc692020-10-05 12:05:15 +030028 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 Abaakoukb64d2b02018-09-03 11:50:49 +020033 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 Abaakoukb64d2b02018-09-03 11:50:49 +020051 if not result.wasSuccessful():
Andreas Jaeger7716ce02020-05-03 11:11:49 +020052 failures = (result.errors + result.failures
53 + result.unexpectedSuccesses)
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +020054 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
63def 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
71def 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))