blob: f8e278dd2a52da7cdef9b13b1c165ae7db31ee80 [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
13from __future__ import absolute_import
14
15import os
16import unittest
17
18from gabbi import runner
19from gabbi import suitemaker
20from gabbi import utils
Pavlo Shchelokovskyyc8499db2020-10-05 12:05:15 +030021from oslo_config import cfg
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +020022from oslo_log import log as logging
23
24LOG = logging.getLogger(__name__)
Pavlo Shchelokovskyyc8499db2020-10-05 12:05:15 +030025CONF = cfg.CONF
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +020026
27
28def run_test(test_class_instance, test_dir, filename):
29 d = utils.load_yaml(yaml_file=os.path.join(test_dir, filename))
Pavlo Shchelokovskyyc8499db2020-10-05 12:05:15 +030030 cert_validate = not CONF.telemetry.disable_ssl_certificate_validation
31 if 'defaults' in d:
32 d['defaults']['cert_validate'] = cert_validate
33 else:
34 d['defaults'] = {'cert_validate': cert_validate}
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +020035 test_suite = suitemaker.test_suite_from_dict(
36 loader=unittest.defaultTestLoader,
37 test_base_name="gabbi",
38 suite_dict=d,
39 test_directory=test_dir,
40 host='example.com', port=None,
41 fixture_module=None,
42 intercept=None,
43 handlers=runner.initialize_handlers([]),
44 test_loader_name="tempest")
45
46 # NOTE(sileht): We hide stdout/stderr and reraise the failure
47 # manually, tempest will print it ittest_class.
48 with open(os.devnull, 'w') as stream:
49 result = unittest.TextTestRunner(
50 stream=stream, verbosity=0, failfast=True,
51 ).run(test_suite)
52
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +020053 if not result.wasSuccessful():
54 failures = (result.errors + result.failures +
55 result.unexpectedSuccesses)
56 if failures:
57 test, bt = failures[0]
58 name = test.test_data.get('name', test.id())
59 msg = 'From test "%s" :\n%s' % (name, bt)
60 test_class_instance.fail(msg)
61
62 test_class_instance.assertTrue(result.wasSuccessful())
63
64
65def test_maker(test_dir, filename, name):
66 def test(self):
67 self._prep_test(filename)
68 run_test(self, test_dir, filename)
69 test.__name__ = name
70 return test
71
72
73def generate_tests(test_class, test_dir):
74 # Create one scenario per yaml file
75 filenames = os.listdir(test_dir)
76 if not filenames:
77 raise RuntimeError("%s is empty" % test_dir)
78 for filename in filenames:
79 if not filename.endswith('.yaml'):
80 continue
81 name = "test_%s" % filename[:-5].lower().replace("-", "_")
82 setattr(test_class, name,
83 test_maker(test_dir, filename, name))