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
10 """vping_userdata testcase."""
14 from snaps.config.network import PortConfig
15 from snaps.config.vm_inst import VmInstanceConfig
16 from snaps.openstack.utils import deploy_utils
17 from xtesting.core import testcase
19 from functest.opnfv_tests.openstack.vping import vping_base
22 class VPingUserdata(vping_base.VPingBase):
24 Class to execute the vPing test using userdata and the VM's console
27 def __init__(self, **kwargs):
28 if "case_name" not in kwargs:
29 kwargs["case_name"] = "vping_userdata"
30 super(VPingUserdata, self).__init__(**kwargs)
32 def run(self, **kwargs):
34 Sets up the OpenStack VM instance objects then executes the ping and
36 :return: the exit code from the super.execute() method
39 super(VPingUserdata, self).run()
42 port1_settings = PortConfig(
43 name=self.vm1_name + '-vPingPort',
44 network_name=self.network_creator.network_settings.name)
45 instance1_settings = VmInstanceConfig(
47 flavor=self.flavor_name,
48 vm_boot_timeout=self.vm_boot_timeout,
49 port_settings=[port1_settings])
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)
59 userdata = _get_userdata(
60 self.vm1_creator.get_port_ip(port1_settings.name))
63 port2_settings = PortConfig(
64 name=self.vm2_name + '-vPingPort',
65 network_name=self.network_creator.network_settings.name)
66 instance2_settings = VmInstanceConfig(
68 flavor=self.flavor_name,
69 vm_boot_timeout=self.vm_boot_timeout,
70 port_settings=[port2_settings],
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)
81 raise Exception('Userdata is None')
83 return self._execute()
88 def _do_vping(self, vm_creator, test_ip):
92 self.logger.info("Waiting for ping...")
93 exit_code = testcase.TestCase.EX_TESTCASE_FAILED
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.TestCase.EX_OK
104 elif "failed to read iid from metadata" in p_console or tries > 5:
105 self.logger.info("Failed to read iid from metadata")
107 elif sec == self.ping_timeout:
108 self.logger.info("Timeout reached.")
111 if "request failed" in p_console:
113 "It seems userdata is not supported in nova boot. " +
118 "Pinging %s. Waiting for response...", test_ip)
124 def _get_userdata(test_ip):
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
131 return ("#!/bin/sh\n\n"
133 " ping -c 1 %s 2>&1 >/dev/null\n"
135 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
142 "done\n" % str(test_ip))