diff --git a/test.py b/test.py index e4d5746..1b77cb4 100755 --- a/test.py +++ b/test.py @@ -23,13 +23,53 @@ do the unit tests! """ import sys, os, unittest +from optparse import OptionParser +import paramiko + sys.path.append('tests/') +from test_message import MessageTest from test_file import BufferedFileTest +from test_pkey import KeyTest +#from test_transport import TransportTest from test_sftp import SFTPTest -suite = unittest.TestSuite() -suite.addTest(unittest.makeSuite(BufferedFileTest)) -suite.addTest(unittest.makeSuite(SFTPTest)) -unittest.TextTestRunner(verbosity=2).run(suite) +default_host = 'localhost' +default_user = os.environ.get('USER', 'nobody') +default_keyfile = os.path.join(os.environ.get('HOME', '/'), '.ssh/id_rsa') +default_passwd = None +parser = OptionParser('usage: %prog [options]') +parser.add_option('--sftp', action='store_true', dest='use_sftp', default=False, + help='run sftp tests (currently require an external sftp server)') +parser.add_option('-H', '--sftp-host', dest='hostname', type='string', default=default_host, + metavar='', + help='remote host for sftp tests (default: %s)' % default_host) +parser.add_option('-U', '--sftp-user', dest='username', type='string', default=default_user, + metavar='', + help='username for sftp tests (default: %s)' % default_user) +parser.add_option('-K', '--sftp-key', dest='keyfile', type='string', default=default_keyfile, + metavar='', + help='location of private key for sftp tests (default: %s)' % default_keyfile) +parser.add_option('-P', '--sftp-passwd', dest='password', type='string', default=default_passwd, + metavar='', + help='(optional) password to unlock the private key for sftp tests') + +options, args = parser.parse_args() +if len(args) > 0: + parser.error('unknown argument(s)') + +if options.use_sftp: + SFTPTest.init(options.hostname, options.username, options.keyfile, options.password) + +# setup logging +paramiko.util.log_to_file('test.log') + +suite = unittest.TestSuite() +suite.addTest(unittest.makeSuite(MessageTest)) +suite.addTest(unittest.makeSuite(BufferedFileTest)) +suite.addTest(unittest.makeSuite(KeyTest)) +#suite.addTest(unittest.makeSuite(TransportTest)) +if options.use_sftp: + suite.addTest(unittest.makeSuite(SFTPTest)) +unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/tests/test_sftp.py b/tests/test_sftp.py index e16b061..f112561 100755 --- a/tests/test_sftp.py +++ b/tests/test_sftp.py @@ -29,13 +29,6 @@ import sys, os import random import logging -# need a host and private-key where we have acecss -HOST = os.environ.get('TEST_HOST', 'localhost') -USER = os.environ.get('TEST_USER', os.environ.get('USER', 'nobody')) -PKEY = os.environ.get('TEST_PKEY', os.path.join(os.environ.get('HOME', '/'), '.ssh/id_rsa')) -PKEY_PASSWD = os.environ.get('TEST_PKEY_PASSWD', None) -FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing') - import paramiko, unittest ARTICLE = ''' @@ -65,42 +58,45 @@ liver insulin receptors. Their sensitivity to insulin is, however, similarly decreased compared with chicken. ''' +FOLDER = os.environ.get('TEST_FOLDER', 'temp-testing') -# setup logging -paramiko.util.log_to_file('test.log') - -t = paramiko.Transport(HOST) -try: - key = paramiko.RSAKey.from_private_key_file(PKEY, PKEY_PASSWD) -except paramiko.PasswordRequiredException: - sys.stderr.write('\n\nparamiko.RSAKey.from_private_key_file REQUIRES PASSWORD.\n') - sys.stderr.write('You have two options:\n') - sys.stderr.write('* Change environment variable TEST_PKEY to point to a different\n') - sys.stderr.write(' (non-password-protected) private key file.\n') - sys.stderr.write('* Set environment variable TEST_PKEY_PASSWD to the password needed\n') - sys.stderr.write(' to unlock this private key.\n') - sys.stderr.write('\n') - sys.exit(1) - -try: - t.connect(username=USER, pkey=key) -except paramiko.SSHException: - t.close() - sys.stderr.write('\n\nparamiko.Transport.connect FAILED.\n') - sys.stderr.write('There are several possible reasons why it might fail so quickly:\n\n') - sys.stderr.write('* The host to connect to (%s) is not a valid SSH server.\n' % HOST) - sys.stderr.write(' (Override the SSH host with environment variable TEST_HOST.)\n') - sys.stderr.write('* The username to auth as (%s) is invalid.\n' % USER) - sys.stderr.write(' (Override the auth user with environment variable TEST_USER.)\n') - sys.stderr.write('* The private key given (%s) is not accepted by the server.\n' % PKEY) - sys.stderr.write(' (Override the private key location with environment variable TEST_PKEY.)\n') - sys.stderr.write('\n') - sys.exit(1) -sftp = paramiko.SFTP.from_transport(t) +sftp = None class SFTPTest (unittest.TestCase): + def init(hostname, username, keyfile, passwd): + global sftp + + t = paramiko.Transport(hostname) + try: + key = paramiko.RSAKey.from_private_key_file(keyfile, passwd) + except paramiko.PasswordRequiredException: + sys.stderr.write('\n\nparamiko.RSAKey.from_private_key_file REQUIRES PASSWORD.\n') + sys.stderr.write('You have two options:\n') + sys.stderr.write('* Use the "-K" option to point to a different (non-password-protected)\n') + sys.stderr.write(' private key file.\n') + sys.stderr.write('* Use the "-P" option to provide the password needed to unlock this private\n') + sys.stderr.write(' key.\n') + sys.stderr.write('\n') + sys.exit(1) + try: + t.connect(username=username, pkey=key) + except paramiko.SSHException: + t.close() + sys.stderr.write('\n\nparamiko.Transport.connect FAILED.\n') + sys.stderr.write('There are several possible reasons why it might fail so quickly:\n\n') + sys.stderr.write('* The host to connect to (%s) is not a valid SSH server.\n' % hostname) + sys.stderr.write(' (Use the "-H" option to change the host.)\n') + sys.stderr.write('* The username to auth as (%s) is invalid.\n' % username) + sys.stderr.write(' (Use the "-U" option to change the username.)\n') + sys.stderr.write('* The private key given (%s) is not accepted by the server.\n' % keyfile) + sys.stderr.write(' (Use the "-K" option to provide a different key file.)\n') + sys.stderr.write('\n') + sys.exit(1) + sftp = paramiko.SFTP.from_transport(t) + init = staticmethod(init) + def setUp(self): sftp.mkdir(FOLDER)