From 9281078dd201ff98cdad3747100072ab25619a03 Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Date: Wed, 11 Sep 2024 10:47:23 +0200 Subject: [PATCH] tests: Run test.py with full logging in the presence of TEST_LOGS environment variable --- test.py | 16 ++++++++++++++-- utils/tests/test-ns3.py | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/test.py b/test.py index 41dd8628c..421c478f9 100755 --- a/test.py +++ b/test.py @@ -846,6 +846,11 @@ def make_paths(): VALGRIND_SUPPRESSIONS_FILE = ".ns3.supp" # VALGRIND_SUPPRESSIONS_FILE = None +# When the TEST_LOGS environment variable is set to 1 or true, +# NS_LOG is set to NS_LOG=*, and stdout/stderr +# from tests are discarded to prevent running out of memory. +TEST_LOGS = bool(os.getenv("TEST_LOGS", False)) + def run_job_synchronously(shell_command, directory, valgrind, is_python, build_path=""): if VALGRIND_SUPPRESSIONS_FILE is not None: @@ -878,9 +883,16 @@ def run_job_synchronously(shell_command, directory, valgrind, is_python, build_p start_time = time.time() proc = subprocess.Popen( - cmd, shell=True, cwd=directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE + cmd, + shell=True, + cwd=directory, + stdout=subprocess.PIPE if not TEST_LOGS else subprocess.DEVNULL, + stderr=subprocess.PIPE if not TEST_LOGS else subprocess.STDOUT, ) stdout_results, stderr_results = proc.communicate() + stdout_results = b"" if stdout_results is None else stdout_results + stderr_results = b"" if stderr_results is None else stderr_results + elapsed_time = time.time() - start_time retval = proc.returncode @@ -1343,7 +1355,7 @@ def run_tests(): # test.py runs. If you want to see logging output from your tests, you # have to run them using the test-runner directly. # - os.environ["NS_LOG"] = "" + os.environ["NS_LOG"] = "*" if TEST_LOGS else "" # # There are a couple of options that imply we can to exit before starting diff --git a/utils/tests/test-ns3.py b/utils/tests/test-ns3.py index 965689f48..356a246de 100755 --- a/utils/tests/test-ns3.py +++ b/utils/tests/test-ns3.py @@ -3422,6 +3422,23 @@ class NS3QualityControlTestCase(unittest.TestCase): % (brightness, brightness_threshold, image), ) + def test_04_CheckForBrokenLogs(self): + """! + Check if one of the log statements of examples/tests contains/exposes a bug. + @return None + """ + # First enable examples and tests with sanitizers + return_code, stdout, stderr = run_ns3( + 'configure -G "{generator}" -d release --enable-examples --enable-tests --enable-sanitizers' + ) + self.assertEqual(return_code, 0) + + # Then build and run tests setting the environment variable + return_code, stdout, stderr = run_program( + "test.py", "", python=True, env={"TEST_LOGS": "1"} + ) + self.assertEqual(return_code, 0) + def main(): """!