regression/regression.py
changeset 2881 81d1080cd0ae
parent 2878 580a733e49d6
child 2882 542f4d57464b
equal deleted inserted replaced
2878:580a733e49d6 2881:81d1080cd0ae
     1 #! /usr/bin/env python
       
     2 # regression.py adapted from python language regression scripts.
       
     3 
       
     4 """Regression test.
       
     5 
       
     6 This will find all modules whose name is "test-*" in the tests
       
     7 directory, and run them.
       
     8 
       
     9 Command line options:
       
    10 
       
    11 -v: verbose   -- run tests in verbose mode (print diff output)
       
    12 -g: generate  -- write the output file for a test instead of comparing it
       
    13 
       
    14 If non-option arguments are present, they are names for tests to run.
       
    15 If no test names are given, all tests are run.
       
    16 
       
    17 """
       
    18 
       
    19 import getopt
       
    20 import sys
       
    21 import os
       
    22 import urllib
       
    23 import subprocess
       
    24 
       
    25 verbose = 0
       
    26 generate = 0
       
    27 
       
    28 #
       
    29 # The directory in which the tarball of the reference traces lives.  This is
       
    30 # used if Mercurial is not on the system.
       
    31 #
       
    32 refUrl = "http://www.nsnam.org/releases/"
       
    33 
       
    34 #
       
    35 # The name of the tarball to find the reference traces in if there is no
       
    36 # mercurial on the system.  It is expected to be created using tar -cjf and
       
    37 # will be extracted using tar -xjf
       
    38 #
       
    39 refTarName = "ns-3.0.12-ref-traces.tar.bz2"
       
    40 
       
    41 #
       
    42 # The path to the Mercurial repository used to find the reference traces if
       
    43 # we find "hg" on the system.  We expect that the repository will be named
       
    44 # identically to refDirName below
       
    45 #
       
    46 refRepo = "http://code.nsnam.org/"
       
    47 
       
    48 #
       
    49 # The local directory name into which the reference traces will go in either
       
    50 # case (net or hg).
       
    51 #
       
    52 refDirName = "ns-3-dev-ref-traces"
       
    53 
       
    54 def main(tests = None, testdir = None):
       
    55     """Execute regression tests.
       
    56 
       
    57     Arguments:
       
    58     tests -- a list of strings containing test names (optional)
       
    59     testdir -- the directory in which to look for tests (optional)
       
    60     """
       
    61 
       
    62     global verbose
       
    63     global generate
       
    64     global refUrl
       
    65     global refTarName
       
    66     global refRepo
       
    67     global refRepoName
       
    68     global refDirName
       
    69     
       
    70     try:
       
    71         opts, args = getopt.getopt(sys.argv[1:], 'vg')
       
    72     except getopt.error, msg:
       
    73         print msg
       
    74         print __doc__
       
    75         return 2
       
    76 
       
    77     for o, a in opts:
       
    78         if o == '-v': verbose = 1
       
    79         if o == '-g': generate = 1
       
    80 
       
    81     if not testdir:
       
    82         testdir = os.path.join(os.curdir, "tests")
       
    83 
       
    84     if not os.path.exists(testdir):
       
    85         print "Tests directory does not exist"
       
    86         return 3
       
    87     
       
    88     sys.path.append(testdir)
       
    89         
       
    90     for i in range(len(args)):
       
    91         if args[i][-3:] == '.py':
       
    92             args[i] = args[i][:-3]
       
    93 
       
    94     if not tests:
       
    95         tests = args
       
    96 
       
    97     if not tests:
       
    98         tests = findtests(testdir)
       
    99         if not generate:
       
   100             print "========== Running Unit Tests =========="
       
   101             os.system("./waf check")
       
   102 
       
   103     print "========== Running Regression Tests =========="
       
   104 
       
   105     if os.system("hg version > /dev/null 2>&1") == 0:
       
   106         print "Synchronizing reference traces using Mercurial."
       
   107         if not os.path.exists(refDirName):
       
   108             os.system("hg clone " + refRepo + refDirName + " > /dev/null 2>&1")
       
   109         else:
       
   110             os.chdir(refDirName)
       
   111             os.system("hg pull " + refRepo + refDirName + " > /dev/null 2>&1")
       
   112             os.chdir("..")
       
   113     else:
       
   114         print "Synchronizing reference traces from web."
       
   115         urllib.urlretrieve(refUrl + refTarName, refTarName)
       
   116         os.system("tar -xjf " + refTarName)
       
   117 
       
   118     print "Done."
       
   119 
       
   120     if not os.path.exists(refDirName):
       
   121         print "Reference traces directory does not exist"
       
   122         return 3
       
   123     
       
   124     bad = []
       
   125 
       
   126 
       
   127     for test in tests:
       
   128         result = run_test(test)
       
   129         if result == 0:
       
   130             if generate:
       
   131                 print "GENERATE " + test
       
   132             else:
       
   133                 print "PASS " + test
       
   134         else:
       
   135             bad.append(test)
       
   136             print "FAIL " + test
       
   137 
       
   138     return len(bad) > 0
       
   139 
       
   140 def findtests(testdir):
       
   141     """Return a list of test modules in the test directory
       
   142 
       
   143     Arguments:
       
   144     testdir -- the directory to look in for tests
       
   145     """
       
   146     names = os.listdir(testdir)
       
   147     tests = []
       
   148     for name in names:
       
   149         if name[:5] == "test-" and name[-3:] == ".py":
       
   150             testname = name[:-3]
       
   151             tests.append(testname)
       
   152     tests.sort()
       
   153     return tests
       
   154 
       
   155 def run_test(test):
       
   156     """Run a single test.
       
   157 
       
   158     Arguments:
       
   159     test -- the name of the test
       
   160     """
       
   161     if os.path.exists("traces"):
       
   162         files = os.listdir("traces")
       
   163         for file in files:
       
   164             if file == '.' or file == '..':
       
   165                 continue
       
   166             path = "traces" + os.sep + file
       
   167             os.remove(path)
       
   168     else:
       
   169         os.mkdir("traces")
       
   170     
       
   171     mod = __import__(test, globals(), locals(), [])
       
   172     return mod.run(verbose, generate, refDirName)
       
   173 
       
   174 if __name__ == '__main__':
       
   175     sys.exit(main())