7 import functest.utils.functest_utils as ft_utils
8 import functest.utils.openstack_utils as os_utils
10 from scp import SCPClient
13 REPO_PATH = os.environ['repos_dir'] + '/functest/'
15 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
16 functest_yaml = yaml.safe_load(f)
19 NAME_VM_1 = functest_yaml.get("vping").get("vm_name_1")
20 NAME_VM_2 = functest_yaml.get("vping").get("vm_name_2")
23 VM_DELETE_TIMEOUT = 100
24 PING_TIMEOUT = functest_yaml.get("vping").get("ping_timeout")
26 GLANCE_IMAGE_NAME = functest_yaml.get("vping").get("image_name")
27 GLANCE_IMAGE_FILENAME = functest_yaml.get("general").get(
28 "openstack").get("image_file_name")
29 GLANCE_IMAGE_FORMAT = functest_yaml.get("general").get(
30 "openstack").get("image_disk_format")
31 GLANCE_IMAGE_PATH = functest_yaml.get("general").get("directories").get(
32 "dir_functest_data") + "/" + GLANCE_IMAGE_FILENAME
34 FLAVOR = functest_yaml.get("vping").get("vm_flavor")
36 # NEUTRON Private Network parameters
37 PRIVATE_NET_NAME = functest_yaml.get("vping").get(
38 "vping_private_net_name")
39 PRIVATE_SUBNET_NAME = functest_yaml.get("vping").get(
40 "vping_private_subnet_name")
41 PRIVATE_SUBNET_CIDR = functest_yaml.get("vping").get(
42 "vping_private_subnet_cidr")
43 ROUTER_NAME = functest_yaml.get("vping").get(
46 SECGROUP_NAME = functest_yaml.get("vping").get("vping_sg_name")
47 SECGROUP_DESCR = functest_yaml.get("vping").get("vping_sg_descr")
55 pp = pprint.PrettyPrinter(indent=4)
63 def check_repo_exist():
64 if not os.path.exists(REPO_PATH):
65 logger.error("Functest repository not found '%s'" % REPO_PATH)
77 def init(vping_logger):
79 nova_client = os_utils.get_nova_client()
81 neutron_client = os_utils.get_neutron_client()
83 glance_client = os_utils.get_glance_client()
88 def waitVmActive(nova, vm):
90 # sleep and wait for VM status change
92 count = VM_BOOT_TIMEOUT / sleep_time
94 status = os_utils.get_instance_status(nova, vm)
95 logger.debug("Status: %s" % status)
96 if status == "ACTIVE":
98 if status == "ERROR" or status == "error":
101 logger.debug("Booting a VM timed out...")
104 time.sleep(sleep_time)
108 def create_security_group():
109 sg_id = os_utils.get_security_group_id(neutron_client,
112 logger.info("Using existing security group '%s'..." % SECGROUP_NAME)
114 logger.info("Creating security group '%s'..." % SECGROUP_NAME)
115 SECGROUP = os_utils.create_security_group(neutron_client,
119 logger.error("Failed to create the security group...")
122 sg_id = SECGROUP['id']
124 logger.debug("Security group '%s' with ID=%s created successfully."
125 % (SECGROUP['name'], sg_id))
127 logger.debug("Adding ICMP rules in security group '%s'..."
129 if not os_utils.create_secgroup_rule(neutron_client, sg_id,
131 logger.error("Failed to create the security group rule...")
134 logger.debug("Adding SSH rules in security group '%s'..."
136 if not os_utils.create_secgroup_rule(neutron_client, sg_id,
139 logger.error("Failed to create the security group rule...")
142 if not os_utils.create_secgroup_rule(
143 neutron_client, sg_id, 'egress', 'tcp', '22', '22'):
144 logger.error("Failed to create the security group rule...")
152 # Check if the given image exists
153 image_id = os_utils.get_image_id(glance_client, GLANCE_IMAGE_NAME)
155 logger.info("Using existing image '%s'..." % GLANCE_IMAGE_NAME)
158 logger.info("Creating image '%s' from '%s'..." % (GLANCE_IMAGE_NAME,
160 image_id = os_utils.create_glance_image(glance_client,
165 logger.error("Failed to create a Glance image...")
167 logger.debug("Image '%s' with ID=%s created successfully."
168 % (GLANCE_IMAGE_NAME, image_id))
171 return image_exists, image_id
177 # Check if the given flavor exists
179 flavor = nova_client.flavors.find(name=FLAVOR)
180 logger.info("Using existing Flavor '%s'..." % FLAVOR)
183 logger.error("Flavor '%s' not found." % FLAVOR)
184 logger.info("Available flavors are: ")
185 pMsg(nova_client.flavor.list())
189 def create_network_full():
192 network_dic = os_utils.create_network_full(neutron_client,
200 "There has been a problem when creating the neutron network")
202 network_id = network_dic["net_id"]
206 def delete_exist_vms():
207 servers = nova_client.servers.list()
208 for server in servers:
209 if server.name == NAME_VM_1 or server.name == NAME_VM_2:
210 logger.info("Instance %s found. Deleting..." % server.name)
214 def is_userdata(case):
215 return case == 'vping_userdata'
219 return case == 'vping_ssh'
222 def boot_vm(case, name, image_id, flavor, network_id, test_ip, sg_id):
226 config['name'] = name
227 config['flavor'] = flavor
228 config['image'] = image_id
229 config['nics'] = [{"net-id": network_id}]
230 if is_userdata(case):
231 config['config_drive'] = True
232 if name == NAME_VM_2:
235 " ping -c 1 %s 2>&1 >/dev/null\n"
237 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
245 config['userdata'] = u
247 logger.info("Creating instance '%s'..." % name)
248 logger.debug("Configuration: %s" % config)
249 vm = nova_client.servers.create(**config)
251 # wait until VM status is active
252 if not waitVmActive(nova_client, vm):
254 logger.error("Instance '%s' cannot be booted. Status is '%s'" % (
255 name, os_utils.get_instance_status(nova_client, vm)))
258 logger.info("Instance '%s' is ACTIVE." % name)
260 add_secgroup(name, vm.id, sg_id)
266 test_ip = vm.networks.get(PRIVATE_NET_NAME)[0]
267 logger.debug("Instance '%s' got %s" % (vm.name, test_ip))
271 def add_secgroup(vmname, vm_id, sg_id):
272 logger.info("Adding '%s' to security group '%s'..." %
273 (vmname, SECGROUP_NAME))
274 os_utils.add_secgroup_to_instance(nova_client, vm_id, sg_id)
277 def add_float_ip(vm):
280 logger.info("Creating floating IP for VM '%s'..." % NAME_VM_2)
281 floatip_dic = os_utils.create_floating_ip(neutron_client)
282 floatip = floatip_dic['fip_addr']
285 logger.error("Cannot create floating IP.")
287 logger.info("Floating IP created: '%s'" % floatip)
289 logger.info("Associating floating ip: '%s' to VM '%s' "
290 % (floatip, NAME_VM_2))
291 if not os_utils.add_floating_ip(nova_client, vm.id, floatip):
292 logger.error("Cannot associate floating IP to VM.")
298 def establish_ssh(vm, floatip):
301 logger.info("Trying to establish SSH connection to %s..." % floatip)
303 password = 'cubswin:)'
304 ssh = paramiko.SSHClient()
305 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
311 cidr_first_octet = PRIVATE_SUBNET_CIDR.split('.')[0]
314 ssh.connect(floatip, username=username,
315 password=password, timeout=2)
316 logger.debug("SSH connection established to %s." % floatip)
319 logger.debug("Waiting for %s..." % floatip)
323 console_log = vm.get_console_output()
325 # print each "Sending discover" captured on the console log
326 if (len(re.findall("Sending discover", console_log)) >
327 discover_count and not got_ip):
329 logger.debug("Console-log '%s': Sending discover..."
332 # check if eth0 got an ip,the line looks like this:
333 # "inet addr:192.168."....
334 # if the dhcp agent fails to assing ip, this line will not appear
335 if "inet addr:" + cidr_first_octet in console_log and not got_ip:
337 logger.debug("The instance '%s' succeeded to get the IP "
338 "from the dhcp agent." % NAME_VM_2)
340 # if dhcp doesnt work,it shows "No lease, failing".The test will fail
341 if "No lease, failing" in console_log and not nolease and not got_ip:
343 logger.debug("Console-log '%s': No lease, failing..."
345 logger.info("The instance failed to get an IP from the "
346 "DHCP agent. The test will probably timeout...")
348 if timeout == 0: # 300 sec timeout (5 min)
349 logger.error("Cannot establish connection to IP '%s'. Aborting"
355 def transfer_ping_script(ssh, floatip):
358 logger.info("Trying to transfer ping.sh to %s..." % floatip)
359 scp = SCPClient(ssh.get_transport())
361 ping_script = REPO_PATH + "testcases/OpenStack/vPing/ping.sh"
363 scp.put(ping_script, "~/")
365 logger.error("Cannot SCP the file '%s' to VM '%s'"
366 % (ping_script, floatip))
369 cmd = 'chmod 755 ~/ping.sh'
370 (stdin, stdout, stderr) = ssh.exec_command(cmd)
371 for line in stdout.readlines():
375 def do_vping_ssh(ssh, test_ip):
376 logger.info("Waiting for ping...")
379 cmd = '~/ping.sh ' + test_ip
384 (stdin, stdout, stderr) = ssh.exec_command(cmd)
385 output = stdout.readlines()
388 if "vPing OK" in line:
389 logger.info("vPing detected!")
394 elif sec == PING_TIMEOUT:
395 logger.info("Timeout reached.")
400 logger.debug("Pinging %s. Waiting for response..." % test_ip)
402 return EXIT_CODE, time.time()
405 def do_vping_userdata(vm, test_ip):
406 logger.info("Waiting for ping...")
413 console_log = vm.get_console_output()
414 if "vPing OK" in console_log:
415 logger.info("vPing detected!")
418 elif ("failed to read iid from metadata" in console_log or
422 elif sec == PING_TIMEOUT:
423 logger.info("Timeout reached.")
426 if "request failed" in console_log:
427 logger.debug("It seems userdata is not supported in "
428 "nova boot. Waiting a bit...")
431 logger.debug("Pinging %s. Waiting for response..." % test_ip)
434 return EXIT_CODE, time.time()
437 def do_vping(case, vm, test_ip):
438 if is_userdata(case):
439 return do_vping_userdata(vm, test_ip)
441 floatip = add_float_ip(vm)
442 ssh = establish_ssh(vm, floatip)
443 transfer_ping_script(ssh, floatip)
444 return do_vping_ssh(ssh, test_ip)
447 def check_result(code, start_time, stop_time):
450 logger.info("vPing OK")
451 duration = round(stop_time - start_time, 1)
452 logger.info("vPing duration:'%s'" % duration)
456 logger.info("Userdata is not supported in nova boot. Aborting test...")
459 logger.error("vPing FAILED")
461 details = {'timestart': start_time,
462 'duration': duration,
463 'status': test_status}
468 def push_result(report, case, start_time, stop_time, details):
471 logger.debug("Pushing vPing %s results into DB..." % case)
472 ft_utils.push_results_to_db('functest',
480 logger.error("Error pushing results into Database '%s'"