summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorThomas Kluyver <takowl@gmail.com>2013-10-21 16:24:38 -0700
committerThomas Kluyver <takowl@gmail.com>2013-10-21 16:24:38 -0700
commit1e830ff97c628fc20ed727dc3fd0a4d971d279d5 (patch)
tree6ff1fe23c059eba99d508b04dc50c174520ed959 /tests
parent92f18089889c1c75b5ce4b31574e730bdb273530 (diff)
downloadpexpect-1e830ff97c628fc20ed727dc3fd0a4d971d279d5.tar.gz
Add test for pxssh module
Diffstat (limited to 'tests')
-rwxr-xr-xtests/fakessh/ssh23
-rw-r--r--tests/test_pxssh.py39
2 files changed, 62 insertions, 0 deletions
diff --git a/tests/fakessh/ssh b/tests/fakessh/ssh
new file mode 100755
index 0000000..318c18a
--- /dev/null
+++ b/tests/fakessh/ssh
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+import getpass
+import sys
+PY3 = (sys.version_info[0] >= 3)
+if not PY3:
+ input = raw_input
+
+print("Mock SSH client for tests. Do not enter real security info.")
+
+pw = getpass.getpass('password:')
+if pw != 's3cret':
+ print('Permission denied!')
+ sys.exit(1)
+
+prompt = "$"
+while True:
+ cmd = input(prompt)
+ if cmd.startswith('PS1='):
+ prompt = eval(cmd[4:]).replace('\$', '$')
+ elif cmd == 'ping':
+ print('pong')
+ elif cmd in ('exit', 'logout'):
+ break \ No newline at end of file
diff --git a/tests/test_pxssh.py b/tests/test_pxssh.py
new file mode 100644
index 0000000..8ffa95c
--- /dev/null
+++ b/tests/test_pxssh.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+import os
+import unittest
+
+from pexpect import pxssh
+
+class PxsshTestCase(unittest.TestCase):
+ def setUp(self):
+ self.orig_path = os.environ.get('PATH')
+ fakessh_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'fakessh'))
+ os.environ['PATH'] = fakessh_dir + \
+ ((os.pathsep + self.orig_path) if self.orig_path else '')
+
+ def tearDown(self):
+ if self.orig_path:
+ os.environ['PATH'] = self.orig_path
+ else:
+ del os.environ['PATH']
+
+ def test_fake_ssh(self):
+ ssh = pxssh.pxssh()
+ #ssh.logfile_read = sys.stdout # DEBUG
+ ssh.login('server', 'me', password='s3cret')
+ ssh.sendline('ping')
+ ssh.expect('pong', timeout=10)
+ assert ssh.prompt(timeout=10)
+ ssh.logout()
+
+ def test_wrong_pw(self):
+ ssh = pxssh.pxssh()
+ try:
+ ssh.login('server', 'me', password='wr0ng')
+ except pxssh.ExceptionPxssh:
+ pass
+ else:
+ assert False, 'Password should have been refused'
+
+if __name__ == '__main__':
+ unittest.main() \ No newline at end of file