regression.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Sun, 18 Jan 2009 22:18:49 +0000
changeset 4109 1a251c8ad317
parent 4064 10222f483860
child 4110 04170734fa8b
permissions -rw-r--r--
Cleanup: remove regression reference traces downloading code (moved to allinone)
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
def dev_null():
gjc@3866
    18
    if sys.platform == 'win32':
gjc@3866
    19
        return open("NUL:", "w")
gjc@3866
    20
    else:
gjc@3866
    21
        return open("/dev/null", "w")
gjc@3866
    22
gjc@3866
    23
gjc@3866
    24
### Regression testing
gjc@3866
    25
class Regression(object):
gjc@3872
    26
    def __init__(self, testdir, reference_traces):
gjc@3866
    27
        self.testdir = testdir
gjc@3872
    28
        self.reference_traces = reference_traces
gjc@4064
    29
        self.env = Build.bld.env
gjc@3866
    30
gjc@3872
    31
    def run_test(self, verbose, generate, testName, arguments=[], pyscript=None, refTestName=None):
gjc@3866
    32
        """
gjc@3866
    33
        @param verbose: enable verbose execution
gjc@3866
    34
gjc@3866
    35
        @param generate: generate new traces instead of comparing with the reference
gjc@3866
    36
gjc@3866
    37
        @param testName: name of the test
gjc@3866
    38
gjc@3866
    39
        @arguments: list of extra parameters to pass to the program to be tested
gjc@3866
    40
gjc@3866
    41
        @pyscript: if not None, the test is written in Python and this
gjc@3866
    42
        parameter contains the path to the python script, relative to
gjc@3866
    43
        the project root dir
gjc@3866
    44
gjc@3866
    45
        @param refTestName: if not None, this is the name of the directory under refDirName
gjc@3866
    46
        that contains the reference traces. Otherwise "refDirname/testName + .ref" is used.
gjc@3866
    47
gjc@3866
    48
        """
gjc@3866
    49
        if not isinstance(arguments, list):
gjc@3866
    50
            raise TypeError
gjc@3866
    51
        
gjc@3866
    52
        if refTestName is None:
gjc@3872
    53
            refTestDirName = os.path.join(self.reference_traces, (testName + ".ref"))
gjc@3866
    54
        else:
gjc@3872
    55
            refTestDirName = os.path.join(self.reference_traces, refTestName)
gjc@3866
    56
gjc@3872
    57
        if not os.path.exists(self.reference_traces):
gjc@3866
    58
            print"No reference trace repository"
gjc@3866
    59
            return 1
gjc@3866
    60
gjc@3866
    61
        if generate:
gjc@3866
    62
            if not os.path.exists(refTestDirName):
gjc@3866
    63
                print "creating new " + refTestDirName
gjc@3866
    64
                os.mkdir(refTestDirName)
gjc@3866
    65
gjc@3866
    66
            if pyscript is None:
gjc@4064
    67
                Options.options.cwd_launch = refTestDirName
gjc@3866
    68
                tmpl = "%s"
gjc@3866
    69
                for arg in arguments:
gjc@3866
    70
                    tmpl = tmpl + " " + arg
gjc@3866
    71
                wutils.run_program(testName, tmpl)
gjc@3866
    72
            else:
gjc@4064
    73
                argv = [self.env['PYTHON'], os.path.join(Options.cwd_launch, *os.path.split(pyscript))] + arguments
gjc@3866
    74
                before = os.getcwd()
gjc@3866
    75
                os.chdir(refTestDirName)
gjc@3866
    76
                try:
gjc@3866
    77
                    wutils.run_argv(argv)
gjc@3866
    78
                finally:
gjc@3866
    79
                    os.chdir(before)
gjc@3866
    80
            print "Remember to commit " + refTestDirName
gjc@3866
    81
            return 0
gjc@3866
    82
        else:
gjc@3866
    83
            if not os.path.exists(refTestDirName):
gjc@3866
    84
                print "Cannot locate reference traces in " + refTestDirName
gjc@3866
    85
                return 1
gjc@3866
    86
gjc@3866
    87
            
gjc@3866
    88
            if refTestName is None:
gjc@3866
    89
                traceDirName = testName + ".ref"
gjc@3866
    90
            else:
gjc@3866
    91
                traceDirName = refTestName
gjc@3866
    92
            traceDirName = os.path.join ('traces', traceDirName)
gjc@3866
    93
gjc@3866
    94
            try:
gjc@3866
    95
                shutil.rmtree(traceDirName)
gjc@3866
    96
            except OSError:
gjc@3866
    97
                pass
gjc@3866
    98
            os.mkdir(traceDirName)
gjc@3866
    99
gjc@3866
   100
            #os.system("./waf --cwd regression/traces --run " +
gjc@3866
   101
            #  testName + " > /dev/null 2>&1")
gjc@3866
   102
gjc@3866
   103
            if pyscript is None:
gjc@4064
   104
                Options.options.cwd_launch = traceDirName
gjc@3866
   105
                wutils.run_program(testName, command_template=wutils.get_command_template(*arguments))
gjc@3866
   106
            else:
gjc@3866
   107
                argv = [self.env['PYTHON'], os.path.join('..', '..', '..', *os.path.split(pyscript))] + arguments
gjc@3866
   108
                before = os.getcwd()
gjc@3866
   109
                os.chdir(traceDirName)
gjc@3866
   110
                try:
gjc@3866
   111
                    wutils.run_argv(argv)
gjc@3866
   112
                finally:
gjc@3866
   113
                    os.chdir(before)
gjc@3866
   114
gjc@3866
   115
            if verbose:
gjc@3866
   116
                #diffCmd = "diff traces " + refTestDirName + " | head"
gjc@3866
   117
                diffCmd = subprocess.Popen([self.env['DIFF'], traceDirName, refTestDirName],
gjc@3866
   118
                                           stdout=subprocess.PIPE)
gjc@3866
   119
                headCmd = subprocess.Popen("head", stdin=diffCmd.stdout)
gjc@3866
   120
                rc2 = headCmd.wait()
gjc@3866
   121
                diffCmd.stdout.close()
gjc@3866
   122
                rc1 = diffCmd.wait()
gjc@3866
   123
                rc = rc1 or rc2
gjc@3866
   124
            else:
gjc@3866
   125
                rc = subprocess.Popen([self.env['DIFF'], traceDirName, refTestDirName], stdout=dev_null()).wait()
gjc@3866
   126
            if rc:
gjc@3866
   127
                print "----------"
gjc@3866
   128
                print "Traces differ in test: test-" + testName
gjc@3866
   129
                print "Reference traces in directory: regression/" + refTestDirName
gjc@3866
   130
                print "Traces in directory: traces"
gjc@3866
   131
                print "Rerun regression test as: " + \
gjc@3866
   132
                    "\"./waf --regression --regression-tests=test-" + testName + "\""
gjc@3866
   133
                print "Then do \"diff -u regression/" + refTestDirName + " regression/" + traceDirName +\
gjc@3866
   134
                    "\" for details"
gjc@3866
   135
                print "----------"
gjc@3866
   136
            return rc
gjc@3866
   137
gjc@3866
   138
def _find_tests(testdir):
gjc@3866
   139
    """Return a list of test modules in the test directory
gjc@3866
   140
gjc@3866
   141
    Arguments:
gjc@3866
   142
    testdir -- the directory to look in for tests
gjc@3866
   143
    """
gjc@3866
   144
    names = os.listdir(testdir)
gjc@3866
   145
    tests = []
gjc@3866
   146
    for name in names:
gjc@3866
   147
        if name[:5] == "test-" and name[-3:] == ".py":
gjc@3866
   148
            testname = name[:-3]
gjc@3866
   149
            tests.append(testname)
gjc@3866
   150
    tests.sort()
gjc@3866
   151
    return tests
gjc@3866
   152
gjc@3872
   153
def run_regression(reference_traces):
gjc@3872
   154
    """Execute regression tests.  Called with cwd set to the 'regression' subdir of ns-3.
gjc@3872
   155
gjc@4109
   156
    @param reference_traces: reference traces directory.
gjc@3872
   157
gjc@3872
   158
    """
gjc@3866
   159
gjc@3866
   160
    testdir = "tests"
gjc@3866
   161
    if not os.path.exists(testdir):
gjc@3866
   162
        print "Tests directory does not exist"
gjc@3866
   163
        sys.exit(3)
gjc@3872
   164
gjc@3866
   165
    sys.path.append(testdir)
gjc@3872
   166
    sys.modules['tracediff'] = Regression(testdir, reference_traces)
gjc@3866
   167
gjc@4064
   168
    if Options.options.regression_tests:
gjc@4064
   169
        tests = Options.options.regression_tests.split(',')
gjc@3866
   170
    else:
gjc@3866
   171
        tests = _find_tests(testdir)
gjc@3866
   172
gjc@3866
   173
    print "========== Running Regression Tests =========="
gjc@4064
   174
    env = Build.bld.env
gjc@3866
   175
gjc@3872
   176
    if not os.path.exists(reference_traces):
gjc@3872
   177
        print "Reference traces directory (%s) does not exist" % reference_traces
gjc@3866
   178
        return 3
gjc@3866
   179
    
gjc@3866
   180
    bad = []
gjc@3866
   181
gjc@3866
   182
    for test in tests:
gjc@3866
   183
        try:
gjc@3866
   184
            result = _run_regression_test(test)
gjc@3866
   185
            if result == 0:
gjc@4064
   186
                if Options.options.regression_generate:
gjc@3866
   187
                    print "GENERATE " + test
gjc@3866
   188
                else:
gjc@3866
   189
                    print "PASS " + test
gjc@3866
   190
            else:
gjc@3866
   191
                bad.append(test)
gjc@3866
   192
                print "FAIL " + test
gjc@3866
   193
        except NotImplementedError:
gjc@4064
   194
            print "SKIP " + test            
gjc@3866
   195
gjc@3866
   196
    return len(bad) > 0
gjc@3866
   197
gjc@3866
   198
gjc@3866
   199
def _run_regression_test(test):
gjc@3866
   200
    """Run a single test.
gjc@3866
   201
gjc@3866
   202
    Arguments:
gjc@3866
   203
    test -- the name of the test
gjc@3866
   204
    """
gjc@3866
   205
gjc@3866
   206
    if os.path.exists("traces"):
gjc@3866
   207
        files = os.listdir("traces")
gjc@3866
   208
        for file in files:
gjc@3866
   209
            if file == '.' or file == '..':
gjc@3866
   210
                continue
gjc@3866
   211
            shutil.rmtree(os.path.join("traces", file), ignore_errors=True)
gjc@3866
   212
    else:
gjc@3866
   213
        os.mkdir("traces")
gjc@3866
   214
    
gjc@3866
   215
    mod = __import__(test, globals(), locals(), [])
gjc@4064
   216
    return mod.run(verbose=(Options.options.verbose > 0),
gjc@4064
   217
                   generate=Options.options.regression_generate)
gjc@3872
   218