- Notifications
You must be signed in to change notification settings - Fork 415
/
Copy pathtest_ssh.py
43 lines (39 loc) · 1.27 KB
/
test_ssh.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
# -*- coding: utf-8 -*-
importmock
fromStringIOimportStringIO
fromcontextlibimportcontextmanager
fromparamikoimportSSHClient
importapp.utils.SshUtilasssh
from . importRSA_PRIVATE_KEY
@contextmanager
defpatch_ssh(stdout='', stderr=''):
defmock_exec_command(*args, **kwargs):
stdin=StringIO()
stdin.close()
returnstdin, StringIO(stdout), StringIO(stderr)
withmock.patch.object(SSHClient, 'exec_command', new=mock_exec_command):
withmock.patch.object(SSHClient, 'connect'):
yield
deftest_ssh():
kwargs=dict(
ip='127.0.0.1',
port='22',
account='root',
pkey=RSA_PRIVATE_KEY,
shell='echo hello\n echo hello'
)
withpatch_ssh('', ''):
success, log=ssh.do_ssh_cmd(**kwargs)
assertsuccess
withpatch_ssh('', 'message'):
success, log=ssh.do_ssh_cmd(**kwargs)
assertnotsuccess
assert'message'inlog
msg= ('fatal: Not a git repository '
'(or any parent up to mount point /tmp)\n'
'Stopping at filesystem boundary '
'(GIT_DISCOVERY_ACROSS_FILESYSTEM not set).')
withpatch_ssh(msg, ''):
success, log=ssh.do_ssh_cmd(**kwargs)
assertnotsuccess
assertmsginlog