regression.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Tue, 21 Apr 2009 11:13:19 +0100
changeset 4388 fd90dd412e67
parent 4332 a1c7bc503a0c
child 4584 7f4ee7f84b19
permissions -rw-r--r--
Force valgrind to not be used for Python tests
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@4308
     6
import errno
gjc@3866
     7
gjc@4064
     8
# WAF modules
gjc@4064
     9
import Options
gjc@4064
    10
import Utils
gjc@4114
    11
import Task
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
def _find_tests(testdir):
gjc@3866
    25
    """Return a list of test modules in the test directory
gjc@3866
    26
gjc@3866
    27
    Arguments:
gjc@3866
    28
    testdir -- the directory to look in for tests
gjc@3866
    29
    """
gjc@3866
    30
    names = os.listdir(testdir)
gjc@3866
    31
    tests = []
gjc@3866
    32
    for name in names:
gjc@3866
    33
        if name[:5] == "test-" and name[-3:] == ".py":
gjc@3866
    34
            testname = name[:-3]
gjc@3866
    35
            tests.append(testname)
gjc@3866
    36
    tests.sort()
gjc@3866
    37
    return tests
gjc@3866
    38
gjc@4202
    39
def diff(dir1, dir2, verbose):
gjc@4202
    40
    import filecmp
gjc@4202
    41
    comp = filecmp.dircmp(dir1, dir2)
gjc@4202
    42
    differ = (comp.left_only or comp.right_only or comp.diff_files)
gjc@4202
    43
    if differ:
gjc@4202
    44
        if verbose:
gjc@4202
    45
            comp.report()
gjc@4202
    46
            import difflib
gjc@4202
    47
            for diff_fname in comp.diff_files:
gjc@4202
    48
                if not (diff_fname.endswith(".tr") or diff_fname.endswith(".mob")):
gjc@4202
    49
                    print "The different file %r does not sound like a text file, not compared." % (diff_fname,)
gjc@4202
    50
                diff_file1 = open(os.path.join(dir1, diff_fname), "rt").readlines()
gjc@4202
    51
                diff_file2 = open(os.path.join(dir2, diff_fname), "rt").readlines()
gjc@4202
    52
                diff = difflib.unified_diff(diff_file1, diff_file2)
gjc@4202
    53
                count = 0
gjc@4202
    54
                print "Differences in file %r" % (diff_fname,)
gjc@4202
    55
                for line in diff:
gjc@4202
    56
                    print line
gjc@4202
    57
                    count += 1
gjc@4202
    58
                    if count > 100:
gjc@4202
    59
                        break
gjc@4202
    60
        return 1
gjc@4202
    61
    else:
gjc@4202
    62
        return 0
gjc@4114
    63
gjc@4114
    64
class regression_test_task(Task.TaskBase):
gjc@4114
    65
    after = 'cc cxx cc_link cxx_link'
gjc@4114
    66
    color = 'BLUE'
gjc@4114
    67
gjc@4326
    68
    def __init__(self, bld, env, test_name, test_scripts_dir, build_traces_dir, reference_traces):
gjc@4326
    69
        self.bld = bld
gjc@4326
    70
        self.generator = self
gjc@4114
    71
        self.env = env
gjc@4326
    72
        super(regression_test_task, self).__init__(generator=self, env=env)
gjc@4114
    73
        self.test_name = test_name
gjc@4114
    74
        self.test_scripts_dir = test_scripts_dir
gjc@4114
    75
        self.build_traces_dir = build_traces_dir
gjc@4114
    76
        self.reference_traces_dir = reference_traces
gjc@4114
    77
gjc@4114
    78
    def __str__(self):
gjc@4114
    79
        return 'regression-test (%s)\n' % self.test_name
gjc@4114
    80
gjc@4326
    81
    def runnable_status(self):
gjc@4326
    82
        return Task.RUN_ME
gjc@4326
    83
gjc@4114
    84
    def run(self):
gjc@4114
    85
        """Run a single test"""
gjc@4114
    86
        sys.path.insert(0, self.test_scripts_dir)
gjc@4114
    87
        try:
gjc@4114
    88
            mod = __import__(self.test_name, globals(), locals(), [])
gjc@4114
    89
        finally:
gjc@4114
    90
            sys.path.remove(self.test_scripts_dir)
gjc@4114
    91
gjc@4114
    92
        assert self.test_name.startswith('test-')
gjc@4114
    93
        short_name = self.test_name[len('test-'):]
gjc@4114
    94
gjc@4114
    95
        trace_dir_name = getattr(mod, "trace_dir_name", None)
gjc@4114
    96
        if trace_dir_name is None:
gjc@4114
    97
            trace_dir_name = "%s.ref" % short_name
gjc@4114
    98
        trace_output_path = os.path.join(self.build_traces_dir, trace_dir_name)
gjc@4114
    99
        reference_traces_path = os.path.join(self.reference_traces_dir, trace_dir_name)
gjc@4114
   100
gjc@4114
   101
        if hasattr(mod, 'get_arguments'):
gjc@4114
   102
            arguments = mod.get_arguments(self.env, '..')
gjc@4114
   103
        else:
gjc@4114
   104
            arguments = getattr(mod, "arguments", [])
gjc@4114
   105
gjc@4114
   106
        pyscript = getattr(mod, "pyscript", None)
gjc@4114
   107
        if pyscript:
gjc@4114
   108
            is_pyscript = True
gjc@4114
   109
            program = pyscript
gjc@4114
   110
        else:
gjc@4114
   111
            is_pyscript = False
gjc@4114
   112
            program = getattr(mod, "program", short_name)
gjc@4114
   113
gjc@4114
   114
        if hasattr(mod, 'may_run'):
gjc@4130
   115
            reason_cannot_run = mod.may_run(self.env, Options.options)
gjc@4114
   116
        else:
gjc@4114
   117
            reason_cannot_run = None
gjc@4114
   118
        if reason_cannot_run:
gjc@4114
   119
            print "SKIP %s (%s)" % (self.test_name, reason_cannot_run)
gjc@4114
   120
            self.result = None
gjc@4114
   121
            return 0
gjc@4114
   122
gjc@4114
   123
        if Options.options.regression_generate:
gjc@4114
   124
            # clean the target dir
gjc@4308
   125
            try:
gjc@4326
   126
                shutil.rmtree(reference_traces_path)
gjc@4308
   127
            except OSError, ex:
gjc@4308
   128
                if ex.errno not in [errno.ENOENT]:
gjc@4308
   129
                    raise
gjc@4114
   130
            os.makedirs(reference_traces_path)
gjc@4114
   131
            result = self.run_reference_generate(reference_traces_path, program, arguments, is_pyscript)
gjc@4114
   132
            if result == 0:
gjc@4114
   133
                print "GENERATE " + self.test_name
gjc@4114
   134
            else:
gjc@4114
   135
                print "GENERATE FAIL " + self.test_name
gjc@4114
   136
        else:
gjc@4114
   137
            # clean the target dir
gjc@4308
   138
            try:
gjc@4308
   139
                shutil.rmtree(trace_output_path)
gjc@4308
   140
            except OSError, ex:
gjc@4308
   141
                if ex.errno not in [errno.ENOENT]:
gjc@4308
   142
                    raise
gjc@4114
   143
            os.makedirs(trace_output_path)
gjc@4114
   144
            # run it
gjc@4114
   145
            result = self.run_reference_test(reference_traces_path, trace_output_path, program, arguments, is_pyscript)
gjc@4114
   146
            if result == 0:
gjc@4114
   147
                print "PASS " + self.test_name
gjc@4114
   148
            else:
gjc@4114
   149
                print "FAIL " + self.test_name
gjc@4114
   150
        self.result = result
gjc@4114
   151
        return 0
gjc@4114
   152
gjc@4114
   153
    def run_reference_test(self, reference_traces_path, trace_output_path, program, arguments, is_pyscript):
gjc@4114
   154
        if not os.path.exists(reference_traces_path):
gjc@4114
   155
            print "Cannot locate reference traces in " + reference_traces_path
gjc@4114
   156
            return 1
gjc@4114
   157
gjc@4114
   158
        if is_pyscript:
gjc@4114
   159
            script = os.path.abspath(os.path.join('..', *os.path.split(program)))
gjc@4114
   160
            argv = [self.env['PYTHON'], script] + arguments
gjc@4129
   161
            try:
gjc@4388
   162
                wutils.run_argv(argv, self.env, cwd=trace_output_path, force_no_valgrind=True)
gjc@4129
   163
            except Utils.WafError, ex:
gjc@4129
   164
                print >> sys.stderr, ex
gjc@4129
   165
                return 1
gjc@4114
   166
        else:
gjc@4129
   167
            try:
gjc@4332
   168
                wutils.run_program(program, self.env,
gjc@4129
   169
                                   command_template=wutils.get_command_template(self.env, arguments),
gjc@4129
   170
                                   cwd=trace_output_path)
gjc@4129
   171
            except Utils.WafError, ex:
gjc@4129
   172
                print >> sys.stderr, ex
gjc@4129
   173
                return 1
gjc@4114
   174
gjc@4202
   175
        rc = diff(trace_output_path, reference_traces_path, Options.options.verbose)
gjc@4114
   176
        if rc:
gjc@4114
   177
            print "----------"
gjc@4114
   178
            print "Traces differ in test: ", self.test_name
gjc@4114
   179
            print "Reference traces in directory: " + reference_traces_path
gjc@4114
   180
            print "Traces in directory: " + trace_output_path
gjc@4114
   181
            print "Run the following command for details:"
gjc@4114
   182
            print "\tdiff -u %s %s" % (reference_traces_path, trace_output_path)
gjc@4202
   183
            if not Options.options.verbose:
gjc@4202
   184
                print "Or re-run regression testing with option -v"
gjc@4114
   185
            print "----------"
gjc@4114
   186
        return rc
gjc@4114
   187
gjc@4114
   188
gjc@4114
   189
    def run_reference_generate(self, trace_output_path, program, arguments, is_pyscript):
gjc@4114
   190
        if is_pyscript:
gjc@4114
   191
            script = os.path.abspath(os.path.join('..', *os.path.split(program)))
gjc@4114
   192
            argv = [self.env['PYTHON'], script] + arguments
gjc@4129
   193
            try:
gjc@4388
   194
                retval = wutils.run_argv(argv, self.env, cwd=trace_output_path, force_no_valgrind=True)
gjc@4129
   195
            except Utils.WafError, ex:
gjc@4129
   196
                print >> sys.stderr, ex
gjc@4129
   197
                return 1
gjc@4114
   198
        else:
gjc@4129
   199
            try:
gjc@4332
   200
                retval = wutils.run_program(program, self.env,
gjc@4129
   201
                                            command_template=wutils.get_command_template(self.env, arguments),
gjc@4129
   202
                                            cwd=trace_output_path)
gjc@4129
   203
            except Utils.WafError, ex:
gjc@4129
   204
                print >> sys.stderr, ex
gjc@4129
   205
                return 1
gjc@4114
   206
        return retval
gjc@4114
   207
gjc@4114
   208
gjc@4114
   209
class regression_test_collector_task(Task.TaskBase):
gjc@4114
   210
    after = 'regression_test_task'
gjc@4114
   211
    color = 'BLUE'
gjc@4114
   212
gjc@4326
   213
    def __init__(self, bld, test_tasks):
gjc@4326
   214
        self.bld = bld
gjc@4326
   215
        super(regression_test_collector_task, self).__init__(generator=self)
gjc@4114
   216
        self.test_tasks = test_tasks
gjc@4114
   217
gjc@4114
   218
    def __str__(self):
gjc@4114
   219
        return 'regression-test-collector\n'
gjc@4114
   220
gjc@4326
   221
    def runnable_status(self):
gjc@4326
   222
        return Task.RUN_ME
gjc@4326
   223
gjc@4114
   224
    def run(self):
gjc@4114
   225
        failed_tests = [test for test in self.test_tasks if test.result is not None and test.result != 0]
gjc@4114
   226
        skipped_tests = [test for test in self.test_tasks if test.result is None]
gjc@4114
   227
        print "Regression testing summary:"
gjc@4114
   228
        if skipped_tests:
gjc@4114
   229
            print "SKIP: %i of %i tests have been skipped (%s)" % (
gjc@4114
   230
                len(skipped_tests), len(self.test_tasks),
gjc@4114
   231
                ', '.join([test.test_name for test in skipped_tests]))
gjc@4114
   232
        if failed_tests:
gjc@4114
   233
            print "FAIL: %i of %i tests have failed (%s)" % (
gjc@4114
   234
                len(failed_tests), len(self.test_tasks),
gjc@4114
   235
                ', '.join([test.test_name for test in failed_tests]))
gjc@4114
   236
            return 1
gjc@4114
   237
        else:
gjc@4114
   238
            print "PASS: %i of %i tests passed" % (len(self.test_tasks) - len(skipped_tests),
gjc@4114
   239
                                                   len(self.test_tasks))
gjc@4114
   240
            return 0
gjc@4114
   241
gjc@4114
   242
def run_regression(bld, reference_traces):
gjc@3872
   243
    """Execute regression tests.  Called with cwd set to the 'regression' subdir of ns-3.
gjc@3872
   244
gjc@4109
   245
    @param reference_traces: reference traces directory.
gjc@3872
   246
gjc@3872
   247
    """
gjc@3866
   248
gjc@4110
   249
    testdir = os.path.join("regression", "tests")
gjc@3866
   250
    if not os.path.exists(testdir):
gjc@3866
   251
        print "Tests directory does not exist"
gjc@3866
   252
        sys.exit(3)
gjc@3872
   253
gjc@4064
   254
    if Options.options.regression_tests:
gjc@4064
   255
        tests = Options.options.regression_tests.split(',')
gjc@3866
   256
    else:
gjc@3866
   257
        tests = _find_tests(testdir)
gjc@3866
   258
gjc@3872
   259
    if not os.path.exists(reference_traces):
gjc@3872
   260
        print "Reference traces directory (%s) does not exist" % reference_traces
gjc@3866
   261
        return 3
gjc@3866
   262
    
gjc@4114
   263
    test_scripts_dir = bld.path.find_dir('regression/tests').abspath()
gjc@4114
   264
    build_traces_dir = bld.path.find_or_declare('regression/traces').abspath(bld.env)
gjc@4114
   265
    tasks = []
gjc@3866
   266
    for test in tests:
gjc@4326
   267
        task = regression_test_task(bld, bld.env, test, test_scripts_dir, build_traces_dir, reference_traces)
gjc@4326
   268
        #bld.task_manager.add_task(task)
gjc@4326
   269
        tasks.append(task)
gjc@4326
   270
    regression_test_collector_task(bld, tasks)