blob: 286e02294a3f6c9abbdb2bbfee5f7ec8427384a3 [file] [log] [blame]
Walter A. Boring IVb725e622013-07-11 17:21:33 -07001# (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-Gaaffd247f52014-08-13 11:25:24 +020015import abc
Walter A. Boring IVb725e622013-07-11 17:21:33 -070016import signal
17import sys
18
Danny Al-Gaaffd247f52014-08-13 11:25:24 +020019import six
20
Marc Kodererb714de52013-08-08 09:21:46 +020021from tempest.openstack.common import log as logging
22
Walter A. Boring IVb725e622013-07-11 17:21:33 -070023
Danny Al-Gaaffd247f52014-08-13 11:25:24 +020024@six.add_metaclass(abc.ABCMeta)
Walter A. Boring IVb725e622013-07-11 17:21:33 -070025class StressAction(object):
26
Marc Kodererb714de52013-08-08 09:21:46 +020027 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 IVb725e622013-07-11 17:21:33 -070030 self.manager = manager
Marc Koderer69d3bea2013-07-18 08:32:11 +020031 self.max_runs = max_runs
Marc Koderer3414d732013-07-31 08:36:36 +020032 self.stop_on_error = stop_on_error
Walter A. Boring IVb725e622013-07-11 17:21:33 -070033
34 def _shutdown_handler(self, signal, frame):
Marc Koderer8f940ab2013-09-25 17:31:50 +020035 try:
36 self.tearDown()
37 except Exception:
38 self.logger.exception("Error while tearDown")
Walter A. Boring IVb725e622013-07-11 17:21:33 -070039 sys.exit(0)
40
Marc Koderer33ca6ee2013-08-29 09:06:36 +020041 @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 IVb725e622013-07-11 17:21:33 -070048 def setUp(self, **kwargs):
49 """This method is called before the run method
Chang Bo Guocc1623c2013-09-13 20:11:27 -070050 to help the test initialize any structures.
Walter A. Boring IVb725e622013-07-11 17:21:33 -070051 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 Koderer69d3bea2013-07-18 08:32:11 +020064 def execute(self, shared_statistic):
Walter A. Boring IVb725e622013-07-11 17:21:33 -070065 """This is the main execution entry point called
66 by the driver. We register a signal handler to
Chang Bo Guocc1623c2013-09-13 20:11:27 -070067 allow us to tearDown gracefully, and then exit.
Walter A. Boring IVb725e622013-07-11 17:21:33 -070068 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 Koderer69d3bea2013-07-18 08:32:11 +020072
73 while self.max_runs is None or (shared_statistic['runs'] <
74 self.max_runs):
Marc Koderer32221b8e2013-08-23 13:57:50 +020075 self.logger.debug("Trigger new run (run %d)" %
76 shared_statistic['runs'])
Marc Koderer69d3bea2013-07-18 08:32:11 +020077 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 Koderer3414d732013-07-31 08:36:36 +020084 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 IVb725e622013-07-11 17:21:33 -070089
Danny Al-Gaaffd247f52014-08-13 11:25:24 +020090 @abc.abstractmethod
Walter A. Boring IVb725e622013-07-11 17:21:33 -070091 def run(self):
92 """This method is where the stress test code runs."""
Danny Al-Gaaffd247f52014-08-13 11:25:24 +020093 return