blob: 2beaaa9d140c77f70cd9f1f5b6d23cbc67ada637 [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
Fei Long Wangd39431f2015-05-14 11:30:48 +120024
llg821243b20502014-02-22 10:32:49 +080025
David Kranzb9d97502013-05-01 15:55:04 -040026from tempest import clients
Jamie Lennox15350172015-08-17 10:54:25 +100027from tempest.common import cred_client
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010028from tempest.common import credentials_factory as credentials
Fei Long Wangd39431f2015-05-14 11:30:48 +120029from tempest.common.utils import data_utils
Matthew Treinish88f49ef2014-01-29 18:36:27 +000030from tempest import config
David Kranzb9d97502013-05-01 15:55:04 -040031from tempest import exceptions
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050032from tempest.lib.common import ssh
David Kranzb9d97502013-05-01 15:55:04 -040033from 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
zhangguoqing6c096642016-01-04 06:17:21 +0000109 LOG.warning("Process %d hangs. Send SIGKILL." % pid)
Marc Koderer8f940ab2013-09-25 17:31:50 +0200110 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
Andrea Frittoli (andreaf)bc0a7a62016-05-26 19:31:49 +0100138 # TODO(andreaf) This has to be reworked to use the credential
139 # provider interface. For now only tests marked as 'use_admin' will
140 # work.
David Kranzb9d97502013-05-01 15:55:04 -0400141 if test.get('use_admin', False):
142 manager = admin_manager
143 else:
Andrea Frittoli (andreaf)bc0a7a62016-05-26 19:31:49 +0100144 raise NotImplemented('Non admin tests are not supported')
llg821243b20502014-02-22 10:32:49 +0800145 for p_number in moves.xrange(test.get('threads', default_thread_num)):
David Kranzb9d97502013-05-01 15:55:04 -0400146 if test.get('use_isolated_tenants', False):
Masayuki Igawa259c1132013-10-31 17:48:44 +0900147 username = data_utils.rand_name("stress_user")
148 tenant_name = data_utils.rand_name("stress_tenant")
David Kranzb9d97502013-05-01 15:55:04 -0400149 password = "pass"
Andrea Frittolif2f7a372015-03-04 15:07:39 +0000150 if CONF.identity.auth_version == 'v2':
151 identity_client = admin_manager.identity_client
Daniel Melladob04da902015-11-20 17:43:12 +0100152 projects_client = admin_manager.tenants_client
Daniel Mellado6b16b922015-12-07 12:43:08 +0000153 roles_client = admin_manager.roles_client
Daniel Mellado82c83a52015-12-09 15:16:49 +0000154 users_client = admin_manager.users_client
Daniel Mellado91a26b62016-02-11 11:13:04 +0000155 domains_client = None
Andrea Frittolif2f7a372015-03-04 15:07:39 +0000156 else:
157 identity_client = admin_manager.identity_v3_client
Yaroslav Lobankov47a93ab2016-02-07 16:32:49 -0600158 projects_client = admin_manager.projects_client
Arx Cruz24bcb882016-02-10 15:20:16 +0100159 roles_client = admin_manager.roles_v3_client
Daniel Mellado7aea5342016-02-09 09:10:12 +0000160 users_client = admin_manager.users_v3_client
Daniel Mellado91a26b62016-02-11 11:13:04 +0000161 domains_client = admin_manager.domains_client
Ghanshyam627debf2016-01-22 18:11:00 +0900162 domain = (identity_client.auth_provider.credentials.
163 get('project_domain_name', 'Default'))
Jamie Lennox15350172015-08-17 10:54:25 +1000164 credentials_client = cred_client.get_creds_client(
Daniel Mellado7aea5342016-02-09 09:10:12 +0000165 identity_client, projects_client, users_client,
Daniel Mellado91a26b62016-02-11 11:13:04 +0000166 roles_client, domains_client, project_domain_name=domain)
Andrea Frittolif2f7a372015-03-04 15:07:39 +0000167 project = credentials_client.create_project(
168 name=tenant_name, description=tenant_name)
169 user = credentials_client.create_user(username, password,
Daniel Melladob04da902015-11-20 17:43:12 +0100170 project, "email")
Andrea Frittolif2f7a372015-03-04 15:07:39 +0000171 # Add roles specified in config file
172 for conf_role in CONF.auth.tempest_roles:
173 credentials_client.assign_user_role(user, project,
174 conf_role)
175 creds = credentials_client.get_credentials(user, project,
176 password)
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000177 manager = clients.Manager(credentials=creds)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700178
Attila Fazekas1e30d5d2013-07-30 14:38:20 +0200179 test_obj = importutils.import_class(test['action'])
Marc Kodererb714de52013-08-08 09:21:46 +0200180 test_run = test_obj(manager, max_runs, stop_on_error)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700181
182 kwargs = test.get('kwargs', {})
Matthew Treinish71426682015-04-23 11:19:38 -0400183 test_run.setUp(**dict(six.iteritems(kwargs)))
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700184
Marc Kodererb714de52013-08-08 09:21:46 +0200185 LOG.debug("calling Target Object %s" %
186 test_run.__class__.__name__)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700187
Marc Koderer69d3bea2013-07-18 08:32:11 +0200188 mp_manager = multiprocessing.Manager()
189 shared_statistic = mp_manager.dict()
190 shared_statistic['runs'] = 0
191 shared_statistic['fails'] = 0
192
193 p = multiprocessing.Process(target=test_run.execute,
194 args=(shared_statistic,))
195
196 process = {'process': p,
197 'p_number': p_number,
Marc Koderer33ca6ee2013-08-29 09:06:36 +0200198 'action': test_run.action,
Marc Koderer69d3bea2013-07-18 08:32:11 +0200199 'statistic': shared_statistic}
200
201 processes.append(process)
David Kranzb9d97502013-05-01 15:55:04 -0400202 p.start()
Marc Koderer3414d732013-07-31 08:36:36 +0200203 if stop_on_error:
204 # NOTE(mkoderer): only the parent should register the handler
205 signal.signal(signal.SIGCHLD, sigchld_handler)
David Kranzb9d97502013-05-01 15:55:04 -0400206 end_time = time.time() + duration
207 had_errors = False
Pavel Sedlák400c4132014-04-29 16:31:48 +0200208 try:
209 while True:
210 if max_runs is None:
211 remaining = end_time - time.time()
212 if remaining <= 0:
Marc Koderer69d3bea2013-07-18 08:32:11 +0200213 break
Pavel Sedlák400c4132014-04-29 16:31:48 +0200214 else:
215 remaining = log_check_interval
216 all_proc_term = True
217 for process in processes:
218 if process['process'].is_alive():
219 all_proc_term = False
220 break
221 if all_proc_term:
Marc Koderer3414d732013-07-31 08:36:36 +0200222 break
223
Pavel Sedlák400c4132014-04-29 16:31:48 +0200224 time.sleep(min(remaining, log_check_interval))
225 if stop_on_error:
Pavel Sedlákfa6666c2014-04-29 16:56:48 +0200226 if any([True for proc in processes
227 if proc['statistic']['fails'] > 0]):
228 break
Pavel Sedlák400c4132014-04-29 16:31:48 +0200229
230 if not logfiles:
231 continue
232 if _has_error_in_logs(logfiles, computes, ssh_user, ssh_key,
233 stop_on_error):
234 had_errors = True
235 break
236 except KeyboardInterrupt:
237 LOG.warning("Interrupted, going to print statistics and exit ...")
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700238
Attila Fazekasd047d1d2014-04-19 21:58:47 +0200239 if stop_on_error:
240 signal.signal(signal.SIGCHLD, signal.SIG_DFL)
Marc Koderer3414d732013-07-31 08:36:36 +0200241 terminate_all_processes()
Marc Koderer69d3bea2013-07-18 08:32:11 +0200242
243 sum_fails = 0
244 sum_runs = 0
245
Marc Kodererb714de52013-08-08 09:21:46 +0200246 LOG.info("Statistics (per process):")
Marc Koderer69d3bea2013-07-18 08:32:11 +0200247 for process in processes:
248 if process['statistic']['fails'] > 0:
249 had_errors = True
250 sum_runs += process['statistic']['runs']
251 sum_fails += process['statistic']['fails']
Marc Kodererf863b3b2015-12-18 09:21:13 +0100252 print ("Process %d (%s): Run %d actions (%d failed)" % (
253 process['p_number'],
254 process['action'],
255 process['statistic']['runs'],
256 process['statistic']['fails']))
257 print ("Summary:")
258 print ("Run %d actions (%d failed)" % (sum_runs, sum_fails))
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700259
Julien Leloupa5ee5422014-02-13 14:29:02 +0100260 if not had_errors and CONF.stress.full_clean_stack:
Marc Kodererb714de52013-08-08 09:21:46 +0200261 LOG.info("cleaning up")
262 cleanup.cleanup()
Marc Koderer888ddc42013-07-23 16:13:07 +0200263 if had_errors:
264 return 1
265 else:
266 return 0