Attila Fazekas | 0aababb | 2013-07-28 20:19:00 +0200 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | from tempest.openstack.common import importutils |
| 16 | import tempest.stress.stressaction as stressaction |
| 17 | |
| 18 | |
| 19 | class SetUpClassRunTime(object): |
| 20 | |
| 21 | process = 'process' |
| 22 | action = 'action' |
| 23 | application = 'application' |
| 24 | |
| 25 | allowed = set((process, action, application)) |
| 26 | |
| 27 | @classmethod |
| 28 | def validate(cls, name): |
| 29 | if name not in cls.allowed: |
| 30 | raise KeyError("\'%s\' not a valid option" % name) |
| 31 | |
| 32 | |
| 33 | class UnitTest(stressaction.StressAction): |
| 34 | """This is a special action for running existing unittests as stress test. |
| 35 | You need to pass ``test_method`` and ``class_setup_per`` |
| 36 | using ``kwargs`` in the JSON descriptor; |
| 37 | ``test_method`` should be the fully qualified name of a unittest, |
| 38 | ``class_setup_per`` should be one from: |
| 39 | ``application``: once in the stress job lifetime |
| 40 | ``process``: once in the worker process lifetime |
| 41 | ``action``: on each action |
| 42 | Not all combination working in every case. |
| 43 | """ |
| 44 | |
| 45 | def setUp(self, **kwargs): |
| 46 | method = kwargs['test_method'].split('.') |
| 47 | self.test_method = method.pop() |
| 48 | self.klass = importutils.import_class('.'.join(method)) |
| 49 | # 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 | |
| 58 | def run_core(self): |
| 59 | res = self.klass(self.test_method).run() |
| 60 | if res.errors: |
| 61 | raise RuntimeError(res.errors) |
| 62 | |
| 63 | def run(self): |
| 64 | if self.class_setup_per != SetUpClassRunTime.application: |
| 65 | if (self.class_setup_per == SetUpClassRunTime.action |
| 66 | or self.setupclass_called is False): |
| 67 | self.klass.setUpClass() |
| 68 | self.setupclass_called = True |
| 69 | |
| 70 | self.run_core() |
| 71 | |
| 72 | if (self.class_setup_per == SetUpClassRunTime.action): |
| 73 | self.klass.tearDownClass() |
| 74 | else: |
| 75 | self.run_core() |
| 76 | |
| 77 | def tearDown(self): |
| 78 | if self.class_setup_per != SetUpClassRunTime.action: |
| 79 | self.klass.tearDownClass() |