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