blob: cf0a08a463e4afd70e068b8cd56eca7ca74c8547 [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
Doug Hellmann583ce2c2015-03-11 14:55:46 +000021from oslo_log import log as logging
Marc Kodererb714de52013-08-08 09:21:46 +020022
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):
Ken'ichi Ohmichi0afa8812015-11-19 08:58:54 +000043 """This methods returns the action.
44
45 Overload this if you create a stress test wrapper.
Marc Koderer33ca6ee2013-08-29 09:06:36 +020046 """
47 return self.__class__.__name__
48
Walter A. Boring IVb725e622013-07-11 17:21:33 -070049 def setUp(self, **kwargs):
Ken'ichi Ohmichi0afa8812015-11-19 08:58:54 +000050 """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 IVb725e622013-07-11 17:21:33 -070055
56 setUp doesn't count against the time duration.
57 """
58 self.logger.debug("setUp")
59
60 def tearDown(self):
Ken'ichi Ohmichi0afa8812015-11-19 08:58:54 +000061 """Cleanup test structures/resources
62
63 This method is called to do any cleanup after the test is complete.
Walter A. Boring IVb725e622013-07-11 17:21:33 -070064 """
65 self.logger.debug("tearDown")
66
Marc Koderer69d3bea2013-07-18 08:32:11 +020067 def execute(self, shared_statistic):
Ken'ichi Ohmichi0afa8812015-11-19 08:58:54 +000068 """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 IVb725e622013-07-11 17:21:33 -070072 """
73 signal.signal(signal.SIGHUP, self._shutdown_handler)
74 signal.signal(signal.SIGTERM, self._shutdown_handler)
Marc Koderer69d3bea2013-07-18 08:32:11 +020075
76 while self.max_runs is None or (shared_statistic['runs'] <
77 self.max_runs):
Marc Koderer32221b8e2013-08-23 13:57:50 +020078 self.logger.debug("Trigger new run (run %d)" %
79 shared_statistic['runs'])
Marc Koderer69d3bea2013-07-18 08:32:11 +020080 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 Koderer3414d732013-07-31 08:36:36 +020087 if self.stop_on_error and (shared_statistic['fails'] > 1):
BinBin Congc7f7feb2016-02-17 11:22:46 +000088 self.logger.warning("Stop process due to"
89 "\"stop-on-error\" argument")
Marc Koderer3414d732013-07-31 08:36:36 +020090 self.tearDown()
91 sys.exit(1)
Walter A. Boring IVb725e622013-07-11 17:21:33 -070092
Danny Al-Gaaffd247f52014-08-13 11:25:24 +020093 @abc.abstractmethod
Walter A. Boring IVb725e622013-07-11 17:21:33 -070094 def run(self):
95 """This method is where the stress test code runs."""
Danny Al-Gaaffd247f52014-08-13 11:25:24 +020096 return