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 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 21 | from oslo_log import log as logging |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 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): |
Ken'ichi Ohmichi | 0afa881 | 2015-11-19 08:58:54 +0000 | [diff] [blame] | 43 | """This methods returns the action. |
| 44 | |
| 45 | Overload this if you create a stress test wrapper. |
Marc Koderer | 33ca6ee | 2013-08-29 09:06:36 +0200 | [diff] [blame] | 46 | """ |
| 47 | return self.__class__.__name__ |
| 48 | |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 49 | def setUp(self, **kwargs): |
Ken'ichi Ohmichi | 0afa881 | 2015-11-19 08:58:54 +0000 | [diff] [blame] | 50 | """Initialize test structures/resources |
| 51 | |
| 52 | This method is called before "run" method to help the test |
| 53 | initialize any structures. kwargs contains arguments passed |
| 54 | in from the configuration json file. |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 55 | |
| 56 | setUp doesn't count against the time duration. |
| 57 | """ |
| 58 | self.logger.debug("setUp") |
| 59 | |
| 60 | def tearDown(self): |
Ken'ichi Ohmichi | 0afa881 | 2015-11-19 08:58:54 +0000 | [diff] [blame] | 61 | """Cleanup test structures/resources |
| 62 | |
| 63 | This method is called to do any cleanup after the test is complete. |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 64 | """ |
| 65 | self.logger.debug("tearDown") |
| 66 | |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 67 | def execute(self, shared_statistic): |
Ken'ichi Ohmichi | 0afa881 | 2015-11-19 08:58:54 +0000 | [diff] [blame] | 68 | """This is the main execution entry point called by the driver. |
| 69 | |
| 70 | We register a signal handler to allow us to tearDown gracefully, |
| 71 | and then exit. We also keep track of how many runs we do. |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 72 | """ |
| 73 | signal.signal(signal.SIGHUP, self._shutdown_handler) |
| 74 | signal.signal(signal.SIGTERM, self._shutdown_handler) |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 75 | |
| 76 | while self.max_runs is None or (shared_statistic['runs'] < |
| 77 | self.max_runs): |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 78 | self.logger.debug("Trigger new run (run %d)" % |
| 79 | shared_statistic['runs']) |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 80 | try: |
| 81 | self.run() |
| 82 | except Exception: |
| 83 | shared_statistic['fails'] += 1 |
| 84 | self.logger.exception("Failure in run") |
| 85 | finally: |
| 86 | shared_statistic['runs'] += 1 |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 87 | if self.stop_on_error and (shared_statistic['fails'] > 1): |
BinBin Cong | c7f7feb | 2016-02-17 11:22:46 +0000 | [diff] [blame] | 88 | self.logger.warning("Stop process due to" |
| 89 | "\"stop-on-error\" argument") |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 90 | self.tearDown() |
| 91 | sys.exit(1) |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 92 | |
Danny Al-Gaaf | fd247f5 | 2014-08-13 11:25:24 +0200 | [diff] [blame] | 93 | @abc.abstractmethod |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 94 | def run(self): |
| 95 | """This method is where the stress test code runs.""" |
Danny Al-Gaaf | fd247f5 | 2014-08-13 11:25:24 +0200 | [diff] [blame] | 96 | return |