Add RemoteClient under tempest.lib
remote_client under tempest.common is used in many projects but the
module is not defined as stable. This patch adds the essential part
of the module into tempest.lib as stable interface.
Change-Id: Idb2206c239f25bc8b8530275e84c834233b9544b
diff --git a/releasenotes/notes/add-tempest-lib-remote-client-adbeb3f42a36910b.yaml b/releasenotes/notes/add-tempest-lib-remote-client-adbeb3f42a36910b.yaml
new file mode 100644
index 0000000..c21751b
--- /dev/null
+++ b/releasenotes/notes/add-tempest-lib-remote-client-adbeb3f42a36910b.yaml
@@ -0,0 +1,10 @@
+---
+features:
+ - Add remote_client under tempest.lib.
+ This remote_client under tempest.lib is defined as stable
+ interface, and now this module provides the following
+ essential methods.
+
+ * exec_command
+ * validate_authentication
+ * ping_host
diff --git a/tempest/common/utils/linux/remote_client.py b/tempest/common/utils/linux/remote_client.py
index e9ea92a..b309335 100644
--- a/tempest/common/utils/linux/remote_client.py
+++ b/tempest/common/utils/linux/remote_client.py
@@ -11,17 +11,12 @@
# under the License.
import re
-import sys
import time
-import netaddr
-import six
-
from oslo_log import log as logging
from tempest import config
-from tempest.lib.common import ssh
-from tempest.lib.common.utils import test_utils
+from tempest.lib.common.utils.linux import remote_client
import tempest.lib.exceptions
CONF = config.CONF
@@ -29,39 +24,11 @@
LOG = logging.getLogger(__name__)
-def debug_ssh(function):
- """Decorator to generate extra debug info in case off SSH failure"""
- def wrapper(self, *args, **kwargs):
- try:
- return function(self, *args, **kwargs)
- except tempest.lib.exceptions.SSHTimeout:
- try:
- original_exception = sys.exc_info()
- caller = test_utils.find_test_caller() or "not found"
- if self.server:
- msg = 'Caller: %s. Timeout trying to ssh to server %s'
- LOG.debug(msg, caller, self.server)
- if self.log_console and self.servers_client:
- try:
- msg = 'Console log for server %s: %s'
- console_log = (
- self.servers_client.get_console_output(
- self.server['id'])['output'])
- LOG.debug(msg, self.server['id'], console_log)
- except Exception:
- msg = 'Could not get console_log for server %s'
- LOG.debug(msg, self.server['id'])
- # re-raise the original ssh timeout exception
- six.reraise(*original_exception)
- finally:
- # Delete the traceback to avoid circular references
- _, _, trace = original_exception
- del trace
- return wrapper
+class RemoteClient(remote_client.RemoteClient):
-
-class RemoteClient(object):
-
+ # TODO(oomichi): Make this class deprecated after migrating
+ # necessary methods to tempest.lib and cleaning
+ # unnecessary methods up from this class.
def __init__(self, ip_address, username, password=None, pkey=None,
server=None, servers_client=None):
"""Executes commands in a VM over ssh
@@ -73,35 +40,15 @@
:param server: server dict, used for debugging purposes
:param servers_client: servers client, used for debugging purposes
"""
- self.server = server
- self.servers_client = servers_client
-
- ssh_timeout = CONF.validation.ssh_timeout
- connect_timeout = CONF.validation.connect_timeout
- self.log_console = CONF.compute_feature_enabled.console_output
- self.ssh_shell_prologue = CONF.validation.ssh_shell_prologue
- self.ping_count = CONF.validation.ping_count
- self.ping_size = CONF.validation.ping_size
-
- self.ssh_client = ssh.Client(ip_address, username, password,
- ssh_timeout, pkey=pkey,
- channel_timeout=connect_timeout)
-
- @debug_ssh
- def exec_command(self, cmd):
- # Shell options below add more clearness on failures,
- # path is extended for some non-cirros guest oses (centos7)
- cmd = self.ssh_shell_prologue + " " + cmd
- LOG.debug("Remote command: %s", cmd)
- return self.ssh_client.exec_command(cmd)
-
- @debug_ssh
- def validate_authentication(self):
- """Validate ssh connection and authentication
-
- This method raises an Exception when the validation fails.
- """
- self.ssh_client.test_connection_auth()
+ super(RemoteClient, self).__init__(
+ ip_address, username, password=password, pkey=pkey,
+ server=server, servers_client=servers_client,
+ ssh_timeout=CONF.validation.ssh_timeout,
+ connect_timeout=CONF.validation.connect_timeout,
+ console_output_enabled=CONF.compute_feature_enabled.console_output,
+ ssh_shell_prologue=CONF.validation.ssh_shell_prologue,
+ ping_count=CONF.validation.ping_count,
+ ping_size=CONF.validation.ping_size)
def get_disks(self):
# Select root disk devices as shown by lsblk
@@ -132,19 +79,6 @@
cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message
return self.exec_command(cmd)
- def ping_host(self, host, count=None, size=None, nic=None):
- if count is None:
- count = self.ping_count
- if size is None:
- size = self.ping_size
-
- addr = netaddr.IPAddress(host)
- cmd = 'ping6' if addr.version == 6 else 'ping'
- if nic:
- cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic)
- cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host)
- return self.exec_command(cmd)
-
def set_mac_address(self, nic, address):
self.set_nic_state(nic=nic, state="down")
cmd = "sudo ip link set dev {0} address {1}".format(nic, address)
diff --git a/tempest/lib/common/utils/linux/__init__.py b/tempest/lib/common/utils/linux/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tempest/lib/common/utils/linux/__init__.py
diff --git a/tempest/lib/common/utils/linux/remote_client.py b/tempest/lib/common/utils/linux/remote_client.py
new file mode 100644
index 0000000..64d6be2
--- /dev/null
+++ b/tempest/lib/common/utils/linux/remote_client.py
@@ -0,0 +1,117 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import sys
+
+import netaddr
+from oslo_log import log as logging
+import six
+
+from tempest.lib.common import ssh
+from tempest.lib.common.utils import test_utils
+import tempest.lib.exceptions
+
+LOG = logging.getLogger(__name__)
+
+
+def debug_ssh(function):
+ """Decorator to generate extra debug info in case off SSH failure"""
+ def wrapper(self, *args, **kwargs):
+ try:
+ return function(self, *args, **kwargs)
+ except tempest.lib.exceptions.SSHTimeout:
+ try:
+ original_exception = sys.exc_info()
+ caller = test_utils.find_test_caller() or "not found"
+ if self.server:
+ msg = 'Caller: %s. Timeout trying to ssh to server %s'
+ LOG.debug(msg, caller, self.server)
+ if self.console_output_enabled and self.servers_client:
+ try:
+ msg = 'Console log for server %s: %s'
+ console_log = (
+ self.servers_client.get_console_output(
+ self.server['id'])['output'])
+ LOG.debug(msg, self.server['id'], console_log)
+ except Exception:
+ msg = 'Could not get console_log for server %s'
+ LOG.debug(msg, self.server['id'])
+ # re-raise the original ssh timeout exception
+ six.reraise(*original_exception)
+ finally:
+ # Delete the traceback to avoid circular references
+ _, _, trace = original_exception
+ del trace
+ return wrapper
+
+
+class RemoteClient(object):
+
+ def __init__(self, ip_address, username, password=None, pkey=None,
+ server=None, servers_client=None, ssh_timeout=300,
+ connect_timeout=60, console_output_enabled=True,
+ ssh_shell_prologue="set -eu -o pipefail; PATH=$$PATH:/sbin;",
+ ping_count=1, ping_size=56):
+ """Executes commands in a VM over ssh
+
+ :param ip_address: IP address to ssh to
+ :param username: Ssh username
+ :param password: Ssh password
+ :param pkey: Ssh public key
+ :param server: Server dict, used for debugging purposes
+ :param servers_client: Servers client, used for debugging purposes
+ :param ssh_timeout: Timeout in seconds to wait for the ssh banner
+ :param connect_timeout: Timeout in seconds to wait for TCP connection
+ :param console_output_enabled: Support serial console output?
+ :param ssh_shell_prologue: Shell fragments to use before command
+ :param ping_count: Number of ping packets
+ :param ping_size: Packet size for ping packets
+ """
+ self.server = server
+ self.servers_client = servers_client
+ self.console_output_enabled = console_output_enabled
+ self.ssh_shell_prologue = ssh_shell_prologue
+ self.ping_count = ping_count
+ self.ping_size = ping_size
+
+ self.ssh_client = ssh.Client(ip_address, username, password,
+ ssh_timeout, pkey=pkey,
+ channel_timeout=connect_timeout)
+
+ @debug_ssh
+ def exec_command(self, cmd):
+ # Shell options below add more clearness on failures,
+ # path is extended for some non-cirros guest oses (centos7)
+ cmd = self.ssh_shell_prologue + " " + cmd
+ LOG.debug("Remote command: %s", cmd)
+ return self.ssh_client.exec_command(cmd)
+
+ @debug_ssh
+ def validate_authentication(self):
+ """Validate ssh connection and authentication
+
+ This method raises an Exception when the validation fails.
+ """
+ self.ssh_client.test_connection_auth()
+
+ def ping_host(self, host, count=None, size=None, nic=None):
+ if count is None:
+ count = self.ping_count
+ if size is None:
+ size = self.ping_size
+
+ addr = netaddr.IPAddress(host)
+ cmd = 'ping6' if addr.version == 6 else 'ping'
+ if nic:
+ cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic)
+ cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host)
+ return self.exec_command(cmd)
diff --git a/tempest/tests/common/utils/linux/test_remote_client.py b/tempest/tests/common/utils/linux/test_remote_client.py
index 7199206..48cb86b 100644
--- a/tempest/tests/common/utils/linux/test_remote_client.py
+++ b/tempest/tests/common/utils/linux/test_remote_client.py
@@ -182,7 +182,7 @@
user='user',
password='pass')))
self.log = self.useFixture(fixtures.FakeLogger(
- name='tempest.common.utils.linux.remote_client',
+ name='tempest.lib.common.utils.linux.remote_client',
level='DEBUG'))
def test_validate_debug_ssh_console(self):
diff --git a/tempest/tests/lib/common/utils/linux/__init__.py b/tempest/tests/lib/common/utils/linux/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tempest/tests/lib/common/utils/linux/__init__.py
diff --git a/tempest/tests/lib/common/utils/linux/test_remote_client.py b/tempest/tests/lib/common/utils/linux/test_remote_client.py
new file mode 100644
index 0000000..cf312f4
--- /dev/null
+++ b/tempest/tests/lib/common/utils/linux/test_remote_client.py
@@ -0,0 +1,67 @@
+# Copyright 2017 NEC Corporation.
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import mock
+
+from tempest.lib.common import ssh
+from tempest.lib.common.utils.linux import remote_client
+from tempest.lib import exceptions as lib_exc
+from tempest.tests import base
+
+
+class FakeServersClient(object):
+
+ def get_console_output(self, server_id):
+ return {"output": "fake_output"}
+
+
+class TestRemoteClient(base.TestCase):
+
+ @mock.patch.object(ssh.Client, 'exec_command', return_value='success')
+ def test_exec_command(self, mock_ssh_exec_command):
+ client = remote_client.RemoteClient('192.168.1.10', 'username')
+ client.exec_command('ls')
+ mock_ssh_exec_command.assert_called_once_with(
+ 'set -eu -o pipefail; PATH=$$PATH:/sbin; ls')
+
+ @mock.patch.object(ssh.Client, 'test_connection_auth')
+ def test_validate_authentication(self, mock_test_connection_auth):
+ client = remote_client.RemoteClient('192.168.1.10', 'username')
+ client.validate_authentication()
+ mock_test_connection_auth.assert_called_once_with()
+
+ @mock.patch.object(remote_client.LOG, 'debug')
+ @mock.patch.object(ssh.Client, 'exec_command')
+ def test_debug_ssh_without_console(self, mock_exec_command, mock_debug):
+ mock_exec_command.side_effect = lib_exc.SSHTimeout
+ server = {'id': 'fake_id'}
+ client = remote_client.RemoteClient('192.168.1.10', 'username',
+ server=server)
+ self.assertRaises(lib_exc.SSHTimeout, client.exec_command, 'ls')
+ mock_debug.assert_called_with(
+ 'Caller: %s. Timeout trying to ssh to server %s',
+ 'TestRemoteClient:test_debug_ssh_without_console', server)
+
+ @mock.patch.object(remote_client.LOG, 'debug')
+ @mock.patch.object(ssh.Client, 'exec_command')
+ def test_debug_ssh_with_console(self, mock_exec_command, mock_debug):
+ mock_exec_command.side_effect = lib_exc.SSHTimeout
+ server = {'id': 'fake_id'}
+ client = remote_client.RemoteClient('192.168.1.10', 'username',
+ server=server,
+ servers_client=FakeServersClient())
+ self.assertRaises(lib_exc.SSHTimeout, client.exec_command, 'ls')
+ mock_debug.assert_called_with(
+ 'Console log for server %s: %s', server['id'], 'fake_output')