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):
27 def __init__(self, case_name='vping_ssh'):
28 super(VPingSSH, self).__init__(case_name)
29 self.logger = ft_logger.Logger(self.case_name).getLogger()
31 def do_vping(self, vm, test_ip):
32 floatip = self.add_float_ip(vm)
34 return testcase.TestCase.EX_RUN_ERROR
35 ssh = self.establish_ssh(vm, floatip)
37 return testcase.TestCase.EX_RUN_ERROR
38 if not self.transfer_ping_script(ssh, floatip):
39 return testcase.TestCase.EX_RUN_ERROR
40 return self.do_vping_ssh(ssh, test_ip)
42 def add_float_ip(self, vm):
43 self.logger.info("Creating floating IP for VM '%s'..." % self.vm2_name)
44 floatip_dic = os_utils.create_floating_ip(self.neutron_client)
45 floatip = floatip_dic['fip_addr']
48 self.logger.error("Cannot create floating IP.")
50 self.logger.info("Floating IP created: '%s'" % floatip)
52 self.logger.info("Associating floating ip: '%s' to VM '%s' "
53 % (floatip, self.vm2_name))
54 if not os_utils.add_floating_ip(self.nova_client, vm.id, floatip):
55 self.logger.error("Cannot associate floating IP to VM.")
60 def establish_ssh(self, vm, floatip):
61 self.logger.info("Trying to establish SSH connection to %s..."
63 ssh = paramiko.SSHClient()
64 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
70 cidr_first_octet = self.private_subnet_cidr.split('.')[0]
73 ssh.connect(floatip, username=self.image_username,
74 password=self.image_password, timeout=2)
75 self.logger.debug("SSH connection established to %s."
79 self.logger.debug("Waiting for %s..." % floatip)
83 console_log = vm.get_console_output()
85 # print each "Sending discover" captured on the console log
86 if (len(re.findall("Sending discover", console_log)) >
87 discover_count and not got_ip):
89 self.logger.debug("Console-log '%s': Sending discover..."
92 # check if eth0 got an ip,the line looks like this:
93 # "inet addr:192.168."....
94 # if the dhcp agent fails to assing ip, this line will not appear
95 if "inet addr:" + cidr_first_octet in console_log and not got_ip:
97 self.logger.debug("The instance '%s' succeeded to get the IP "
98 "from the dhcp agent." % self.vm2_name)
100 # if dhcp not work,it shows "No lease, failing".The test will fail
101 if ("No lease, failing" in console_log and
105 self.logger.debug("Console-log '%s': No lease, failing..."
107 self.logger.info("The instance failed to get an IP from DHCP "
108 "agent. The test will probably timeout...")
110 if timeout == 0: # 300 sec timeout (5 min)
111 self.logger.error("Cannot establish connection to IP '%s'. "
112 "Aborting" % floatip)
116 def transfer_ping_script(self, ssh, floatip):
117 self.logger.info("Trying to transfer ping.sh to %s..." % floatip)
118 scp = SCPClient(ssh.get_transport())
119 local_path = self.functest_repo + "/" + self.repo
120 ping_script = os.path.join(local_path, "ping.sh")
122 scp.put(ping_script, "~/")
124 self.logger.error("Cannot SCP the file '%s' to VM '%s'"
125 % (ping_script, floatip))
128 cmd = 'chmod 755 ~/ping.sh'
129 (stdin, stdout, stderr) = ssh.exec_command(cmd)
130 for line in stdout.readlines():
135 def do_vping_ssh(self, ssh, test_ip):
137 self.logger.info("Waiting for ping...")
140 cmd = '~/ping.sh ' + test_ip
145 (stdin, stdout, stderr) = ssh.exec_command(cmd)
146 output = stdout.readlines()
149 if "vPing OK" in line:
150 self.logger.info("vPing detected!")
155 elif sec == self.ping_timeout:
156 self.logger.info("Timeout reached.")
161 self.logger.debug("Pinging %s. Waiting for response..." % test_ip)
166 if __name__ == '__main__':
167 args_parser = argparse.ArgumentParser()
168 args_parser.add_argument("-r", "--report",
169 help="Create json result file",
171 args = vars(args_parser.parse_args())
172 sys.exit(vping_base.VPingMain(VPingSSH).main(**args))