forked from ParallelSSH/parallel-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_single_client.py
328 lines (293 loc) · 12.4 KB
/
test_single_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# This file is part of parallel-ssh.
#
# Copyright (C) 2014-2022 Panos Kittenis and contributors.
#
# 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
import logging
from datetime import datetime
from gevent import sleep, Timeout as GTimeout, spawn
from ssh.exceptions import AuthenticationDenied
from pssh.clients.ssh.single import SSHClient, logger as ssh_logger
from pssh.exceptions import AuthenticationException, ConnectionErrorException, \
SessionError, Timeout, \
AuthenticationError
from .base_ssh_case import SSHTestCase
ssh_logger.setLevel(logging.DEBUG)
logging.basicConfig()
class SSHClientTest(SSHTestCase):
def test_context_manager(self):
with SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=1) as client:
self.assertIsInstance(client, SSHClient)
def test_pkey_from_memory(self):
with open(self.user_key, 'rb') as fh:
key_data = fh.read()
SSHClient(self.host, port=self.port,
pkey=key_data, num_retries=1, timeout=1)
def test_execute(self):
host_out = self.client.run_command(self.cmd)
output = list(host_out.stdout)
stderr = list(host_out.stderr)
expected = [self.resp]
self.assertEqual(expected, output)
exit_code = host_out.channel.get_exit_status()
self.assertEqual(exit_code, 0)
def test_finished(self):
self.assertFalse(self.client.finished(None))
host_out = self.client.run_command(self.cmd)
channel = host_out.channel
self.assertFalse(self.client.finished(channel))
self.assertRaises(ValueError, self.client.wait_finished, host_out.channel)
self.client.wait_finished(host_out)
stdout = list(host_out.stdout)
self.assertTrue(self.client.finished(channel))
self.assertListEqual(stdout, [self.resp])
self.assertRaises(ValueError, self.client.wait_finished, None)
host_out.channel = None
self.assertIsNone(self.client.wait_finished(host_out))
def test_finished_error(self):
self.assertRaises(ValueError, self.client.wait_finished, None)
self.assertIsNone(self.client.finished(None))
def test_stderr(self):
host_out = self.client.run_command('echo "me" >&2')
self.client.wait_finished(host_out)
output = list(host_out.stdout)
stderr = list(host_out.stderr)
expected = ['me']
self.assertListEqual(expected, stderr)
self.assertEqual(len(output), 0)
def test_identity_auth(self):
class _SSHClient(SSHClient):
IDENTITIES = (self.user_key,)
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=1,
timeout=1,
allow_agent=False)
client.disconnect()
client.pkey = None
del client.session
del client.sock
client._connect(self.host, self.port)
client._init_session()
client.IDENTITIES = (self.user_key,)
# Default identities auth only should succeed
client._identity_auth()
client.disconnect()
del client.session
del client.sock
client._connect(self.host, self.port)
client._init_session()
# Auth should succeed
self.assertIsNone(client.auth())
# Standard init with custom identities
client = _SSHClient(self.host, port=self.port,
num_retries=1,
allow_agent=False)
self.assertIsInstance(client, SSHClient)
def test_long_running_cmd(self):
host_out = self.client.run_command('sleep .2; exit 2')
self.assertRaises(ValueError, self.client.wait_finished, host_out.channel)
self.client.wait_finished(host_out)
exit_code = host_out.exit_code
self.assertEqual(exit_code, 2)
def test_wait_finished_timeout(self):
host_out = self.client.run_command('sleep .2')
timeout = .1
self.assertFalse(self.client.finished(host_out.channel))
start = datetime.now()
self.assertRaises(Timeout, self.client.wait_finished, host_out, timeout=timeout)
dt = datetime.now() - start
self.assertTrue(timeout*1.1 > dt.total_seconds() > timeout)
self.client.wait_finished(host_out)
self.assertTrue(self.client.finished(host_out.channel))
def test_client_disconnect_on_del(self):
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=1)
client_sock = client.sock
del client
self.assertTrue(client_sock.closed)
def test_client_bad_sock(self):
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=1)
client.disconnect()
client.sock = None
self.assertIsNone(client.poll())
def test_multiple_clients_exec_terminates_channels(self):
# See #200 - Multiple clients should not interfere with
# each other. session.disconnect can leave state in library
# and break subsequent sessions even on different socket and
# session
def scope_killer():
for _ in range(5):
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=1,
allow_agent=False)
host_out = client.run_command(self.cmd)
output = list(host_out.stdout)
self.assertListEqual(output, [self.resp])
scope_killer()
def test_interactive_shell(self):
with self.client.open_shell() as shell:
shell.run(self.cmd)
shell.run(self.cmd)
stdout = list(shell.output.stdout)
self.assertListEqual(stdout, [self.resp, self.resp])
self.assertEqual(shell.output.exit_code, 0)
def test_interactive_shell_exit_code(self):
with self.client.open_shell() as shell:
shell.run(self.cmd)
shell.run('sleep .1')
shell.run(self.cmd)
shell.run('exit 1')
stdout = list(shell.output.stdout)
self.assertListEqual(stdout, [self.resp, self.resp])
self.assertEqual(shell.output.exit_code, 1)
def test_identity_auth_failure(self):
self.assertRaises(AuthenticationException,
SSHClient, self.host, port=self.port, num_retries=1,
allow_agent=False)
def test_password_auth_failure(self):
try:
client = SSHClient(self.host, port=self.port, num_retries=1,
allow_agent=False,
identity_auth=False,
password='blah blah blah',
)
except AuthenticationException as ex:
self.assertIsInstance(ex.args[3], AuthenticationDenied)
else:
raise AssertionError
def test_retry_failure(self):
self.assertRaises(ConnectionErrorException,
SSHClient, self.host, port=12345,
timeout=1,
retry_delay=.1,
num_retries=2, _auth_thread_pool=False)
def test_auth_retry_failure(self):
self.assertRaises(AuthenticationException,
SSHClient, self.host, port=self.port,
user=self.user,
password='fake',
timeout=1,
retry_delay=.1,
num_retries=2,
allow_agent=False,
identity_auth=False,
)
def test_connection_timeout(self):
cmd = spawn(SSHClient, 'fakehost.com', port=12345,
retry_delay=.1,
num_retries=2, timeout=.2, _auth_thread_pool=False)
# Should fail within greenlet timeout, otherwise greenlet will
# raise timeout which will fail the test
self.assertRaises(ConnectionErrorException, cmd.get, timeout=2)
def test_open_session_timeout(self):
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=2,
timeout=.1)
def _session(timeout=None):
sleep(.2)
client.open_session = _session
self.assertRaises(GTimeout, client.run_command, self.cmd)
def test_client_read_timeout(self):
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=1)
host_out = client.run_command('sleep 2; echo me', timeout=0.2)
self.assertRaises(Timeout, list, host_out.stdout)
def test_open_session_exc(self):
class Error(Exception):
pass
def _session():
raise Error
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=1)
client._open_session = _session
self.assertRaises(SessionError, client.open_session)
def test_session_connect_exc(self):
class Error(Exception):
pass
def _con():
raise Error
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=2,
retry_delay=.2,
)
client._session_connect = _con
self.assertRaises(Error, client._init_session)
def test_invalid_mkdir(self):
self.assertRaises(OSError, self.client._make_local_dir, '/my_new_dir')
def test_no_auth(self):
self.assertRaises(
AuthenticationError,
SSHClient,
self.host,
port=self.port,
num_retries=1,
allow_agent=False,
identity_auth=False,
)
def test_agent_auth_failure(self):
class UnknownError(Exception):
pass
def _agent_auth_unk():
raise UnknownError
def _agent_auth_agent_err():
raise AuthenticationDenied
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=1,
allow_agent=True,
identity_auth=False)
client.session.disconnect()
client.pkey = None
client._connect(self.host, self.port)
client._agent_auth = _agent_auth_unk
self.assertRaises(AuthenticationError, client.auth)
client._agent_auth = _agent_auth_agent_err
self.assertRaises(AuthenticationError, client.auth)
def test_agent_auth_fake_success(self):
def _agent_auth():
return
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=1,
allow_agent=True,
identity_auth=False)
client.session.disconnect()
client.pkey = None
client._connect(self.host, self.port)
client._agent_auth = _agent_auth
self.assertIsNone(client.auth())
def test_disconnect_exc(self):
class DiscError(Exception):
pass
def _disc():
raise DiscError
client = SSHClient(self.host, port=self.port,
pkey=self.user_key,
num_retries=1,
retry_delay=.1,
)
client._disconnect_eagain = _disc
client._connect_init_session_retry(0)
client.disconnect()