blob: 8dc88cf83b30325b7deef9da78ed4ed03d5b4aa3 [file] [log] [blame]
Sean Dague70112362012-04-03 13:48:49 -04001# Copyright 2011 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"""The entry point for the execution of a workloadTo execute a workload.
15Users pass in a description of the workload and a nova manager object
16to the bash_openstack function call"""
17
Sean Dague70112362012-04-03 13:48:49 -040018import datetime
Matthew Treinish8d6836b2012-12-10 10:07:56 -050019import random
Sean Dague70112362012-04-03 13:48:49 -040020import time
21
Sean Dague70112362012-04-03 13:48:49 -040022from config import StressConfig
Matthew Treinish8d6836b2012-12-10 10:07:56 -050023from state import ClusterState
24from state import FloatingIpState
25from state import KeyPairState
26from state import VolumeState
Attila Fazekas73c152d2013-02-16 16:41:03 +010027import stress.utils
28from test_case import logging
29
David Kranz779c7f82012-05-01 16:50:32 -040030from tempest.common.utils.data_utils import rand_name
Sean Dague70112362012-04-03 13:48:49 -040031
32# setup logging to file
33logging.basicConfig(
34 format='%(asctime)s %(name)-20s %(levelname)-8s %(message)s',
35 datefmt='%m-%d %H:%M:%S',
36 filename="stress.debug.log",
37 filemode="w",
38 level=logging.DEBUG,
Zhongyue Luo30a563f2012-09-30 23:43:50 +090039)
Sean Dague70112362012-04-03 13:48:49 -040040
41# define a Handler which writes INFO messages or higher to the sys.stdout
42_console = logging.StreamHandler()
43_console.setLevel(logging.INFO)
44# set a format which is simpler for console use
45_formatter = logging.Formatter('%(name)-20s: %(levelname)-8s %(message)s')
46# tell the handler to use this format
47_console.setFormatter(_formatter)
48# add the handler to the root logger
49logging.getLogger('').addHandler(_console)
50
51
52def _create_cases(choice_spec):
53 """
54 Generate a workload of tests from workload description
55 """
56 cases = []
57 count = 0
58 for choice in choice_spec:
59 p = choice.probability
60 for i in range(p):
61 cases.append(choice)
62 i = i + p
63 count = count + p
64 assert(count == 100)
65 return cases
66
67
68def _get_compute_nodes(keypath, user, controller):
69 """
70 Returns a list of active compute nodes. List is generated by running
71 nova-manage on the controller.
72 """
73 nodes = []
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080074 if keypath is None or user is None:
Sean Dague70112362012-04-03 13:48:49 -040075 return nodes
Zhongyue Luo79d8d362012-09-25 13:49:27 +080076 cmd = "nova-manage service list | grep ^nova-compute"
Attila Fazekas73c152d2013-02-16 16:41:03 +010077 lines = stress.utils.ssh(keypath, user, controller, cmd).split('\n')
Sean Dague70112362012-04-03 13:48:49 -040078 # For example: nova-compute xg11eth0 nova enabled :-) 2011-10-31 18:57:46
79 # This is fragile but there is, at present, no other way to get this info.
80 for line in lines:
81 words = line.split()
82 if len(words) > 0 and words[4] == ":-)":
83 nodes.append(words[1])
84 return nodes
85
86
87def _error_in_logs(keypath, logdir, user, nodes):
88 """
89 Detect errors in the nova log files on the controller and compute nodes.
90 """
91 grep = 'egrep "ERROR\|TRACE" %s/*.log' % logdir
92 for node in nodes:
Attila Fazekas73c152d2013-02-16 16:41:03 +010093 errors = stress.utils.ssh(keypath, user, node, grep, check=False)
Sean Dague70112362012-04-03 13:48:49 -040094 if len(errors) > 0:
95 logging.error('%s: %s' % (node, errors))
96 return True
97 return False
98
99
David Kranz779c7f82012-05-01 16:50:32 -0400100def create_initial_vms(manager, state, count):
101 image = manager.config.compute.image_ref
102 flavor = manager.config.compute.flavor_ref
103 servers = []
104 logging.info('Creating %d vms' % count)
105 for _ in xrange(count):
106 name = rand_name('initial_vm-')
107 _, server = manager.servers_client.create_server(name, image, flavor)
108 servers.append(server)
109 for server in servers:
110 manager.servers_client.wait_for_server_status(server['id'], 'ACTIVE')
111 logging.info('Server Name: %s Id: %s' % (name, server['id']))
112 state.set_instance_state(server['id'], (server, 'ACTIVE'))
113
114
115def create_initial_floating_ips(manager, state, count):
116 logging.info('Creating %d floating ips' % count)
117 for _ in xrange(count):
118 _, ip = manager.floating_ips_client.create_floating_ip()
119 logging.info('Ip: %s' % ip['ip'])
120 state.add_floating_ip(FloatingIpState(ip))
121
122
123def create_initial_keypairs(manager, state, count):
124 logging.info('Creating %d keypairs' % count)
125 for _ in xrange(count):
126 name = rand_name('keypair-')
127 _, keypair = manager.keypairs_client.create_keypair(name)
128 logging.info('Keypair: %s' % name)
129 state.add_keypair(KeyPairState(keypair))
130
131
132def create_initial_volumes(manager, state, count):
133 volumes = []
134 logging.info('Creating %d volumes' % count)
135 for _ in xrange(count):
136 name = rand_name('volume-')
137 _, volume = manager.volumes_client.create_volume(size=1,
138 display_name=name)
139 volumes.append(volume)
140 for volume in volumes:
141 manager.volumes_client.wait_for_volume_status(volume['id'],
142 'available')
143 logging.info('Volume Name: %s Id: %s' % (name, volume['id']))
144 state.add_volume(VolumeState(volume))
145
146
Sean Dague70112362012-04-03 13:48:49 -0400147def bash_openstack(manager,
148 choice_spec,
149 **kwargs):
150 """
151 Workload driver. Executes a workload as specified by the `choice_spec`
152 parameter against a nova-cluster.
153
154 `manager` : Manager object
155 `choice_spec` : list of BasherChoice actions to run on the cluster
156 `kargs` : keyword arguments to the constructor of `test_case`
157 `duration` = how long this test should last (3 sec)
158 `sleep_time` = time to sleep between actions (in msec)
159 `test_name` = human readable workload description
160 (default: unnamed test)
161 `max_vms` = maximum number of instances to launch
162 (default: 32)
163 `seed` = random seed (default: None)
164 """
165 stress_config = StressConfig(manager.config._conf)
166 # get keyword arguments
167 duration = kwargs.get('duration', datetime.timedelta(seconds=10))
168 seed = kwargs.get('seed', None)
169 sleep_time = float(kwargs.get('sleep_time', 3000)) / 1000
170 max_vms = int(kwargs.get('max_vms', stress_config.max_instances))
171 test_name = kwargs.get('test_name', 'unamed test')
172
173 keypath = stress_config.host_private_key_path
174 user = stress_config.host_admin_user
175 logdir = stress_config.nova_logdir
176 computes = _get_compute_nodes(keypath, user, manager.config.identity.host)
Attila Fazekas73c152d2013-02-16 16:41:03 +0100177 stress.utils.execute_on_all(keypath, user, computes,
178 "rm -f %s/*.log" % logdir)
Sean Dague70112362012-04-03 13:48:49 -0400179 random.seed(seed)
180 cases = _create_cases(choice_spec)
David Kranz779c7f82012-05-01 16:50:32 -0400181 state = ClusterState(max_vms=max_vms)
182 create_initial_keypairs(manager, state,
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800183 int(kwargs.get('initial_keypairs', 0)))
David Kranz779c7f82012-05-01 16:50:32 -0400184 create_initial_vms(manager, state,
185 int(kwargs.get('initial_vms', 0)))
186 create_initial_floating_ips(manager, state,
187 int(kwargs.get('initial_floating_ips', 0)))
188 create_initial_volumes(manager, state,
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800189 int(kwargs.get('initial_volumes', 0)))
Sean Dague70112362012-04-03 13:48:49 -0400190 test_end_time = time.time() + duration.seconds
Sean Dague70112362012-04-03 13:48:49 -0400191
192 retry_list = []
193 last_retry = time.time()
194 cooldown = False
195 logcheck_count = 0
196 test_succeeded = True
197 logging.debug('=== Test \"%s\" on %s ===' %
198 (test_name, time.asctime(time.localtime())))
199 for kw in kwargs:
200 logging.debug('\t%s = %s', kw, kwargs[kw])
201
202 while True:
203 if not cooldown:
204 if time.time() < test_end_time:
205 case = random.choice(cases)
206 logging.debug('Chose %s' % case)
207 retry = case.invoke(manager, state)
Zhongyue Luoe471d6e2012-09-17 17:02:43 +0800208 if retry is not None:
Sean Dague70112362012-04-03 13:48:49 -0400209 retry_list.append(retry)
210 else:
211 logging.info('Cooling down...')
212 cooldown = True
213 if cooldown and len(retry_list) == 0:
214 if _error_in_logs(keypath, logdir, user, computes):
215 test_succeeded = False
216 break
217 # Retry verifications every 5 seconds.
218 if time.time() - last_retry > 5:
219 logging.debug('retry verifications for %d tasks', len(retry_list))
220 new_retry_list = []
221 for v in retry_list:
David Kranz779c7f82012-05-01 16:50:32 -0400222 v.check_timeout()
Sean Dague70112362012-04-03 13:48:49 -0400223 if not v.retry():
224 new_retry_list.append(v)
225 retry_list = new_retry_list
226 last_retry = time.time()
227 time.sleep(sleep_time)
228 # Check error logs after 100 actions
229 if logcheck_count > 100:
230 if _error_in_logs(keypath, logdir, user, computes):
231 test_succeeded = False
232 break
233 else:
234 logcheck_count = 0
235 else:
236 logcheck_count = logcheck_count + 1
237 # Cleanup
238 logging.info('Cleaning up: terminating virtual machines...')
239 vms = state.get_instances()
David Kranz779c7f82012-05-01 16:50:32 -0400240 active_vms = [v for _k, v in vms.iteritems()
241 if v and v[1] != 'TERMINATING']
Sean Dague70112362012-04-03 13:48:49 -0400242 for target in active_vms:
243 manager.servers_client.delete_server(target[0]['id'])
244 # check to see that the server was actually killed
245 for target in active_vms:
246 kill_id = target[0]['id']
247 i = 0
248 while True:
249 try:
250 manager.servers_client.get_server(kill_id)
251 except Exception:
252 break
253 i += 1
254 if i > 60:
255 _error_in_logs(keypath, logdir, user, computes)
256 raise Exception("Cleanup timed out")
257 time.sleep(1)
258 logging.info('killed %s' % kill_id)
259 state.delete_instance_state(kill_id)
David Kranz779c7f82012-05-01 16:50:32 -0400260 for floating_ip_state in state.get_floating_ips():
261 manager.floating_ips_client.delete_floating_ip(
262 floating_ip_state.resource_id)
263 for keypair_state in state.get_keypairs():
264 manager.keypairs_client.delete_keypair(keypair_state.name)
265 for volume_state in state.get_volumes():
266 manager.volumes_client.delete_volume(volume_state.resource_id)
Sean Dague70112362012-04-03 13:48:49 -0400267
268 if test_succeeded:
269 logging.info('*** Test succeeded ***')
270 else:
271 logging.info('*** Test had errors ***')
272 return test_succeeded