Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # (c) Copyright 2013 Hewlett-Packard Development Company, L.P. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | import signal |
| 18 | import sys |
| 19 | |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 20 | from tempest.openstack.common import log as logging |
| 21 | |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 22 | |
| 23 | class StressAction(object): |
| 24 | |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 25 | def __init__(self, manager, max_runs=None, stop_on_error=False): |
| 26 | full_cname = self.__module__ + "." + self.__class__.__name__ |
| 27 | self.logger = logging.getLogger(full_cname) |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 28 | self.manager = manager |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 29 | self.max_runs = max_runs |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 30 | self.stop_on_error = stop_on_error |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 31 | |
| 32 | def _shutdown_handler(self, signal, frame): |
| 33 | self.tearDown() |
| 34 | sys.exit(0) |
| 35 | |
Marc Koderer | 33ca6ee | 2013-08-29 09:06:36 +0200 | [diff] [blame] | 36 | @property |
| 37 | def action(self): |
| 38 | """This methods returns the action. Overload this if you |
| 39 | create a stress test wrapper. |
| 40 | """ |
| 41 | return self.__class__.__name__ |
| 42 | |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 43 | def setUp(self, **kwargs): |
| 44 | """This method is called before the run method |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 45 | to help the test initialize any structures. |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 46 | kwargs contains arguments passed in from the |
| 47 | configuration json file. |
| 48 | |
| 49 | setUp doesn't count against the time duration. |
| 50 | """ |
| 51 | self.logger.debug("setUp") |
| 52 | |
| 53 | def tearDown(self): |
| 54 | """This method is called to do any cleanup |
| 55 | after the test is complete. |
| 56 | """ |
| 57 | self.logger.debug("tearDown") |
| 58 | |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 59 | def execute(self, shared_statistic): |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 60 | """This is the main execution entry point called |
| 61 | by the driver. We register a signal handler to |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 62 | allow us to tearDown gracefully, and then exit. |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 63 | We also keep track of how many runs we do. |
| 64 | """ |
| 65 | signal.signal(signal.SIGHUP, self._shutdown_handler) |
| 66 | signal.signal(signal.SIGTERM, self._shutdown_handler) |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 67 | |
| 68 | while self.max_runs is None or (shared_statistic['runs'] < |
| 69 | self.max_runs): |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 70 | self.logger.debug("Trigger new run (run %d)" % |
| 71 | shared_statistic['runs']) |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 72 | try: |
| 73 | self.run() |
| 74 | except Exception: |
| 75 | shared_statistic['fails'] += 1 |
| 76 | self.logger.exception("Failure in run") |
| 77 | finally: |
| 78 | shared_statistic['runs'] += 1 |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 79 | if self.stop_on_error and (shared_statistic['fails'] > 1): |
| 80 | self.logger.warn("Stop process due to" |
| 81 | "\"stop-on-error\" argument") |
| 82 | self.tearDown() |
| 83 | sys.exit(1) |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 84 | |
| 85 | def run(self): |
| 86 | """This method is where the stress test code runs.""" |
| 87 | raise NotImplemented() |