nitunit: use a safe_exec function with high ulimits
authorJean Privat <jean@pryen.org>
Thu, 19 May 2016 03:40:01 +0000 (23:40 -0400)
committerJean Privat <jean@pryen.org>
Thu, 19 May 2016 03:40:01 +0000 (23:40 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/testing/testing_base.nit
src/testing/testing_doc.nit
src/testing/testing_suite.nit

index 7ca4c77..5e9b83b 100644 (file)
@@ -68,4 +68,28 @@ redef class ToolContext
                end
                return nitc
        end
+
+       # Execute a system command in a more safe context than `Sys::system`.
+       fun safe_exec(command: String): Int
+       do
+               info(command, 2)
+               var real_command = """
+bash -c "
+ulimit -f {{{ulimit_file}}} 2> /dev/null
+ulimit -t {{{ulimit_usertime}}} 2> /dev/null
+{{{command}}}
+"
+"""
+               return system(real_command)
+       end
+
+       # The maximum size (in KB) of files written by a command executed trough `safe_exec`
+       #
+       # Default: 64MB
+       var ulimit_file = 65536 is writable
+
+       # The maximum amount of cpu time (in seconds) for a command executed trough `safe_exec`
+       #
+       # Default: 10 CPU minute
+       var ulimit_usertime = 600 is writable
 end
index 4c5883b..f040253 100644 (file)
@@ -157,7 +157,7 @@ class NitUnitExecutor
                        toolcontext.modelbuilder.unit_entities += 1
                        i += 1
                        toolcontext.info("Execute doc-unit {du.testcase.attrs["name"]} in {file} {i}", 1)
-                       var res2 = sys.system("{file.to_program_name}.bin {i} >>'{file}.out1' 2>&1 </dev/null")
+                       var res2 = toolcontext.safe_exec("{file.to_program_name}.bin {i} >'{file}.out1' 2>&1 </dev/null")
 
                        var msg
                        f = new FileReader.open("{file}.out1")
@@ -206,7 +206,7 @@ class NitUnitExecutor
                var res = compile_unitfile(file)
                var res2 = 0
                if res == 0 then
-                       res2 = sys.system("{file.to_program_name}.bin >>'{file}.out1' 2>&1 </dev/null")
+                       res2 = toolcontext.safe_exec("{file.to_program_name}.bin >'{file}.out1' 2>&1 </dev/null")
                end
 
                var msg
@@ -274,7 +274,7 @@ class NitUnitExecutor
                        opts.add "-I {mmodule.filepath.dirname}"
                end
                var cmd = "{nitc} --ignore-visibility --no-color '{file}' {opts.join(" ")} >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
-               var res = sys.system(cmd)
+               var res = toolcontext.safe_exec(cmd)
                return res
        end
 end
index f79b03b..06fc90c 100644 (file)
@@ -194,7 +194,7 @@ class TestSuite
                end
                var include_dir = module_file.filename.dirname
                var cmd = "{nitc} --no-color '{file}.nit' -I {include_dir} -o '{file}.bin' > '{file}.out' 2>&1 </dev/null"
-               var res = sys.system(cmd)
+               var res = toolcontext.safe_exec(cmd)
                var f = new FileReader.open("{file}.out")
                var msg = f.read_all
                f.close
@@ -248,7 +248,7 @@ class TestCase
                var method_name = test_method.name
                var test_file = test_suite.test_file
                var res_name = "{test_file}_{method_name.escape_to_c}"
-               var res = sys.system("{test_file}.bin {method_name} > '{res_name}.out1' 2>&1 </dev/null")
+               var res = toolcontext.safe_exec("{test_file}.bin {method_name} > '{res_name}.out1' 2>&1 </dev/null")
                var f = new FileReader.open("{res_name}.out1")
                var msg = f.read_all
                f.close