Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 1 | # (c) Copyright 2013 Hewlett-Packard Development Company, L.P. |
| 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 | |
Danny Al-Gaaf | fd247f5 | 2014-08-13 11:25:24 +0200 | [diff] [blame] | 15 | import abc |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 16 | import signal |
| 17 | import sys |
| 18 | |
Danny Al-Gaaf | fd247f5 | 2014-08-13 11:25:24 +0200 | [diff] [blame] | 19 | import six |
| 20 | |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 21 | from tempest.openstack.common import log as logging |
| 22 | |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 23 | |
Danny Al-Gaaf | fd247f5 | 2014-08-13 11:25:24 +0200 | [diff] [blame] | 24 | @six.add_metaclass(abc.ABCMeta) |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 25 | class StressAction(object): |
| 26 | |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 27 | def __init__(self, manager, max_runs=None, stop_on_error=False): |
| 28 | full_cname = self.__module__ + "." + self.__class__.__name__ |
| 29 | self.logger = logging.getLogger(full_cname) |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 30 | self.manager = manager |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 31 | self.max_runs = max_runs |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 32 | self.stop_on_error = stop_on_error |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 33 | |
| 34 | def _shutdown_handler(self, signal, frame): |
Marc Koderer | 8f940ab | 2013-09-25 17:31:50 +0200 | [diff] [blame] | 35 | try: |
| 36 | self.tearDown() |
| 37 | except Exception: |
| 38 | self.logger.exception("Error while tearDown") |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 39 | sys.exit(0) |
| 40 | |
Marc Koderer | 33ca6ee | 2013-08-29 09:06:36 +0200 | [diff] [blame] | 41 | @property |
| 42 | def action(self): |
| 43 | """This methods returns the action. Overload this if you |
| 44 | create a stress test wrapper. |
| 45 | """ |
| 46 | return self.__class__.__name__ |
| 47 | |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 48 | def setUp(self, **kwargs): |
| 49 | """This method is called before the run method |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 50 | to help the test initialize any structures. |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 51 | kwargs contains arguments passed in from the |
| 52 | configuration json file. |
| 53 | |
| 54 | setUp doesn't count against the time duration. |
| 55 | """ |
| 56 | self.logger.debug("setUp") |
| 57 | |
| 58 | def tearDown(self): |
| 59 | """This method is called to do any cleanup |
| 60 | after the test is complete. |
| 61 | """ |
| 62 | self.logger.debug("tearDown") |
| 63 | |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 64 | def execute(self, shared_statistic): |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 65 | """This is the main execution entry point called |
| 66 | by the driver. We register a signal handler to |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 67 | allow us to tearDown gracefully, and then exit. |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 68 | We also keep track of how many runs we do. |
| 69 | """ |
| 70 | signal.signal(signal.SIGHUP, self._shutdown_handler) |
| 71 | signal.signal(signal.SIGTERM, self._shutdown_handler) |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 72 | |
| 73 | while self.max_runs is None or (shared_statistic['runs'] < |
| 74 | self.max_runs): |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 75 | self.logger.debug("Trigger new run (run %d)" % |
| 76 | shared_statistic['runs']) |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 77 | try: |
| 78 | self.run() |
| 79 | except Exception: |
| 80 | shared_statistic['fails'] += 1 |
| 81 | self.logger.exception("Failure in run") |
| 82 | finally: |
| 83 | shared_statistic['runs'] += 1 |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 84 | if self.stop_on_error and (shared_statistic['fails'] > 1): |
| 85 | self.logger.warn("Stop process due to" |
| 86 | "\"stop-on-error\" argument") |
| 87 | self.tearDown() |
| 88 | sys.exit(1) |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 89 | |
Danny Al-Gaaf | fd247f5 | 2014-08-13 11:25:24 +0200 | [diff] [blame] | 90 | @abc.abstractmethod |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 91 | def run(self): |
| 92 | """This method is where the stress test code runs.""" |
Danny Al-Gaaf | fd247f5 | 2014-08-13 11:25:24 +0200 | [diff] [blame] | 93 | return |