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