blob: 3b27885718fb6f0ff5ee801f8fcc9be3e81a7d00 [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.
Ken'ichi Ohmichi0afa8812015-11-19 08:58:54 +000038
Attila Fazekas0aababb2013-07-28 20:19:00 +020039 You need to pass ``test_method`` and ``class_setup_per``
40 using ``kwargs`` in the JSON descriptor;
41 ``test_method`` should be the fully qualified name of a unittest,
42 ``class_setup_per`` should be one from:
43 ``application``: once in the stress job lifetime
44 ``process``: once in the worker process lifetime
45 ``action``: on each action
46 Not all combination working in every case.
47 """
48
49 def setUp(self, **kwargs):
50 method = kwargs['test_method'].split('.')
51 self.test_method = method.pop()
52 self.klass = importutils.import_class('.'.join(method))
Marc Koderer33ca6ee2013-08-29 09:06:36 +020053 self.logger = logging.getLogger('.'.join(method))
Attila Fazekas0aababb2013-07-28 20:19:00 +020054 # valid options are 'process', 'application' , 'action'
55 self.class_setup_per = kwargs.get('class_setup_per',
56 SetUpClassRunTime.process)
57 SetUpClassRunTime.validate(self.class_setup_per)
58
59 if self.class_setup_per == SetUpClassRunTime.application:
60 self.klass.setUpClass()
61 self.setupclass_called = False
62
Marc Koderer33ca6ee2013-08-29 09:06:36 +020063 @property
64 def action(self):
65 if self.test_method:
66 return self.test_method
67 return super(UnitTest, self).action
68
Attila Fazekas0aababb2013-07-28 20:19:00 +020069 def run_core(self):
70 res = self.klass(self.test_method).run()
71 if res.errors:
72 raise RuntimeError(res.errors)
73
74 def run(self):
75 if self.class_setup_per != SetUpClassRunTime.application:
76 if (self.class_setup_per == SetUpClassRunTime.action
77 or self.setupclass_called is False):
78 self.klass.setUpClass()
79 self.setupclass_called = True
80
Julien Leloup04d40f72014-01-28 11:17:18 +010081 try:
82 self.run_core()
83 except Exception as e:
84 raise e
85 finally:
86 if (CONF.stress.leave_dirty_stack is False
87 and self.class_setup_per == SetUpClassRunTime.action):
88 self.klass.tearDownClass()
Attila Fazekas0aababb2013-07-28 20:19:00 +020089 else:
90 self.run_core()
91
92 def tearDown(self):
93 if self.class_setup_per != SetUpClassRunTime.action:
94 self.klass.tearDownClass()