From 0dfcc14ba57eabd983636c4a4ede32e344dd287b Mon Sep 17 00:00:00 2001 From: Scott Poore Date: Mar 01 2016 16:49:06 +0000 Subject: Add reset_connection to BaseHost class Add ability to reset the ssh connection via del host._transport. This is to allow cases where hostname or ip used for the ssh connection may change. https://fedorahosted.org/python-pytest-multihost/ticket/4 Test added by Petr Viktorin Signed-off-by: Scott Poore Signed-off-by: Petr Viktorin --- diff --git a/pytest_multihost/host.py b/pytest_multihost/host.py index 15fb43c..e6c0db5 100644 --- a/pytest_multihost/host.py +++ b/pytest_multihost/host.py @@ -158,6 +158,19 @@ class BaseHost(object): raise NotImplementedError('transport class not available') return self._transport + def reset_connection(self): + """Reset the connection + + The next time a connection is needed, a new Transport object will be + made. This new transport will take into account any configuration + changes, such as external_hostname, ssh_username, etc., that were made + on the Host. + """ + try: + del self._transport + except: + pass + def get_file_contents(self, filename, encoding=None): """Shortcut for transport.get_file_contents""" return self.transport.get_file_contents(filename, encoding=encoding) diff --git a/test_pytestmultihost/test_localhost.py b/test_pytestmultihost/test_localhost.py index 0ec8216..c51a2ea 100644 --- a/test_pytestmultihost/test_localhost.py +++ b/test_pytestmultihost/test_localhost.py @@ -196,6 +196,21 @@ class TestLocalhost(object): host.transport.rmdir(filename) assert not os.path.exists(filename) + def test_reset(self, multihost): + host = multihost.host + with _first_command(host): + echo = host.run_command(['echo', 'hello', 'world']) + assert echo.stdout_text == 'hello world\n' + + host.ssh_password = 'BAD PASSWORD' + host.ssh_key_filename = None + echo = host.run_command(['echo', 'hello', 'world']) + assert echo.stdout_text == 'hello world\n' + + host.reset_connection() + with pytest.raises((AuthenticationException, RuntimeError)): + echo = host.run_command(['echo', 'hello', 'world']) + def test_baduser(self, multihost_baduser, tmpdir): host = multihost_baduser.host