regression.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Wed, 28 Jan 2009 11:06:50 +0000
changeset 4130 9bddd87225a9
parent 4129 9b1f5d58dfb6
child 4202 272cabf60878
permissions -rw-r--r--
Skip the NSC test when valgrind is enabled.
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@4114
    12
import Task
gjc@4064
    13
gjc@4064
    14
# local modules
gjc@3866
    15
import wutils
gjc@3866
    16
gjc@4064
    17
gjc@3866
    18
def dev_null():
gjc@3866
    19
    if sys.platform == 'win32':
gjc@3866
    20
        return open("NUL:", "w")
gjc@3866
    21
    else:
gjc@3866
    22
        return open("/dev/null", "w")
gjc@3866
    23
gjc@3866
    24
gjc@3866
    25
def _find_tests(testdir):
gjc@3866
    26
    """Return a list of test modules in the test directory
gjc@3866
    27
gjc@3866
    28
    Arguments:
gjc@3866
    29
    testdir -- the directory to look in for tests
gjc@3866
    30
    """
gjc@3866
    31
    names = os.listdir(testdir)
gjc@3866
    32
    tests = []
gjc@3866
    33
    for name in names:
gjc@3866
    34
        if name[:5] == "test-" and name[-3:] == ".py":
gjc@3866
    35
            testname = name[:-3]
gjc@3866
    36
            tests.append(testname)
gjc@3866
    37
    tests.sort()
gjc@3866
    38
    return tests
gjc@3866
    39
gjc@4114
    40
gjc@4114
    41
class regression_test_task(Task.TaskBase):
gjc@4114
    42
    after = 'cc cxx cc_link cxx_link'
gjc@4114
    43
    color = 'BLUE'
gjc@4114
    44
gjc@4114
    45
    def __init__(self, env, test_name, test_scripts_dir, build_traces_dir, reference_traces):
gjc@4114
    46
        super(regression_test_task, self).__init__()
gjc@4114
    47
        self.env = env
gjc@4114
    48
        self.test_name = test_name
gjc@4114
    49
        self.test_scripts_dir = test_scripts_dir
gjc@4114
    50
        self.build_traces_dir = build_traces_dir
gjc@4114
    51
        self.reference_traces_dir = reference_traces
gjc@4114
    52
gjc@4114
    53
    def __str__(self):
gjc@4114
    54
        return 'regression-test (%s)\n' % self.test_name
gjc@4114
    55
gjc@4114
    56
    def run(self):
gjc@4114
    57
        """Run a single test"""
gjc@4114
    58
        sys.path.insert(0, self.test_scripts_dir)
gjc@4114
    59
        try:
gjc@4114
    60
            mod = __import__(self.test_name, globals(), locals(), [])
gjc@4114
    61
        finally:
gjc@4114
    62
            sys.path.remove(self.test_scripts_dir)
gjc@4114
    63
gjc@4114
    64
        assert self.test_name.startswith('test-')
gjc@4114
    65
        short_name = self.test_name[len('test-'):]
gjc@4114
    66
gjc@4114
    67
        trace_dir_name = getattr(mod, "trace_dir_name", None)
gjc@4114
    68
        if trace_dir_name is None:
gjc@4114
    69
            trace_dir_name = "%s.ref" % short_name
gjc@4114
    70
        trace_output_path = os.path.join(self.build_traces_dir, trace_dir_name)
gjc@4114
    71
        reference_traces_path = os.path.join(self.reference_traces_dir, trace_dir_name)
gjc@4114
    72
gjc@4114
    73
        if hasattr(mod, 'get_arguments'):
gjc@4114
    74
            arguments = mod.get_arguments(self.env, '..')
gjc@4114
    75
        else:
gjc@4114
    76
            arguments = getattr(mod, "arguments", [])
gjc@4114
    77
gjc@4114
    78
        pyscript = getattr(mod, "pyscript", None)
gjc@4114
    79
        if pyscript:
gjc@4114
    80
            is_pyscript = True
gjc@4114
    81
            program = pyscript
gjc@4114
    82
        else:
gjc@4114
    83
            is_pyscript = False
gjc@4114
    84
            program = getattr(mod, "program", short_name)
gjc@4114
    85
gjc@4114
    86
        if hasattr(mod, 'may_run'):
gjc@4130
    87
            reason_cannot_run = mod.may_run(self.env, Options.options)
gjc@4114
    88
        else:
gjc@4114
    89
            reason_cannot_run = None
gjc@4114
    90
        if reason_cannot_run:
gjc@4114
    91
            print "SKIP %s (%s)" % (self.test_name, reason_cannot_run)
gjc@4114
    92
            self.result = None
gjc@4114
    93
            return 0
gjc@4114
    94
gjc@4114
    95
        if Options.options.regression_generate:
gjc@4114
    96
            # clean the target dir
gjc@4114
    97
            shutil.rmtree(reference_traces_path, ignore_errors=True)
gjc@4114
    98
            os.makedirs(reference_traces_path)
gjc@4114
    99
            result = self.run_reference_generate(reference_traces_path, program, arguments, is_pyscript)
gjc@4114
   100
            if result == 0:
gjc@4114
   101
                print "GENERATE " + self.test_name
gjc@4114
   102
            else:
gjc@4114
   103
                print "GENERATE FAIL " + self.test_name
gjc@4114
   104
        else:
gjc@4114
   105
            # clean the target dir
gjc@4114
   106
            shutil.rmtree(trace_output_path, ignore_errors=True)
gjc@4114
   107
            os.makedirs(trace_output_path)
gjc@4114
   108
            # run it
gjc@4114
   109
            result = self.run_reference_test(reference_traces_path, trace_output_path, program, arguments, is_pyscript)
gjc@4114
   110
            if result == 0:
gjc@4114
   111
                print "PASS " + self.test_name
gjc@4114
   112
            else:
gjc@4114
   113
                print "FAIL " + self.test_name
gjc@4114
   114
        self.result = result
gjc@4114
   115
        return 0
gjc@4114
   116
gjc@4114
   117
    def run_reference_test(self, reference_traces_path, trace_output_path, program, arguments, is_pyscript):
gjc@4114
   118
        if not os.path.exists(reference_traces_path):
gjc@4114
   119
            print "Cannot locate reference traces in " + reference_traces_path
gjc@4114
   120
            return 1
gjc@4114
   121
gjc@4114
   122
        if is_pyscript:
gjc@4114
   123
            script = os.path.abspath(os.path.join('..', *os.path.split(program)))
gjc@4114
   124
            argv = [self.env['PYTHON'], script] + arguments
gjc@4129
   125
            try:
gjc@4129
   126
                wutils.run_argv(argv, cwd=trace_output_path)
gjc@4129
   127
            except Utils.WafError, ex:
gjc@4129
   128
                print >> sys.stderr, ex
gjc@4129
   129
                return 1
gjc@4114
   130
        else:
gjc@4129
   131
            try:
gjc@4129
   132
                wutils.run_program(program,
gjc@4129
   133
                                   command_template=wutils.get_command_template(self.env, arguments),
gjc@4129
   134
                                   cwd=trace_output_path)
gjc@4129
   135
            except Utils.WafError, ex:
gjc@4129
   136
                print >> sys.stderr, ex
gjc@4129
   137
                return 1
gjc@4114
   138
gjc@4114
   139
        if Options.options.verbose:
gjc@4114
   140
            #diffCmd = "diff traces " + refTestDirName + " | head"
gjc@4114
   141
            diffCmd = subprocess.Popen([self.env['DIFF'], trace_output_path, reference_traces_path],
gjc@4114
   142
                                       stdout=subprocess.PIPE)
gjc@4114
   143
            headCmd = subprocess.Popen("head", stdin=diffCmd.stdout)
gjc@4114
   144
            rc2 = headCmd.wait()
gjc@4114
   145
            diffCmd.stdout.close()
gjc@4114
   146
            rc1 = diffCmd.wait()
gjc@4114
   147
            rc = rc1 or rc2
gjc@4114
   148
        else:
gjc@4114
   149
            rc = subprocess.Popen([self.env['DIFF'], trace_output_path, reference_traces_path], stdout=dev_null()).wait()
gjc@4114
   150
        if rc:
gjc@4114
   151
            print "----------"
gjc@4114
   152
            print "Traces differ in test: ", self.test_name
gjc@4114
   153
            print "Reference traces in directory: " + reference_traces_path
gjc@4114
   154
            print "Traces in directory: " + trace_output_path
gjc@4114
   155
            print "Run the following command for details:"
gjc@4114
   156
            print "\tdiff -u %s %s" % (reference_traces_path, trace_output_path)
gjc@4114
   157
            print "----------"
gjc@4114
   158
        return rc
gjc@4114
   159
gjc@4114
   160
gjc@4114
   161
    def run_reference_generate(self, trace_output_path, program, arguments, is_pyscript):
gjc@4114
   162
        if is_pyscript:
gjc@4114
   163
            script = os.path.abspath(os.path.join('..', *os.path.split(program)))
gjc@4114
   164
            argv = [self.env['PYTHON'], script] + arguments
gjc@4129
   165
            try:
gjc@4129
   166
                retval = wutils.run_argv(argv, cwd=trace_output_path)
gjc@4129
   167
            except Utils.WafError, ex:
gjc@4129
   168
                print >> sys.stderr, ex
gjc@4129
   169
                return 1
gjc@4114
   170
        else:
gjc@4129
   171
            try:
gjc@4129
   172
                retval = wutils.run_program(program,
gjc@4129
   173
                                            command_template=wutils.get_command_template(self.env, arguments),
gjc@4129
   174
                                            cwd=trace_output_path)
gjc@4129
   175
            except Utils.WafError, ex:
gjc@4129
   176
                print >> sys.stderr, ex
gjc@4129
   177
                return 1
gjc@4114
   178
        return retval
gjc@4114
   179
gjc@4114
   180
gjc@4114
   181
class regression_test_collector_task(Task.TaskBase):
gjc@4114
   182
    after = 'regression_test_task'
gjc@4114
   183
    color = 'BLUE'
gjc@4114
   184
gjc@4114
   185
    def __init__(self, test_tasks):
gjc@4114
   186
        super(regression_test_collector_task, self).__init__()
gjc@4114
   187
        self.test_tasks = test_tasks
gjc@4114
   188
gjc@4114
   189
    def __str__(self):
gjc@4114
   190
        return 'regression-test-collector\n'
gjc@4114
   191
gjc@4114
   192
    def run(self):
gjc@4114
   193
        failed_tests = [test for test in self.test_tasks if test.result is not None and test.result != 0]
gjc@4114
   194
        skipped_tests = [test for test in self.test_tasks if test.result is None]
gjc@4114
   195
        print "Regression testing summary:"
gjc@4114
   196
        if skipped_tests:
gjc@4114
   197
            print "SKIP: %i of %i tests have been skipped (%s)" % (
gjc@4114
   198
                len(skipped_tests), len(self.test_tasks),
gjc@4114
   199
                ', '.join([test.test_name for test in skipped_tests]))
gjc@4114
   200
        if failed_tests:
gjc@4114
   201
            print "FAIL: %i of %i tests have failed (%s)" % (
gjc@4114
   202
                len(failed_tests), len(self.test_tasks),
gjc@4114
   203
                ', '.join([test.test_name for test in failed_tests]))
gjc@4114
   204
            return 1
gjc@4114
   205
        else:
gjc@4114
   206
            print "PASS: %i of %i tests passed" % (len(self.test_tasks) - len(skipped_tests),
gjc@4114
   207
                                                   len(self.test_tasks))
gjc@4114
   208
            return 0
gjc@4114
   209
gjc@4114
   210
def run_regression(bld, reference_traces):
gjc@3872
   211
    """Execute regression tests.  Called with cwd set to the 'regression' subdir of ns-3.
gjc@3872
   212
gjc@4109
   213
    @param reference_traces: reference traces directory.
gjc@3872
   214
gjc@3872
   215
    """
gjc@3866
   216
gjc@4110
   217
    testdir = os.path.join("regression", "tests")
gjc@3866
   218
    if not os.path.exists(testdir):
gjc@3866
   219
        print "Tests directory does not exist"
gjc@3866
   220
        sys.exit(3)
gjc@3872
   221
gjc@4064
   222
    if Options.options.regression_tests:
gjc@4064
   223
        tests = Options.options.regression_tests.split(',')
gjc@3866
   224
    else:
gjc@3866
   225
        tests = _find_tests(testdir)
gjc@3866
   226
gjc@3872
   227
    if not os.path.exists(reference_traces):
gjc@3872
   228
        print "Reference traces directory (%s) does not exist" % reference_traces
gjc@3866
   229
        return 3
gjc@3866
   230
    
gjc@4114
   231
    test_scripts_dir = bld.path.find_dir('regression/tests').abspath()
gjc@4114
   232
    build_traces_dir = bld.path.find_or_declare('regression/traces').abspath(bld.env)
gjc@4114
   233
    tasks = []
gjc@3866
   234
    for test in tests:
gjc@4114
   235
        tasks.append(regression_test_task(bld.env, test, test_scripts_dir, build_traces_dir, reference_traces))
gjc@4114
   236
    regression_test_collector_task(tasks)