regression.py
author Josh Pelkey <jpelkey@gatech.edu>
Wed, 11 Aug 2010 11:37:37 -0400
changeset 6553 fb5ad9c7755a
parent 4760 f774ff724ee4
permissions -rw-r--r--
update release notes and fix doxygen warnings
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@4760
    94
gjc@4760
    95
        assert self.test_name.startswith('test-')
gjc@4760
    96
        short_name = self.test_name[len('test-'):]
gjc@4760
    97
gjc@4114
    98
        self.test_scripts_dir = test_scripts_dir
gjc@4114
    99
        self.build_traces_dir = build_traces_dir
gjc@4114
   100
        self.reference_traces_dir = reference_traces
gjc@4114
   101
gjc@4760
   102
        sys.path.insert(0, self.test_scripts_dir)
gjc@4760
   103
        try:
gjc@4760
   104
            mod = __import__(self.test_name, globals(), locals(), [])
gjc@4760
   105
        finally:
gjc@4760
   106
            sys.path.remove(self.test_scripts_dir)
gjc@4760
   107
        self.mod = mod
gjc@4760
   108
        if hasattr(mod, 'may_run'):
gjc@4760
   109
            reason_cannot_run = mod.may_run(self.env, Options.options)
gjc@4760
   110
        else:
gjc@4760
   111
            reason_cannot_run = None
gjc@4760
   112
        if not reason_cannot_run:
gjc@4760
   113
            pyscript = getattr(mod, "pyscript", None)
gjc@4760
   114
            if pyscript:
gjc@5201
   115
                Options.options.compile_targets += ',ns3module,pybindgen-command'
gjc@4760
   116
            else:
gjc@4760
   117
                program = getattr(mod, "program", short_name)
gjc@4760
   118
                Options.options.compile_targets += ',' + program
gjc@4760
   119
gjc@4114
   120
    def __str__(self):
gjc@4114
   121
        return 'regression-test (%s)\n' % self.test_name
gjc@4114
   122
gjc@4326
   123
    def runnable_status(self):
gjc@4326
   124
        return Task.RUN_ME
gjc@4326
   125
gjc@4114
   126
    def run(self):
gjc@4114
   127
        """Run a single test"""
gjc@4114
   128
        assert self.test_name.startswith('test-')
gjc@4114
   129
        short_name = self.test_name[len('test-'):]
gjc@4760
   130
        mod = self.mod
gjc@4114
   131
        trace_dir_name = getattr(mod, "trace_dir_name", None)
gjc@4114
   132
        if trace_dir_name is None:
gjc@4114
   133
            trace_dir_name = "%s.ref" % short_name
gjc@4114
   134
        trace_output_path = os.path.join(self.build_traces_dir, trace_dir_name)
gjc@4114
   135
        reference_traces_path = os.path.join(self.reference_traces_dir, trace_dir_name)
gjc@4114
   136
gjc@4114
   137
        if hasattr(mod, 'get_arguments'):
gjc@4114
   138
            arguments = mod.get_arguments(self.env, '..')
gjc@4114
   139
        else:
gjc@4114
   140
            arguments = getattr(mod, "arguments", [])
gjc@4114
   141
gjc@4114
   142
        pyscript = getattr(mod, "pyscript", None)
gjc@4114
   143
        if pyscript:
gjc@4114
   144
            is_pyscript = True
gjc@4114
   145
            program = pyscript
gjc@4114
   146
        else:
gjc@4114
   147
            is_pyscript = False
gjc@4114
   148
            program = getattr(mod, "program", short_name)
gjc@4114
   149
gjc@4114
   150
        if hasattr(mod, 'may_run'):
gjc@4130
   151
            reason_cannot_run = mod.may_run(self.env, Options.options)
gjc@4114
   152
        else:
gjc@4114
   153
            reason_cannot_run = None
gjc@4114
   154
        if reason_cannot_run:
gjc@4114
   155
            print "SKIP %s (%s)" % (self.test_name, reason_cannot_run)
gjc@4114
   156
            self.result = None
gjc@4114
   157
            return 0
gjc@4114
   158
gjc@4114
   159
        if Options.options.regression_generate:
gjc@4114
   160
            # clean the target dir
gjc@4308
   161
            try:
gjc@4326
   162
                shutil.rmtree(reference_traces_path)
gjc@4308
   163
            except OSError, ex:
gjc@4308
   164
                if ex.errno not in [errno.ENOENT]:
gjc@4308
   165
                    raise
gjc@4114
   166
            os.makedirs(reference_traces_path)
gjc@4114
   167
            result = self.run_reference_generate(reference_traces_path, program, arguments, is_pyscript)
gjc@4114
   168
            if result == 0:
gjc@4114
   169
                print "GENERATE " + self.test_name
gjc@4114
   170
            else:
gjc@4114
   171
                print "GENERATE FAIL " + self.test_name
gjc@4114
   172
        else:
gjc@4114
   173
            # clean the target dir
gjc@4308
   174
            try:
gjc@4308
   175
                shutil.rmtree(trace_output_path)
gjc@4308
   176
            except OSError, ex:
gjc@4308
   177
                if ex.errno not in [errno.ENOENT]:
gjc@4308
   178
                    raise
gjc@4114
   179
            os.makedirs(trace_output_path)
gjc@4114
   180
            # run it
gjc@4584
   181
            #print "self.run_reference_test:(%r, %r, %r, %r, %r)" \
gjc@4584
   182
            #    % (reference_traces_path, trace_output_path, program, arguments, is_pyscript)
gjc@4114
   183
            result = self.run_reference_test(reference_traces_path, trace_output_path, program, arguments, is_pyscript)
gjc@4114
   184
            if result == 0:
gjc@4114
   185
                print "PASS " + self.test_name
gjc@4114
   186
            else:
gjc@4114
   187
                print "FAIL " + self.test_name
gjc@4114
   188
        self.result = result
gjc@4114
   189
        return 0
gjc@4114
   190
gjc@4114
   191
    def run_reference_test(self, reference_traces_path, trace_output_path, program, arguments, is_pyscript):
gjc@4114
   192
        if not os.path.exists(reference_traces_path):
gjc@4114
   193
            print "Cannot locate reference traces in " + reference_traces_path
gjc@4114
   194
            return 1
gjc@4114
   195
gjc@4114
   196
        if is_pyscript:
gjc@4114
   197
            script = os.path.abspath(os.path.join('..', *os.path.split(program)))
gjc@4114
   198
            argv = [self.env['PYTHON'], script] + arguments
gjc@4129
   199
            try:
gjc@4388
   200
                wutils.run_argv(argv, self.env, cwd=trace_output_path, force_no_valgrind=True)
gjc@4129
   201
            except Utils.WafError, ex:
gjc@4129
   202
                print >> sys.stderr, ex
gjc@4129
   203
                return 1
gjc@4114
   204
        else:
gjc@4129
   205
            try:
gjc@4332
   206
                wutils.run_program(program, self.env,
gjc@4129
   207
                                   command_template=wutils.get_command_template(self.env, arguments),
gjc@4129
   208
                                   cwd=trace_output_path)
gjc@4129
   209
            except Utils.WafError, ex:
gjc@4129
   210
                print >> sys.stderr, ex
gjc@4129
   211
                return 1
gjc@4114
   212
gjc@4202
   213
        rc = diff(trace_output_path, reference_traces_path, Options.options.verbose)
gjc@4114
   214
        if rc:
gjc@4114
   215
            print "----------"
gjc@4114
   216
            print "Traces differ in test: ", self.test_name
gjc@4114
   217
            print "Reference traces in directory: " + reference_traces_path
gjc@4114
   218
            print "Traces in directory: " + trace_output_path
gjc@4114
   219
            print "Run the following command for details:"
gjc@4114
   220
            print "\tdiff -u %s %s" % (reference_traces_path, trace_output_path)
gjc@4202
   221
            if not Options.options.verbose:
gjc@4202
   222
                print "Or re-run regression testing with option -v"
gjc@4114
   223
            print "----------"
gjc@4114
   224
        return rc
gjc@4114
   225
gjc@4114
   226
gjc@4114
   227
    def run_reference_generate(self, trace_output_path, program, arguments, is_pyscript):
gjc@4114
   228
        if is_pyscript:
gjc@4114
   229
            script = os.path.abspath(os.path.join('..', *os.path.split(program)))
gjc@4114
   230
            argv = [self.env['PYTHON'], script] + arguments
gjc@4129
   231
            try:
gjc@4388
   232
                retval = wutils.run_argv(argv, self.env, cwd=trace_output_path, force_no_valgrind=True)
gjc@4129
   233
            except Utils.WafError, ex:
gjc@4129
   234
                print >> sys.stderr, ex
gjc@4129
   235
                return 1
gjc@4114
   236
        else:
gjc@4129
   237
            try:
gjc@4332
   238
                retval = wutils.run_program(program, self.env,
gjc@4129
   239
                                            command_template=wutils.get_command_template(self.env, arguments),
gjc@4129
   240
                                            cwd=trace_output_path)
gjc@4129
   241
            except Utils.WafError, ex:
gjc@4129
   242
                print >> sys.stderr, ex
gjc@4129
   243
                return 1
gjc@4114
   244
        return retval
gjc@4114
   245
gjc@4114
   246
gjc@4114
   247
class regression_test_collector_task(Task.TaskBase):
gjc@4114
   248
    after = 'regression_test_task'
gjc@4114
   249
    color = 'BLUE'
gjc@4114
   250
gjc@4326
   251
    def __init__(self, bld, test_tasks):
gjc@4326
   252
        self.bld = bld
gjc@4326
   253
        super(regression_test_collector_task, self).__init__(generator=self)
gjc@4114
   254
        self.test_tasks = test_tasks
gjc@4114
   255
gjc@4114
   256
    def __str__(self):
gjc@4114
   257
        return 'regression-test-collector\n'
gjc@4114
   258
gjc@4326
   259
    def runnable_status(self):
gjc@4326
   260
        return Task.RUN_ME
gjc@4326
   261
gjc@4114
   262
    def run(self):
gjc@4114
   263
        failed_tests = [test for test in self.test_tasks if test.result is not None and test.result != 0]
gjc@4114
   264
        skipped_tests = [test for test in self.test_tasks if test.result is None]
gjc@4114
   265
        print "Regression testing summary:"
gjc@4114
   266
        if skipped_tests:
gjc@4114
   267
            print "SKIP: %i of %i tests have been skipped (%s)" % (
gjc@4114
   268
                len(skipped_tests), len(self.test_tasks),
gjc@4114
   269
                ', '.join([test.test_name for test in skipped_tests]))
gjc@4114
   270
        if failed_tests:
gjc@4114
   271
            print "FAIL: %i of %i tests have failed (%s)" % (
gjc@4114
   272
                len(failed_tests), len(self.test_tasks),
gjc@4114
   273
                ', '.join([test.test_name for test in failed_tests]))
gjc@4114
   274
            return 1
gjc@4114
   275
        else:
gjc@4114
   276
            print "PASS: %i of %i tests passed" % (len(self.test_tasks) - len(skipped_tests),
gjc@4114
   277
                                                   len(self.test_tasks))
gjc@4114
   278
            return 0
gjc@4114
   279
gjc@4114
   280
def run_regression(bld, reference_traces):
gjc@3872
   281
    """Execute regression tests.  Called with cwd set to the 'regression' subdir of ns-3.
gjc@3872
   282
gjc@4109
   283
    @param reference_traces: reference traces directory.
gjc@3872
   284
gjc@3872
   285
    """
gjc@3866
   286
gjc@4110
   287
    testdir = os.path.join("regression", "tests")
gjc@3866
   288
    if not os.path.exists(testdir):
gjc@3866
   289
        print "Tests directory does not exist"
gjc@3866
   290
        sys.exit(3)
gjc@3872
   291
gjc@4064
   292
    if Options.options.regression_tests:
gjc@4064
   293
        tests = Options.options.regression_tests.split(',')
gjc@3866
   294
    else:
gjc@3866
   295
        tests = _find_tests(testdir)
gjc@3866
   296
gjc@3872
   297
    if not os.path.exists(reference_traces):
gjc@3872
   298
        print "Reference traces directory (%s) does not exist" % reference_traces
gjc@3866
   299
        return 3
gjc@3866
   300
    
gjc@4114
   301
    test_scripts_dir = bld.path.find_dir('regression/tests').abspath()
gjc@4114
   302
    build_traces_dir = bld.path.find_or_declare('regression/traces').abspath(bld.env)
gjc@4114
   303
    tasks = []
gjc@3866
   304
    for test in tests:
gjc@4326
   305
        task = regression_test_task(bld, bld.env, test, test_scripts_dir, build_traces_dir, reference_traces)
gjc@4326
   306
        #bld.task_manager.add_task(task)
gjc@4326
   307
        tasks.append(task)
gjc@4326
   308
    regression_test_collector_task(bld, tasks)