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...")
150 _, image_id = os_utils.get_or_create_image(GLANCE_IMAGE_NAME,
162 # Check if the given flavor exists
164 flavor = nova_client.flavors.find(name=FLAVOR)
165 logger.info("Using existing Flavor '%s'..." % FLAVOR)
168 logger.error("Flavor '%s' not found." % FLAVOR)
169 logger.info("Available flavors are: ")
170 pMsg(nova_client.flavor.list())
174 def create_network_full():
177 network_dic = os_utils.create_network_full(neutron_client,
185 "There has been a problem when creating the neutron network")
187 network_id = network_dic["net_id"]
191 def delete_exist_vms():
192 servers = nova_client.servers.list()
193 for server in servers:
194 if server.name == NAME_VM_1 or server.name == NAME_VM_2:
195 logger.info("Instance %s found. Deleting..." % server.name)
199 def is_userdata(case):
200 return case == 'vping_userdata'
204 return case == 'vping_ssh'
207 def boot_vm(case, name, image_id, flavor, network_id, test_ip, sg_id):
211 config['name'] = name
212 config['flavor'] = flavor
213 config['image'] = image_id
214 config['nics'] = [{"net-id": network_id}]
215 if is_userdata(case):
216 config['config_drive'] = True
217 if name == NAME_VM_2:
220 " ping -c 1 %s 2>&1 >/dev/null\n"
222 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
230 config['userdata'] = u
232 logger.info("Creating instance '%s'..." % name)
233 logger.debug("Configuration: %s" % config)
234 vm = nova_client.servers.create(**config)
236 # wait until VM status is active
237 if not waitVmActive(nova_client, vm):
239 logger.error("Instance '%s' cannot be booted. Status is '%s'" % (
240 name, os_utils.get_instance_status(nova_client, vm)))
243 logger.info("Instance '%s' is ACTIVE." % name)
245 add_secgroup(name, vm.id, sg_id)
251 test_ip = vm.networks.get(PRIVATE_NET_NAME)[0]
252 logger.debug("Instance '%s' got %s" % (vm.name, test_ip))
256 def add_secgroup(vmname, vm_id, sg_id):
257 logger.info("Adding '%s' to security group '%s'..." %
258 (vmname, SECGROUP_NAME))
259 os_utils.add_secgroup_to_instance(nova_client, vm_id, sg_id)
262 def add_float_ip(vm):
265 logger.info("Creating floating IP for VM '%s'..." % NAME_VM_2)
266 floatip_dic = os_utils.create_floating_ip(neutron_client)
267 floatip = floatip_dic['fip_addr']
270 logger.error("Cannot create floating IP.")
272 logger.info("Floating IP created: '%s'" % floatip)
274 logger.info("Associating floating ip: '%s' to VM '%s' "
275 % (floatip, NAME_VM_2))
276 if not os_utils.add_floating_ip(nova_client, vm.id, floatip):
277 logger.error("Cannot associate floating IP to VM.")
283 def establish_ssh(vm, floatip):
286 logger.info("Trying to establish SSH connection to %s..." % floatip)
288 password = 'cubswin:)'
289 ssh = paramiko.SSHClient()
290 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
296 cidr_first_octet = PRIVATE_SUBNET_CIDR.split('.')[0]
299 ssh.connect(floatip, username=username,
300 password=password, timeout=2)
301 logger.debug("SSH connection established to %s." % floatip)
304 logger.debug("Waiting for %s..." % floatip)
308 console_log = vm.get_console_output()
310 # print each "Sending discover" captured on the console log
311 if (len(re.findall("Sending discover", console_log)) >
312 discover_count and not got_ip):
314 logger.debug("Console-log '%s': Sending discover..."
317 # check if eth0 got an ip,the line looks like this:
318 # "inet addr:192.168."....
319 # if the dhcp agent fails to assing ip, this line will not appear
320 if "inet addr:" + cidr_first_octet in console_log and not got_ip:
322 logger.debug("The instance '%s' succeeded to get the IP "
323 "from the dhcp agent." % NAME_VM_2)
325 # if dhcp doesnt work,it shows "No lease, failing".The test will fail
326 if "No lease, failing" in console_log and not nolease and not got_ip:
328 logger.debug("Console-log '%s': No lease, failing..."
330 logger.info("The instance failed to get an IP from the "
331 "DHCP agent. The test will probably timeout...")
333 if timeout == 0: # 300 sec timeout (5 min)
334 logger.error("Cannot establish connection to IP '%s'. Aborting"
340 def transfer_ping_script(ssh, floatip):
343 logger.info("Trying to transfer ping.sh to %s..." % floatip)
344 scp = SCPClient(ssh.get_transport())
346 ping_script = REPO_PATH + "testcases/OpenStack/vPing/ping.sh"
348 scp.put(ping_script, "~/")
350 logger.error("Cannot SCP the file '%s' to VM '%s'"
351 % (ping_script, floatip))
354 cmd = 'chmod 755 ~/ping.sh'
355 (stdin, stdout, stderr) = ssh.exec_command(cmd)
356 for line in stdout.readlines():
360 def do_vping_ssh(ssh, test_ip):
361 logger.info("Waiting for ping...")
364 cmd = '~/ping.sh ' + test_ip
369 (stdin, stdout, stderr) = ssh.exec_command(cmd)
370 output = stdout.readlines()
373 if "vPing OK" in line:
374 logger.info("vPing detected!")
379 elif sec == PING_TIMEOUT:
380 logger.info("Timeout reached.")
385 logger.debug("Pinging %s. Waiting for response..." % test_ip)
387 return EXIT_CODE, time.time()
390 def do_vping_userdata(vm, test_ip):
391 logger.info("Waiting for ping...")
398 console_log = vm.get_console_output()
399 if "vPing OK" in console_log:
400 logger.info("vPing detected!")
403 elif ("failed to read iid from metadata" in console_log or
407 elif sec == PING_TIMEOUT:
408 logger.info("Timeout reached.")
411 if "request failed" in console_log:
412 logger.debug("It seems userdata is not supported in "
413 "nova boot. Waiting a bit...")
416 logger.debug("Pinging %s. Waiting for response..." % test_ip)
419 return EXIT_CODE, time.time()
422 def do_vping(case, vm, test_ip):
423 if is_userdata(case):
424 return do_vping_userdata(vm, test_ip)
426 floatip = add_float_ip(vm)
427 ssh = establish_ssh(vm, floatip)
428 transfer_ping_script(ssh, floatip)
429 return do_vping_ssh(ssh, test_ip)
432 def check_result(code, start_time, stop_time):
435 logger.info("vPing OK")
436 duration = round(stop_time - start_time, 1)
437 logger.info("vPing duration:'%s'" % duration)
441 logger.info("Userdata is not supported in nova boot. Aborting test...")
444 logger.error("vPing FAILED")
446 details = {'timestart': start_time,
447 'duration': duration,
448 'status': test_status}
453 def push_result(report, case, start_time, stop_time, details):
456 logger.debug("Pushing vPing %s results into DB..." % case)
457 ft_utils.push_results_to_db('functest',
465 logger.error("Error pushing results into Database '%s'"