blob: 45a628d596738102004ff32dd4125b7d07a07a66 [file] [log] [blame]
Walter A. Boring IVb725e622013-07-11 17:21:33 -07001# 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
17import signal
18import sys
19
Marc Kodererb714de52013-08-08 09:21:46 +020020from tempest.openstack.common import log as logging
21
Walter A. Boring IVb725e622013-07-11 17:21:33 -070022
23class StressAction(object):
24
Marc Kodererb714de52013-08-08 09:21:46 +020025 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 IVb725e622013-07-11 17:21:33 -070028 self.manager = manager
Marc Koderer69d3bea2013-07-18 08:32:11 +020029 self.max_runs = max_runs
Marc Koderer3414d732013-07-31 08:36:36 +020030 self.stop_on_error = stop_on_error
Walter A. Boring IVb725e622013-07-11 17:21:33 -070031
32 def _shutdown_handler(self, signal, frame):
33 self.tearDown()
34 sys.exit(0)
35
Marc Koderer33ca6ee2013-08-29 09:06:36 +020036 @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 IVb725e622013-07-11 17:21:33 -070043 def setUp(self, **kwargs):
44 """This method is called before the run method
Chang Bo Guocc1623c2013-09-13 20:11:27 -070045 to help the test initialize any structures.
Walter A. Boring IVb725e622013-07-11 17:21:33 -070046 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 Koderer69d3bea2013-07-18 08:32:11 +020059 def execute(self, shared_statistic):
Walter A. Boring IVb725e622013-07-11 17:21:33 -070060 """This is the main execution entry point called
61 by the driver. We register a signal handler to
Chang Bo Guocc1623c2013-09-13 20:11:27 -070062 allow us to tearDown gracefully, and then exit.
Walter A. Boring IVb725e622013-07-11 17:21:33 -070063 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 Koderer69d3bea2013-07-18 08:32:11 +020067
68 while self.max_runs is None or (shared_statistic['runs'] <
69 self.max_runs):
Marc Koderer32221b8e2013-08-23 13:57:50 +020070 self.logger.debug("Trigger new run (run %d)" %
71 shared_statistic['runs'])
Marc Koderer69d3bea2013-07-18 08:32:11 +020072 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 Koderer3414d732013-07-31 08:36:36 +020079 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 IVb725e622013-07-11 17:21:33 -070084
85 def run(self):
86 """This method is where the stress test code runs."""
87 raise NotImplemented()