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
12 from snaps.openstack.utils import deploy_utils
13 from snaps.openstack.create_instance import VmInstanceSettings
14 from snaps.openstack.create_network import PortSettings
16 from functest.core.testcase import TestCase
17 from functest.opnfv_tests.openstack.vping import vping_base
20 class VPingUserdata(vping_base.VPingBase):
22 Class to execute the vPing test using userdata and the VM's console
25 def __init__(self, **kwargs):
26 if "case_name" not in kwargs:
27 kwargs["case_name"] = "vping_userdata"
28 super(VPingUserdata, self).__init__(**kwargs)
32 Sets up the OpenStack VM instance objects then executes the ping and
34 :return: the exit code from the super.execute() method
37 super(VPingUserdata, self).run()
40 port1_settings = PortSettings(
41 name=self.vm1_name + '-vPingPort',
42 network_name=self.network_creator.network_settings.name)
43 instance1_settings = VmInstanceSettings(
45 flavor=self.flavor_name,
46 vm_boot_timeout=self.vm_boot_timeout,
47 port_settings=[port1_settings])
50 "Creating VM 1 instance with name: '%s'"
51 % instance1_settings.name)
52 self.vm1_creator = deploy_utils.create_vm_instance(
53 self.os_creds, instance1_settings,
54 self.image_creator.image_settings)
55 self.creators.append(self.vm1_creator)
57 userdata = _get_userdata(
58 self.vm1_creator.get_port_ip(port1_settings.name))
61 port2_settings = PortSettings(
62 name=self.vm2_name + '-vPingPort',
63 network_name=self.network_creator.network_settings.name)
64 instance2_settings = VmInstanceSettings(
66 flavor=self.flavor_name,
67 vm_boot_timeout=self.vm_boot_timeout,
68 port_settings=[port2_settings],
72 "Creating VM 2 instance with name: '%s'"
73 % instance2_settings.name)
74 self.vm2_creator = deploy_utils.create_vm_instance(
75 self.os_creds, instance2_settings,
76 self.image_creator.image_settings)
77 self.creators.append(self.vm2_creator)
79 raise Exception('Userdata is None')
81 return self._execute()
86 def _do_vping(self, vm_creator, test_ip):
90 self.logger.info("Waiting for ping...")
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.EX_OK
102 elif "failed to read iid from metadata" in p_console or tries > 5:
103 exit_code = TestCase.EX_TESTCASE_FAILED
105 elif sec == self.ping_timeout:
106 self.logger.info("Timeout reached.")
109 if "request failed" in p_console:
111 "It seems userdata is not supported in nova boot. " +
116 "Pinging %s. Waiting for response..." % test_ip)
122 def _get_userdata(test_ip):
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
129 return ("#!/bin/sh\n\n"
131 " ping -c 1 %s 2>&1 >/dev/null\n"
133 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"