def test_process_spawn_limit(self): # Make sure that wrapping commands in bash -c doesn't affect # the needed process spawn limit. with AutograderSandbox() as sandbox: ag_command = ag_models.AGCommand.objects.validate_and_create( cmd='echo hello', process_spawn_limit=0) result = tasks.run_command_from_args( ag_command.cmd, sandbox, max_num_processes=ag_command.process_spawn_limit, max_stack_size=ag_command.stack_size_limit, max_virtual_memory=ag_command.virtual_memory_limit, timeout=ag_command.time_limit, ) self.assertEqual(0, result.return_code) print(result.stdout.read()) print(result.stderr.read()) extra_bash_dash_c = ag_models.AGCommand.objects.validate_and_create( cmd='bash -c "echo hello"', process_spawn_limit=0) result = tasks.run_command_from_args( extra_bash_dash_c.cmd, sandbox, max_num_processes=ag_command.process_spawn_limit, max_stack_size=ag_command.stack_size_limit, max_virtual_memory=ag_command.virtual_memory_limit, timeout=ag_command.time_limit, ) self.assertEqual(0, result.return_code) print(result.stdout.read()) print(result.stderr.read())
def test_shell_output_redirection(self): with AutograderSandbox() as sandbox: ag_command = ag_models.AGCommand.objects.validate_and_create( cmd='printf "spam" > file', process_spawn_limit=0) tasks.run_command_from_args( ag_command.cmd, sandbox, max_num_processes=ag_command.process_spawn_limit, max_stack_size=ag_command.stack_size_limit, max_virtual_memory=ag_command.virtual_memory_limit, timeout=ag_command.time_limit, ) result = sandbox.run_command(['cat', 'file'], check=True) self.assertEqual(0, result.return_code) self.assertEqual('spam', result.stdout.read().decode())
def test_shell_parse_error(self): with AutograderSandbox() as sandbox: ag_command = ag_models.AGCommand.objects.validate_and_create( cmd='echo hello"') result = tasks.run_command_from_args( ag_command.cmd, sandbox, max_num_processes=ag_command.process_spawn_limit, max_stack_size=ag_command.stack_size_limit, max_virtual_memory=ag_command.virtual_memory_limit, timeout=ag_command.time_limit, ) self.assertNotEqual(0, result.return_code) print(result.stdout.read()) print(result.stderr.read())
def test_no_stdin_specified_redirects_devnull(self): # If no stdin is redirected, this command will time out. # If /dev/null is redirected it should terminate normally. # This behavior is handled by the autograder_sandbox library. cmd = 'python3 -c "import sys; sys.stdin.read(); print(\'done\')"' # Run command from args with AutograderSandbox() as sandbox: result = tasks.run_command_from_args(cmd, sandbox, max_num_processes=10, max_stack_size=10000000, max_virtual_memory=500000000, timeout=2) self.assertFalse(result.timed_out) self.assertEqual(0, result.return_code) self.assertEqual('done\n', result.stdout.read().decode()) # Run ag command with AutograderSandbox() as sandbox: ag_command = ag_models.AGCommand.objects.validate_and_create( cmd=cmd, process_spawn_limit=10, time_limit=2) result = tasks.run_ag_command(ag_command, sandbox) self.assertFalse(result.timed_out) self.assertEqual(0, result.return_code) self.assertEqual('done\n', result.stdout.read().decode()) project = obj_build.make_project() ag_test_suite = ag_models.AGTestSuite.objects.validate_and_create( name='Suite', project=project) ag_test_case = ag_models.AGTestCase.objects.validate_and_create( name='Case', ag_test_suite=ag_test_suite) # Run ag test command with AutograderSandbox() as sandbox: ag_test_command = ag_models.AGTestCommand.objects.validate_and_create( ag_test_case=ag_test_case, name='Read stdin', cmd=cmd, stdin_source=ag_models.StdinSource.none, time_limit=2, process_spawn_limit=10) result = tasks.run_ag_test_command(ag_test_command, sandbox, ag_test_suite) self.assertFalse(result.timed_out) self.assertEqual(0, result.return_code) self.assertEqual('done\n', result.stdout.read().decode())
def test_permission_denied(self): with AutograderSandbox() as sandbox: sandbox.run_command(['touch', 'not_executable'], check=True) sandbox.run_command(['chmod', '666', 'not_executable'], check=True) ag_command = ag_models.AGCommand.objects.validate_and_create( cmd='./not_executable') result = tasks.run_command_from_args( ag_command.cmd, sandbox, max_num_processes=ag_command.process_spawn_limit, max_stack_size=ag_command.stack_size_limit, max_virtual_memory=ag_command.virtual_memory_limit, timeout=ag_command.time_limit, ) self.assertNotEqual(0, result.return_code) print(result.stdout.read()) print(result.stderr.read())