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