gjc@4064: # python lib modules gjc@3866: import os gjc@3866: import sys gjc@3866: import shutil gjc@3866: import pproc as subprocess gjc@3866: import urllib gjc@3866: gjc@4064: # WAF modules gjc@4064: import Build gjc@4064: import Options gjc@4064: import Utils gjc@4114: import Task gjc@4064: gjc@4064: # local modules gjc@3866: import wutils gjc@3866: gjc@4064: gjc@3866: def dev_null(): gjc@3866: if sys.platform == 'win32': gjc@3866: return open("NUL:", "w") gjc@3866: else: gjc@3866: return open("/dev/null", "w") gjc@3866: gjc@3866: gjc@3866: def _find_tests(testdir): gjc@3866: """Return a list of test modules in the test directory gjc@3866: gjc@3866: Arguments: gjc@3866: testdir -- the directory to look in for tests gjc@3866: """ gjc@3866: names = os.listdir(testdir) gjc@3866: tests = [] gjc@3866: for name in names: gjc@3866: if name[:5] == "test-" and name[-3:] == ".py": gjc@3866: testname = name[:-3] gjc@3866: tests.append(testname) gjc@3866: tests.sort() gjc@3866: return tests gjc@3866: gjc@4114: gjc@4114: class regression_test_task(Task.TaskBase): gjc@4114: after = 'cc cxx cc_link cxx_link' gjc@4114: color = 'BLUE' gjc@4114: gjc@4114: def __init__(self, env, test_name, test_scripts_dir, build_traces_dir, reference_traces): gjc@4114: super(regression_test_task, self).__init__() gjc@4114: self.env = env gjc@4114: self.test_name = test_name gjc@4114: self.test_scripts_dir = test_scripts_dir gjc@4114: self.build_traces_dir = build_traces_dir gjc@4114: self.reference_traces_dir = reference_traces gjc@4114: gjc@4114: def __str__(self): gjc@4114: return 'regression-test (%s)\n' % self.test_name gjc@4114: gjc@4114: def run(self): gjc@4114: """Run a single test""" gjc@4114: sys.path.insert(0, self.test_scripts_dir) gjc@4114: try: gjc@4114: mod = __import__(self.test_name, globals(), locals(), []) gjc@4114: finally: gjc@4114: sys.path.remove(self.test_scripts_dir) gjc@4114: gjc@4114: assert self.test_name.startswith('test-') gjc@4114: short_name = self.test_name[len('test-'):] gjc@4114: gjc@4114: trace_dir_name = getattr(mod, "trace_dir_name", None) gjc@4114: if trace_dir_name is None: gjc@4114: trace_dir_name = "%s.ref" % short_name gjc@4114: trace_output_path = os.path.join(self.build_traces_dir, trace_dir_name) gjc@4114: reference_traces_path = os.path.join(self.reference_traces_dir, trace_dir_name) gjc@4114: gjc@4114: if hasattr(mod, 'get_arguments'): gjc@4114: arguments = mod.get_arguments(self.env, '..') gjc@4114: else: gjc@4114: arguments = getattr(mod, "arguments", []) gjc@4114: gjc@4114: pyscript = getattr(mod, "pyscript", None) gjc@4114: if pyscript: gjc@4114: is_pyscript = True gjc@4114: program = pyscript gjc@4114: else: gjc@4114: is_pyscript = False gjc@4114: program = getattr(mod, "program", short_name) gjc@4114: gjc@4114: if hasattr(mod, 'may_run'): gjc@4114: reason_cannot_run = mod.may_run(self.env) gjc@4114: else: gjc@4114: reason_cannot_run = None gjc@4114: if reason_cannot_run: gjc@4114: print "SKIP %s (%s)" % (self.test_name, reason_cannot_run) gjc@4114: self.result = None gjc@4114: return 0 gjc@4114: gjc@4114: if Options.options.regression_generate: gjc@4114: # clean the target dir gjc@4114: shutil.rmtree(reference_traces_path, ignore_errors=True) gjc@4114: os.makedirs(reference_traces_path) gjc@4114: result = self.run_reference_generate(reference_traces_path, program, arguments, is_pyscript) gjc@4114: if result == 0: gjc@4114: print "GENERATE " + self.test_name gjc@4114: else: gjc@4114: print "GENERATE FAIL " + self.test_name gjc@4114: else: gjc@4114: # clean the target dir gjc@4114: shutil.rmtree(trace_output_path, ignore_errors=True) gjc@4114: os.makedirs(trace_output_path) gjc@4114: # run it gjc@4114: result = self.run_reference_test(reference_traces_path, trace_output_path, program, arguments, is_pyscript) gjc@4114: if result == 0: gjc@4114: print "PASS " + self.test_name gjc@4114: else: gjc@4114: print "FAIL " + self.test_name gjc@4114: self.result = result gjc@4114: return 0 gjc@4114: gjc@4114: def run_reference_test(self, reference_traces_path, trace_output_path, program, arguments, is_pyscript): gjc@4114: if not os.path.exists(reference_traces_path): gjc@4114: print "Cannot locate reference traces in " + reference_traces_path gjc@4114: return 1 gjc@4114: gjc@4114: if is_pyscript: gjc@4114: script = os.path.abspath(os.path.join('..', *os.path.split(program))) gjc@4114: argv = [self.env['PYTHON'], script] + arguments gjc@4129: try: gjc@4129: wutils.run_argv(argv, cwd=trace_output_path) gjc@4129: except Utils.WafError, ex: gjc@4129: print >> sys.stderr, ex gjc@4129: return 1 gjc@4114: else: gjc@4129: try: gjc@4129: wutils.run_program(program, gjc@4129: command_template=wutils.get_command_template(self.env, arguments), gjc@4129: cwd=trace_output_path) gjc@4129: except Utils.WafError, ex: gjc@4129: print >> sys.stderr, ex gjc@4129: return 1 gjc@4114: gjc@4114: if Options.options.verbose: gjc@4114: #diffCmd = "diff traces " + refTestDirName + " | head" gjc@4114: diffCmd = subprocess.Popen([self.env['DIFF'], trace_output_path, reference_traces_path], gjc@4114: stdout=subprocess.PIPE) gjc@4114: headCmd = subprocess.Popen("head", stdin=diffCmd.stdout) gjc@4114: rc2 = headCmd.wait() gjc@4114: diffCmd.stdout.close() gjc@4114: rc1 = diffCmd.wait() gjc@4114: rc = rc1 or rc2 gjc@4114: else: gjc@4114: rc = subprocess.Popen([self.env['DIFF'], trace_output_path, reference_traces_path], stdout=dev_null()).wait() gjc@4114: if rc: gjc@4114: print "----------" gjc@4114: print "Traces differ in test: ", self.test_name gjc@4114: print "Reference traces in directory: " + reference_traces_path gjc@4114: print "Traces in directory: " + trace_output_path gjc@4114: print "Run the following command for details:" gjc@4114: print "\tdiff -u %s %s" % (reference_traces_path, trace_output_path) gjc@4114: print "----------" gjc@4114: return rc gjc@4114: gjc@4114: gjc@4114: def run_reference_generate(self, trace_output_path, program, arguments, is_pyscript): gjc@4114: if is_pyscript: gjc@4114: script = os.path.abspath(os.path.join('..', *os.path.split(program))) gjc@4114: argv = [self.env['PYTHON'], script] + arguments gjc@4129: try: gjc@4129: retval = wutils.run_argv(argv, cwd=trace_output_path) gjc@4129: except Utils.WafError, ex: gjc@4129: print >> sys.stderr, ex gjc@4129: return 1 gjc@4114: else: gjc@4129: try: gjc@4129: retval = wutils.run_program(program, gjc@4129: command_template=wutils.get_command_template(self.env, arguments), gjc@4129: cwd=trace_output_path) gjc@4129: except Utils.WafError, ex: gjc@4129: print >> sys.stderr, ex gjc@4129: return 1 gjc@4114: return retval gjc@4114: gjc@4114: gjc@4114: class regression_test_collector_task(Task.TaskBase): gjc@4114: after = 'regression_test_task' gjc@4114: color = 'BLUE' gjc@4114: gjc@4114: def __init__(self, test_tasks): gjc@4114: super(regression_test_collector_task, self).__init__() gjc@4114: self.test_tasks = test_tasks gjc@4114: gjc@4114: def __str__(self): gjc@4114: return 'regression-test-collector\n' gjc@4114: gjc@4114: def run(self): gjc@4114: failed_tests = [test for test in self.test_tasks if test.result is not None and test.result != 0] gjc@4114: skipped_tests = [test for test in self.test_tasks if test.result is None] gjc@4114: print "Regression testing summary:" gjc@4114: if skipped_tests: gjc@4114: print "SKIP: %i of %i tests have been skipped (%s)" % ( gjc@4114: len(skipped_tests), len(self.test_tasks), gjc@4114: ', '.join([test.test_name for test in skipped_tests])) gjc@4114: if failed_tests: gjc@4114: print "FAIL: %i of %i tests have failed (%s)" % ( gjc@4114: len(failed_tests), len(self.test_tasks), gjc@4114: ', '.join([test.test_name for test in failed_tests])) gjc@4114: return 1 gjc@4114: else: gjc@4114: print "PASS: %i of %i tests passed" % (len(self.test_tasks) - len(skipped_tests), gjc@4114: len(self.test_tasks)) gjc@4114: return 0 gjc@4114: gjc@4114: def run_regression(bld, reference_traces): gjc@3872: """Execute regression tests. Called with cwd set to the 'regression' subdir of ns-3. gjc@3872: gjc@4109: @param reference_traces: reference traces directory. gjc@3872: gjc@3872: """ gjc@3866: gjc@4110: testdir = os.path.join("regression", "tests") gjc@3866: if not os.path.exists(testdir): gjc@3866: print "Tests directory does not exist" gjc@3866: sys.exit(3) gjc@3872: gjc@4064: if Options.options.regression_tests: gjc@4064: tests = Options.options.regression_tests.split(',') gjc@3866: else: gjc@3866: tests = _find_tests(testdir) gjc@3866: gjc@3872: if not os.path.exists(reference_traces): gjc@3872: print "Reference traces directory (%s) does not exist" % reference_traces gjc@3866: return 3 gjc@3866: gjc@4114: test_scripts_dir = bld.path.find_dir('regression/tests').abspath() gjc@4114: build_traces_dir = bld.path.find_or_declare('regression/traces').abspath(bld.env) gjc@4114: tasks = [] gjc@3866: for test in tests: gjc@4114: tasks.append(regression_test_task(bld.env, test, test_scripts_dir, build_traces_dir, reference_traces)) gjc@4114: regression_test_collector_task(tasks)