718f47f0db0033e8497501541dbd9031768a0fb3
[functest.git] / functest / opnfv_tests / openstack / vping / vping_userdata.py
1 #!/usr/bin/env 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 import argparse
11 import sys
12 import time
13
14 from functest.core.testcase import TestCase
15
16 from snaps.openstack.utils import deploy_utils
17 from snaps.openstack.create_instance import VmInstanceSettings
18 from snaps.openstack.create_network import PortSettings
19
20 import vping_base
21
22
23 class VPingUserdata(vping_base.VPingBase):
24     """
25     Class to execute the vPing test using userdata and the VM's console
26     """
27
28     def __init__(self, **kwargs):
29         if "case_name" not in kwargs:
30             kwargs["case_name"] = "vping_userdata"
31         super(VPingUserdata, self).__init__(**kwargs)
32
33     def run(self):
34         """
35         Sets up the OpenStack VM instance objects then executes the ping and
36         validates.
37         :return: the exit code from the super.execute() method
38         """
39         try:
40             super(VPingUserdata, self).run()
41
42             # Creating Instance 1
43             port1_settings = PortSettings(
44                 name=self.vm1_name + '-vPingPort',
45                 network_name=self.network_creator.network_settings.name)
46             instance1_settings = VmInstanceSettings(
47                 name=self.vm1_name,
48                 flavor=self.flavor_name,
49                 vm_boot_timeout=self.vm_boot_timeout,
50                 port_settings=[port1_settings])
51
52             self.logger.info(
53                 "Creating VM 1 instance with name: '%s'"
54                 % instance1_settings.name)
55             self.vm1_creator = deploy_utils.create_vm_instance(
56                 self.os_creds, instance1_settings,
57                 self.image_creator.image_settings)
58             self.creators.append(self.vm1_creator)
59
60             userdata = _get_userdata(
61                 self.vm1_creator.get_port_ip(port1_settings.name))
62             if userdata:
63                 # Creating Instance 2
64                 port2_settings = PortSettings(
65                     name=self.vm2_name + '-vPingPort',
66                     network_name=self.network_creator.network_settings.name)
67                 instance2_settings = VmInstanceSettings(
68                     name=self.vm2_name,
69                     flavor=self.flavor_name,
70                     vm_boot_timeout=self.vm_boot_timeout,
71                     port_settings=[port2_settings],
72                     userdata=userdata)
73
74                 self.logger.info(
75                     "Creating VM 2 instance with name: '%s'"
76                     % instance2_settings.name)
77                 self.vm2_creator = deploy_utils.create_vm_instance(
78                     self.os_creds, instance2_settings,
79                     self.image_creator.image_settings)
80                 self.creators.append(self.vm2_creator)
81             else:
82                 raise Exception('Userdata is None')
83
84             return self._execute()
85
86         finally:
87             self._cleanup()
88
89     def _do_vping(self, vm_creator, test_ip):
90         """
91         Override from super
92         """
93         self.logger.info("Waiting for ping...")
94         exit_code = -1
95         sec = 0
96         tries = 0
97
98         while True:
99             time.sleep(1)
100             p_console = vm_creator.get_os_vm_server_obj().get_console_output()
101             if "vPing OK" in p_console:
102                 self.logger.info("vPing detected!")
103                 exit_code = TestCase.EX_OK
104                 break
105             elif "failed to read iid from metadata" in p_console or tries > 5:
106                 exit_code = TestCase.EX_TESTCASE_FAILED
107                 break
108             elif sec == self.ping_timeout:
109                 self.logger.info("Timeout reached.")
110                 break
111             elif sec % 10 == 0:
112                 if "request failed" in p_console:
113                     self.logger.debug(
114                         "It seems userdata is not supported in nova boot. " +
115                         "Waiting a bit...")
116                     tries += 1
117                 else:
118                     self.logger.debug(
119                         "Pinging %s. Waiting for response..." % test_ip)
120             sec += 1
121
122         return exit_code
123
124
125 def _get_userdata(test_ip):
126     """
127     Returns the post VM creation script to be added into the VM's userdata
128     :param test_ip: the IP value to substitute into the script
129     :return: the bash script contents
130     """
131     if test_ip:
132         return ("#!/bin/sh\n\n"
133                 "while true; do\n"
134                 " ping -c 1 %s 2>&1 >/dev/null\n"
135                 " RES=$?\n"
136                 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
137                 "  echo 'vPing OK'\n"
138                 "  break\n"
139                 " else\n"
140                 "  echo 'vPing KO'\n"
141                 " fi\n"
142                 " sleep 1\n"
143                 "done\n" % test_ip)
144     return None
145
146
147 if __name__ == '__main__':
148     args_parser = argparse.ArgumentParser()
149     args_parser.add_argument("-r", "--report",
150                              help="Create json result file",
151                              action="store_true")
152     args = vars(args_parser.parse_args())
153
154     sys.exit(vping_base.VPingMain(VPingUserdata).main(**args))