2727import shutil
2828import unittest
2929from pssh import SSHClient , ParallelSSHClient , UnknownHostException , AuthenticationException ,\
30- logger , ConnectionErrorException , UnknownHostException , SSHException
30+ logger , ConnectionErrorException , UnknownHostException , SSHException , utils
3131from embedded_server .embedded_server import start_server , make_socket , logger as server_logger , \
3232 paramiko_logger
3333from embedded_server .fake_agent import FakeAgent
3434import paramiko
3535import os
3636from test_pssh_client import USER_KEY
3737import 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
4243class 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
292319if __name__ == '__main__' :
293320 unittest .main ()
0 commit comments