c2ff75150cd5dc32c06139339f6470c59304aaf5
[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 """vping_userdata testcase."""
11
12 import logging
13 import time
14
15 from snaps.config.network import PortConfig
16 from snaps.config.vm_inst import VmInstanceConfig
17 from snaps.openstack.utils import deploy_utils
18 from xtesting.core import testcase
19
20 from functest.opnfv_tests.openstack.vping 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         self.logger = logging.getLogger(__name__)
33
34     def run(self, **kwargs):
35         """
36         Sets up the OpenStack VM instance objects then executes the ping and
37         validates.
38         :return: the exit code from the super.execute() method
39         """
40         super(VPingUserdata, self).run()
41
42         # Creating Instance 1
43         port1_settings = PortConfig(
44             name=self.vm1_name + '-vPingPort',
45             network_name=self.network_creator.network_settings.name)
46         instance1_settings = VmInstanceConfig(
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 = PortConfig(
65                 name=self.vm2_name + '-vPingPort',
66                 network_name=self.network_creator.network_settings.name)
67             instance2_settings = VmInstanceConfig(
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     def _do_vping(self, vm_creator, test_ip):
87         """
88         Override from super
89         """
90         self.logger.info("Waiting for ping...")
91         exit_code = testcase.TestCase.EX_TESTCASE_FAILED
92         sec = 0
93         tries = 0
94
95         while True:
96             time.sleep(1)
97             p_console = vm_creator.get_console_output()
98             if "vPing OK" in p_console:
99                 self.logger.info("vPing detected!")
100                 exit_code = testcase.TestCase.EX_OK
101                 break
102             elif "failed to read iid from metadata" in p_console or tries > 5:
103                 self.logger.info("Failed to read iid from metadata")
104                 break
105             elif sec == self.ping_timeout:
106                 self.logger.info("Timeout reached.")
107                 break
108             elif sec % 10 == 0:
109                 if "request failed" in p_console:
110                     self.logger.debug(
111                         "It seems userdata is not supported in nova boot. " +
112                         "Waiting a bit...")
113                     tries += 1
114                 else:
115                     self.logger.debug(
116                         "Pinging %s. Waiting for response...", test_ip)
117             sec += 1
118
119         return exit_code
120
121
122 def _get_userdata(test_ip):
123     """
124     Returns the post VM creation script to be added into the VM's userdata
125     :param test_ip: the IP value to substitute into the script
126     :return: the bash script contents
127     """
128     if test_ip:
129         return ("#!/bin/sh\n\n"
130                 "while true; do\n"
131                 " ping -c 1 %s 2>&1 >/dev/null\n"
132                 " RES=$?\n"
133                 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
134                 "  echo 'vPing OK'\n"
135                 "  break\n"
136                 " else\n"
137                 "  echo 'vPing KO'\n"
138                 " fi\n"
139                 " sleep 1\n"
140                 "done\n" % str(test_ip))
141     return None