Skip to content

Commit 418c380

Browse files
author
Dan
committed
Added unit and integration test for openssh config reading. Resolves #55
1 parent 982a060 commit 418c380

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

pssh/ssh_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def __init__(self, host,
4444
pkey=None, forward_ssh_agent=True,
4545
num_retries=DEFAULT_RETRIES, agent=None,
4646
allow_agent=True, timeout=10, proxy_host=None,
47-
proxy_port=22, channel_timeout=None):
47+
proxy_port=22, channel_timeout=None,
48+
_openssh_config_file=None):
4849
"""Connect to host honouring any user set configuration in ~/.ssh/config \
4950
or /etc/ssh/ssh_config
5051
@@ -93,7 +94,8 @@ def __init__(self, host,
9394
the SSH agent
9495
:type allow_agent: bool
9596
"""
96-
host, _user, _port, _pkey = read_openssh_config(host)
97+
host, _user, _port, _pkey = read_openssh_config(
98+
host, config_file=_openssh_config_file)
9799
user = user if user else _user
98100
client = paramiko.SSHClient()
99101
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())

tests/test_ssh_client.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@
2727
import shutil
2828
import unittest
2929
from pssh import SSHClient, ParallelSSHClient, UnknownHostException, AuthenticationException,\
30-
logger, ConnectionErrorException, UnknownHostException, SSHException
30+
logger, ConnectionErrorException, UnknownHostException, SSHException, utils
3131
from embedded_server.embedded_server import start_server, make_socket, logger as server_logger, \
3232
paramiko_logger
3333
from embedded_server.fake_agent import FakeAgent
3434
import paramiko
3535
import os
3636
from test_pssh_client import USER_KEY
3737
import random, string
38+
import tempfile
3839

39-
USER_KEY = paramiko.RSAKey.from_private_key_file(
40-
os.path.sep.join([os.path.dirname(__file__), 'test_client_private_key']))
40+
USER_KEY_PATH = os.path.sep.join([os.path.dirname(__file__), 'test_client_private_key'])
41+
USER_KEY = paramiko.RSAKey.from_private_key_file(USER_KEY_PATH)
4142

4243
class SSHClientTest(unittest.TestCase):
4344

@@ -261,7 +262,7 @@ def test_ssh_client_utf_encoding(self):
261262
output, expected,))
262263
del client
263264

264-
def test_ssh_client_pty(self):
265+
def test_ssh_client_shell(self):
265266
"""Test that running command sans shell works as expected
266267
and that shell commands fail accordingly"""
267268
client = SSHClient(self.host, port=self.listen_port,
@@ -273,7 +274,7 @@ def test_ssh_client_pty(self):
273274
exit_code = channel.recv_exit_status()
274275
self.assertEqual(expected, output,
275276
msg = "Got unexpected command output - %s" % (output,))
276-
self.assertTrue(exit_code==127,
277+
self.assertTrue(exit_code == 127,
277278
msg="Expected cmd not found error code 127, got %s instead" % (
278279
exit_code,))
279280
channel, host, stdout, stderr, stdin = client.exec_command('id', use_shell=False)
@@ -287,7 +288,33 @@ def test_ssh_client_pty(self):
287288
del client
288289

289290
def test_openssh_config(self):
290-
pass
291+
"""Test reading and using OpenSSH config file"""
292+
config_file = tempfile.NamedTemporaryFile()
293+
_host = "127.0.0.2"
294+
_user = "config_user"
295+
_listen_socket = make_socket(_host)
296+
_server = start_server(_listen_socket)
297+
_port = _listen_socket.getsockname()[1]
298+
_key = USER_KEY_PATH
299+
content = ["""Host %s\n""" % (_host,),
300+
""" User %s\n""" % (_user,),
301+
""" Port %s\n""" % (_port,),
302+
""" IdentityFile %s\n""" % (_key,),
303+
]
304+
config_file.writelines(content)
305+
config_file.flush()
306+
host, user, port, pkey = utils.read_openssh_config(
307+
_host, config_file=config_file.name)
308+
self.assertEqual(host, _host)
309+
self.assertEqual(user, _user)
310+
self.assertEqual(port, _port)
311+
self.assertTrue(pkey)
312+
client = SSHClient(_host, _openssh_config_file=config_file.name)
313+
self.assertEqual(client.host, _host)
314+
self.assertEqual(client.user, _user)
315+
self.assertEqual(client.port, _port)
316+
self.assertTrue(client.pkey)
317+
del _server, _listen_socket
291318

292319
if __name__ == '__main__':
293320
unittest.main()

0 commit comments

Comments
 (0)