-
Notifications
You must be signed in to change notification settings - Fork 147
/
test_ssh_client.py
199 lines (176 loc) · 8.12 KB
/
test_ssh_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# This file is part of parallel-ssh.
# Copyright (C) 2015 Panos Kittenis
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, version 2.1.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Unittests for :mod:`pssh.SSHClient` class"""
import gevent
import socket
import time
import unittest
from pssh import SSHClient, ParallelSSHClient, UnknownHostException, AuthenticationException,\
logger, ConnectionErrorException, UnknownHostException, SSHException
from embedded_server.embedded_server import start_server, make_socket, logger as server_logger, \
paramiko_logger
from embedded_server.fake_agent import FakeAgent
import paramiko
import os
from test_pssh_client import USER_KEY
import random, string
import shutil
USER_KEY = paramiko.RSAKey.from_private_key_file(
os.path.sep.join([os.path.dirname(__file__), 'test_client_private_key']))
class SSHClientTest(unittest.TestCase):
def setUp(self):
self.fake_cmd = 'echo me'
self.fake_resp = 'me'
self.user_key = USER_KEY
self.host = '127.0.0.1'
self.listen_socket = make_socket(self.host)
self.listen_port = self.listen_socket.getsockname()[1]
self.server = start_server(self.listen_socket)
def tearDown(self):
del self.server
del self.listen_socket
def test_ssh_client_mkdir_recursive(self):
"""Test SFTP mkdir of SSHClient"""
base_path = 'remote_test_dir1'
remote_dir = os.path.sep.join([base_path,
'remote_test_dir2',
'remote_test_dir3'])
try:
shutil.rmtree(base_path)
except OSError:
pass
client = SSHClient(self.host, port=self.listen_port,
pkey=self.user_key)
client.mkdir(client._make_sftp(), remote_dir)
self.assertTrue(os.path.isdir(remote_dir),
msg="SFTP recursive mkdir failed")
shutil.rmtree(base_path)
del client
def test_ssh_client_mkdir_recursive_abspath(self):
"""Test SFTP mkdir of SSHClient with absolute path
Absolute SFTP paths resolve under the users' home directory,
not the root filesystem
"""
base_path = 'tmp'
remote_dir = os.path.sep.join([base_path,
'remote_test_dir2',
'remote_test_dir3'])
try:
shutil.rmtree(base_path)
except OSError:
pass
client = SSHClient(self.host, port=self.listen_port,
pkey=self.user_key)
client.mkdir(client._make_sftp(), '/' + remote_dir)
self.assertTrue(os.path.isdir(remote_dir),
msg="SFTP recursive mkdir failed")
shutil.rmtree(base_path)
del client
def test_ssh_client_mkdir_single(self):
"""Test SFTP mkdir of SSHClient"""
remote_dir = 'remote_test_dir1'
try:
shutil.rmtree(remote_dir)
except OSError:
pass
client = SSHClient(self.host, port=self.listen_port,
pkey=self.user_key)
client.mkdir(client._make_sftp(), remote_dir)
self.assertTrue(os.path.isdir(remote_dir),
msg="SFTP recursive mkdir failed")
shutil.rmtree(remote_dir)
del client
def test_ssh_client_sftp(self):
"""Test SFTP features of SSHClient. Copy local filename to server,
check that data in both files is the same, make new directory on
server, remove files and directory."""
test_file_data = 'test'
local_filename = 'test_file'
remote_test_dir, remote_filename = 'remote_test_dir', 'test_file_copy'
remote_filename = os.path.sep.join([remote_test_dir, remote_filename])
remote_dir = 'remote_dir'
test_file = open(local_filename, 'w')
test_file.writelines([test_file_data + os.linesep])
test_file.close()
client = SSHClient(self.host, port=self.listen_port,
pkey=self.user_key)
client.copy_file(local_filename, remote_filename)
self.assertTrue(os.path.isdir(remote_test_dir),
msg="SFTP create remote directory failed")
self.assertTrue(os.path.isfile(remote_filename),
msg="SFTP copy failed")
copied_file = open(remote_filename, 'r')
copied_file_data = copied_file.readlines()[0].strip()
copied_file.close()
self.assertEqual(test_file_data, copied_file_data,
msg="Data in destination file %s does \
not match source %s" % (copied_file_data, test_file_data))
for filepath in [local_filename, remote_filename]:
os.unlink(filepath)
client.mkdir(client._make_sftp(), remote_dir)
self.assertTrue(os.path.isdir(remote_dir))
for dirpath in [remote_dir, remote_test_dir]:
os.rmdir(dirpath)
del client
def test_ssh_agent_authentication(self):
"""Test authentication via SSH agent.
Do not provide public key to use when creating SSHClient,
instead override the client's agent with our own fake SSH agent,
add our to key to agent and try to login to server.
Key should be automatically picked up from the overriden agent"""
agent = FakeAgent()
agent.add_key(USER_KEY)
client = SSHClient(self.host, port=self.listen_port,
_agent=agent)
channel, host, stdout, stderr = client.exec_command(self.fake_cmd)
channel.close()
output = list(stdout)
stderr = list(stderr)
expected = [self.fake_resp]
self.assertEqual(expected, output,
msg = "Got unexpected command output - %s" % (output,))
del client
def test_ssh_client_conn_failure(self):
"""Test connection error failure case - ConnectionErrorException"""
self.assertRaises(ConnectionErrorException,
SSHClient, '127.0.0.100', port=self.listen_port,
pkey=self.user_key, num_retries=0)
def test_ssh_client_retries(self):
"""Test connection error exceptions"""
self.assertRaises(ConnectionErrorException, SSHClient, '127.0.0.100', port=self.listen_port,
pkey=self.user_key, num_retries=1)
host = ''.join([random.choice(string.ascii_letters) for n in xrange(8)])
self.assertRaises(UnknownHostException, SSHClient, host, port=self.listen_port,
pkey=self.user_key, num_retries=1)
def test_ssh_client_unknown_host_failure(self):
"""Test connection error failure case - ConnectionErrorException"""
host = ''.join([random.choice(string.ascii_letters) for n in xrange(8)])
self.assertRaises(UnknownHostException,
SSHClient, host, port=self.listen_port,
pkey=self.user_key, num_retries=0)
def test_ssh_client_pty(self):
"""Test that we get a new pty for our non-interactive SSH sessions"""
client = SSHClient(self.host, port=self.listen_port,
pkey=self.user_key)
channel = client.client.get_transport().open_session()
self.assertFalse(channel.event.is_set(),
msg="Got pty without requesting it")
channel.get_pty()
self.assertTrue(channel.event.is_set(),
msg="Requested pty but got none")
channel.close()
del channel
del client
if __name__ == '__main__':
unittest.main()