blob: 07f3f66e7583bdc2a5d236a5d0606e11b9358ed9 [file] [log] [blame]
David Kranzb9d97502013-05-01 15:55:04 -04001#!/usr/bin/env python
2
David Kranzb9d97502013-05-01 15:55:04 -04003# 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
17import argparse
Marc Kodererb0604412013-09-02 09:43:40 +020018import inspect
David Kranzb9d97502013-05-01 15:55:04 -040019import json
Marc Koderer888ddc42013-07-23 16:13:07 +020020import sys
Masayuki Igawa7e9eb542014-02-17 15:03:44 +090021from testtools import testsuite
Will16a778a2014-01-12 21:49:14 -050022try:
23 from unittest import loader
24except ImportError:
25 # unittest in python 2.6 does not contain loader, so uses unittest2
26 from unittest2 import loader
Marc Koderer32221b8e2013-08-23 13:57:50 +020027
Marc Koderer16648e92013-10-05 10:49:13 +020028from tempest.openstack.common import log as logging
Marc Koderer211ca962013-12-05 12:20:21 +010029from tempest.stress import driver
Marc Koderer16648e92013-10-05 10:49:13 +020030
31LOG = logging.getLogger(__name__)
32
Marc Koderer32221b8e2013-08-23 13:57:50 +020033
Marc Kodererb0604412013-09-02 09:43:40 +020034def discover_stress_tests(path="./", filter_attr=None, call_inherited=False):
Marc Koderer32221b8e2013-08-23 13:57:50 +020035 """Discovers all tempest tests and create action out of them
36 """
Marc Koderer16648e92013-10-05 10:49:13 +020037 LOG.info("Start test discovery")
Marc Koderer32221b8e2013-08-23 13:57:50 +020038 tests = []
39 testloader = loader.TestLoader()
40 list = testloader.discover(path)
Masayuki Igawa7e9eb542014-02-17 15:03:44 +090041 for func in (testsuite.iterate_tests(list)):
Marc Koderer16648e92013-10-05 10:49:13 +020042 attrs = []
Marc Koderer32221b8e2013-08-23 13:57:50 +020043 try:
44 method_name = getattr(func, '_testMethodName')
45 full_name = "%s.%s.%s" % (func.__module__,
46 func.__class__.__name__,
47 method_name)
48 test_func = getattr(func, method_name)
49 # NOTE(mkoderer): this contains a list of all type attributes
50 attrs = getattr(test_func, "__testtools_attrs")
51 except Exception:
52 next
53 if 'stress' in attrs:
Matthew Treinishc795b9e2014-06-09 17:01:10 -040054 if filter_attr is not None and filter_attr not in attrs:
Marc Koderer32221b8e2013-08-23 13:57:50 +020055 continue
56 class_setup_per = getattr(test_func, "st_class_setup_per")
57
58 action = {'action':
59 "tempest.stress.actions.unit_test.UnitTest",
60 'kwargs': {"test_method": full_name,
61 "class_setup_per": class_setup_per
62 }
63 }
Marc Kodererb0604412013-09-02 09:43:40 +020064 if (not call_inherited and
65 getattr(test_func, "st_allow_inheritance") is not True):
66 class_structure = inspect.getmro(test_func.im_class)
67 if test_func.__name__ not in class_structure[0].__dict__:
68 continue
Marc Koderer32221b8e2013-08-23 13:57:50 +020069 tests.append(action)
70 return tests
David Kranzb9d97502013-05-01 15:55:04 -040071
David Kranzb9d97502013-05-01 15:55:04 -040072
Matthew Treinish55e29b42014-05-07 01:04:17 -040073parser = argparse.ArgumentParser(description='Run stress tests')
74parser.add_argument('-d', '--duration', default=300, type=int,
75 help="Duration of test in secs")
76parser.add_argument('-s', '--serial', action='store_true',
77 help="Trigger running tests serially")
78parser.add_argument('-S', '--stop', action='store_true',
79 default=False, help="Stop on first error")
80parser.add_argument('-n', '--number', type=int,
81 help="How often an action is executed for each process")
82group = parser.add_mutually_exclusive_group(required=True)
83group.add_argument('-a', '--all', action='store_true',
84 help="Execute all stress tests")
85parser.add_argument('-T', '--type',
86 help="Filters tests of a certain type (e.g. gate)")
87parser.add_argument('-i', '--call-inherited', action='store_true',
88 default=False,
89 help="Call also inherited function with stress attribute")
90group.add_argument('-t', "--tests", nargs='?',
91 help="Name of the file with test description")
92
93
94def main():
95 ns = parser.parse_args()
Marc Koderer888ddc42013-07-23 16:13:07 +020096 result = 0
Marc Koderer32221b8e2013-08-23 13:57:50 +020097 if not ns.all:
98 tests = json.load(open(ns.tests, 'r'))
99 else:
Marc Kodererb0604412013-09-02 09:43:40 +0200100 tests = discover_stress_tests(filter_attr=ns.type,
101 call_inherited=ns.call_inherited)
Marc Koderer32221b8e2013-08-23 13:57:50 +0200102
Matthew Treinish8e663fc2013-07-16 10:55:22 -0400103 if ns.serial:
104 for test in tests:
Marc Koderer888ddc42013-07-23 16:13:07 +0200105 step_result = driver.stress_openstack([test],
106 ns.duration,
Marc Koderer3414d732013-07-31 08:36:36 +0200107 ns.number,
108 ns.stop)
109 # NOTE(mkoderer): we just save the last result code
Marc Koderer888ddc42013-07-23 16:13:07 +0200110 if (step_result != 0):
111 result = step_result
Attila Fazekasf2a8bbd2014-02-23 17:19:12 +0100112 if ns.stop:
113 return result
Matthew Treinish8e663fc2013-07-16 10:55:22 -0400114 else:
Attila Fazekasf2a8bbd2014-02-23 17:19:12 +0100115 result = driver.stress_openstack(tests,
116 ns.duration,
117 ns.number,
118 ns.stop)
Marc Koderer888ddc42013-07-23 16:13:07 +0200119 return result
David Kranzb9d97502013-05-01 15:55:04 -0400120
121
Marc Koderer888ddc42013-07-23 16:13:07 +0200122if __name__ == "__main__":
Marc Koderer16648e92013-10-05 10:49:13 +0200123 try:
Matthew Treinish55e29b42014-05-07 01:04:17 -0400124 sys.exit(main())
Marc Koderer16648e92013-10-05 10:49:13 +0200125 except Exception:
126 LOG.exception("Failure in the stress test framework")
127 sys.exit(1)