Skip to content

Commit

Permalink
remtoe execution refactored to do connect as needed and not right awa…
Browse files Browse the repository at this point in the history
…y in the constructor
  • Loading branch information
Jonas Depoix committed Jun 6, 2017
1 parent 46dbc01 commit 5b2c5e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
3 changes: 1 addition & 2 deletions goto_cloud/remote_execution/remote_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down
16 changes: 11 additions & 5 deletions goto_cloud/remote_execution/tests/test_remote_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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(
Expand All @@ -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))

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5b2c5e3

Please sign in to comment.