regression.py
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 4584 7f4ee7f84b19
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
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@4586
    43
gjc@4586
    44
    if differ:
gjc@4586
    45
        # ok, stupid binary comparison reports differences, but maybe
gjc@4586
    46
        # only text files differ, in which case we should compare
gjc@4586
    47
        # again while ignoring newline differences between
gjc@4586
    48
        # windows/mac/unix.
gjc@4586
    49
        if not comp.left_only and not comp.right_only:
gjc@4586
    50
            for diff_fname in comp.diff_files:
gjc@4586
    51
                if not (diff_fname.endswith(".tr") or diff_fname.endswith(".mob")):
gjc@4586
    52
                    # doesn't look like a text file; it has to differ
gjc@4586
    53
                    break
gjc@4586
    54
                diff_file1 = open(os.path.join(dir1, diff_fname), "rtU").readlines()
gjc@4586
    55
                diff_file2 = open(os.path.join(dir2, diff_fname), "rtU").readlines()
gjc@4586
    56
                if diff_file1 != diff_file2:
gjc@4586
    57
                    break
gjc@4586
    58
                #else:
gjc@4586
    59
                #    print ">>>>>>>> %s file does not really differ!" % (diff_fname)
gjc@4586
    60
            else:
gjc@4586
    61
                differ = False
gjc@4586
    62
gjc@4202
    63
    if differ:
gjc@4202
    64
        if verbose:
gjc@4202
    65
            comp.report()
gjc@4202
    66
            import difflib
gjc@4202
    67
            for diff_fname in comp.diff_files:
gjc@4202
    68
                if not (diff_fname.endswith(".tr") or diff_fname.endswith(".mob")):
gjc@4202
    69
                    print "The different file %r does not sound like a text file, not compared." % (diff_fname,)
gjc@4202
    70
                diff_file1 = open(os.path.join(dir1, diff_fname), "rt").readlines()
gjc@4202
    71
                diff_file2 = open(os.path.join(dir2, diff_fname), "rt").readlines()
gjc@4202
    72
                diff = difflib.unified_diff(diff_file1, diff_file2)
gjc@4202
    73
                count = 0
gjc@4202
    74
                print "Differences in file %r" % (diff_fname,)
gjc@4202
    75
                for line in diff:
gjc@4202
    76
                    print line
gjc@4202
    77
                    count += 1
gjc@4202
    78
                    if count > 100:
gjc@4202
    79
                        break
gjc@4202
    80
        return 1
gjc@4202
    81
    else:
gjc@4202
    82
        return 0
gjc@4114
    83
gjc@4114
    84
class regression_test_task(Task.TaskBase):
gjc@4114
    85
    after = 'cc cxx cc_link cxx_link'
gjc@4114
    86
    color = 'BLUE'
gjc@4114
    87
gjc@4326
    88
    def __init__(self, bld, env, test_name, test_scripts_dir, build_traces_dir, reference_traces):
gjc@4326
    89
        self.bld = bld
gjc@4326
    90
        self.generator = self
gjc@4114
    91
        self.env = env
gjc@4326
    92
        super(regression_test_task, self).__init__(generator=self, env=env)
gjc@4114
    93
        self.test_name = test_name
gjc@4114
    94
        self.test_scripts_dir = test_scripts_dir
gjc@4114
    95
        self.build_traces_dir = build_traces_dir
gjc@4114
    96
        self.reference_traces_dir = reference_traces
gjc@4114
    97
gjc@4114
    98
    def __str__(self):
gjc@4114
    99
        return 'regression-test (%s)\n' % self.test_name
gjc@4114
   100
gjc@4326
   101
    def runnable_status(self):
gjc@4326
   102
        return Task.RUN_ME
gjc@4326
   103
gjc@4114
   104
    def run(self):
gjc@4114
   105
        """Run a single test"""
gjc@4114
   106
        sys.path.insert(0, self.test_scripts_dir)
gjc@4114
   107
        try:
gjc@4114
   108
            mod = __import__(self.test_name, globals(), locals(), [])
gjc@4114
   109
        finally:
gjc@4114
   110
            sys.path.remove(self.test_scripts_dir)
gjc@4114
   111
gjc@4114
   112
        assert self.test_name.startswith('test-')
gjc@4114
   113
        short_name = self.test_name[len('test-'):]
gjc@4114
   114
gjc@4114
   115
        trace_dir_name = getattr(mod, "trace_dir_name", None)
gjc@4114
   116
        if trace_dir_name is None:
gjc@4114
   117
            trace_dir_name = "%s.ref" % short_name
gjc@4114
   118
        trace_output_path = os.path.join(self.build_traces_dir, trace_dir_name)
gjc@4114
   119
        reference_traces_path = os.path.join(self.reference_traces_dir, trace_dir_name)
gjc@4114
   120
gjc@4114
   121
        if hasattr(mod, 'get_arguments'):
gjc@4114
   122
            arguments = mod.get_arguments(self.env, '..')
gjc@4114
   123
        else:
gjc@4114
   124
            arguments = getattr(mod, "arguments", [])
gjc@4114
   125
gjc@4114
   126
        pyscript = getattr(mod, "pyscript", None)
gjc@4114
   127
        if pyscript:
gjc@4114
   128
            is_pyscript = True
gjc@4114
   129
            program = pyscript
gjc@4114
   130
        else:
gjc@4114
   131
            is_pyscript = False
gjc@4114
   132
            program = getattr(mod, "program", short_name)
gjc@4114
   133
gjc@4114
   134
        if hasattr(mod, 'may_run'):
gjc@4130
   135
            reason_cannot_run = mod.may_run(self.env, Options.options)
gjc@4114
   136
        else:
gjc@4114
   137
            reason_cannot_run = None
gjc@4114
   138
        if reason_cannot_run:
gjc@4114
   139
            print "SKIP %s (%s)" % (self.test_name, reason_cannot_run)
gjc@4114
   140
            self.result = None
gjc@4114
   141
            return 0
gjc@4114
   142
gjc@4114
   143
        if Options.options.regression_generate:
gjc@4114
   144
            # clean the target dir
gjc@4308
   145
            try:
gjc@4326
   146
                shutil.rmtree(reference_traces_path)
gjc@4308
   147
            except OSError, ex:
gjc@4308
   148
                if ex.errno not in [errno.ENOENT]:
gjc@4308
   149
                    raise
gjc@4114
   150
            os.makedirs(reference_traces_path)
gjc@4114
   151
            result = self.run_reference_generate(reference_traces_path, program, arguments, is_pyscript)
gjc@4114
   152
            if result == 0:
gjc@4114
   153
                print "GENERATE " + self.test_name
gjc@4114
   154
            else:
gjc@4114
   155
                print "GENERATE FAIL " + self.test_name
gjc@4114
   156
        else:
gjc@4114
   157
            # clean the target dir
gjc@4308
   158
            try:
gjc@4308
   159
                shutil.rmtree(trace_output_path)
gjc@4308
   160
            except OSError, ex:
gjc@4308
   161
                if ex.errno not in [errno.ENOENT]:
gjc@4308
   162
                    raise
gjc@4114
   163
            os.makedirs(trace_output_path)
gjc@4114
   164
            # run it
gjc@4584
   165
            #print "self.run_reference_test:(%r, %r, %r, %r, %r)" \
gjc@4584
   166
            #    % (reference_traces_path, trace_output_path, program, arguments, is_pyscript)
gjc@4114
   167
            result = self.run_reference_test(reference_traces_path, trace_output_path, program, arguments, is_pyscript)
gjc@4114
   168
            if result == 0:
gjc@4114
   169
                print "PASS " + self.test_name
gjc@4114
   170
            else:
gjc@4114
   171
                print "FAIL " + self.test_name
gjc@4114
   172
        self.result = result
gjc@4114
   173
        return 0
gjc@4114
   174
gjc@4114
   175
    def run_reference_test(self, reference_traces_path, trace_output_path, program, arguments, is_pyscript):
gjc@4114
   176
        if not os.path.exists(reference_traces_path):
gjc@4114
   177
            print "Cannot locate reference traces in " + reference_traces_path
gjc@4114
   178
            return 1
gjc@4114
   179
gjc@4114
   180
        if is_pyscript:
gjc@4114
   181
            script = os.path.abspath(os.path.join('..', *os.path.split(program)))
gjc@4114
   182
            argv = [self.env['PYTHON'], script] + arguments
gjc@4129
   183
            try:
gjc@4388
   184
                wutils.run_argv(argv, self.env, cwd=trace_output_path, force_no_valgrind=True)
gjc@4129
   185
            except Utils.WafError, ex:
gjc@4129
   186
                print >> sys.stderr, ex
gjc@4129
   187
                return 1
gjc@4114
   188
        else:
gjc@4129
   189
            try:
gjc@4332
   190
                wutils.run_program(program, self.env,
gjc@4129
   191
                                   command_template=wutils.get_command_template(self.env, arguments),
gjc@4129
   192
                                   cwd=trace_output_path)
gjc@4129
   193
            except Utils.WafError, ex:
gjc@4129
   194
                print >> sys.stderr, ex
gjc@4129
   195
                return 1
gjc@4114
   196
gjc@4202
   197
        rc = diff(trace_output_path, reference_traces_path, Options.options.verbose)
gjc@4114
   198
        if rc:
gjc@4114
   199
            print "----------"
gjc@4114
   200
            print "Traces differ in test: ", self.test_name
gjc@4114
   201
            print "Reference traces in directory: " + reference_traces_path
gjc@4114
   202
            print "Traces in directory: " + trace_output_path
gjc@4114
   203
            print "Run the following command for details:"
gjc@4114
   204
            print "\tdiff -u %s %s" % (reference_traces_path, trace_output_path)
gjc@4202
   205
            if not Options.options.verbose:
gjc@4202
   206
                print "Or re-run regression testing with option -v"
gjc@4114
   207
            print "----------"
gjc@4114
   208
        return rc
gjc@4114
   209
gjc@4114
   210
gjc@4114
   211
    def run_reference_generate(self, trace_output_path, program, arguments, is_pyscript):
gjc@4114
   212
        if is_pyscript:
gjc@4114
   213
            script = os.path.abspath(os.path.join('..', *os.path.split(program)))
gjc@4114
   214
            argv = [self.env['PYTHON'], script] + arguments
gjc@4129
   215
            try:
gjc@4388
   216
                retval = wutils.run_argv(argv, self.env, cwd=trace_output_path, force_no_valgrind=True)
gjc@4129
   217
            except Utils.WafError, ex:
gjc@4129
   218
                print >> sys.stderr, ex
gjc@4129
   219
                return 1
gjc@4114
   220
        else:
gjc@4129
   221
            try:
gjc@4332
   222
                retval = wutils.run_program(program, self.env,
gjc@4129
   223
                                            command_template=wutils.get_command_template(self.env, arguments),
gjc@4129
   224
                                            cwd=trace_output_path)
gjc@4129
   225
            except Utils.WafError, ex:
gjc@4129
   226
                print >> sys.stderr, ex
gjc@4129
   227
                return 1
gjc@4114
   228
        return retval
gjc@4114
   229
gjc@4114
   230
gjc@4114
   231
class regression_test_collector_task(Task.TaskBase):
gjc@4114
   232
    after = 'regression_test_task'
gjc@4114
   233
    color = 'BLUE'
gjc@4114
   234
gjc@4326
   235
    def __init__(self, bld, test_tasks):
gjc@4326
   236
        self.bld = bld
gjc@4326
   237
        super(regression_test_collector_task, self).__init__(generator=self)
gjc@4114
   238
        self.test_tasks = test_tasks
gjc@4114
   239
gjc@4114
   240
    def __str__(self):
gjc@4114
   241
        return 'regression-test-collector\n'
gjc@4114
   242
gjc@4326
   243
    def runnable_status(self):
gjc@4326
   244
        return Task.RUN_ME
gjc@4326
   245
gjc@4114
   246
    def run(self):
gjc@4114
   247
        failed_tests = [test for test in self.test_tasks if test.result is not None and test.result != 0]
gjc@4114
   248
        skipped_tests = [test for test in self.test_tasks if test.result is None]
gjc@4114
   249
        print "Regression testing summary:"
gjc@4114
   250
        if skipped_tests:
gjc@4114
   251
            print "SKIP: %i of %i tests have been skipped (%s)" % (
gjc@4114
   252
                len(skipped_tests), len(self.test_tasks),
gjc@4114
   253
                ', '.join([test.test_name for test in skipped_tests]))
gjc@4114
   254
        if failed_tests:
gjc@4114
   255
            print "FAIL: %i of %i tests have failed (%s)" % (
gjc@4114
   256
                len(failed_tests), len(self.test_tasks),
gjc@4114
   257
                ', '.join([test.test_name for test in failed_tests]))
gjc@4114
   258
            return 1
gjc@4114
   259
        else:
gjc@4114
   260
            print "PASS: %i of %i tests passed" % (len(self.test_tasks) - len(skipped_tests),
gjc@4114
   261
                                                   len(self.test_tasks))
gjc@4114
   262
            return 0
gjc@4114
   263
gjc@4114
   264
def run_regression(bld, reference_traces):
gjc@3872
   265
    """Execute regression tests.  Called with cwd set to the 'regression' subdir of ns-3.
gjc@3872
   266
gjc@4109
   267
    @param reference_traces: reference traces directory.
gjc@3872
   268
gjc@3872
   269
    """
gjc@3866
   270
gjc@4110
   271
    testdir = os.path.join("regression", "tests")
gjc@3866
   272
    if not os.path.exists(testdir):
gjc@3866
   273
        print "Tests directory does not exist"
gjc@3866
   274
        sys.exit(3)
gjc@3872
   275
gjc@4064
   276
    if Options.options.regression_tests:
gjc@4064
   277
        tests = Options.options.regression_tests.split(',')
gjc@3866
   278
    else:
gjc@3866
   279
        tests = _find_tests(testdir)
gjc@3866
   280
gjc@3872
   281
    if not os.path.exists(reference_traces):
gjc@3872
   282
        print "Reference traces directory (%s) does not exist" % reference_traces
gjc@3866
   283
        return 3
gjc@3866
   284
    
gjc@4114
   285
    test_scripts_dir = bld.path.find_dir('regression/tests').abspath()
gjc@4114
   286
    build_traces_dir = bld.path.find_or_declare('regression/traces').abspath(bld.env)
gjc@4114
   287
    tasks = []
gjc@3866
   288
    for test in tests:
gjc@4326
   289
        task = regression_test_task(bld, bld.env, test, test_scripts_dir, build_traces_dir, reference_traces)
gjc@4326
   290
        #bld.task_manager.add_task(task)
gjc@4326
   291
        tasks.append(task)
gjc@4326
   292
    regression_test_collector_task(bld, tasks)