regression.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Mon, 29 Dec 2008 13:28:54 +0000
changeset 4064 10222f483860
parent 3872 8e757a83fb36
child 4109 1a251c8ad317
permissions -rw-r--r--
Upgrade to new WAF, work in progress
gjc@4064
     1
# python lib modules
gjc@3866
     2
import os
gjc@3866
     3
import sys
gjc@3866
     4
import shutil
gjc@3866
     5
import pproc as subprocess
gjc@3866
     6
import urllib
gjc@3866
     7
gjc@4064
     8
# WAF modules
gjc@4064
     9
import Build
gjc@4064
    10
import Options
gjc@4064
    11
import Utils
gjc@4064
    12
gjc@4064
    13
# local modules
gjc@3866
    14
import wutils
gjc@3866
    15
gjc@4064
    16
gjc@3866
    17
#
gjc@3866
    18
# The directory in which the tarball of the reference traces lives.  This is
gjc@3866
    19
# used if Mercurial is not on the system.
gjc@3866
    20
#
gjc@3866
    21
REGRESSION_TRACES_URL = "http://www.nsnam.org/releases/"
gjc@3866
    22
gjc@3866
    23
#
gjc@3866
    24
# The path to the Mercurial repository used to find the reference traces if
gjc@3866
    25
# we find "hg" on the system.  We expect that the repository will be named
gjc@3866
    26
# identically to refDirName below
gjc@3866
    27
#
gjc@3866
    28
REGRESSION_TRACES_REPO = "http://code.nsnam.org/"
gjc@3866
    29
gjc@3866
    30
#
gjc@3866
    31
# Name of the local directory where the regression code lives.
gjc@3866
    32
#
gjc@3866
    33
REGRESSION_DIR = "regression"
gjc@3866
    34
gjc@3866
    35
#
gjc@3866
    36
# The last part of the path name to use to find the regression traces.  The
gjc@3866
    37
# path will be APPNAME + '-' + VERSION + REGRESSION_SUFFIX, e.g.,
gjc@3866
    38
# ns-3-dev-ref-traces
gjc@3866
    39
#
gjc@3866
    40
REGRESSION_SUFFIX = "-ref-traces"
gjc@3866
    41
gjc@3866
    42
gjc@3866
    43
def dev_null():
gjc@3866
    44
    if sys.platform == 'win32':
gjc@3866
    45
        return open("NUL:", "w")
gjc@3866
    46
    else:
gjc@3866
    47
        return open("/dev/null", "w")
gjc@3866
    48
gjc@3866
    49
gjc@3866
    50
### Regression testing
gjc@3866
    51
class Regression(object):
gjc@3872
    52
    def __init__(self, testdir, reference_traces):
gjc@3866
    53
        self.testdir = testdir
gjc@3872
    54
        self.reference_traces = reference_traces
gjc@4064
    55
        self.env = Build.bld.env
gjc@3866
    56
gjc@3872
    57
    def run_test(self, verbose, generate, testName, arguments=[], pyscript=None, refTestName=None):
gjc@3866
    58
        """
gjc@3866
    59
        @param verbose: enable verbose execution
gjc@3866
    60
gjc@3866
    61
        @param generate: generate new traces instead of comparing with the reference
gjc@3866
    62
gjc@3866
    63
        @param testName: name of the test
gjc@3866
    64
gjc@3866
    65
        @arguments: list of extra parameters to pass to the program to be tested
gjc@3866
    66
gjc@3866
    67
        @pyscript: if not None, the test is written in Python and this
gjc@3866
    68
        parameter contains the path to the python script, relative to
gjc@3866
    69
        the project root dir
gjc@3866
    70
gjc@3866
    71
        @param refTestName: if not None, this is the name of the directory under refDirName
gjc@3866
    72
        that contains the reference traces. Otherwise "refDirname/testName + .ref" is used.
gjc@3866
    73
gjc@3866
    74
        """
gjc@3866
    75
        if not isinstance(arguments, list):
gjc@3866
    76
            raise TypeError
gjc@3866
    77
        
gjc@3866
    78
        if refTestName is None:
gjc@3872
    79
            refTestDirName = os.path.join(self.reference_traces, (testName + ".ref"))
gjc@3866
    80
        else:
gjc@3872
    81
            refTestDirName = os.path.join(self.reference_traces, refTestName)
gjc@3866
    82
gjc@3872
    83
        if not os.path.exists(self.reference_traces):
gjc@3866
    84
            print"No reference trace repository"
gjc@3866
    85
            return 1
gjc@3866
    86
gjc@3866
    87
        if generate:
gjc@3866
    88
            if not os.path.exists(refTestDirName):
gjc@3866
    89
                print "creating new " + refTestDirName
gjc@3866
    90
                os.mkdir(refTestDirName)
gjc@3866
    91
gjc@3866
    92
            if pyscript is None:
gjc@4064
    93
                Options.options.cwd_launch = refTestDirName
gjc@3866
    94
                tmpl = "%s"
gjc@3866
    95
                for arg in arguments:
gjc@3866
    96
                    tmpl = tmpl + " " + arg
gjc@3866
    97
                wutils.run_program(testName, tmpl)
gjc@3866
    98
            else:
gjc@4064
    99
                argv = [self.env['PYTHON'], os.path.join(Options.cwd_launch, *os.path.split(pyscript))] + arguments
gjc@3866
   100
                before = os.getcwd()
gjc@3866
   101
                os.chdir(refTestDirName)
gjc@3866
   102
                try:
gjc@3866
   103
                    wutils.run_argv(argv)
gjc@3866
   104
                finally:
gjc@3866
   105
                    os.chdir(before)
gjc@3866
   106
            print "Remember to commit " + refTestDirName
gjc@3866
   107
            return 0
gjc@3866
   108
        else:
gjc@3866
   109
            if not os.path.exists(refTestDirName):
gjc@3866
   110
                print "Cannot locate reference traces in " + refTestDirName
gjc@3866
   111
                return 1
gjc@3866
   112
gjc@3866
   113
            
gjc@3866
   114
            if refTestName is None:
gjc@3866
   115
                traceDirName = testName + ".ref"
gjc@3866
   116
            else:
gjc@3866
   117
                traceDirName = refTestName
gjc@3866
   118
            traceDirName = os.path.join ('traces', traceDirName)
gjc@3866
   119
gjc@3866
   120
            try:
gjc@3866
   121
                shutil.rmtree(traceDirName)
gjc@3866
   122
            except OSError:
gjc@3866
   123
                pass
gjc@3866
   124
            os.mkdir(traceDirName)
gjc@3866
   125
gjc@3866
   126
            #os.system("./waf --cwd regression/traces --run " +
gjc@3866
   127
            #  testName + " > /dev/null 2>&1")
gjc@3866
   128
gjc@3866
   129
            if pyscript is None:
gjc@4064
   130
                Options.options.cwd_launch = traceDirName
gjc@3866
   131
                wutils.run_program(testName, command_template=wutils.get_command_template(*arguments))
gjc@3866
   132
            else:
gjc@3866
   133
                argv = [self.env['PYTHON'], os.path.join('..', '..', '..', *os.path.split(pyscript))] + arguments
gjc@3866
   134
                before = os.getcwd()
gjc@3866
   135
                os.chdir(traceDirName)
gjc@3866
   136
                try:
gjc@3866
   137
                    wutils.run_argv(argv)
gjc@3866
   138
                finally:
gjc@3866
   139
                    os.chdir(before)
gjc@3866
   140
gjc@3866
   141
            if verbose:
gjc@3866
   142
                #diffCmd = "diff traces " + refTestDirName + " | head"
gjc@3866
   143
                diffCmd = subprocess.Popen([self.env['DIFF'], traceDirName, refTestDirName],
gjc@3866
   144
                                           stdout=subprocess.PIPE)
gjc@3866
   145
                headCmd = subprocess.Popen("head", stdin=diffCmd.stdout)
gjc@3866
   146
                rc2 = headCmd.wait()
gjc@3866
   147
                diffCmd.stdout.close()
gjc@3866
   148
                rc1 = diffCmd.wait()
gjc@3866
   149
                rc = rc1 or rc2
gjc@3866
   150
            else:
gjc@3866
   151
                rc = subprocess.Popen([self.env['DIFF'], traceDirName, refTestDirName], stdout=dev_null()).wait()
gjc@3866
   152
            if rc:
gjc@3866
   153
                print "----------"
gjc@3866
   154
                print "Traces differ in test: test-" + testName
gjc@3866
   155
                print "Reference traces in directory: regression/" + refTestDirName
gjc@3866
   156
                print "Traces in directory: traces"
gjc@3866
   157
                print "Rerun regression test as: " + \
gjc@3866
   158
                    "\"./waf --regression --regression-tests=test-" + testName + "\""
gjc@3866
   159
                print "Then do \"diff -u regression/" + refTestDirName + " regression/" + traceDirName +\
gjc@3866
   160
                    "\" for details"
gjc@3866
   161
                print "----------"
gjc@3866
   162
            return rc
gjc@3866
   163
gjc@3866
   164
def _find_tests(testdir):
gjc@3866
   165
    """Return a list of test modules in the test directory
gjc@3866
   166
gjc@3866
   167
    Arguments:
gjc@3866
   168
    testdir -- the directory to look in for tests
gjc@3866
   169
    """
gjc@3866
   170
    names = os.listdir(testdir)
gjc@3866
   171
    tests = []
gjc@3866
   172
    for name in names:
gjc@3866
   173
        if name[:5] == "test-" and name[-3:] == ".py":
gjc@3866
   174
            testname = name[:-3]
gjc@3866
   175
            tests.append(testname)
gjc@3866
   176
    tests.sort()
gjc@3866
   177
    return tests
gjc@3866
   178
gjc@3872
   179
def run_regression(reference_traces):
gjc@3872
   180
    """Execute regression tests.  Called with cwd set to the 'regression' subdir of ns-3.
gjc@3872
   181
gjc@3872
   182
    @param reference_traces: reference traces directory, or None for default.
gjc@3872
   183
gjc@3872
   184
    """
gjc@3866
   185
gjc@3866
   186
    testdir = "tests"
gjc@3866
   187
    if not os.path.exists(testdir):
gjc@3866
   188
        print "Tests directory does not exist"
gjc@3866
   189
        sys.exit(3)
gjc@3872
   190
gjc@3872
   191
    dir_name = (wutils.APPNAME + '-' + wutils.VERSION + REGRESSION_SUFFIX)
gjc@3872
   192
    if reference_traces is None:
gjc@3872
   193
        reference_traces = dir_name
gjc@3872
   194
        no_net = False
gjc@3872
   195
    else:
gjc@3872
   196
        no_net = True
gjc@3866
   197
    
gjc@3866
   198
    sys.path.append(testdir)
gjc@3872
   199
    sys.modules['tracediff'] = Regression(testdir, reference_traces)
gjc@3866
   200
gjc@4064
   201
    if Options.options.regression_tests:
gjc@4064
   202
        tests = Options.options.regression_tests.split(',')
gjc@3866
   203
    else:
gjc@3866
   204
        tests = _find_tests(testdir)
gjc@3866
   205
gjc@3866
   206
    print "========== Running Regression Tests =========="
gjc@4064
   207
    env = Build.bld.env
gjc@3872
   208
    if not no_net:
gjc@3872
   209
        if env['MERCURIAL']:
gjc@3872
   210
            print "Synchronizing reference traces using Mercurial."
gjc@3872
   211
            if not os.path.exists(reference_traces):
gjc@3872
   212
                print "Cloning " + REGRESSION_TRACES_REPO + dir_name + " from repo."
gjc@3872
   213
                argv = ["hg", "clone", REGRESSION_TRACES_REPO + dir_name, reference_traces]
gjc@3872
   214
                rv = subprocess.Popen(argv).wait()
gjc@3872
   215
            else:
gjc@3872
   216
                _dir = os.getcwd()
gjc@3872
   217
                os.chdir(reference_traces)
gjc@3872
   218
                try:
gjc@3872
   219
                    print "Pulling " + REGRESSION_TRACES_REPO + dir_name + " from repo."
gjc@3872
   220
                    result = subprocess.Popen(["hg", "-q", "pull", REGRESSION_TRACES_REPO + dir_name]).wait()
gjc@3872
   221
                    if not result:
gjc@3872
   222
                        result = subprocess.Popen(["hg", "-q", "update"]).wait()
gjc@3872
   223
                finally:
gjc@3872
   224
                    os.chdir("..")
gjc@3872
   225
                if result:
gjc@4064
   226
                    raise Utils.WafError("Synchronizing reference traces using Mercurial failed.")
gjc@3866
   227
        else:
gjc@3872
   228
            if not os.path.exists(reference_traces):
gjc@3872
   229
                traceball = dir_name + wutils.TRACEBALL_SUFFIX
gjc@3872
   230
                print "Retrieving " + traceball + " from web."
gjc@3872
   231
                urllib.urlretrieve(REGRESSION_TRACES_URL + traceball, traceball)
gjc@3872
   232
                os.system("tar -xjf %s -C .." % (traceball))
gjc@3872
   233
                print "Done."
gjc@3866
   234
gjc@3872
   235
    if not os.path.exists(reference_traces):
gjc@3872
   236
        print "Reference traces directory (%s) does not exist" % reference_traces
gjc@3866
   237
        return 3
gjc@3866
   238
    
gjc@3866
   239
    bad = []
gjc@3866
   240
gjc@3866
   241
    for test in tests:
gjc@3866
   242
        try:
gjc@3866
   243
            result = _run_regression_test(test)
gjc@3866
   244
            if result == 0:
gjc@4064
   245
                if Options.options.regression_generate:
gjc@3866
   246
                    print "GENERATE " + test
gjc@3866
   247
                else:
gjc@3866
   248
                    print "PASS " + test
gjc@3866
   249
            else:
gjc@3866
   250
                bad.append(test)
gjc@3866
   251
                print "FAIL " + test
gjc@3866
   252
        except NotImplementedError:
gjc@4064
   253
            print "SKIP " + test            
gjc@3866
   254
gjc@3866
   255
    return len(bad) > 0
gjc@3866
   256
gjc@3866
   257
gjc@3866
   258
def _run_regression_test(test):
gjc@3866
   259
    """Run a single test.
gjc@3866
   260
gjc@3866
   261
    Arguments:
gjc@3866
   262
    test -- the name of the test
gjc@3866
   263
    """
gjc@3866
   264
gjc@3866
   265
    if os.path.exists("traces"):
gjc@3866
   266
        files = os.listdir("traces")
gjc@3866
   267
        for file in files:
gjc@3866
   268
            if file == '.' or file == '..':
gjc@3866
   269
                continue
gjc@3866
   270
            shutil.rmtree(os.path.join("traces", file), ignore_errors=True)
gjc@3866
   271
    else:
gjc@3866
   272
        os.mkdir("traces")
gjc@3866
   273
    
gjc@3866
   274
    mod = __import__(test, globals(), locals(), [])
gjc@4064
   275
    return mod.run(verbose=(Options.options.verbose > 0),
gjc@4064
   276
                   generate=Options.options.regression_generate)
gjc@3872
   277