From 5b2c5e309bdf6696dbc5a528271cffc49f858a8c Mon Sep 17 00:00:00 2001 From: Jonas Depoix Date: Tue, 6 Jun 2017 17:00:33 +0200 Subject: [PATCH] remtoe execution refactored to do connect as needed and not right away in the constructor --- goto_cloud/remote_execution/remote_execution.py | 3 +-- .../tests/test_remote_execution.py | 16 +++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/goto_cloud/remote_execution/remote_execution.py b/goto_cloud/remote_execution/remote_execution.py index 7ac3311..6eba687 100644 --- a/goto_cloud/remote_execution/remote_execution.py +++ b/goto_cloud/remote_execution/remote_execution.py @@ -39,7 +39,7 @@ class ExecutionException(Exception): def __init__(self, hostname, username=None, password=None, port=22): """ - initializes the RemoteExecutor and opens a connection using the implemented remote client + initializes the RemoteExecutor using the implemented remote client :param hostname: the hostname to use :type hostname: str @@ -55,7 +55,6 @@ def __init__(self, hostname, username=None, password=None, port=22): self.username = username self.password = password self.port = port - self.connect() def close(self): """ diff --git a/goto_cloud/remote_execution/tests/test_remote_execution.py b/goto_cloud/remote_execution/tests/test_remote_execution.py index c48182f..953706b 100644 --- a/goto_cloud/remote_execution/tests/test_remote_execution.py +++ b/goto_cloud/remote_execution/tests/test_remote_execution.py @@ -58,28 +58,32 @@ def raise_exception(self, *args, **kwargs): return raise_exception +@patch('paramiko.SSHClient.connect', connect_mock) @patch('paramiko.SSHClient.close', close_mock) @patch('paramiko.SSHClient.get_transport', lambda self: self.connected if self.connected else None) @patch('paramiko.SSHClient.exec_command', execute_mock) class TestSshRemoteExecutor(unittest.TestCase): - @patch('paramiko.SSHClient.connect', connect_mock) def setUp(self): self.remote_executor = SshRemoteExecutor('test') def test_close(self): + self.remote_executor.execute('successful_command') self.assertTrue(self.remote_executor.remote_client.connected) self.remote_executor.close() self.assertFalse(self.remote_executor.remote_client.connected) def test_connect(self): + self.remote_executor.execute('successful_command') self.assertTrue(self.remote_executor.remote_client.connected) def test_reconnect(self): + self.remote_executor.execute('successful_command') self.assertTrue(self.remote_executor.is_connected()) self.remote_executor.reconnect() self.assertTrue(self.remote_executor.is_connected()) def test_is_connected(self): + self.remote_executor.execute('successful_command') self.assertTrue(self.remote_executor.is_connected()) self.remote_executor.close() self.assertFalse(self.remote_executor.is_connected()) @@ -102,19 +106,19 @@ def test_connect__connection_exception(self): SSHClient.connect = raise_exception_mock(SSHException()) with self.assertRaises(RemoteExecutor.ConnectionException): - SshRemoteExecutor('test') + SshRemoteExecutor('test').execute('successful_command') def test_connect__authentication_exception(self): SSHClient.connect = raise_exception_mock(AuthenticationException()) with self.assertRaises(RemoteExecutor.AuthenticationException): - SshRemoteExecutor('test') + SshRemoteExecutor('test').execute('successful_command') def test_connect__no_valid_connection_exception(self): SSHClient.connect = raise_exception_mock(NoValidConnectionsError({'127.0.0.1': 22})) with self.assertRaises(RemoteExecutor.NoValidConnectionException): - SshRemoteExecutor('test') + SshRemoteExecutor('test').execute('successful_command') def test_execute__raise_no_exception_on_failure(self): self.assertEqual( @@ -123,12 +127,12 @@ def test_execute__raise_no_exception_on_failure(self): ) +@patch('paramiko.SSHClient.connect', connect_mock) @patch('paramiko.SSHClient.connect', connect_mock) @patch('paramiko.SSHClient.close', close_mock) @patch('paramiko.SSHClient.get_transport', lambda self: self.connected if self.connected else None) @patch('paramiko.SSHClient.exec_command', execute_mock) class TestRemoteHostExecutor(TestCase, TestSshRemoteExecutor): - @patch('paramiko.SSHClient.connect', connect_mock) def setUp(self): self.remote_executor = RemoteHostExecutor(RemoteHost.objects.create(os=OperatingSystem.LINUX)) @@ -161,11 +165,13 @@ def test_initialization__os_not_supported(self): RemoteHostExecutor(RemoteHost.objects.create(os=OperatingSystem.WINDOWS)) def test_close(self): + self.remote_executor.execute('successful_command') self.assertTrue(self.remote_executor.operator.remote_client.connected) self.remote_executor.close() self.assertFalse(self.remote_executor.operator.remote_client.connected) def test_connect(self): + self.remote_executor.execute('successful_command') self.remote_executor.close() self.assertFalse(self.remote_executor.operator.remote_client.connected) self.remote_executor.connect()