clean up test.py a bit and allow filtering from the command line
This commit is contained in:
Robey Pointer 2006-07-31 00:01:37 -07:00
parent 5bbe1a8f36
commit 157484f5eb
1 changed files with 95 additions and 61 deletions

156
test.py
View File

@ -22,7 +22,10 @@
do the unit tests! do the unit tests!
""" """
import sys, os, unittest import os
import re
import sys
import unittest
from optparse import OptionParser from optparse import OptionParser
import paramiko import paramiko
@ -46,67 +49,98 @@ default_user = os.environ.get('USER', 'nobody')
default_keyfile = os.path.join(os.environ.get('HOME', '/'), '.ssh/id_rsa') default_keyfile = os.path.join(os.environ.get('HOME', '/'), '.ssh/id_rsa')
default_passwd = None default_passwd = None
parser = OptionParser('usage: %prog [options]')
parser.add_option('--verbose', action='store_true', dest='verbose', default=False,
help='verbose display (one line per test)')
parser.add_option('--no-pkey', action='store_false', dest='use_pkey', default=True,
help='skip RSA/DSS private key tests (which can take a while)')
parser.add_option('--no-transport', action='store_false', dest='use_transport', default=True,
help='skip transport tests (which can take a while)')
parser.add_option('--no-sftp', action='store_false', dest='use_sftp', default=True,
help='skip SFTP client/server tests, which can be slow')
parser.add_option('--no-big-file', action='store_false', dest='use_big_file', default=True,
help='skip big file SFTP tests, which are slow as molasses')
parser.add_option('-R', action='store_false', dest='use_loopback_sftp', default=True,
help='perform SFTP tests against a remote server (by default, SFTP tests ' +
'are done through a loopback socket)')
parser.add_option('-H', '--sftp-host', dest='hostname', type='string', default=default_host,
metavar='<host>',
help='[with -R] host for remote sftp tests (default: %s)' % default_host)
parser.add_option('-U', '--sftp-user', dest='username', type='string', default=default_user,
metavar='<username>',
help='[with -R] username for remote sftp tests (default: %s)' % default_user)
parser.add_option('-K', '--sftp-key', dest='keyfile', type='string', default=default_keyfile,
metavar='<keyfile>',
help='[with -R] location of private key for remote sftp tests (default: %s)' %
default_keyfile)
parser.add_option('-P', '--sftp-passwd', dest='password', type='string', default=default_passwd,
metavar='<password>',
help='[with -R] (optional) password to unlock the private key for remote sftp tests')
options, args = parser.parse_args() def iter_suite_tests(suite):
if len(args) > 0: """Return all tests in a suite, recursing through nested suites"""
parser.error('unknown argument(s)') for item in suite._tests:
if isinstance(item, unittest.TestCase):
yield item
elif isinstance(item, unittest.TestSuite):
for r in iter_suite_tests(item):
yield r
else:
raise Exception('unknown object %r inside test suite %r'
% (item, suite))
# setup logging
paramiko.util.log_to_file('test.log')
if options.use_sftp: def filter_suite_by_re(suite, pattern):
if options.use_loopback_sftp: result = unittest.TestSuite()
SFTPTest.init_loopback() filter_re = re.compile(pattern)
else: for test in iter_suite_tests(suite):
SFTPTest.init(options.hostname, options.username, options.keyfile, options.password) if filter_re.search(test.id()):
if not options.use_big_file: result.addTest(test)
SFTPTest.set_big_file_test(False) return result
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(MessageTest)) def main():
suite.addTest(unittest.makeSuite(BufferedFileTest)) parser = OptionParser('usage: %prog [options]')
suite.addTest(unittest.makeSuite(BufferedPipeTest)) parser.add_option('--verbose', action='store_true', dest='verbose', default=False,
suite.addTest(unittest.makeSuite(UtilTest)) help='verbose display (one line per test)')
suite.addTest(unittest.makeSuite(HostKeysTest)) parser.add_option('--no-pkey', action='store_false', dest='use_pkey', default=True,
if options.use_pkey: help='skip RSA/DSS private key tests (which can take a while)')
suite.addTest(unittest.makeSuite(KeyTest)) parser.add_option('--no-transport', action='store_false', dest='use_transport', default=True,
suite.addTest(unittest.makeSuite(KexTest)) help='skip transport tests (which can take a while)')
suite.addTest(unittest.makeSuite(PacketizerTest)) parser.add_option('--no-sftp', action='store_false', dest='use_sftp', default=True,
if options.use_transport: help='skip SFTP client/server tests, which can be slow')
suite.addTest(unittest.makeSuite(TransportTest)) parser.add_option('--no-big-file', action='store_false', dest='use_big_file', default=True,
suite.addTest(unittest.makeSuite(SSHClientTest)) help='skip big file SFTP tests, which are slow as molasses')
if options.use_sftp: parser.add_option('-R', action='store_false', dest='use_loopback_sftp', default=True,
suite.addTest(unittest.makeSuite(SFTPTest)) help='perform SFTP tests against a remote server (by default, SFTP tests ' +
if options.use_big_file: 'are done through a loopback socket)')
suite.addTest(unittest.makeSuite(BigSFTPTest)) parser.add_option('-H', '--sftp-host', dest='hostname', type='string', default=default_host,
verbosity = 1 metavar='<host>',
if options.verbose: help='[with -R] host for remote sftp tests (default: %s)' % default_host)
verbosity = 2 parser.add_option('-U', '--sftp-user', dest='username', type='string', default=default_user,
unittest.TextTestRunner(verbosity=verbosity).run(suite) metavar='<username>',
help='[with -R] username for remote sftp tests (default: %s)' % default_user)
parser.add_option('-K', '--sftp-key', dest='keyfile', type='string', default=default_keyfile,
metavar='<keyfile>',
help='[with -R] location of private key for remote sftp tests (default: %s)' %
default_keyfile)
parser.add_option('-P', '--sftp-passwd', dest='password', type='string', default=default_passwd,
metavar='<password>',
help='[with -R] (optional) password to unlock the private key for remote sftp tests')
options, args = parser.parse_args()
# setup logging
paramiko.util.log_to_file('test.log')
if options.use_sftp:
if options.use_loopback_sftp:
SFTPTest.init_loopback()
else:
SFTPTest.init(options.hostname, options.username, options.keyfile, options.password)
if not options.use_big_file:
SFTPTest.set_big_file_test(False)
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(MessageTest))
suite.addTest(unittest.makeSuite(BufferedFileTest))
suite.addTest(unittest.makeSuite(BufferedPipeTest))
suite.addTest(unittest.makeSuite(UtilTest))
suite.addTest(unittest.makeSuite(HostKeysTest))
if options.use_pkey:
suite.addTest(unittest.makeSuite(KeyTest))
suite.addTest(unittest.makeSuite(KexTest))
suite.addTest(unittest.makeSuite(PacketizerTest))
if options.use_transport:
suite.addTest(unittest.makeSuite(TransportTest))
suite.addTest(unittest.makeSuite(SSHClientTest))
if options.use_sftp:
suite.addTest(unittest.makeSuite(SFTPTest))
if options.use_big_file:
suite.addTest(unittest.makeSuite(BigSFTPTest))
verbosity = 1
if options.verbose:
verbosity = 2
runner = unittest.TextTestRunner(verbosity=verbosity)
if len(args) > 0:
filter = '|'.join(args)
suite = filter_suite_by_re(suite, filter)
runner.run(suite)
if __name__ == '__main__':
main()