4040class SSHClientTest (unittest .TestCase ):
4141
4242 def setUp (self ):
43- self .fake_cmd = 'fake cmd '
44- self .fake_resp = 'fake response '
43+ self .fake_cmd = 'echo me '
44+ self .fake_resp = 'me '
4545 self .user_key = USER_KEY
46- self .listen_socket = make_socket ('127.0.0.1' )
46+ self .host = '127.0.0.1'
47+ self .listen_socket = make_socket (self .host )
4748 self .listen_port = self .listen_socket .getsockname ()[1 ]
49+ self .server = start_server (self .listen_socket )
50+
51+ def tearDown (self ):
52+ del self .server
53+ del self .listen_socket
4854
4955 def test_ssh_client_sftp (self ):
5056 """Test SFTP features of SSHClient. Copy local filename to server,
@@ -58,9 +64,7 @@ def test_ssh_client_sftp(self):
5864 test_file = open (local_filename , 'w' )
5965 test_file .writelines ([test_file_data + os .linesep ])
6066 test_file .close ()
61- server = start_server ({ self .fake_cmd : self .fake_resp },
62- self .listen_socket )
63- client = SSHClient ('127.0.0.1' , port = self .listen_port ,
67+ client = SSHClient (self .host , port = self .listen_port ,
6468 pkey = self .user_key )
6569 client .copy_file (local_filename , remote_filename )
6670 self .assertTrue (os .path .isdir (remote_test_dir ),
@@ -80,7 +84,6 @@ def test_ssh_client_sftp(self):
8084 for dirpath in [remote_dir , remote_test_dir ]:
8185 os .rmdir (dirpath )
8286 del client
83- server .join ()
8487
8588 def test_ssh_agent_authentication (self ):
8689 """Test authentication via SSH agent.
@@ -90,9 +93,7 @@ def test_ssh_agent_authentication(self):
9093 Key should be automatically picked up from the overriden agent"""
9194 agent = FakeAgent ()
9295 agent .add_key (USER_KEY )
93- server = start_server ({ self .fake_cmd : self .fake_resp },
94- self .listen_socket )
95- client = SSHClient ('127.0.0.1' , port = self .listen_port ,
96+ client = SSHClient (self .host , port = self .listen_port ,
9697 _agent = agent )
9798 channel , host , stdout , stderr = client .exec_command (self .fake_cmd )
9899 channel .close ()
@@ -102,17 +103,16 @@ def test_ssh_agent_authentication(self):
102103 self .assertEqual (expected , output ,
103104 msg = "Got unexpected command output - %s" % (output ,))
104105 del client
105- server .join ()
106106
107107 def test_ssh_client_conn_failure (self ):
108108 """Test connection error failure case - ConnectionErrorException"""
109109 self .assertRaises (ConnectionErrorException ,
110- SSHClient , '127.0.0.1 ' , port = self .listen_port ,
110+ SSHClient , '127.0.0.100 ' , port = self .listen_port ,
111111 pkey = self .user_key , num_retries = 0 )
112112
113113 def test_ssh_client_retries (self ):
114114 """Test connection error exceptions"""
115- self .assertRaises (ConnectionErrorException , SSHClient , '127.0.0.1 ' , port = self .listen_port ,
115+ self .assertRaises (ConnectionErrorException , SSHClient , '127.0.0.100 ' , port = self .listen_port ,
116116 pkey = self .user_key , num_retries = 1 )
117117 host = '' .join ([random .choice (string .ascii_letters ) for n in xrange (8 )])
118118 self .assertRaises (UnknownHostException , SSHClient , host , port = self .listen_port ,
@@ -127,9 +127,7 @@ def test_ssh_client_unknown_host_failure(self):
127127
128128 def test_ssh_client_pty (self ):
129129 """Test that we get a new pty for our non-interactive SSH sessions"""
130- server = start_server ({ self .fake_cmd : self .fake_resp },
131- self .listen_socket )
132- client = SSHClient ('127.0.0.1' , port = self .listen_port ,
130+ client = SSHClient (self .host , port = self .listen_port ,
133131 pkey = self .user_key )
134132 channel = client .client .get_transport ().open_session ()
135133 self .assertFalse (channel .event .is_set (),
@@ -140,7 +138,6 @@ def test_ssh_client_pty(self):
140138 channel .close ()
141139 del channel
142140 del client
143- server .join ()
144141
145142if __name__ == '__main__' :
146143 unittest .main ()
0 commit comments