David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 1 | # 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 Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 15 | import multiprocessing |
Marc Koderer | 8f940ab | 2013-09-25 17:31:50 +0200 | [diff] [blame] | 16 | import os |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 17 | import signal |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 18 | import time |
| 19 | |
| 20 | from tempest import clients |
| 21 | from tempest.common import ssh |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 22 | from tempest.common.utils import data_utils |
Matthew Treinish | 88f49ef | 2014-01-29 18:36:27 +0000 | [diff] [blame] | 23 | from tempest import config |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 24 | from tempest import exceptions |
Attila Fazekas | 1e30d5d | 2013-07-30 14:38:20 +0200 | [diff] [blame] | 25 | from tempest.openstack.common import importutils |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 26 | from tempest.openstack.common import log as logging |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 27 | from tempest.stress import cleanup |
| 28 | |
Matthew Treinish | 88f49ef | 2014-01-29 18:36:27 +0000 | [diff] [blame] | 29 | CONF = config.CONF |
| 30 | |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 31 | LOG = logging.getLogger(__name__) |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 32 | processes = [] |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 33 | |
| 34 | |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 35 | def do_ssh(command, host, ssh_user, ssh_key=None): |
| 36 | ssh_client = ssh.Client(host, ssh_user, key_filename=ssh_key) |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 37 | try: |
| 38 | return ssh_client.exec_command(command) |
| 39 | except exceptions.SSHExecCommandFailed: |
DennyZhang | 6baa667 | 2013-09-24 17:49:30 -0700 | [diff] [blame] | 40 | LOG.error('do_ssh raise exception. command:%s, host:%s.' |
| 41 | % (command, host)) |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 42 | return None |
| 43 | |
| 44 | |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 45 | def _get_compute_nodes(controller, ssh_user, ssh_key=None): |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 46 | """ |
| 47 | Returns a list of active compute nodes. List is generated by running |
| 48 | nova-manage on the controller. |
| 49 | """ |
| 50 | nodes = [] |
| 51 | cmd = "nova-manage service list | grep ^nova-compute" |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 52 | output = do_ssh(cmd, controller, ssh_user, ssh_key) |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 53 | if not output: |
| 54 | return nodes |
| 55 | # For example: nova-compute xg11eth0 nova enabled :-) 2011-10-31 18:57:46 |
| 56 | # This is fragile but there is, at present, no other way to get this info. |
| 57 | for line in output.split('\n'): |
| 58 | words = line.split() |
| 59 | if len(words) > 0 and words[4] == ":-)": |
| 60 | nodes.append(words[1]) |
| 61 | return nodes |
| 62 | |
| 63 | |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 64 | def _has_error_in_logs(logfiles, nodes, ssh_user, ssh_key=None, |
| 65 | stop_on_error=False): |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 66 | """ |
| 67 | Detect errors in the nova log files on the controller and compute nodes. |
| 68 | """ |
| 69 | grep = 'egrep "ERROR|TRACE" %s' % logfiles |
DennyZhang | 49b21ab | 2013-09-24 16:24:23 -0500 | [diff] [blame] | 70 | ret = False |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 71 | for node in nodes: |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 72 | errors = do_ssh(grep, node, ssh_user, ssh_key) |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 73 | if len(errors) > 0: |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 74 | LOG.error('%s: %s' % (node, errors)) |
DennyZhang | 49b21ab | 2013-09-24 16:24:23 -0500 | [diff] [blame] | 75 | ret = True |
| 76 | if stop_on_error: |
| 77 | break |
| 78 | return ret |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 79 | |
| 80 | |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 81 | def sigchld_handler(signal, frame): |
| 82 | """ |
| 83 | Signal handler (only active if stop_on_error is True). |
| 84 | """ |
| 85 | terminate_all_processes() |
| 86 | |
| 87 | |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 88 | def terminate_all_processes(check_interval=20): |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 89 | """ |
| 90 | Goes through the process list and terminates all child processes. |
| 91 | """ |
| 92 | for process in processes: |
| 93 | if process['process'].is_alive(): |
| 94 | try: |
| 95 | process['process'].terminate() |
| 96 | except Exception: |
| 97 | pass |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 98 | time.sleep(check_interval) |
Marc Koderer | 8f940ab | 2013-09-25 17:31:50 +0200 | [diff] [blame] | 99 | for process in processes: |
| 100 | if process['process'].is_alive(): |
| 101 | try: |
| 102 | pid = process['process'].pid |
| 103 | LOG.warn("Process %d hangs. Send SIGKILL." % pid) |
| 104 | os.kill(pid, signal.SIGKILL) |
| 105 | except Exception: |
| 106 | pass |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 107 | process['process'].join() |
| 108 | |
| 109 | |
| 110 | def stress_openstack(tests, duration, max_runs=None, stop_on_error=False): |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 111 | """ |
| 112 | Workload driver. Executes an action function against a nova-cluster. |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 113 | """ |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 114 | admin_manager = clients.AdminManager() |
| 115 | |
Matthew Treinish | 88f49ef | 2014-01-29 18:36:27 +0000 | [diff] [blame] | 116 | ssh_user = CONF.stress.target_ssh_user |
| 117 | ssh_key = CONF.stress.target_private_key_path |
| 118 | logfiles = CONF.stress.target_logfiles |
| 119 | log_check_interval = int(CONF.stress.log_check_interval) |
| 120 | default_thread_num = int(CONF.stress.default_thread_number_per_action) |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 121 | if logfiles: |
Matthew Treinish | 88f49ef | 2014-01-29 18:36:27 +0000 | [diff] [blame] | 122 | controller = CONF.stress.target_controller |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 123 | computes = _get_compute_nodes(controller, ssh_user, ssh_key) |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 124 | for node in computes: |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 125 | do_ssh("rm -f %s" % logfiles, node, ssh_user, ssh_key) |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 126 | for test in tests: |
| 127 | if test.get('use_admin', False): |
| 128 | manager = admin_manager |
| 129 | else: |
| 130 | manager = clients.Manager() |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 131 | for p_number in xrange(test.get('threads', default_thread_num)): |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 132 | if test.get('use_isolated_tenants', False): |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 133 | username = data_utils.rand_name("stress_user") |
| 134 | tenant_name = data_utils.rand_name("stress_tenant") |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 135 | password = "pass" |
| 136 | identity_client = admin_manager.identity_client |
| 137 | _, tenant = identity_client.create_tenant(name=tenant_name) |
| 138 | identity_client.create_user(username, |
| 139 | password, |
| 140 | tenant['id'], |
| 141 | "email") |
| 142 | manager = clients.Manager(username=username, |
| 143 | password="pass", |
| 144 | tenant_name=tenant_name) |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 145 | |
Attila Fazekas | 1e30d5d | 2013-07-30 14:38:20 +0200 | [diff] [blame] | 146 | test_obj = importutils.import_class(test['action']) |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 147 | test_run = test_obj(manager, max_runs, stop_on_error) |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 148 | |
| 149 | kwargs = test.get('kwargs', {}) |
| 150 | test_run.setUp(**dict(kwargs.iteritems())) |
| 151 | |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 152 | LOG.debug("calling Target Object %s" % |
| 153 | test_run.__class__.__name__) |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 154 | |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 155 | mp_manager = multiprocessing.Manager() |
| 156 | shared_statistic = mp_manager.dict() |
| 157 | shared_statistic['runs'] = 0 |
| 158 | shared_statistic['fails'] = 0 |
| 159 | |
| 160 | p = multiprocessing.Process(target=test_run.execute, |
| 161 | args=(shared_statistic,)) |
| 162 | |
| 163 | process = {'process': p, |
| 164 | 'p_number': p_number, |
Marc Koderer | 33ca6ee | 2013-08-29 09:06:36 +0200 | [diff] [blame] | 165 | 'action': test_run.action, |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 166 | 'statistic': shared_statistic} |
| 167 | |
| 168 | processes.append(process) |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 169 | p.start() |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 170 | if stop_on_error: |
| 171 | # NOTE(mkoderer): only the parent should register the handler |
| 172 | signal.signal(signal.SIGCHLD, sigchld_handler) |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 173 | end_time = time.time() + duration |
| 174 | had_errors = False |
| 175 | while True: |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 176 | if max_runs is None: |
| 177 | remaining = end_time - time.time() |
| 178 | if remaining <= 0: |
| 179 | break |
| 180 | else: |
| 181 | remaining = log_check_interval |
| 182 | all_proc_term = True |
| 183 | for process in processes: |
| 184 | if process['process'].is_alive(): |
| 185 | all_proc_term = False |
| 186 | break |
| 187 | if all_proc_term: |
| 188 | break |
| 189 | |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 190 | time.sleep(min(remaining, log_check_interval)) |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 191 | if stop_on_error: |
| 192 | for process in processes: |
| 193 | if process['statistic']['fails'] > 0: |
| 194 | break |
| 195 | |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 196 | if not logfiles: |
| 197 | continue |
Marc Koderer | f13e487 | 2013-11-25 14:50:33 +0100 | [diff] [blame] | 198 | if _has_error_in_logs(logfiles, computes, ssh_user, ssh_key, |
| 199 | stop_on_error): |
David Kranz | b9d9750 | 2013-05-01 15:55:04 -0400 | [diff] [blame] | 200 | had_errors = True |
| 201 | break |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 202 | |
Marc Koderer | 3414d73 | 2013-07-31 08:36:36 +0200 | [diff] [blame] | 203 | terminate_all_processes() |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 204 | |
| 205 | sum_fails = 0 |
| 206 | sum_runs = 0 |
| 207 | |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 208 | LOG.info("Statistics (per process):") |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 209 | for process in processes: |
| 210 | if process['statistic']['fails'] > 0: |
| 211 | had_errors = True |
| 212 | sum_runs += process['statistic']['runs'] |
| 213 | sum_fails += process['statistic']['fails'] |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 214 | LOG.info(" Process %d (%s): Run %d actions (%d failed)" % |
| 215 | (process['p_number'], |
| 216 | process['action'], |
| 217 | process['statistic']['runs'], |
Marc Koderer | 69d3bea | 2013-07-18 08:32:11 +0200 | [diff] [blame] | 218 | process['statistic']['fails'])) |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 219 | LOG.info("Summary:") |
| 220 | LOG.info("Run %d actions (%d failed)" % |
| 221 | (sum_runs, sum_fails)) |
Walter A. Boring IV | b725e62 | 2013-07-11 17:21:33 -0700 | [diff] [blame] | 222 | |
Julien Leloup | a5ee542 | 2014-02-13 14:29:02 +0100 | [diff] [blame] | 223 | if not had_errors and CONF.stress.full_clean_stack: |
Marc Koderer | b714de5 | 2013-08-08 09:21:46 +0200 | [diff] [blame] | 224 | LOG.info("cleaning up") |
| 225 | cleanup.cleanup() |
Marc Koderer | 888ddc4 | 2013-07-23 16:13:07 +0200 | [diff] [blame] | 226 | if had_errors: |
| 227 | return 1 |
| 228 | else: |
| 229 | return 0 |