7 import functest.utils.functest_utils as ft_utils
8 import functest.utils.openstack_utils as os_utils
10 from functest.utils.functest_utils import FUNCTEST_REPO as REPO_PATH
11 from scp import SCPClient
13 functest_yaml = ft_utils.get_functest_yaml()
15 NAME_VM_1 = functest_yaml.get("vping").get("vm_name_1")
16 NAME_VM_2 = functest_yaml.get("vping").get("vm_name_2")
19 VM_DELETE_TIMEOUT = 100
20 PING_TIMEOUT = functest_yaml.get("vping").get("ping_timeout")
22 GLANCE_IMAGE_NAME = functest_yaml.get("vping").get("image_name")
23 GLANCE_IMAGE_FILENAME = functest_yaml.get("general").get(
24 "openstack").get("image_file_name")
25 GLANCE_IMAGE_FORMAT = functest_yaml.get("general").get(
26 "openstack").get("image_disk_format")
27 GLANCE_IMAGE_PATH = functest_yaml.get("general").get("directories").get(
28 "dir_functest_data") + "/" + GLANCE_IMAGE_FILENAME
30 FLAVOR = functest_yaml.get("vping").get("vm_flavor")
32 # NEUTRON Private Network parameters
33 PRIVATE_NET_NAME = functest_yaml.get("vping").get(
34 "vping_private_net_name")
35 PRIVATE_SUBNET_NAME = functest_yaml.get("vping").get(
36 "vping_private_subnet_name")
37 PRIVATE_SUBNET_CIDR = functest_yaml.get("vping").get(
38 "vping_private_subnet_cidr")
39 ROUTER_NAME = functest_yaml.get("vping").get(
42 SECGROUP_NAME = functest_yaml.get("vping").get("vping_sg_name")
43 SECGROUP_DESCR = functest_yaml.get("vping").get("vping_sg_descr")
51 pp = pprint.PrettyPrinter(indent=4)
59 def check_repo_exist():
60 if not os.path.exists(REPO_PATH):
61 logger.error("Functest repository not found '%s'" % REPO_PATH)
73 def init(vping_logger):
75 nova_client = os_utils.get_nova_client()
77 neutron_client = os_utils.get_neutron_client()
79 glance_client = os_utils.get_glance_client()
84 def waitVmActive(nova, vm):
86 # sleep and wait for VM status change
88 count = VM_BOOT_TIMEOUT / sleep_time
90 status = os_utils.get_instance_status(nova, vm)
91 logger.debug("Status: %s" % status)
92 if status == "ACTIVE":
94 if status == "ERROR" or status == "error":
97 logger.debug("Booting a VM timed out...")
100 time.sleep(sleep_time)
104 def create_security_group():
105 sg_id = os_utils.get_security_group_id(neutron_client,
108 logger.info("Using existing security group '%s'..." % SECGROUP_NAME)
110 logger.info("Creating security group '%s'..." % SECGROUP_NAME)
111 SECGROUP = os_utils.create_security_group(neutron_client,
115 logger.error("Failed to create the security group...")
118 sg_id = SECGROUP['id']
120 logger.debug("Security group '%s' with ID=%s created successfully."
121 % (SECGROUP['name'], sg_id))
123 logger.debug("Adding ICMP rules in security group '%s'..."
125 if not os_utils.create_secgroup_rule(neutron_client, sg_id,
127 logger.error("Failed to create the security group rule...")
130 logger.debug("Adding SSH rules in security group '%s'..."
132 if not os_utils.create_secgroup_rule(neutron_client, sg_id,
135 logger.error("Failed to create the security group rule...")
138 if not os_utils.create_secgroup_rule(
139 neutron_client, sg_id, 'egress', 'tcp', '22', '22'):
140 logger.error("Failed to create the security group rule...")
146 _, image_id = os_utils.get_or_create_image(GLANCE_IMAGE_NAME,
158 # Check if the given flavor exists
160 flavor = nova_client.flavors.find(name=FLAVOR)
161 logger.info("Using existing Flavor '%s'..." % FLAVOR)
164 logger.error("Flavor '%s' not found." % FLAVOR)
165 logger.info("Available flavors are: ")
166 pMsg(nova_client.flavor.list())
170 def create_network_full():
173 network_dic = os_utils.create_network_full(neutron_client,
181 "There has been a problem when creating the neutron network")
183 network_id = network_dic["net_id"]
187 def delete_exist_vms():
188 servers = nova_client.servers.list()
189 for server in servers:
190 if server.name == NAME_VM_1 or server.name == NAME_VM_2:
191 logger.info("Instance %s found. Deleting..." % server.name)
195 def is_userdata(case):
196 return case == 'vping_userdata'
200 return case == 'vping_ssh'
203 def boot_vm(case, name, image_id, flavor, network_id, test_ip, sg_id):
207 config['name'] = name
208 config['flavor'] = flavor
209 config['image'] = image_id
210 config['nics'] = [{"net-id": network_id}]
211 if is_userdata(case):
212 config['config_drive'] = True
213 if name == NAME_VM_2:
216 " ping -c 1 %s 2>&1 >/dev/null\n"
218 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
226 config['userdata'] = u
228 logger.info("Creating instance '%s'..." % name)
229 logger.debug("Configuration: %s" % config)
230 vm = nova_client.servers.create(**config)
232 # wait until VM status is active
233 if not waitVmActive(nova_client, vm):
235 logger.error("Instance '%s' cannot be booted. Status is '%s'" % (
236 name, os_utils.get_instance_status(nova_client, vm)))
239 logger.info("Instance '%s' is ACTIVE." % name)
241 add_secgroup(name, vm.id, sg_id)
247 test_ip = vm.networks.get(PRIVATE_NET_NAME)[0]
248 logger.debug("Instance '%s' got %s" % (vm.name, test_ip))
252 def add_secgroup(vmname, vm_id, sg_id):
253 logger.info("Adding '%s' to security group '%s'..." %
254 (vmname, SECGROUP_NAME))
255 os_utils.add_secgroup_to_instance(nova_client, vm_id, sg_id)
258 def add_float_ip(vm):
261 logger.info("Creating floating IP for VM '%s'..." % NAME_VM_2)
262 floatip_dic = os_utils.create_floating_ip(neutron_client)
263 floatip = floatip_dic['fip_addr']
266 logger.error("Cannot create floating IP.")
268 logger.info("Floating IP created: '%s'" % floatip)
270 logger.info("Associating floating ip: '%s' to VM '%s' "
271 % (floatip, NAME_VM_2))
272 if not os_utils.add_floating_ip(nova_client, vm.id, floatip):
273 logger.error("Cannot associate floating IP to VM.")
279 def establish_ssh(vm, floatip):
282 logger.info("Trying to establish SSH connection to %s..." % floatip)
284 password = 'cubswin:)'
285 ssh = paramiko.SSHClient()
286 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
292 cidr_first_octet = PRIVATE_SUBNET_CIDR.split('.')[0]
295 ssh.connect(floatip, username=username,
296 password=password, timeout=2)
297 logger.debug("SSH connection established to %s." % floatip)
300 logger.debug("Waiting for %s..." % floatip)
304 console_log = vm.get_console_output()
306 # print each "Sending discover" captured on the console log
307 if (len(re.findall("Sending discover", console_log)) >
308 discover_count and not got_ip):
310 logger.debug("Console-log '%s': Sending discover..."
313 # check if eth0 got an ip,the line looks like this:
314 # "inet addr:192.168."....
315 # if the dhcp agent fails to assing ip, this line will not appear
316 if "inet addr:" + cidr_first_octet in console_log and not got_ip:
318 logger.debug("The instance '%s' succeeded to get the IP "
319 "from the dhcp agent." % NAME_VM_2)
321 # if dhcp doesnt work,it shows "No lease, failing".The test will fail
322 if "No lease, failing" in console_log and not nolease and not got_ip:
324 logger.debug("Console-log '%s': No lease, failing..."
326 logger.info("The instance failed to get an IP from the "
327 "DHCP agent. The test will probably timeout...")
329 if timeout == 0: # 300 sec timeout (5 min)
330 logger.error("Cannot establish connection to IP '%s'. Aborting"
336 def transfer_ping_script(ssh, floatip):
339 logger.info("Trying to transfer ping.sh to %s..." % floatip)
340 scp = SCPClient(ssh.get_transport())
342 ping_script = REPO_PATH + '/' + "testcases/OpenStack/vPing/ping.sh"
344 scp.put(ping_script, "~/")
346 logger.error("Cannot SCP the file '%s' to VM '%s'"
347 % (ping_script, floatip))
350 cmd = 'chmod 755 ~/ping.sh'
351 (stdin, stdout, stderr) = ssh.exec_command(cmd)
352 for line in stdout.readlines():
356 def do_vping_ssh(ssh, test_ip):
357 logger.info("Waiting for ping...")
360 cmd = '~/ping.sh ' + test_ip
365 (stdin, stdout, stderr) = ssh.exec_command(cmd)
366 output = stdout.readlines()
369 if "vPing OK" in line:
370 logger.info("vPing detected!")
375 elif sec == PING_TIMEOUT:
376 logger.info("Timeout reached.")
381 logger.debug("Pinging %s. Waiting for response..." % test_ip)
383 return EXIT_CODE, time.time()
386 def do_vping_userdata(vm, test_ip):
387 logger.info("Waiting for ping...")
394 console_log = vm.get_console_output()
395 if "vPing OK" in console_log:
396 logger.info("vPing detected!")
399 elif ("failed to read iid from metadata" in console_log or
403 elif sec == PING_TIMEOUT:
404 logger.info("Timeout reached.")
407 if "request failed" in console_log:
408 logger.debug("It seems userdata is not supported in "
409 "nova boot. Waiting a bit...")
412 logger.debug("Pinging %s. Waiting for response..." % test_ip)
415 return EXIT_CODE, time.time()
418 def do_vping(case, vm, test_ip):
419 if is_userdata(case):
420 return do_vping_userdata(vm, test_ip)
422 floatip = add_float_ip(vm)
423 ssh = establish_ssh(vm, floatip)
424 transfer_ping_script(ssh, floatip)
425 return do_vping_ssh(ssh, test_ip)
428 def check_result(code, start_time, stop_time):
431 logger.info("vPing OK")
432 duration = round(stop_time - start_time, 1)
433 logger.info("vPing duration:'%s'" % duration)
437 logger.info("Userdata is not supported in nova boot. Aborting test...")
440 logger.error("vPing FAILED")
442 details = {'timestart': start_time,
443 'duration': duration,
444 'status': test_status}
449 def push_result(report, case, start_time, stop_time, details):
452 logger.debug("Pushing vPing %s results into DB..." % case)
453 ft_utils.push_results_to_db('functest',
461 logger.error("Error pushing results into Database '%s'"