Daniel Mellado | 21d4d5c | 2016-11-08 17:02:42 +0000 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
Daniel Mellado | 21d4d5c | 2016-11-08 17:02:42 +0000 | [diff] [blame] | 13 | import sys |
| 14 | |
| 15 | from oslo_log import log |
Daniel Mellado | 21d4d5c | 2016-11-08 17:02:42 +0000 | [diff] [blame] | 16 | from tempest import config |
| 17 | from tempest.lib.common import ssh |
| 18 | from tempest.lib.common.utils import test_utils |
lkuchlan | 1d1461d | 2020-08-04 11:19:11 +0300 | [diff] [blame] | 19 | from tempest.lib import exceptions |
Daniel Mellado | 21d4d5c | 2016-11-08 17:02:42 +0000 | [diff] [blame] | 20 | |
| 21 | CONF = config.CONF |
| 22 | |
| 23 | LOG = log.getLogger(__name__) |
| 24 | |
| 25 | |
| 26 | def debug_ssh(function): |
| 27 | """Decorator to generate extra debug info in case of ssh failure""" |
| 28 | def wrapper(self, *args, **kwargs): |
| 29 | try: |
| 30 | return function(self, *args, **kwargs) |
lkuchlan | 1d1461d | 2020-08-04 11:19:11 +0300 | [diff] [blame] | 31 | except exceptions.SSHTimeout: |
Daniel Mellado | 21d4d5c | 2016-11-08 17:02:42 +0000 | [diff] [blame] | 32 | try: |
| 33 | original_exception = sys.exc_info() |
| 34 | caller = test_utils.find_test_caller() or "not found" |
| 35 | if self.server: |
| 36 | msg = 'Caller: %s. Timeout trying to ssh to server %s' |
| 37 | LOG.debug(msg, caller, self.server) |
| 38 | if self.log_console and self.servers_client: |
| 39 | try: |
| 40 | msg = 'Console log for server %s: %s' |
| 41 | console_log = ( |
| 42 | self.servers_client.get_console_output( |
| 43 | self.server['id'])['output']) |
| 44 | LOG.debug(msg, self.server['id'], console_log) |
| 45 | except Exception: |
| 46 | msg = 'Could not get console_log for server %s' |
| 47 | LOG.debug(msg, self.server['id']) |
| 48 | # re-raise the original ssh timeout exception |
haixin | 4889581 | 2020-09-30 13:50:37 +0800 | [diff] [blame^] | 49 | raise original_exception |
Daniel Mellado | 21d4d5c | 2016-11-08 17:02:42 +0000 | [diff] [blame] | 50 | finally: |
| 51 | # Delete the traceback to avoid circular references |
| 52 | _, _, trace = original_exception |
| 53 | del trace |
| 54 | return wrapper |
| 55 | |
| 56 | |
| 57 | class RemoteClient(object): |
| 58 | |
| 59 | def __init__(self, ip_address, username, password=None, pkey=None, |
| 60 | server=None, servers_client=None): |
| 61 | """Executes commands in a VM over ssh |
| 62 | |
| 63 | :param ip_address: IP address to ssh to |
| 64 | :param username: ssh username |
| 65 | :param password: ssh password (optional) |
| 66 | :param pkey: ssh public key (optional) |
| 67 | :param server: server dict, used for debugging purposes |
| 68 | :param servers_client: servers client, used for debugging purposes |
| 69 | """ |
| 70 | self.server = server |
| 71 | self.servers_client = servers_client |
| 72 | self.log_console = CONF.compute_feature_enabled.console_output |
| 73 | |
| 74 | self.ssh_client = ssh.Client(ip_address, username, password, pkey=pkey) |
| 75 | |
| 76 | @debug_ssh |
| 77 | def exec_command(self, cmd): |
| 78 | # Shell options below add more clearness on failures, |
| 79 | # path is extended for some non-cirros guest oses (centos7) |
| 80 | cmd = CONF.validation.ssh_shell_prologue + " " + cmd |
junboli | b236c24 | 2017-07-18 18:12:37 +0800 | [diff] [blame] | 81 | LOG.debug("Remote command: %s", cmd) |
Daniel Mellado | 21d4d5c | 2016-11-08 17:02:42 +0000 | [diff] [blame] | 82 | return self.ssh_client.exec_command(cmd) |
| 83 | |
| 84 | @debug_ssh |
| 85 | def validate_authentication(self): |
| 86 | """Validate ssh connection and authentication |
| 87 | |
| 88 | This method raises an Exception when the validation fails. |
| 89 | """ |
| 90 | self.ssh_client.test_connection_auth() |