Merge "Remove tacker library from functest"
[functest.git] / functest / opnfv_tests / vnf / router / vnf_controller / ssh_client.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Okinawa Open Laboratory and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """ssh client module for vrouter testing"""
11
12 import logging
13 import paramiko
14 import time
15 import yaml
16
17 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
18
19 RECEIVE_ROOP_WAIT = 1
20
21 DEFAULT_CONNECT_TIMEOUT = 10
22 DEFAULT_CONNECT_RETRY_COUNT = 10
23 DEFAULT_SEND_TIMEOUT = 10
24
25
26 class SshClient(object):
27     """ssh client class for vrouter testing"""
28
29     logger = logging.getLogger(__name__)
30
31     def __init__(self, ip_address, user, password=None, key_filename=None):
32         self.ip_address = ip_address
33         self.user = user
34         self.password = password
35         self.key_filename = key_filename
36         self.connected = False
37         self.shell = None
38
39         self.logger.setLevel(logging.INFO)
40
41         self.ssh = paramiko.SSHClient()
42         self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
43
44         self.util = Utilvnf()
45         with open(self.util.test_env_config_yaml) as file_fd:
46             test_env_config_yaml = yaml.safe_load(file_fd)
47         file_fd.close()
48
49         self.ssh_revieve_buff = test_env_config_yaml.get("general").get(
50             "ssh_receive_buffer")
51
52     def connect(self, time_out=DEFAULT_CONNECT_TIMEOUT,
53                 retrycount=DEFAULT_CONNECT_RETRY_COUNT):
54         while retrycount > 0:
55             try:
56                 self.logger.info("SSH connect to %s.", self.ip_address)
57                 self.ssh.connect(self.ip_address,
58                                  username=self.user,
59                                  password=self.password,
60                                  key_filename=self.key_filename,
61                                  timeout=time_out,
62                                  look_for_keys=False,
63                                  allow_agent=False)
64
65                 self.logger.info("SSH connection established to %s.",
66                                  self.ip_address)
67
68                 self.shell = self.ssh.invoke_shell()
69
70                 while not self.shell.recv_ready():
71                     time.sleep(RECEIVE_ROOP_WAIT)
72
73                 self.shell.recv(self.ssh_revieve_buff)
74                 break
75             except:  # pylint: disable=broad-except
76                 self.logger.info("SSH timeout for %s...", self.ip_address)
77                 time.sleep(time_out)
78                 retrycount -= 1
79
80         if retrycount == 0:
81             self.logger.error("Cannot establish connection to IP '%s'. " +
82                               "Aborting",
83                               self.ip_address)
84             self.connected = False
85             return self.connected
86
87         self.connected = True
88         return self.connected
89
90     def send(self, cmd, prompt, timeout=DEFAULT_SEND_TIMEOUT):
91         if self.connected is True:
92             self.shell.settimeout(timeout)
93             self.logger.debug("Commandset : '%s'", cmd)
94
95             try:
96                 self.shell.send(cmd + '\n')
97             except:  # pylint: disable=broad-except
98                 self.logger.error("ssh send timeout : Command : '%s'", cmd)
99                 return None
100
101             res_buff = ''
102             while not res_buff.endswith(prompt):
103                 time.sleep(RECEIVE_ROOP_WAIT)
104                 try:
105                     res = self.shell.recv(self.ssh_revieve_buff)
106                 except:  # pylint: disable=broad-except
107                     self.logger.error("ssh receive timeout : Command : '%s'",
108                                       cmd)
109                     break
110
111                 res_buff += res
112
113             self.logger.debug("Response : '%s'", res_buff)
114             return res_buff
115         else:
116             self.logger.error("Cannot connected to IP '%s'.", self.ip_address)
117             return None
118
119     def close(self):
120         if self.connected is True:
121             self.ssh.close()
122
123     def error_check(response, err_strs=["error",
124                                         "warn",
125                                         "unknown command",
126                                         "already exist"]):
127         for err in err_strs:
128             if err in response:
129                 return False
130
131         return True