Attila Fazekas | 0aababb | 2013-07-28 20:19:00 +0200 | [diff] [blame] | 1 | # 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 Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 13 | from oslo_log import log as logging |
| 14 | from oslo_utils import importutils |
| 15 | |
Julien Leloup | 04d40f7 | 2014-01-28 11:17:18 +0100 | [diff] [blame] | 16 | from tempest import config |
Attila Fazekas | 0aababb | 2013-07-28 20:19:00 +0200 | [diff] [blame] | 17 | import tempest.stress.stressaction as stressaction |
| 18 | |
Julien Leloup | 04d40f7 | 2014-01-28 11:17:18 +0100 | [diff] [blame] | 19 | CONF = config.CONF |
| 20 | |
Attila Fazekas | 0aababb | 2013-07-28 20:19:00 +0200 | [diff] [blame] | 21 | |
| 22 | class 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 | |
| 36 | class 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 Koderer | 33ca6ee | 2013-08-29 09:06:36 +0200 | [diff] [blame] | 52 | self.logger = logging.getLogger('.'.join(method)) |
Attila Fazekas | 0aababb | 2013-07-28 20:19:00 +0200 | [diff] [blame] | 53 | # 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 Koderer | 33ca6ee | 2013-08-29 09:06:36 +0200 | [diff] [blame] | 62 | @property |
| 63 | def action(self): |
| 64 | if self.test_method: |
| 65 | return self.test_method |
| 66 | return super(UnitTest, self).action |
| 67 | |
Attila Fazekas | 0aababb | 2013-07-28 20:19:00 +0200 | [diff] [blame] | 68 | 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 Leloup | 04d40f7 | 2014-01-28 11:17:18 +0100 | [diff] [blame] | 80 | 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 Fazekas | 0aababb | 2013-07-28 20:19:00 +0200 | [diff] [blame] | 88 | else: |
| 89 | self.run_core() |
| 90 | |
| 91 | def tearDown(self): |
| 92 | if self.class_setup_per != SetUpClassRunTime.action: |
| 93 | self.klass.tearDownClass() |