blob: eec52cb9c4ac50c39fe447edabdcf150c914d540 [file] [log] [blame]
David Kranzb9d97502013-05-01 15:55:04 -04001# Copyright 2013 Quanta Research Cambridge, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain 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,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
David Kranzb9d97502013-05-01 15:55:04 -040015import multiprocessing
Marc Koderer8f940ab2013-09-25 17:31:50 +020016import os
Marc Koderer3414d732013-07-31 08:36:36 +020017import signal
David Kranzb9d97502013-05-01 15:55:04 -040018import time
19
Doug Hellmann583ce2c2015-03-11 14:55:46 +000020from oslo_log import log as logging
21from oslo_utils import importutils
Matthew Treinish71426682015-04-23 11:19:38 -040022import six
llg821243b20502014-02-22 10:32:49 +080023from six import moves
Andrey Pavlov64723762015-04-29 06:24:58 +030024from tempest_lib.common import ssh
Fei Long Wangd39431f2015-05-14 11:30:48 +120025
llg821243b20502014-02-22 10:32:49 +080026
David Kranzb9d97502013-05-01 15:55:04 -040027from tempest import clients
Jamie Lennox15350172015-08-17 10:54:25 +100028from tempest.common import cred_client
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010029from tempest.common import credentials_factory as credentials
Fei Long Wangd39431f2015-05-14 11:30:48 +120030from tempest.common.utils import data_utils
Matthew Treinish88f49ef2014-01-29 18:36:27 +000031from tempest import config
David Kranzb9d97502013-05-01 15:55:04 -040032from tempest import exceptions
33from tempest.stress import cleanup
34
Matthew Treinish88f49ef2014-01-29 18:36:27 +000035CONF = config.CONF
36
Marc Kodererb714de52013-08-08 09:21:46 +020037LOG = logging.getLogger(__name__)
Marc Koderer3414d732013-07-31 08:36:36 +020038processes = []
David Kranzb9d97502013-05-01 15:55:04 -040039
40
Marc Kodererf13e4872013-11-25 14:50:33 +010041def do_ssh(command, host, ssh_user, ssh_key=None):
42 ssh_client = ssh.Client(host, ssh_user, key_filename=ssh_key)
David Kranzb9d97502013-05-01 15:55:04 -040043 try:
44 return ssh_client.exec_command(command)
45 except exceptions.SSHExecCommandFailed:
DennyZhang6baa6672013-09-24 17:49:30 -070046 LOG.error('do_ssh raise exception. command:%s, host:%s.'
47 % (command, host))
David Kranzb9d97502013-05-01 15:55:04 -040048 return None
49
50
Marc Kodererf13e4872013-11-25 14:50:33 +010051def _get_compute_nodes(controller, ssh_user, ssh_key=None):
David Kranzb9d97502013-05-01 15:55:04 -040052 """
53 Returns a list of active compute nodes. List is generated by running
54 nova-manage on the controller.
55 """
56 nodes = []
57 cmd = "nova-manage service list | grep ^nova-compute"
Marc Kodererf13e4872013-11-25 14:50:33 +010058 output = do_ssh(cmd, controller, ssh_user, ssh_key)
David Kranzb9d97502013-05-01 15:55:04 -040059 if not output:
60 return nodes
61 # For example: nova-compute xg11eth0 nova enabled :-) 2011-10-31 18:57:46
62 # This is fragile but there is, at present, no other way to get this info.
63 for line in output.split('\n'):
64 words = line.split()
65 if len(words) > 0 and words[4] == ":-)":
66 nodes.append(words[1])
67 return nodes
68
69
Marc Kodererf13e4872013-11-25 14:50:33 +010070def _has_error_in_logs(logfiles, nodes, ssh_user, ssh_key=None,
71 stop_on_error=False):
David Kranzb9d97502013-05-01 15:55:04 -040072 """
73 Detect errors in the nova log files on the controller and compute nodes.
74 """
75 grep = 'egrep "ERROR|TRACE" %s' % logfiles
DennyZhang49b21ab2013-09-24 16:24:23 -050076 ret = False
David Kranzb9d97502013-05-01 15:55:04 -040077 for node in nodes:
Marc Kodererf13e4872013-11-25 14:50:33 +010078 errors = do_ssh(grep, node, ssh_user, ssh_key)
David Kranzb9d97502013-05-01 15:55:04 -040079 if len(errors) > 0:
Marc Kodererb714de52013-08-08 09:21:46 +020080 LOG.error('%s: %s' % (node, errors))
DennyZhang49b21ab2013-09-24 16:24:23 -050081 ret = True
82 if stop_on_error:
83 break
84 return ret
David Kranzb9d97502013-05-01 15:55:04 -040085
86
Attila Fazekasd047d1d2014-04-19 21:58:47 +020087def sigchld_handler(signalnum, frame):
Marc Koderer3414d732013-07-31 08:36:36 +020088 """
89 Signal handler (only active if stop_on_error is True).
90 """
Attila Fazekasd047d1d2014-04-19 21:58:47 +020091 for process in processes:
92 if (not process['process'].is_alive() and
93 process['process'].exitcode != 0):
94 signal.signal(signalnum, signal.SIG_DFL)
95 terminate_all_processes()
96 break
Marc Koderer3414d732013-07-31 08:36:36 +020097
98
Marc Kodererf13e4872013-11-25 14:50:33 +010099def terminate_all_processes(check_interval=20):
Marc Koderer3414d732013-07-31 08:36:36 +0200100 """
101 Goes through the process list and terminates all child processes.
102 """
Pavel Sedlák400c4132014-04-29 16:31:48 +0200103 LOG.info("Stopping all processes.")
Marc Koderer3414d732013-07-31 08:36:36 +0200104 for process in processes:
105 if process['process'].is_alive():
106 try:
107 process['process'].terminate()
108 except Exception:
109 pass
Marc Kodererf13e4872013-11-25 14:50:33 +0100110 time.sleep(check_interval)
Marc Koderer8f940ab2013-09-25 17:31:50 +0200111 for process in processes:
112 if process['process'].is_alive():
113 try:
114 pid = process['process'].pid
115 LOG.warn("Process %d hangs. Send SIGKILL." % pid)
116 os.kill(pid, signal.SIGKILL)
117 except Exception:
118 pass
Marc Koderer3414d732013-07-31 08:36:36 +0200119 process['process'].join()
120
121
122def stress_openstack(tests, duration, max_runs=None, stop_on_error=False):
David Kranzb9d97502013-05-01 15:55:04 -0400123 """
124 Workload driver. Executes an action function against a nova-cluster.
David Kranzb9d97502013-05-01 15:55:04 -0400125 """
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +0100126 admin_manager = credentials.AdminManager()
Marc Kodererf13e4872013-11-25 14:50:33 +0100127
Matthew Treinish88f49ef2014-01-29 18:36:27 +0000128 ssh_user = CONF.stress.target_ssh_user
129 ssh_key = CONF.stress.target_private_key_path
130 logfiles = CONF.stress.target_logfiles
131 log_check_interval = int(CONF.stress.log_check_interval)
132 default_thread_num = int(CONF.stress.default_thread_number_per_action)
David Kranzb9d97502013-05-01 15:55:04 -0400133 if logfiles:
Matthew Treinish88f49ef2014-01-29 18:36:27 +0000134 controller = CONF.stress.target_controller
Marc Kodererf13e4872013-11-25 14:50:33 +0100135 computes = _get_compute_nodes(controller, ssh_user, ssh_key)
David Kranzb9d97502013-05-01 15:55:04 -0400136 for node in computes:
Marc Kodererf13e4872013-11-25 14:50:33 +0100137 do_ssh("rm -f %s" % logfiles, node, ssh_user, ssh_key)
David Kranz6c3fc152015-03-13 14:47:44 -0400138 skip = False
David Kranzb9d97502013-05-01 15:55:04 -0400139 for test in tests:
David Kranz6c3fc152015-03-13 14:47:44 -0400140 for service in test.get('required_services', []):
141 if not CONF.service_available.get(service):
142 skip = True
143 break
144 if skip:
145 break
David Kranzb9d97502013-05-01 15:55:04 -0400146 if test.get('use_admin', False):
147 manager = admin_manager
148 else:
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +0100149 manager = credentials.ConfiguredUserManager()
llg821243b20502014-02-22 10:32:49 +0800150 for p_number in moves.xrange(test.get('threads', default_thread_num)):
David Kranzb9d97502013-05-01 15:55:04 -0400151 if test.get('use_isolated_tenants', False):
Masayuki Igawa259c1132013-10-31 17:48:44 +0900152 username = data_utils.rand_name("stress_user")
153 tenant_name = data_utils.rand_name("stress_tenant")
David Kranzb9d97502013-05-01 15:55:04 -0400154 password = "pass"
Andrea Frittolif2f7a372015-03-04 15:07:39 +0000155 if CONF.identity.auth_version == 'v2':
156 identity_client = admin_manager.identity_client
157 else:
158 identity_client = admin_manager.identity_v3_client
Jamie Lennox15350172015-08-17 10:54:25 +1000159 credentials_client = cred_client.get_creds_client(
Andrea Frittolif2f7a372015-03-04 15:07:39 +0000160 identity_client)
161 project = credentials_client.create_project(
162 name=tenant_name, description=tenant_name)
163 user = credentials_client.create_user(username, password,
164 project['id'], "email")
165 # Add roles specified in config file
166 for conf_role in CONF.auth.tempest_roles:
167 credentials_client.assign_user_role(user, project,
168 conf_role)
169 creds = credentials_client.get_credentials(user, project,
170 password)
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000171 manager = clients.Manager(credentials=creds)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700172
Attila Fazekas1e30d5d2013-07-30 14:38:20 +0200173 test_obj = importutils.import_class(test['action'])
Marc Kodererb714de52013-08-08 09:21:46 +0200174 test_run = test_obj(manager, max_runs, stop_on_error)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700175
176 kwargs = test.get('kwargs', {})
Matthew Treinish71426682015-04-23 11:19:38 -0400177 test_run.setUp(**dict(six.iteritems(kwargs)))
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700178
Marc Kodererb714de52013-08-08 09:21:46 +0200179 LOG.debug("calling Target Object %s" %
180 test_run.__class__.__name__)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700181
Marc Koderer69d3bea2013-07-18 08:32:11 +0200182 mp_manager = multiprocessing.Manager()
183 shared_statistic = mp_manager.dict()
184 shared_statistic['runs'] = 0
185 shared_statistic['fails'] = 0
186
187 p = multiprocessing.Process(target=test_run.execute,
188 args=(shared_statistic,))
189
190 process = {'process': p,
191 'p_number': p_number,
Marc Koderer33ca6ee2013-08-29 09:06:36 +0200192 'action': test_run.action,
Marc Koderer69d3bea2013-07-18 08:32:11 +0200193 'statistic': shared_statistic}
194
195 processes.append(process)
David Kranzb9d97502013-05-01 15:55:04 -0400196 p.start()
Marc Koderer3414d732013-07-31 08:36:36 +0200197 if stop_on_error:
198 # NOTE(mkoderer): only the parent should register the handler
199 signal.signal(signal.SIGCHLD, sigchld_handler)
David Kranzb9d97502013-05-01 15:55:04 -0400200 end_time = time.time() + duration
201 had_errors = False
Pavel Sedlák400c4132014-04-29 16:31:48 +0200202 try:
203 while True:
204 if max_runs is None:
205 remaining = end_time - time.time()
206 if remaining <= 0:
Marc Koderer69d3bea2013-07-18 08:32:11 +0200207 break
Pavel Sedlák400c4132014-04-29 16:31:48 +0200208 else:
209 remaining = log_check_interval
210 all_proc_term = True
211 for process in processes:
212 if process['process'].is_alive():
213 all_proc_term = False
214 break
215 if all_proc_term:
Marc Koderer3414d732013-07-31 08:36:36 +0200216 break
217
Pavel Sedlák400c4132014-04-29 16:31:48 +0200218 time.sleep(min(remaining, log_check_interval))
219 if stop_on_error:
Pavel Sedlákfa6666c2014-04-29 16:56:48 +0200220 if any([True for proc in processes
221 if proc['statistic']['fails'] > 0]):
222 break
Pavel Sedlák400c4132014-04-29 16:31:48 +0200223
224 if not logfiles:
225 continue
226 if _has_error_in_logs(logfiles, computes, ssh_user, ssh_key,
227 stop_on_error):
228 had_errors = True
229 break
230 except KeyboardInterrupt:
231 LOG.warning("Interrupted, going to print statistics and exit ...")
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700232
Attila Fazekasd047d1d2014-04-19 21:58:47 +0200233 if stop_on_error:
234 signal.signal(signal.SIGCHLD, signal.SIG_DFL)
Marc Koderer3414d732013-07-31 08:36:36 +0200235 terminate_all_processes()
Marc Koderer69d3bea2013-07-18 08:32:11 +0200236
237 sum_fails = 0
238 sum_runs = 0
239
Marc Kodererb714de52013-08-08 09:21:46 +0200240 LOG.info("Statistics (per process):")
Marc Koderer69d3bea2013-07-18 08:32:11 +0200241 for process in processes:
242 if process['statistic']['fails'] > 0:
243 had_errors = True
244 sum_runs += process['statistic']['runs']
245 sum_fails += process['statistic']['fails']
Marc Kodererb714de52013-08-08 09:21:46 +0200246 LOG.info(" Process %d (%s): Run %d actions (%d failed)" %
247 (process['p_number'],
248 process['action'],
249 process['statistic']['runs'],
Marc Koderer69d3bea2013-07-18 08:32:11 +0200250 process['statistic']['fails']))
Marc Kodererb714de52013-08-08 09:21:46 +0200251 LOG.info("Summary:")
252 LOG.info("Run %d actions (%d failed)" %
253 (sum_runs, sum_fails))
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700254
Julien Leloupa5ee5422014-02-13 14:29:02 +0100255 if not had_errors and CONF.stress.full_clean_stack:
Marc Kodererb714de52013-08-08 09:21:46 +0200256 LOG.info("cleaning up")
257 cleanup.cleanup()
Marc Koderer888ddc42013-07-23 16:13:07 +0200258 if had_errors:
259 return 1
260 else:
261 return 0