3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # 0.1: This script boots the VM1 and allocates IP address from Nova
11 # Later, the VM2 boots then execute cloud-init to ping VM1.
12 # After successful ping, both the VMs are deleted.
13 # 0.2: measure test duration and publish results under json format
14 # 0.3: adapt push 2 DB after Test API refacroting
24 import functest.utils.functest_logger as ft_logger
25 import functest.utils.functest_utils as functest_utils
26 import functest.utils.openstack_utils as os_utils
30 pp = pprint.PrettyPrinter(indent=4)
32 parser = argparse.ArgumentParser()
35 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
36 parser.add_argument("-r", "--report",
37 help="Create json result file",
40 args = parser.parse_args()
42 """ logging configuration """
43 logger = ft_logger.Logger("vping_userdata").getLogger()
45 REPO_PATH = os.environ['repos_dir'] + '/functest/'
46 if not os.path.exists(REPO_PATH):
47 logger.error("Functest repository directory not found '%s'" % REPO_PATH)
50 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
51 functest_yaml = yaml.safe_load(f)
54 HOME = os.environ['HOME'] + "/"
57 VM_DELETE_TIMEOUT = 100
58 PING_TIMEOUT = functest_yaml.get("vping").get("ping_timeout")
59 TEST_DB = functest_yaml.get("results").get("test_db_url")
60 NAME_VM_1 = functest_yaml.get("vping").get("vm_name_1")
61 NAME_VM_2 = functest_yaml.get("vping").get("vm_name_2")
62 GLANCE_IMAGE_NAME = functest_yaml.get("vping").get("image_name")
63 GLANCE_IMAGE_FILENAME = functest_yaml.get("general").get(
64 "openstack").get("image_file_name")
65 GLANCE_IMAGE_FORMAT = functest_yaml.get("general").get(
66 "openstack").get("image_disk_format")
67 GLANCE_IMAGE_PATH = functest_yaml.get("general").get("directories").get(
68 "dir_functest_data") + "/" + GLANCE_IMAGE_FILENAME
71 FLAVOR = functest_yaml.get("vping").get("vm_flavor")
73 # NEUTRON Private Network parameters
75 PRIVATE_NET_NAME = functest_yaml.get("vping").get(
76 "vping_private_net_name")
77 PRIVATE_SUBNET_NAME = functest_yaml.get("vping").get(
78 "vping_private_subnet_name")
79 PRIVATE_SUBNET_CIDR = functest_yaml.get("vping").get(
80 "vping_private_subnet_cidr")
81 ROUTER_NAME = functest_yaml.get("vping").get("vping_router_name")
83 SECGROUP_NAME = functest_yaml.get("vping").get("vping_sg_name")
84 SECGROUP_DESCR = functest_yaml.get("vping").get("vping_sg_descr")
92 def waitVmActive(nova, vm):
94 # sleep and wait for VM status change
96 count = VM_BOOT_TIMEOUT / sleep_time
98 status = os_utils.get_instance_status(nova, vm)
99 logger.debug("Status: %s" % status)
100 if status == "ACTIVE":
102 if status == "ERROR" or status == "error":
105 logger.debug("Booting a VM timed out...")
108 time.sleep(sleep_time)
112 def waitVmDeleted(nova, vm):
114 # sleep and wait for VM status change
116 count = VM_DELETE_TIMEOUT / sleep_time
118 status = os_utils.get_instance_status(nova, vm)
122 logger.debug("Timeout")
127 time.sleep(sleep_time)
131 def create_security_group(neutron_client):
132 sg_id = os_utils.get_security_group_id(neutron_client,
135 logger.info("Using existing security group '%s'..." % SECGROUP_NAME)
137 logger.info("Creating security group '%s'..." % SECGROUP_NAME)
138 SECGROUP = os_utils.create_security_group(neutron_client,
142 logger.error("Failed to create the security group...")
145 sg_id = SECGROUP['id']
147 logger.debug("Security group '%s' with ID=%s created successfully."
148 % (SECGROUP['name'], sg_id))
150 logger.debug("Adding ICMP rules in security group '%s'..."
152 if not os_utils.create_secgroup_rule(neutron_client, sg_id,
154 logger.error("Failed to create the security group rule...")
157 logger.debug("Adding SSH rules in security group '%s'..."
159 if not os_utils.create_secgroup_rule(neutron_client, sg_id,
162 logger.error("Failed to create the security group rule...")
165 if not os_utils.create_secgroup_rule(neutron_client, sg_id,
168 logger.error("Failed to create the security group rule...")
175 nova_client = os_utils.get_nova_client()
176 neutron_client = os_utils.get_neutron_client()
177 glance_client = os_utils.get_glance_client()
184 # Check if the given image exists
185 image_id = os_utils.get_image_id(glance_client, GLANCE_IMAGE_NAME)
187 logger.info("Using existing image '%s'..." % GLANCE_IMAGE_NAME)
191 logger.info("Creating image '%s' from '%s'..." % (GLANCE_IMAGE_NAME,
193 image_id = os_utils.create_glance_image(glance_client,
198 logger.error("Failed to create a Glance image...")
200 logger.debug("Image '%s' with ID=%s created successfully."
201 % (GLANCE_IMAGE_NAME, image_id))
203 network_dic = os_utils.create_network_full(neutron_client,
210 "There has been a problem when creating the neutron network")
212 network_id = network_dic["net_id"]
214 create_security_group(neutron_client)
216 # Check if the given flavor exists
218 flavor = nova_client.flavors.find(name=FLAVOR)
219 logger.info("Flavor found '%s'" % FLAVOR)
221 logger.error("Flavor '%s' not found." % FLAVOR)
222 logger.info("Available flavors are: ")
223 pMsg(nova_client.flavor.list())
226 # Deleting instances if they exist
227 servers = nova_client.servers.list()
228 for server in servers:
229 if server.name == NAME_VM_1 or server.name == NAME_VM_2:
230 logger.info("Instance %s found. Deleting..." % server.name)
235 # tune (e.g. flavor, images, network) to your specific
236 # openstack configuration here
237 # we consider start time at VM1 booting
238 start_time = time.time()
239 stop_time = start_time
240 logger.info("vPing Start Time:'%s'" % (
241 datetime.datetime.fromtimestamp(start_time).strftime(
242 '%Y-%m-%d %H:%M:%S')))
245 logger.info("Creating instance '%s'..." % NAME_VM_1)
247 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
248 "network=%s \n" % (NAME_VM_1, flavor, image_id, network_id))
249 vm1 = nova_client.servers.create(
254 nics=[{"net-id": network_id}]
257 # wait until VM status is active
258 if not waitVmActive(nova_client, vm1):
260 logger.error("Instance '%s' cannot be booted. Status is '%s'" % (
261 NAME_VM_1, os_utils.get_instance_status(nova_client, vm1)))
264 logger.info("Instance '%s' is ACTIVE." % NAME_VM_1)
266 # Retrieve IP of first VM
267 test_ip = vm1.networks.get(PRIVATE_NET_NAME)[0]
268 logger.debug("Instance '%s' got %s" % (NAME_VM_1, test_ip))
271 # we will boot then execute a ping script with cloud-init
272 # the long chain corresponds to the ping procedure converted with base 64
273 # tune (e.g. flavor, images, network) to your specific openstack
275 u = ("#!/bin/sh\n\nwhile true; do\n ping -c 1 %s 2>&1 >/dev/null\n "
276 "RES=$?\n if [ \"Z$RES\" = \"Z0\" ] ; then\n echo 'vPing OK'\n "
277 "break\n else\n echo 'vPing KO'\n fi\n sleep 1\ndone\n" % test_ip)
280 logger.info("Creating instance '%s'..." % NAME_VM_2)
282 "Configuration:\n name=%s \n flavor=%s \n image=%s \n network=%s "
283 "\n userdata= \n%s" % (
284 NAME_VM_2, flavor, image_id, network_id, u))
285 vm2 = nova_client.servers.create(
289 nics=[{"net-id": network_id}],
294 if not waitVmActive(nova_client, vm2):
295 logger.error("Instance '%s' cannot be booted. Status is '%s'" % (
296 NAME_VM_2, os_utils.get_instance_status(nova_client, vm2)))
299 logger.info("Instance '%s' is ACTIVE." % NAME_VM_2)
301 logger.info("Waiting for ping...")
304 console_log = vm2.get_console_output()
306 stop_time = time.time()
310 console_log = vm2.get_console_output()
311 # print "--"+console_log
312 # report if the test is failed
313 if "vPing OK" in console_log:
314 logger.info("vPing detected!")
316 # we consider start time at VM1 booting
317 stop_time = time.time()
318 duration = round(stop_time - start_time, 1)
319 logger.info("vPing duration:'%s'" % duration)
322 elif ("failed to read iid from metadata" in console_log or
326 elif sec == PING_TIMEOUT:
327 logger.info("Timeout reached.")
330 if "request failed" in console_log:
331 logger.debug("It seems userdata is not supported in "
332 "nova boot. Waiting a bit...")
335 logger.debug("Pinging %s. Waiting for response..." % test_ip)
340 logger.info("vPing OK")
342 elif EXIT_CODE == -2:
344 logger.info("Userdata is not supported in nova boot. Aborting test...")
347 logger.error("vPing FAILED")
351 logger.debug("Pushing vPing userdata results into DB...")
352 functest_utils.push_results_to_db("functest",
358 details={'timestart': start_time,
359 'duration': duration,
360 'status': test_status})
362 logger.error("Error pushing results into Database '%s'"
367 if __name__ == '__main__':