Merge "Colorado Release note"
[functest.git] / testcases / OpenStack / vPing / vPing_userdata.py
1 #!/usr/bin/python
2 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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
15 #
16 #
17
18 import argparse
19 import datetime
20 import os
21 import pprint
22 import sys
23 import time
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
27 import yaml
28
29
30 pp = pprint.PrettyPrinter(indent=4)
31
32 parser = argparse.ArgumentParser()
33 image_exists = False
34
35 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
36 parser.add_argument("-r", "--report",
37                     help="Create json result file",
38                     action="store_true")
39
40 args = parser.parse_args()
41
42 """ logging configuration """
43 logger = ft_logger.Logger("vping_userdata").getLogger()
44
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)
48     exit(-1)
49
50 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
51     functest_yaml = yaml.safe_load(f)
52 f.close()
53
54 HOME = os.environ['HOME'] + "/"
55 # vPing parameters
56 VM_BOOT_TIMEOUT = 180
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
69
70
71 FLAVOR = functest_yaml.get("vping").get("vm_flavor")
72
73 # NEUTRON Private Network parameters
74
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")
82
83 SECGROUP_NAME = functest_yaml.get("vping").get("vping_sg_name")
84 SECGROUP_DESCR = functest_yaml.get("vping").get("vping_sg_descr")
85
86
87 def pMsg(value):
88     """pretty printing"""
89     pp.pprint(value)
90
91
92 def waitVmActive(nova, vm):
93
94     # sleep and wait for VM status change
95     sleep_time = 3
96     count = VM_BOOT_TIMEOUT / sleep_time
97     while True:
98         status = os_utils.get_instance_status(nova, vm)
99         logger.debug("Status: %s" % status)
100         if status == "ACTIVE":
101             return True
102         if status == "ERROR" or status == "error":
103             return False
104         if count == 0:
105             logger.debug("Booting a VM timed out...")
106             return False
107         count -= 1
108         time.sleep(sleep_time)
109     return False
110
111
112 def waitVmDeleted(nova, vm):
113
114     # sleep and wait for VM status change
115     sleep_time = 3
116     count = VM_DELETE_TIMEOUT / sleep_time
117     while True:
118         status = os_utils.get_instance_status(nova, vm)
119         if not status:
120             return True
121         elif count == 0:
122             logger.debug("Timeout")
123             return False
124         else:
125             # return False
126             count -= 1
127         time.sleep(sleep_time)
128     return False
129
130
131 def create_security_group(neutron_client):
132     sg_id = os_utils.get_security_group_id(neutron_client,
133                                            SECGROUP_NAME)
134     if sg_id != '':
135         logger.info("Using existing security group '%s'..." % SECGROUP_NAME)
136     else:
137         logger.info("Creating security group  '%s'..." % SECGROUP_NAME)
138         SECGROUP = os_utils.create_security_group(neutron_client,
139                                                   SECGROUP_NAME,
140                                                   SECGROUP_DESCR)
141         if not SECGROUP:
142             logger.error("Failed to create the security group...")
143             return False
144
145         sg_id = SECGROUP['id']
146
147         logger.debug("Security group '%s' with ID=%s created successfully."
148                      % (SECGROUP['name'], sg_id))
149
150         logger.debug("Adding ICMP rules in security group '%s'..."
151                      % SECGROUP_NAME)
152         if not os_utils.create_secgroup_rule(neutron_client, sg_id,
153                                              'ingress', 'icmp'):
154             logger.error("Failed to create the security group rule...")
155             return False
156
157         logger.debug("Adding SSH rules in security group '%s'..."
158                      % SECGROUP_NAME)
159         if not os_utils.create_secgroup_rule(neutron_client, sg_id,
160                                              'ingress', 'tcp',
161                                              '22', '22'):
162             logger.error("Failed to create the security group rule...")
163             return False
164
165         if not os_utils.create_secgroup_rule(neutron_client, sg_id,
166                                              'egress', 'tcp',
167                                              '22', '22'):
168             logger.error("Failed to create the security group rule...")
169             return False
170     return sg_id
171
172
173 def main():
174
175     nova_client = os_utils.get_nova_client()
176     neutron_client = os_utils.get_neutron_client()
177     glance_client = os_utils.get_glance_client()
178
179     EXIT_CODE = -1
180
181     image_id = None
182     flavor = None
183
184     # Check if the given image exists
185     image_id = os_utils.get_image_id(glance_client, GLANCE_IMAGE_NAME)
186     if image_id != '':
187         logger.info("Using existing image '%s'..." % GLANCE_IMAGE_NAME)
188         global image_exists
189         image_exists = True
190     else:
191         logger.info("Creating image '%s' from '%s'..." % (GLANCE_IMAGE_NAME,
192                                                           GLANCE_IMAGE_PATH))
193         image_id = os_utils.create_glance_image(glance_client,
194                                                 GLANCE_IMAGE_NAME,
195                                                 GLANCE_IMAGE_PATH,
196                                                 GLANCE_IMAGE_FORMAT)
197         if not image_id:
198             logger.error("Failed to create a Glance image...")
199             exit(EXIT_CODE)
200         logger.debug("Image '%s' with ID=%s created successfully."
201                      % (GLANCE_IMAGE_NAME, image_id))
202
203     network_dic = os_utils.create_network_full(neutron_client,
204                                                PRIVATE_NET_NAME,
205                                                PRIVATE_SUBNET_NAME,
206                                                ROUTER_NAME,
207                                                PRIVATE_SUBNET_CIDR)
208     if not network_dic:
209         logger.error(
210             "There has been a problem when creating the neutron network")
211         exit(EXIT_CODE)
212     network_id = network_dic["net_id"]
213
214     create_security_group(neutron_client)
215
216     # Check if the given flavor exists
217     try:
218         flavor = nova_client.flavors.find(name=FLAVOR)
219         logger.info("Flavor found '%s'" % FLAVOR)
220     except:
221         logger.error("Flavor '%s' not found." % FLAVOR)
222         logger.info("Available flavors are: ")
223         pMsg(nova_client.flavor.list())
224         exit(-1)
225
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)
231             server.delete()
232
233     # boot VM 1
234     # basic boot
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')))
243
244     # create VM
245     logger.info("Creating instance '%s'..." % NAME_VM_1)
246     logger.debug(
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(
250         name=NAME_VM_1,
251         flavor=flavor,
252         image=image_id,
253         config_drive=True,
254         nics=[{"net-id": network_id}]
255     )
256
257     # wait until VM status is active
258     if not waitVmActive(nova_client, vm1):
259
260         logger.error("Instance '%s' cannot be booted. Status is '%s'" % (
261             NAME_VM_1, os_utils.get_instance_status(nova_client, vm1)))
262         exit(EXIT_CODE)
263     else:
264         logger.info("Instance '%s' is ACTIVE." % NAME_VM_1)
265
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))
269
270     # boot VM 2
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
274     #  configuration here
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)
278
279     # create VM
280     logger.info("Creating instance '%s'..." % NAME_VM_2)
281     logger.debug(
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(
286         name=NAME_VM_2,
287         flavor=flavor,
288         image=image_id,
289         nics=[{"net-id": network_id}],
290         config_drive=True,
291         userdata=u
292     )
293
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)))
297         exit(EXIT_CODE)
298     else:
299         logger.info("Instance '%s' is ACTIVE." % NAME_VM_2)
300
301     logger.info("Waiting for ping...")
302     sec = 0
303     metadata_tries = 0
304     console_log = vm2.get_console_output()
305     duration = 0
306     stop_time = time.time()
307
308     while True:
309         time.sleep(1)
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!")
315
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)
320             EXIT_CODE = 0
321             break
322         elif ("failed to read iid from metadata" in console_log or
323               metadata_tries > 5):
324             EXIT_CODE = -2
325             break
326         elif sec == PING_TIMEOUT:
327             logger.info("Timeout reached.")
328             break
329         elif sec % 10 == 0:
330             if "request failed" in console_log:
331                 logger.debug("It seems userdata is not supported in "
332                              "nova boot. Waiting a bit...")
333                 metadata_tries += 1
334             else:
335                 logger.debug("Pinging %s. Waiting for response..." % test_ip)
336         sec += 1
337
338     test_status = "FAIL"
339     if EXIT_CODE == 0:
340         logger.info("vPing OK")
341         test_status = "PASS"
342     elif EXIT_CODE == -2:
343         duration = 0
344         logger.info("Userdata is not supported in nova boot. Aborting test...")
345     else:
346         duration = 0
347         logger.error("vPing FAILED")
348
349     if args.report:
350         try:
351             logger.debug("Pushing vPing userdata results into DB...")
352             functest_utils.push_results_to_db("functest",
353                                               "vping_userdata",
354                                               logger,
355                                               start_time,
356                                               stop_time,
357                                               test_status,
358                                               details={'timestart': start_time,
359                                                        'duration': duration,
360                                                        'status': test_status})
361         except:
362             logger.error("Error pushing results into Database '%s'"
363                          % sys.exc_info()[0])
364
365     exit(EXIT_CODE)
366
367 if __name__ == '__main__':
368     main()