David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 4 | |
| 5 | # Copyright 2013 Quanta Research Cambridge, Inc. |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | |
| 19 | import argparse |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 20 | import inspect |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 21 | import json |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 22 | import sys |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 23 | from testtools.testsuite import iterate_tests |
| 24 | from unittest import loader |
| 25 | |
| 26 | |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 27 | def discover_stress_tests(path="./", filter_attr=None, call_inherited=False): |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 28 | """Discovers all tempest tests and create action out of them |
| 29 | """ |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 30 | tests = [] |
| 31 | testloader = loader.TestLoader() |
| 32 | list = testloader.discover(path) |
| 33 | for func in (iterate_tests(list)): |
| 34 | try: |
| 35 | method_name = getattr(func, '_testMethodName') |
| 36 | full_name = "%s.%s.%s" % (func.__module__, |
| 37 | func.__class__.__name__, |
| 38 | method_name) |
| 39 | test_func = getattr(func, method_name) |
| 40 | # NOTE(mkoderer): this contains a list of all type attributes |
| 41 | attrs = getattr(test_func, "__testtools_attrs") |
| 42 | except Exception: |
| 43 | next |
| 44 | if 'stress' in attrs: |
| 45 | if filter_attr is not None and not filter_attr in attrs: |
| 46 | continue |
| 47 | class_setup_per = getattr(test_func, "st_class_setup_per") |
| 48 | |
| 49 | action = {'action': |
| 50 | "tempest.stress.actions.unit_test.UnitTest", |
| 51 | 'kwargs': {"test_method": full_name, |
| 52 | "class_setup_per": class_setup_per |
| 53 | } |
| 54 | } |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 55 | if (not call_inherited and |
| 56 | getattr(test_func, "st_allow_inheritance") is not True): |
| 57 | class_structure = inspect.getmro(test_func.im_class) |
| 58 | if test_func.__name__ not in class_structure[0].__dict__: |
| 59 | continue |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 60 | tests.append(action) |
| 61 | return tests |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 62 | |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 63 | |
| 64 | def main(ns): |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 65 | # NOTE(mkoderer): moved import to make "-h" possible without OpenStack |
Marc Koderer | fa399e7 | 2013-07-16 08:56:01 +0200 | [diff] [blame] | 66 | from tempest.stress import driver |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 67 | result = 0 |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 68 | if not ns.all: |
| 69 | tests = json.load(open(ns.tests, 'r')) |
| 70 | else: |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 71 | tests = discover_stress_tests(filter_attr=ns.type, |
| 72 | call_inherited=ns.call_inherited) |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 73 | |
Matthew Treinish | 8e663fc | 2013-07-16 10:55:22 -0400 | [diff] [blame] | 74 | if ns.serial: |
| 75 | for test in tests: |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 76 | step_result = driver.stress_openstack([test], |
| 77 | ns.duration, |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 78 | ns.number, |
| 79 | ns.stop) |
| 80 | # NOTE(mkoderer): we just save the last result code |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 81 | if (step_result != 0): |
| 82 | result = step_result |
Matthew Treinish | 8e663fc | 2013-07-16 10:55:22 -0400 | [diff] [blame] | 83 | else: |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 84 | driver.stress_openstack(tests, ns.duration, ns.number, ns.stop) |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 85 | return result |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 86 | |
| 87 | |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 88 | parser = argparse.ArgumentParser(description='Run stress tests') |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 89 | parser.add_argument('-d', '--duration', default=300, type=int, |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 90 | help="Duration of test in secs") |
Matthew Treinish | 8e663fc | 2013-07-16 10:55:22 -0400 | [diff] [blame] | 91 | parser.add_argument('-s', '--serial', action='store_true', |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 92 | help="Trigger running tests serially") |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 93 | parser.add_argument('-S', '--stop', action='store_true', |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 94 | default=False, help="Stop on first error") |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 95 | parser.add_argument('-n', '--number', type=int, |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 96 | help="How often an action is executed for each process") |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 97 | group = parser.add_mutually_exclusive_group(required=True) |
| 98 | group.add_argument('-a', '--all', action='store_true', |
| 99 | help="Execute all stress tests") |
| 100 | parser.add_argument('-T', '--type', |
| 101 | help="Filters tests of a certain type (e.g. gate)") |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 102 | parser.add_argument('-i', '--call-inherited', action='store_true', |
| 103 | default=False, |
| 104 | help="Call also inherited function with stress attribute") |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 105 | group.add_argument('-t', "--tests", nargs='?', |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 106 | help="Name of the file with test description") |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 107 | |
| 108 | if __name__ == "__main__": |
| 109 | sys.exit(main(parser.parse_args())) |