#!/usr/bin/env python import os import sys import re import imp import unittest class badcaTester: directory = None fileMatcher = None loader = unittest.defaultTestLoader testRunner = None filter = [] def __init__(self): self.fileMatcher = re.compile("^\S*TestCase\.py$") if len(sys.argv) < 2: self.exitUsage('You MUST supply a directory to scan') if not os.path.isdir(sys.argv[1]): self.exitUsage('You must supply a directory to scan') self.directory = os.path.abspath(sys.argv[1]) files = os.listdir(self.directory) # Tidy up so we start from a known condition # NB we don't know specific file names as they are generated, # so we go nuclear and empty the directories! self.cleanCA() self.emptyDirectory('tests/logs') self.emptyDirectory('tests/crl') if len(sys.argv) > 2: self.filter = sys.argv[2:] print "Filtering tests to " + str(self.filter) files.sort() for f in files: if self.fileMatcher.match(f): m = re.compile("^\d{2}([A-Za-z]*)TestCase\.py").match(f) if len(self.filter) > 0: if m is None or m.group(1) not in self.filter: continue if m is not None: print "\nRunning tests for " + m.group(1) + "\n" else: print "\nRunning tests in " + f + "\n" tests = self.getSuitesFromFilename(f) if tests is not None: self.runTests(tests) def getSuitesFromFilename(self, filename): f = re.compile("(\S*)\.py").match(filename) if f is not None: filename = f.group(1) file, pathname, description = imp.find_module(filename) module = imp.load_module(filename, file, pathname, description) if module is None: return None return self.loader.loadTestsFromModule(module) def runTests(self, tests): if self.testRunner is None: self.testRunner = unittest.TextTestRunner(verbosity=2) result = self.testRunner.run(tests) if not result.wasSuccessful(): sys.exit(1) # In order to give consistent results we destroy the data created # in order to test the CA code before we start. def cleanCA(self): caDir = os.path.join(self.directory, 'ca') if not os.path.isdir(caDir): os.mkdir(caDir) testCA = [ 'test01' ] for d in testCA: theCADir = os.path.join(caDir, d) if os.path.exists(theCADir) and os.path.isdir(theCADir): print "Tidying up test CA '%s'" % d self.cleanTheCA(theCADir) # The CA tests create their directory structure and the various keys, # certs and so on, so simply remove any that are left from previous # tests. def cleanTheCA(self, CAdir): toRemove = [ 'certs', 'crl', 'csr', 'private' ] for d in toRemove: rDir = os.path.join(CAdir, d) if os.path.isdir(rDir): print "Removing %s" % rDir self.removeDirectory(rDir) # Recursively remove directories and their contents... def removeDirectory(self, thedir): self.emptyDirectory(thedir) os.rmdir(thedir) # Recursively remove directories and their contents, but leave the # directory in place. def emptyDirectory(self, thedir): files = os.listdir(thedir) for f in files: if f == '.svn': continue fn = os.path.join(thedir, f) print " removing '%s' [%s]" % (f, fn) if os.path.isdir(fn): self.removeDirectory(fn) else: os.unlink(fn) def exitUsage(self, msg = None): print """ Usage: runTests.py """ if msg is not None: print "Error: %s" % msg sys.exit(2) main = badcaTester if __name__ == "__main__": main()