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