blob: c3766932fbf09d2e6c7bf64939153d40ea7197fd [file] [log] [blame]
Attila Fazekas0aababb2013-07-28 20:19:00 +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
Doug Hellmann583ce2c2015-03-11 14:55:46 +000013from oslo_log import log as logging
14from oslo_utils import importutils
15
Julien Leloup04d40f72014-01-28 11:17:18 +010016from tempest import config
Attila Fazekas0aababb2013-07-28 20:19:00 +020017import tempest.stress.stressaction as stressaction
18
Julien Leloup04d40f72014-01-28 11:17:18 +010019CONF = config.CONF
20
Attila Fazekas0aababb2013-07-28 20:19:00 +020021
22class SetUpClassRunTime(object):
23
24 process = 'process'
25 action = 'action'
26 application = 'application'
27
28 allowed = set((process, action, application))
29
30 @classmethod
31 def validate(cls, name):
32 if name not in cls.allowed:
33 raise KeyError("\'%s\' not a valid option" % name)
34
35
36class UnitTest(stressaction.StressAction):
37 """This is a special action for running existing unittests as stress test.
38 You need to pass ``test_method`` and ``class_setup_per``
39 using ``kwargs`` in the JSON descriptor;
40 ``test_method`` should be the fully qualified name of a unittest,
41 ``class_setup_per`` should be one from:
42 ``application``: once in the stress job lifetime
43 ``process``: once in the worker process lifetime
44 ``action``: on each action
45 Not all combination working in every case.
46 """
47
48 def setUp(self, **kwargs):
49 method = kwargs['test_method'].split('.')
50 self.test_method = method.pop()
51 self.klass = importutils.import_class('.'.join(method))
Marc Koderer33ca6ee2013-08-29 09:06:36 +020052 self.logger = logging.getLogger('.'.join(method))
Attila Fazekas0aababb2013-07-28 20:19:00 +020053 # valid options are 'process', 'application' , 'action'
54 self.class_setup_per = kwargs.get('class_setup_per',
55 SetUpClassRunTime.process)
56 SetUpClassRunTime.validate(self.class_setup_per)
57
58 if self.class_setup_per == SetUpClassRunTime.application:
59 self.klass.setUpClass()
60 self.setupclass_called = False
61
Marc Koderer33ca6ee2013-08-29 09:06:36 +020062 @property
63 def action(self):
64 if self.test_method:
65 return self.test_method
66 return super(UnitTest, self).action
67
Attila Fazekas0aababb2013-07-28 20:19:00 +020068 def run_core(self):
69 res = self.klass(self.test_method).run()
70 if res.errors:
71 raise RuntimeError(res.errors)
72
73 def run(self):
74 if self.class_setup_per != SetUpClassRunTime.application:
75 if (self.class_setup_per == SetUpClassRunTime.action
76 or self.setupclass_called is False):
77 self.klass.setUpClass()
78 self.setupclass_called = True
79
Julien Leloup04d40f72014-01-28 11:17:18 +010080 try:
81 self.run_core()
82 except Exception as e:
83 raise e
84 finally:
85 if (CONF.stress.leave_dirty_stack is False
86 and self.class_setup_per == SetUpClassRunTime.action):
87 self.klass.tearDownClass()
Attila Fazekas0aababb2013-07-28 20:19:00 +020088 else:
89 self.run_core()
90
91 def tearDown(self):
92 if self.class_setup_per != SetUpClassRunTime.action:
93 self.klass.tearDownClass()