blob: b5cab68712d4c981d6531d4c581d8e391eaf03cb [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
20from tempest import clients
21from tempest.common import ssh
22from tempest.common.utils.data_utils import rand_name
23from tempest import exceptions
Attila Fazekas1e30d5d2013-07-30 14:38:20 +020024from tempest.openstack.common import importutils
Marc Kodererb714de52013-08-08 09:21:46 +020025from tempest.openstack.common import log as logging
David Kranzb9d97502013-05-01 15:55:04 -040026from tempest.stress import cleanup
27
28admin_manager = clients.AdminManager()
29
Marc Kodererb714de52013-08-08 09:21:46 +020030LOG = logging.getLogger(__name__)
Marc Koderer3414d732013-07-31 08:36:36 +020031processes = []
David Kranzb9d97502013-05-01 15:55:04 -040032
33
34def do_ssh(command, host):
35 username = admin_manager.config.stress.target_ssh_user
36 key_filename = admin_manager.config.stress.target_private_key_path
37 if not (username and key_filename):
DennyZhang6baa6672013-09-24 17:49:30 -070038 LOG.error('username and key_filename should not be empty')
David Kranzb9d97502013-05-01 15:55:04 -040039 return None
40 ssh_client = ssh.Client(host, username, key_filename=key_filename)
41 try:
42 return ssh_client.exec_command(command)
43 except exceptions.SSHExecCommandFailed:
DennyZhang6baa6672013-09-24 17:49:30 -070044 LOG.error('do_ssh raise exception. command:%s, host:%s.'
45 % (command, host))
David Kranzb9d97502013-05-01 15:55:04 -040046 return None
47
48
49def _get_compute_nodes(controller):
50 """
51 Returns a list of active compute nodes. List is generated by running
52 nova-manage on the controller.
53 """
54 nodes = []
55 cmd = "nova-manage service list | grep ^nova-compute"
56 output = do_ssh(cmd, controller)
57 if not output:
58 return nodes
59 # For example: nova-compute xg11eth0 nova enabled :-) 2011-10-31 18:57:46
60 # This is fragile but there is, at present, no other way to get this info.
61 for line in output.split('\n'):
62 words = line.split()
63 if len(words) > 0 and words[4] == ":-)":
64 nodes.append(words[1])
65 return nodes
66
67
DennyZhang49b21ab2013-09-24 16:24:23 -050068def _has_error_in_logs(logfiles, nodes, stop_on_error=False):
David Kranzb9d97502013-05-01 15:55:04 -040069 """
70 Detect errors in the nova log files on the controller and compute nodes.
71 """
72 grep = 'egrep "ERROR|TRACE" %s' % logfiles
DennyZhang49b21ab2013-09-24 16:24:23 -050073 ret = False
David Kranzb9d97502013-05-01 15:55:04 -040074 for node in nodes:
75 errors = do_ssh(grep, node)
David Kranzb9d97502013-05-01 15:55:04 -040076 if len(errors) > 0:
Marc Kodererb714de52013-08-08 09:21:46 +020077 LOG.error('%s: %s' % (node, errors))
DennyZhang49b21ab2013-09-24 16:24:23 -050078 ret = True
79 if stop_on_error:
80 break
81 return ret
David Kranzb9d97502013-05-01 15:55:04 -040082
83
Marc Koderer3414d732013-07-31 08:36:36 +020084def sigchld_handler(signal, frame):
85 """
86 Signal handler (only active if stop_on_error is True).
87 """
88 terminate_all_processes()
89
90
91def terminate_all_processes():
92 """
93 Goes through the process list and terminates all child processes.
94 """
Marc Koderer8f940ab2013-09-25 17:31:50 +020095 log_check_interval = int(admin_manager.config.stress.log_check_interval)
Marc Koderer3414d732013-07-31 08:36:36 +020096 for process in processes:
97 if process['process'].is_alive():
98 try:
99 process['process'].terminate()
100 except Exception:
101 pass
Marc Koderer8f940ab2013-09-25 17:31:50 +0200102 time.sleep(log_check_interval)
103 for process in processes:
104 if process['process'].is_alive():
105 try:
106 pid = process['process'].pid
107 LOG.warn("Process %d hangs. Send SIGKILL." % pid)
108 os.kill(pid, signal.SIGKILL)
109 except Exception:
110 pass
Marc Koderer3414d732013-07-31 08:36:36 +0200111 process['process'].join()
112
113
114def stress_openstack(tests, duration, max_runs=None, stop_on_error=False):
David Kranzb9d97502013-05-01 15:55:04 -0400115 """
116 Workload driver. Executes an action function against a nova-cluster.
David Kranzb9d97502013-05-01 15:55:04 -0400117 """
118 logfiles = admin_manager.config.stress.target_logfiles
119 log_check_interval = int(admin_manager.config.stress.log_check_interval)
Marc Koderer32221b8e2013-08-23 13:57:50 +0200120 default_thread_num = int(admin_manager.config.stress.
121 default_thread_number_per_action)
David Kranzb9d97502013-05-01 15:55:04 -0400122 if logfiles:
123 controller = admin_manager.config.stress.target_controller
124 computes = _get_compute_nodes(controller)
125 for node in computes:
126 do_ssh("rm -f %s" % logfiles, node)
David Kranzb9d97502013-05-01 15:55:04 -0400127 for test in tests:
128 if test.get('use_admin', False):
129 manager = admin_manager
130 else:
131 manager = clients.Manager()
Marc Koderer32221b8e2013-08-23 13:57:50 +0200132 for p_number in xrange(test.get('threads', default_thread_num)):
David Kranzb9d97502013-05-01 15:55:04 -0400133 if test.get('use_isolated_tenants', False):
134 username = rand_name("stress_user")
135 tenant_name = rand_name("stress_tenant")
136 password = "pass"
137 identity_client = admin_manager.identity_client
138 _, tenant = identity_client.create_tenant(name=tenant_name)
139 identity_client.create_user(username,
140 password,
141 tenant['id'],
142 "email")
143 manager = clients.Manager(username=username,
144 password="pass",
145 tenant_name=tenant_name)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700146
Attila Fazekas1e30d5d2013-07-30 14:38:20 +0200147 test_obj = importutils.import_class(test['action'])
Marc Kodererb714de52013-08-08 09:21:46 +0200148 test_run = test_obj(manager, max_runs, stop_on_error)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700149
150 kwargs = test.get('kwargs', {})
151 test_run.setUp(**dict(kwargs.iteritems()))
152
Marc Kodererb714de52013-08-08 09:21:46 +0200153 LOG.debug("calling Target Object %s" %
154 test_run.__class__.__name__)
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700155
Marc Koderer69d3bea2013-07-18 08:32:11 +0200156 mp_manager = multiprocessing.Manager()
157 shared_statistic = mp_manager.dict()
158 shared_statistic['runs'] = 0
159 shared_statistic['fails'] = 0
160
161 p = multiprocessing.Process(target=test_run.execute,
162 args=(shared_statistic,))
163
164 process = {'process': p,
165 'p_number': p_number,
Marc Koderer33ca6ee2013-08-29 09:06:36 +0200166 'action': test_run.action,
Marc Koderer69d3bea2013-07-18 08:32:11 +0200167 'statistic': shared_statistic}
168
169 processes.append(process)
David Kranzb9d97502013-05-01 15:55:04 -0400170 p.start()
Marc Koderer3414d732013-07-31 08:36:36 +0200171 if stop_on_error:
172 # NOTE(mkoderer): only the parent should register the handler
173 signal.signal(signal.SIGCHLD, sigchld_handler)
David Kranzb9d97502013-05-01 15:55:04 -0400174 end_time = time.time() + duration
175 had_errors = False
176 while True:
Marc Koderer69d3bea2013-07-18 08:32:11 +0200177 if max_runs is None:
178 remaining = end_time - time.time()
179 if remaining <= 0:
180 break
181 else:
182 remaining = log_check_interval
183 all_proc_term = True
184 for process in processes:
185 if process['process'].is_alive():
186 all_proc_term = False
187 break
188 if all_proc_term:
189 break
190
David Kranzb9d97502013-05-01 15:55:04 -0400191 time.sleep(min(remaining, log_check_interval))
Marc Koderer3414d732013-07-31 08:36:36 +0200192 if stop_on_error:
193 for process in processes:
194 if process['statistic']['fails'] > 0:
195 break
196
David Kranzb9d97502013-05-01 15:55:04 -0400197 if not logfiles:
198 continue
Giulio Fidente4ea01942013-10-04 12:31:42 +0200199 if _has_error_in_logs(logfiles, computes, stop_on_error):
David Kranzb9d97502013-05-01 15:55:04 -0400200 had_errors = True
201 break
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700202
Marc Koderer3414d732013-07-31 08:36:36 +0200203 terminate_all_processes()
Marc Koderer69d3bea2013-07-18 08:32:11 +0200204
205 sum_fails = 0
206 sum_runs = 0
207
Marc Kodererb714de52013-08-08 09:21:46 +0200208 LOG.info("Statistics (per process):")
Marc Koderer69d3bea2013-07-18 08:32:11 +0200209 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 Kodererb714de52013-08-08 09:21:46 +0200214 LOG.info(" Process %d (%s): Run %d actions (%d failed)" %
215 (process['p_number'],
216 process['action'],
217 process['statistic']['runs'],
Marc Koderer69d3bea2013-07-18 08:32:11 +0200218 process['statistic']['fails']))
Marc Kodererb714de52013-08-08 09:21:46 +0200219 LOG.info("Summary:")
220 LOG.info("Run %d actions (%d failed)" %
221 (sum_runs, sum_fails))
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700222
David Kranzb9d97502013-05-01 15:55:04 -0400223 if not had_errors:
Marc Kodererb714de52013-08-08 09:21:46 +0200224 LOG.info("cleaning up")
225 cleanup.cleanup()
Marc Koderer888ddc42013-07-23 16:13:07 +0200226 if had_errors:
227 return 1
228 else:
229 return 0