blob: 8359efd92bf6f9fa574c5459f15df5a7f03df601 [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):
Ken'ichi Ohmichi0afa8812015-11-19 08:58:54 +000052 """Returns a list of active compute nodes.
53
54 List is generated by running nova-manage on the controller.
David Kranzb9d97502013-05-01 15:55:04 -040055 """
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):
Ken'ichi Ohmichi0afa8812015-11-19 08:58:54 +000072 """Detect errors in nova log files on the controller and compute nodes."""
David Kranzb9d97502013-05-01 15:55:04 -040073 grep = 'egrep "ERROR|TRACE" %s' % logfiles
DennyZhang49b21ab2013-09-24 16:24:23 -050074 ret = False
David Kranzb9d97502013-05-01 15:55:04 -040075 for node in nodes:
Marc Kodererf13e4872013-11-25 14:50:33 +010076 errors = do_ssh(grep, node, ssh_user, ssh_key)
David Kranzb9d97502013-05-01 15:55:04 -040077 if len(errors) > 0:
Marc Kodererb714de52013-08-08 09:21:46 +020078 LOG.error('%s: %s' % (node, errors))
DennyZhang49b21ab2013-09-24 16:24:23 -050079 ret = True
80 if stop_on_error:
81 break
82 return ret
David Kranzb9d97502013-05-01 15:55:04 -040083
84
Attila Fazekasd047d1d2014-04-19 21:58:47 +020085def sigchld_handler(signalnum, frame):
Ken'ichi Ohmichi0afa8812015-11-19 08:58:54 +000086 """Signal handler (only active if stop_on_error is True)."""
Attila Fazekasd047d1d2014-04-19 21:58:47 +020087 for process in processes:
88 if (not process['process'].is_alive() and
89 process['process'].exitcode != 0):
90 signal.signal(signalnum, signal.SIG_DFL)
91 terminate_all_processes()
92 break
Marc Koderer3414d732013-07-31 08:36:36 +020093
94
Marc Kodererf13e4872013-11-25 14:50:33 +010095def terminate_all_processes(check_interval=20):
Ken'ichi Ohmichi0afa8812015-11-19 08:58:54 +000096 """Goes through the process list and terminates all child processes."""
Pavel Sedlák400c4132014-04-29 16:31:48 +020097 LOG.info("Stopping all processes.")
Marc Koderer3414d732013-07-31 08:36:36 +020098 for process in processes:
99 if process['process'].is_alive():
100 try:
101 process['process'].terminate()
102 except Exception:
103 pass
Marc Kodererf13e4872013-11-25 14:50:33 +0100104 time.sleep(check_interval)
Marc Koderer8f940ab2013-09-25 17:31:50 +0200105 for process in processes:
106 if process['process'].is_alive():
107 try:
108 pid = process['process'].pid
109 LOG.warn("Process %d hangs. Send SIGKILL." % pid)
110 os.kill(pid, signal.SIGKILL)
111 except Exception:
112 pass
Marc Koderer3414d732013-07-31 08:36:36 +0200113 process['process'].join()
114
115
116def stress_openstack(tests, duration, max_runs=None, stop_on_error=False):
Ken'ichi Ohmichi0afa8812015-11-19 08:58:54 +0000117 """Workload driver. Executes an action function against a nova-cluster."""
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +0100118 admin_manager = credentials.AdminManager()
Marc Kodererf13e4872013-11-25 14:50:33 +0100119
Matthew Treinish88f49ef2014-01-29 18:36:27 +0000120 ssh_user = CONF.stress.target_ssh_user
121 ssh_key = CONF.stress.target_private_key_path
122 logfiles = CONF.stress.target_logfiles
123 log_check_interval = int(CONF.stress.log_check_interval)
124 default_thread_num = int(CONF.stress.default_thread_number_per_action)
David Kranzb9d97502013-05-01 15:55:04 -0400125 if logfiles:
Matthew Treinish88f49ef2014-01-29 18:36:27 +0000126 controller = CONF.stress.target_controller
Marc Kodererf13e4872013-11-25 14:50:33 +0100127 computes = _get_compute_nodes(controller, ssh_user, ssh_key)
David Kranzb9d97502013-05-01 15:55:04 -0400128 for node in computes:
Marc Kodererf13e4872013-11-25 14:50:33 +0100129 do_ssh("rm -f %s" % logfiles, node, ssh_user, ssh_key)
David Kranz6c3fc152015-03-13 14:47:44 -0400130 skip = False
David Kranzb9d97502013-05-01 15:55:04 -0400131 for test in tests:
David Kranz6c3fc152015-03-13 14:47:44 -0400132 for service in test.get('required_services', []):
133 if not CONF.service_available.get(service):
134 skip = True
135 break
136 if skip:
137 break
David Kranzb9d97502013-05-01 15:55:04 -0400138 if test.get('use_admin', False):
139 manager = admin_manager
140 else:
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +0100141 manager = credentials.ConfiguredUserManager()
llg821243b20502014-02-22 10:32:49 +0800142 for p_number in moves.xrange(test.get('threads', default_thread_num)):
David Kranzb9d97502013-05-01 15:55:04 -0400143 if test.get('use_isolated_tenants', False):
Masayuki Igawa259c1132013-10-31 17:48:44 +0900144 username = data_utils.rand_name("stress_user")
145 tenant_name = data_utils.rand_name("stress_tenant")
David Kranzb9d97502013-05-01 15:55:04 -0400146 password = "pass"
Andrea Frittolif2f7a372015-03-04 15:07:39 +0000147 if CONF.identity.auth_version == 'v2':
148 identity_client = admin_manager.identity_client
149 else:
150 identity_client = admin_manager.identity_v3_client
Jamie Lennox15350172015-08-17 10:54:25 +1000151 credentials_client = cred_client.get_creds_client(
Andrea Frittolif2f7a372015-03-04 15:07:39 +0000152 identity_client)
153 project = credentials_client.create_project(
154 name=tenant_name, description=tenant_name)
155 user = credentials_client.create_user(username, password,
156 project['id'], "email")
157 # Add roles specified in config file
158 for conf_role in CONF.auth.tempest_roles:
159 credentials_client.assign_user_role(user, project,
160 conf_role)
161 creds = credentials_client.get_credentials(user, project,
162 password)
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000163 manager = clients.Manager(credentials=creds)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700164
Attila Fazekas1e30d5d2013-07-30 14:38:20 +0200165 test_obj = importutils.import_class(test['action'])
Marc Kodererb714de52013-08-08 09:21:46 +0200166 test_run = test_obj(manager, max_runs, stop_on_error)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700167
168 kwargs = test.get('kwargs', {})
Matthew Treinish71426682015-04-23 11:19:38 -0400169 test_run.setUp(**dict(six.iteritems(kwargs)))
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700170
Marc Kodererb714de52013-08-08 09:21:46 +0200171 LOG.debug("calling Target Object %s" %
172 test_run.__class__.__name__)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700173
Marc Koderer69d3bea2013-07-18 08:32:11 +0200174 mp_manager = multiprocessing.Manager()
175 shared_statistic = mp_manager.dict()
176 shared_statistic['runs'] = 0
177 shared_statistic['fails'] = 0
178
179 p = multiprocessing.Process(target=test_run.execute,
180 args=(shared_statistic,))
181
182 process = {'process': p,
183 'p_number': p_number,
Marc Koderer33ca6ee2013-08-29 09:06:36 +0200184 'action': test_run.action,
Marc Koderer69d3bea2013-07-18 08:32:11 +0200185 'statistic': shared_statistic}
186
187 processes.append(process)
David Kranzb9d97502013-05-01 15:55:04 -0400188 p.start()
Marc Koderer3414d732013-07-31 08:36:36 +0200189 if stop_on_error:
190 # NOTE(mkoderer): only the parent should register the handler
191 signal.signal(signal.SIGCHLD, sigchld_handler)
David Kranzb9d97502013-05-01 15:55:04 -0400192 end_time = time.time() + duration
193 had_errors = False
Pavel Sedlák400c4132014-04-29 16:31:48 +0200194 try:
195 while True:
196 if max_runs is None:
197 remaining = end_time - time.time()
198 if remaining <= 0:
Marc Koderer69d3bea2013-07-18 08:32:11 +0200199 break
Pavel Sedlák400c4132014-04-29 16:31:48 +0200200 else:
201 remaining = log_check_interval
202 all_proc_term = True
203 for process in processes:
204 if process['process'].is_alive():
205 all_proc_term = False
206 break
207 if all_proc_term:
Marc Koderer3414d732013-07-31 08:36:36 +0200208 break
209
Pavel Sedlák400c4132014-04-29 16:31:48 +0200210 time.sleep(min(remaining, log_check_interval))
211 if stop_on_error:
Pavel Sedlákfa6666c2014-04-29 16:56:48 +0200212 if any([True for proc in processes
213 if proc['statistic']['fails'] > 0]):
214 break
Pavel Sedlák400c4132014-04-29 16:31:48 +0200215
216 if not logfiles:
217 continue
218 if _has_error_in_logs(logfiles, computes, ssh_user, ssh_key,
219 stop_on_error):
220 had_errors = True
221 break
222 except KeyboardInterrupt:
223 LOG.warning("Interrupted, going to print statistics and exit ...")
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700224
Attila Fazekasd047d1d2014-04-19 21:58:47 +0200225 if stop_on_error:
226 signal.signal(signal.SIGCHLD, signal.SIG_DFL)
Marc Koderer3414d732013-07-31 08:36:36 +0200227 terminate_all_processes()
Marc Koderer69d3bea2013-07-18 08:32:11 +0200228
229 sum_fails = 0
230 sum_runs = 0
231
Marc Kodererb714de52013-08-08 09:21:46 +0200232 LOG.info("Statistics (per process):")
Marc Koderer69d3bea2013-07-18 08:32:11 +0200233 for process in processes:
234 if process['statistic']['fails'] > 0:
235 had_errors = True
236 sum_runs += process['statistic']['runs']
237 sum_fails += process['statistic']['fails']
Marc Kodererb714de52013-08-08 09:21:46 +0200238 LOG.info(" Process %d (%s): Run %d actions (%d failed)" %
239 (process['p_number'],
240 process['action'],
241 process['statistic']['runs'],
Marc Koderer69d3bea2013-07-18 08:32:11 +0200242 process['statistic']['fails']))
Marc Kodererb714de52013-08-08 09:21:46 +0200243 LOG.info("Summary:")
244 LOG.info("Run %d actions (%d failed)" %
245 (sum_runs, sum_fails))
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700246
Julien Leloupa5ee5422014-02-13 14:29:02 +0100247 if not had_errors and CONF.stress.full_clean_stack:
Marc Kodererb714de52013-08-08 09:21:46 +0200248 LOG.info("cleaning up")
249 cleanup.cleanup()
Marc Koderer888ddc42013-07-23 16:13:07 +0200250 if had_errors:
251 return 1
252 else:
253 return 0