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