From cfe57d2c5eb63893f892421e6e6872a8e5c22dbe Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Thu, 4 Feb 2021 18:49:56 -0800 Subject: Suppress NsJail info logs during tests The logs clutter up the test output way too much. --- tests/test_nsjail.py | 1 + 1 file changed, 1 insertion(+) (limited to 'tests') diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index 40c27f9..7e7df9b 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -15,6 +15,7 @@ class NsJailTests(unittest.TestCase): self.nsjail = NsJail() self.nsjail.DEBUG = False self.logger = logging.getLogger("snekbox.nsjail") + self.logger.setLevel(logging.WARNING) def test_print_returns_0(self): result = self.nsjail.python3("print('test')") -- cgit v1.2.3 From a8b9c331fd7a69c9a89083886b7b47b3bba6f78a Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Thu, 4 Feb 2021 19:04:29 -0800 Subject: Close file descriptors when subprocess ends --- snekbox/nsjail.py | 27 +++++++++++++-------------- tests/test_nsjail.py | 2 +- 2 files changed, 14 insertions(+), 15 deletions(-) (limited to 'tests') diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py index 55ece65..d48f72b 100644 --- a/snekbox/nsjail.py +++ b/snekbox/nsjail.py @@ -148,20 +148,19 @@ class NsJail: output_size = 0 output = [] - # We'll consume STDOUT as long as the NsJail subprocess is running. - while nsjail.poll() is None: - chars = nsjail.stdout.read(READ_CHUNK_SIZE) - output_size += sys.getsizeof(chars) - output.append(chars) - - if output_size > OUTPUT_MAX: - # Terminate the NsJail subprocess with SIGKILL. - log.info("Output exceeded the output limit, sending SIGKILL to NsJail.") - nsjail.kill() - break - - # Ensure that we wait for the NsJail subprocess to terminate. - nsjail.wait() + # Context manager will wait for process to terminate and close file descriptors. + with nsjail: + # We'll consume STDOUT as long as the NsJail subprocess is running. + while nsjail.poll() is None: + chars = nsjail.stdout.read(READ_CHUNK_SIZE) + output_size += sys.getsizeof(chars) + output.append(chars) + + if output_size > OUTPUT_MAX: + # Terminate the NsJail subprocess with SIGKILL. + log.info("Output exceeded the output limit, sending SIGKILL to NsJail.") + nsjail.kill() + break return "".join(output) diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index 7e7df9b..820f57e 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -192,7 +192,7 @@ class NsJailTests(unittest.TestCase): chunk = "a" * READ_CHUNK_SIZE expected_chunks = OUTPUT_MAX // sys.getsizeof(chunk) + 1 - nsjail_subprocess = unittest.mock.Mock() + nsjail_subprocess = unittest.mock.MagicMock() # Go 10 chunks over to make sure we exceed the limit nsjail_subprocess.stdout = io.StringIO((expected_chunks + 10) * chunk) -- cgit v1.2.3 From a1b5e5bdc0652eda66bf550ab0423f000918645a Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Thu, 4 Feb 2021 19:08:58 -0800 Subject: Fix the memory limit test and the import error --- tests/test_nsjail.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index 820f57e..06dc5b6 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -5,7 +5,7 @@ import unittest import unittest.mock from textwrap import dedent -from snekbox.nsjail import MEM_MAX, NsJail, OUTPUT_MAX, READ_CHUNK_SIZE +from snekbox.nsjail import NsJail, OUTPUT_MAX, READ_CHUNK_SIZE class NsJailTests(unittest.TestCase): @@ -40,7 +40,7 @@ class NsJailTests(unittest.TestCase): def test_memory_returns_137(self): # Add a kilobyte just to be safe. code = dedent(f""" - x = ' ' * {MEM_MAX + 1000} + x = ' ' * {self.nsjail.config.cgroup_mem_max + 1000} """).strip() result = self.nsjail.python3(code) -- cgit v1.2.3 From 2240dde3748c8a27693bce5df6b517b648e760a6 Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Thu, 4 Feb 2021 19:15:57 -0800 Subject: Fix patch for DEBUG value during testing --- tests/test_nsjail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index 06dc5b6..c0e43b6 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -13,7 +13,6 @@ class NsJailTests(unittest.TestCase): super().setUp() self.nsjail = NsJail() - self.nsjail.DEBUG = False self.logger = logging.getLogger("snekbox.nsjail") self.logger.setLevel(logging.WARNING) @@ -101,6 +100,7 @@ class NsJailTests(unittest.TestCase): self.assertEqual(result.stdout, "ValueError: embedded null byte") self.assertEqual(result.stderr, None) + @unittest.mock.patch("snekbox.nsjail.DEBUG", new=False) def test_log_parser(self): log_lines = ( "[D][2019-06-22T20:07:00+0000][16] void foo::bar()():100 This is a debug message.", -- cgit v1.2.3