7 import functest.utils.functest_utils as ft_utils
8 import functest.utils.openstack_utils as os_utils
10 from scp import SCPClient
12 REPO_PATH = os.environ['repos_dir'] + '/functest/'
14 functest_yaml = ft_utils.get_functest_yaml()
16 NAME_VM_1 = functest_yaml.get("vping").get("vm_name_1")
17 NAME_VM_2 = functest_yaml.get("vping").get("vm_name_2")
20 VM_DELETE_TIMEOUT = 100
21 PING_TIMEOUT = functest_yaml.get("vping").get("ping_timeout")
23 GLANCE_IMAGE_NAME = functest_yaml.get("vping").get("image_name")
24 GLANCE_IMAGE_FILENAME = functest_yaml.get("general").get(
25 "openstack").get("image_file_name")
26 GLANCE_IMAGE_FORMAT = functest_yaml.get("general").get(
27 "openstack").get("image_disk_format")
28 GLANCE_IMAGE_PATH = functest_yaml.get("general").get("directories").get(
29 "dir_functest_data") + "/" + GLANCE_IMAGE_FILENAME
31 FLAVOR = functest_yaml.get("vping").get("vm_flavor")
33 # NEUTRON Private Network parameters
34 PRIVATE_NET_NAME = functest_yaml.get("vping").get(
35 "vping_private_net_name")
36 PRIVATE_SUBNET_NAME = functest_yaml.get("vping").get(
37 "vping_private_subnet_name")
38 PRIVATE_SUBNET_CIDR = functest_yaml.get("vping").get(
39 "vping_private_subnet_cidr")
40 ROUTER_NAME = functest_yaml.get("vping").get(
43 SECGROUP_NAME = functest_yaml.get("vping").get("vping_sg_name")
44 SECGROUP_DESCR = functest_yaml.get("vping").get("vping_sg_descr")
52 pp = pprint.PrettyPrinter(indent=4)
60 def check_repo_exist():
61 if not os.path.exists(REPO_PATH):
62 logger.error("Functest repository not found '%s'" % REPO_PATH)
74 def init(vping_logger):
76 nova_client = os_utils.get_nova_client()
78 neutron_client = os_utils.get_neutron_client()
80 glance_client = os_utils.get_glance_client()
85 def waitVmActive(nova, vm):
87 # sleep and wait for VM status change
89 count = VM_BOOT_TIMEOUT / sleep_time
91 status = os_utils.get_instance_status(nova, vm)
92 logger.debug("Status: %s" % status)
93 if status == "ACTIVE":
95 if status == "ERROR" or status == "error":
98 logger.debug("Booting a VM timed out...")
101 time.sleep(sleep_time)
105 def create_security_group():
106 sg_id = os_utils.get_security_group_id(neutron_client,
109 logger.info("Using existing security group '%s'..." % SECGROUP_NAME)
111 logger.info("Creating security group '%s'..." % SECGROUP_NAME)
112 SECGROUP = os_utils.create_security_group(neutron_client,
116 logger.error("Failed to create the security group...")
119 sg_id = SECGROUP['id']
121 logger.debug("Security group '%s' with ID=%s created successfully."
122 % (SECGROUP['name'], sg_id))
124 logger.debug("Adding ICMP rules in security group '%s'..."
126 if not os_utils.create_secgroup_rule(neutron_client, sg_id,
128 logger.error("Failed to create the security group rule...")
131 logger.debug("Adding SSH rules in security group '%s'..."
133 if not os_utils.create_secgroup_rule(neutron_client, sg_id,
136 logger.error("Failed to create the security group rule...")
139 if not os_utils.create_secgroup_rule(
140 neutron_client, sg_id, 'egress', 'tcp', '22', '22'):
141 logger.error("Failed to create the security group rule...")
147 _, image_id = os_utils.get_or_create_image(GLANCE_IMAGE_NAME,
159 # Check if the given flavor exists
161 flavor = nova_client.flavors.find(name=FLAVOR)
162 logger.info("Using existing Flavor '%s'..." % FLAVOR)
165 logger.error("Flavor '%s' not found." % FLAVOR)
166 logger.info("Available flavors are: ")
167 pMsg(nova_client.flavor.list())
171 def create_network_full():
174 network_dic = os_utils.create_network_full(neutron_client,
182 "There has been a problem when creating the neutron network")
184 network_id = network_dic["net_id"]
188 def delete_exist_vms():
189 servers = nova_client.servers.list()
190 for server in servers:
191 if server.name == NAME_VM_1 or server.name == NAME_VM_2:
192 logger.info("Instance %s found. Deleting..." % server.name)
196 def is_userdata(case):
197 return case == 'vping_userdata'
201 return case == 'vping_ssh'
204 def boot_vm(case, name, image_id, flavor, network_id, test_ip, sg_id):
208 config['name'] = name
209 config['flavor'] = flavor
210 config['image'] = image_id
211 config['nics'] = [{"net-id": network_id}]
212 if is_userdata(case):
213 config['config_drive'] = True
214 if name == NAME_VM_2:
217 " ping -c 1 %s 2>&1 >/dev/null\n"
219 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
227 config['userdata'] = u
229 logger.info("Creating instance '%s'..." % name)
230 logger.debug("Configuration: %s" % config)
231 vm = nova_client.servers.create(**config)
233 # wait until VM status is active
234 if not waitVmActive(nova_client, vm):
236 logger.error("Instance '%s' cannot be booted. Status is '%s'" % (
237 name, os_utils.get_instance_status(nova_client, vm)))
240 logger.info("Instance '%s' is ACTIVE." % name)
242 add_secgroup(name, vm.id, sg_id)
248 test_ip = vm.networks.get(PRIVATE_NET_NAME)[0]
249 logger.debug("Instance '%s' got %s" % (vm.name, test_ip))
253 def add_secgroup(vmname, vm_id, sg_id):
254 logger.info("Adding '%s' to security group '%s'..." %
255 (vmname, SECGROUP_NAME))
256 os_utils.add_secgroup_to_instance(nova_client, vm_id, sg_id)
259 def add_float_ip(vm):
262 logger.info("Creating floating IP for VM '%s'..." % NAME_VM_2)
263 floatip_dic = os_utils.create_floating_ip(neutron_client)
264 floatip = floatip_dic['fip_addr']
267 logger.error("Cannot create floating IP.")
269 logger.info("Floating IP created: '%s'" % floatip)
271 logger.info("Associating floating ip: '%s' to VM '%s' "
272 % (floatip, NAME_VM_2))
273 if not os_utils.add_floating_ip(nova_client, vm.id, floatip):
274 logger.error("Cannot associate floating IP to VM.")
280 def establish_ssh(vm, floatip):
283 logger.info("Trying to establish SSH connection to %s..." % floatip)
285 password = 'cubswin:)'
286 ssh = paramiko.SSHClient()
287 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
293 cidr_first_octet = PRIVATE_SUBNET_CIDR.split('.')[0]
296 ssh.connect(floatip, username=username,
297 password=password, timeout=2)
298 logger.debug("SSH connection established to %s." % floatip)
301 logger.debug("Waiting for %s..." % floatip)
305 console_log = vm.get_console_output()
307 # print each "Sending discover" captured on the console log
308 if (len(re.findall("Sending discover", console_log)) >
309 discover_count and not got_ip):
311 logger.debug("Console-log '%s': Sending discover..."
314 # check if eth0 got an ip,the line looks like this:
315 # "inet addr:192.168."....
316 # if the dhcp agent fails to assing ip, this line will not appear
317 if "inet addr:" + cidr_first_octet in console_log and not got_ip:
319 logger.debug("The instance '%s' succeeded to get the IP "
320 "from the dhcp agent." % NAME_VM_2)
322 # if dhcp doesnt work,it shows "No lease, failing".The test will fail
323 if "No lease, failing" in console_log and not nolease and not got_ip:
325 logger.debug("Console-log '%s': No lease, failing..."
327 logger.info("The instance failed to get an IP from the "
328 "DHCP agent. The test will probably timeout...")
330 if timeout == 0: # 300 sec timeout (5 min)
331 logger.error("Cannot establish connection to IP '%s'. Aborting"
337 def transfer_ping_script(ssh, floatip):
340 logger.info("Trying to transfer ping.sh to %s..." % floatip)
341 scp = SCPClient(ssh.get_transport())
343 ping_script = REPO_PATH + "testcases/OpenStack/vPing/ping.sh"
345 scp.put(ping_script, "~/")
347 logger.error("Cannot SCP the file '%s' to VM '%s'"
348 % (ping_script, floatip))
351 cmd = 'chmod 755 ~/ping.sh'
352 (stdin, stdout, stderr) = ssh.exec_command(cmd)
353 for line in stdout.readlines():
357 def do_vping_ssh(ssh, test_ip):
358 logger.info("Waiting for ping...")
361 cmd = '~/ping.sh ' + test_ip
366 (stdin, stdout, stderr) = ssh.exec_command(cmd)
367 output = stdout.readlines()
370 if "vPing OK" in line:
371 logger.info("vPing detected!")
376 elif sec == PING_TIMEOUT:
377 logger.info("Timeout reached.")
382 logger.debug("Pinging %s. Waiting for response..." % test_ip)
384 return EXIT_CODE, time.time()
387 def do_vping_userdata(vm, test_ip):
388 logger.info("Waiting for ping...")
395 console_log = vm.get_console_output()
396 if "vPing OK" in console_log:
397 logger.info("vPing detected!")
400 elif ("failed to read iid from metadata" in console_log or
404 elif sec == PING_TIMEOUT:
405 logger.info("Timeout reached.")
408 if "request failed" in console_log:
409 logger.debug("It seems userdata is not supported in "
410 "nova boot. Waiting a bit...")
413 logger.debug("Pinging %s. Waiting for response..." % test_ip)
416 return EXIT_CODE, time.time()
419 def do_vping(case, vm, test_ip):
420 if is_userdata(case):
421 return do_vping_userdata(vm, test_ip)
423 floatip = add_float_ip(vm)
424 ssh = establish_ssh(vm, floatip)
425 transfer_ping_script(ssh, floatip)
426 return do_vping_ssh(ssh, test_ip)
429 def check_result(code, start_time, stop_time):
432 logger.info("vPing OK")
433 duration = round(stop_time - start_time, 1)
434 logger.info("vPing duration:'%s'" % duration)
438 logger.info("Userdata is not supported in nova boot. Aborting test...")
441 logger.error("vPing FAILED")
443 details = {'timestart': start_time,
444 'duration': duration,
445 'status': test_status}
450 def push_result(report, case, start_time, stop_time, details):
453 logger.debug("Pushing vPing %s results into DB..." % case)
454 ft_utils.push_results_to_db('functest',
462 logger.error("Error pushing results into Database '%s'"