David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 3 | # Copyright 2013 Quanta Research Cambridge, Inc. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain 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, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import argparse |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 18 | import inspect |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 19 | import json |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 20 | import sys |
Will | 16a778a | 2014-01-12 21:49:14 -0500 | [diff] [blame] | 21 | try: |
| 22 | from unittest import loader |
| 23 | except ImportError: |
| 24 | # unittest in python 2.6 does not contain loader, so uses unittest2 |
| 25 | from unittest2 import loader |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 26 | |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 27 | from testtools import testsuite |
| 28 | |
Marc Koderer | 16648e9 | 2013-10-05 10:49:13 +0200 | [diff] [blame] | 29 | from tempest.openstack.common import log as logging |
Marc Koderer | 211ca96 | 2013-12-05 12:20:21 +0100 | [diff] [blame] | 30 | from tempest.stress import driver |
Marc Koderer | 16648e9 | 2013-10-05 10:49:13 +0200 | [diff] [blame] | 31 | |
| 32 | LOG = logging.getLogger(__name__) |
| 33 | |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 34 | |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 35 | def discover_stress_tests(path="./", filter_attr=None, call_inherited=False): |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 36 | """Discovers all tempest tests and create action out of them |
| 37 | """ |
Marc Koderer | 16648e9 | 2013-10-05 10:49:13 +0200 | [diff] [blame] | 38 | LOG.info("Start test discovery") |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 39 | tests = [] |
| 40 | testloader = loader.TestLoader() |
| 41 | list = testloader.discover(path) |
Masayuki Igawa | 7e9eb54 | 2014-02-17 15:03:44 +0900 | [diff] [blame] | 42 | for func in (testsuite.iterate_tests(list)): |
Marc Koderer | 16648e9 | 2013-10-05 10:49:13 +0200 | [diff] [blame] | 43 | attrs = [] |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 44 | try: |
| 45 | method_name = getattr(func, '_testMethodName') |
| 46 | full_name = "%s.%s.%s" % (func.__module__, |
| 47 | func.__class__.__name__, |
| 48 | method_name) |
| 49 | test_func = getattr(func, method_name) |
| 50 | # NOTE(mkoderer): this contains a list of all type attributes |
| 51 | attrs = getattr(test_func, "__testtools_attrs") |
| 52 | except Exception: |
| 53 | next |
| 54 | if 'stress' in attrs: |
Matthew Treinish | c795b9e | 2014-06-09 17:01:10 -0400 | [diff] [blame] | 55 | if filter_attr is not None and filter_attr not in attrs: |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 56 | continue |
| 57 | class_setup_per = getattr(test_func, "st_class_setup_per") |
| 58 | |
| 59 | action = {'action': |
| 60 | "tempest.stress.actions.unit_test.UnitTest", |
| 61 | 'kwargs': {"test_method": full_name, |
| 62 | "class_setup_per": class_setup_per |
| 63 | } |
| 64 | } |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 65 | if (not call_inherited and |
| 66 | getattr(test_func, "st_allow_inheritance") is not True): |
| 67 | class_structure = inspect.getmro(test_func.im_class) |
| 68 | if test_func.__name__ not in class_structure[0].__dict__: |
| 69 | continue |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 70 | tests.append(action) |
| 71 | return tests |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 72 | |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 73 | |
Matthew Treinish | 55e29b4 | 2014-05-07 01:04:17 -0400 | [diff] [blame] | 74 | parser = argparse.ArgumentParser(description='Run stress tests') |
| 75 | parser.add_argument('-d', '--duration', default=300, type=int, |
| 76 | help="Duration of test in secs") |
| 77 | parser.add_argument('-s', '--serial', action='store_true', |
| 78 | help="Trigger running tests serially") |
| 79 | parser.add_argument('-S', '--stop', action='store_true', |
| 80 | default=False, help="Stop on first error") |
| 81 | parser.add_argument('-n', '--number', type=int, |
| 82 | help="How often an action is executed for each process") |
| 83 | group = parser.add_mutually_exclusive_group(required=True) |
| 84 | group.add_argument('-a', '--all', action='store_true', |
| 85 | help="Execute all stress tests") |
| 86 | parser.add_argument('-T', '--type', |
| 87 | help="Filters tests of a certain type (e.g. gate)") |
| 88 | parser.add_argument('-i', '--call-inherited', action='store_true', |
| 89 | default=False, |
| 90 | help="Call also inherited function with stress attribute") |
| 91 | group.add_argument('-t', "--tests", nargs='?', |
| 92 | help="Name of the file with test description") |
| 93 | |
| 94 | |
| 95 | def main(): |
| 96 | ns = parser.parse_args() |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 97 | result = 0 |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 98 | if not ns.all: |
| 99 | tests = json.load(open(ns.tests, 'r')) |
| 100 | else: |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 101 | tests = discover_stress_tests(filter_attr=ns.type, |
| 102 | call_inherited=ns.call_inherited) |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 103 | |
Matthew Treinish | 8e663fc | 2013-07-16 10:55:22 -0400 | [diff] [blame] | 104 | if ns.serial: |
David Kranz | 71fee47 | 2014-10-13 15:13:02 -0400 | [diff] [blame] | 105 | # Duration is total time |
| 106 | duration = ns.duration / len(tests) |
Matthew Treinish | 8e663fc | 2013-07-16 10:55:22 -0400 | [diff] [blame] | 107 | for test in tests: |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 108 | step_result = driver.stress_openstack([test], |
David Kranz | 71fee47 | 2014-10-13 15:13:02 -0400 | [diff] [blame] | 109 | duration, |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 110 | ns.number, |
| 111 | ns.stop) |
| 112 | # NOTE(mkoderer): we just save the last result code |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 113 | if (step_result != 0): |
| 114 | result = step_result |
Attila Fazekas | f2a8bbd | 2014-02-23 17:19:12 +0100 | [diff] [blame] | 115 | if ns.stop: |
| 116 | return result |
Matthew Treinish | 8e663fc | 2013-07-16 10:55:22 -0400 | [diff] [blame] | 117 | else: |
Attila Fazekas | f2a8bbd | 2014-02-23 17:19:12 +0100 | [diff] [blame] | 118 | result = driver.stress_openstack(tests, |
| 119 | ns.duration, |
| 120 | ns.number, |
| 121 | ns.stop) |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 122 | return result |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 123 | |
| 124 | |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 125 | if __name__ == "__main__": |
Marc Koderer | 16648e9 | 2013-10-05 10:49:13 +0200 | [diff] [blame] | 126 | try: |
Matthew Treinish | 55e29b4 | 2014-05-07 01:04:17 -0400 | [diff] [blame] | 127 | sys.exit(main()) |
Marc Koderer | 16648e9 | 2013-10-05 10:49:13 +0200 | [diff] [blame] | 128 | except Exception: |
| 129 | LOG.exception("Failure in the stress test framework") |
| 130 | sys.exit(1) |