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
17 from scp import SCPClient
19 import functest.utils.functest_logger as ft_logger
20 import functest.utils.openstack_utils as os_utils
22 import functest.core.testcase as testcase
25 class VPingSSH(vping_base.VPingBase):
28 super(VPingSSH, self).__init__()
29 self.case_name = 'vping_ssh'
30 self.logger = ft_logger.Logger(self.case_name).getLogger()
32 def do_vping(self, vm, test_ip):
33 floatip = self.add_float_ip(vm)
35 return testcase.TestCase.EX_RUN_ERROR
36 ssh = self.establish_ssh(vm, floatip)
38 return testcase.TestCase.EX_RUN_ERROR
39 if not self.transfer_ping_script(ssh, floatip):
40 return testcase.TestCase.EX_RUN_ERROR
41 return self.do_vping_ssh(ssh, test_ip)
43 def add_float_ip(self, vm):
44 self.logger.info("Creating floating IP for VM '%s'..." % self.vm2_name)
45 floatip_dic = os_utils.create_floating_ip(self.neutron_client)
46 floatip = floatip_dic['fip_addr']
49 self.logger.error("Cannot create floating IP.")
51 self.logger.info("Floating IP created: '%s'" % floatip)
53 self.logger.info("Associating floating ip: '%s' to VM '%s' "
54 % (floatip, self.vm2_name))
55 if not os_utils.add_floating_ip(self.nova_client, vm.id, floatip):
56 self.logger.error("Cannot associate floating IP to VM.")
61 def establish_ssh(self, vm, floatip):
62 self.logger.info("Trying to establish SSH connection to %s..."
64 ssh = paramiko.SSHClient()
65 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
71 cidr_first_octet = self.private_subnet_cidr.split('.')[0]
74 ssh.connect(floatip, username=self.image_username,
75 password=self.image_password, timeout=2)
76 self.logger.debug("SSH connection established to %s."
80 self.logger.debug("Waiting for %s..." % floatip)
84 console_log = vm.get_console_output()
86 # print each "Sending discover" captured on the console log
87 if (len(re.findall("Sending discover", console_log)) >
88 discover_count and not got_ip):
90 self.logger.debug("Console-log '%s': Sending discover..."
93 # check if eth0 got an ip,the line looks like this:
94 # "inet addr:192.168."....
95 # if the dhcp agent fails to assing ip, this line will not appear
96 if "inet addr:" + cidr_first_octet in console_log and not got_ip:
98 self.logger.debug("The instance '%s' succeeded to get the IP "
99 "from the dhcp agent." % self.vm2_name)
101 # if dhcp not work,it shows "No lease, failing".The test will fail
102 if ("No lease, failing" in console_log and
106 self.logger.debug("Console-log '%s': No lease, failing..."
108 self.logger.info("The instance failed to get an IP from DHCP "
109 "agent. The test will probably timeout...")
111 if timeout == 0: # 300 sec timeout (5 min)
112 self.logger.error("Cannot establish connection to IP '%s'. "
113 "Aborting" % floatip)
117 def transfer_ping_script(self, ssh, floatip):
118 self.logger.info("Trying to transfer ping.sh to %s..." % floatip)
119 scp = SCPClient(ssh.get_transport())
120 local_path = self.functest_repo + "/" + self.repo
121 ping_script = os.path.join(local_path, "ping.sh")
123 scp.put(ping_script, "~/")
125 self.logger.error("Cannot SCP the file '%s' to VM '%s'"
126 % (ping_script, floatip))
129 cmd = 'chmod 755 ~/ping.sh'
130 (stdin, stdout, stderr) = ssh.exec_command(cmd)
131 for line in stdout.readlines():
136 def do_vping_ssh(self, ssh, test_ip):
138 self.logger.info("Waiting for ping...")
141 cmd = '~/ping.sh ' + test_ip
146 (stdin, stdout, stderr) = ssh.exec_command(cmd)
147 output = stdout.readlines()
150 if "vPing OK" in line:
151 self.logger.info("vPing detected!")
156 elif sec == self.ping_timeout:
157 self.logger.info("Timeout reached.")
162 self.logger.debug("Pinging %s. Waiting for response..." % test_ip)
167 if __name__ == '__main__':
168 args_parser = argparse.ArgumentParser()
169 args_parser.add_argument("-r", "--report",
170 help="Create json result file",
172 args = vars(args_parser.parse_args())
173 sys.exit(vping_base.VPingMain(VPingSSH).main(**args))