Exemple #1
0
    def testExitStatuses(self):
        start_time = str(time.time())

        env = os.environ.copy()
        env.update([('START_TIME', start_time)])

        def run(cmd, oom_adj=None):
            p = GuardedProcessCWDWrapper(
                cmd,
                close_fds=True,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                env=env,
                oom_adj=oom_adj,
            )
            out, err = p.communicate('INPUT')
            return p.returncode, out, err

        self.assertEqual(
            run(['sh', '-c', 'exit 0']),
            (0, '', '')
        )

        my_oom_adj = get_oom_adj()

        self.assertEqual(
            run(['sh', '-c', 'cat /proc/self/oom_adj']),
            (0, '%d\n' % my_oom_adj, '')
        )

        child_oom_adj = my_oom_adj + 3
        self.assertEqual(
            run(['sh', '-c', 'cat /proc/self/oom_adj'], oom_adj=child_oom_adj),
            (0, '%d\n' % (child_oom_adj - 1), '')
        )

        self.assertEqual(
            run(['sh', '-c', 'echo -n Out; echo -n Err >&2; exit 5']),
            (5, 'Out', 'Err')
        )

        self.assertEqual(
            run(['sh', '-c', 'echo -n $START_TIME']),
            (0, start_time, '')
        )

        self.assertEqual(
            run(['sh', '-c', 'echo -n Out; echo -n Err >&2; exit %d' % _EXIT_STATUS_IN_FILE]),
            (_EXIT_STATUS_IN_FILE, 'Out', 'Err')
        )

        self.assertEqual(
            run(['sh', '-c', 'echo -n Out; echo -n Err >&2; kill -SEGV $$']),
            (-signal.SIGSEGV, 'Out', 'Err')
        )
Exemple #2
0
    def _do_test_common(self, runner, use_pgrpguard, do_terminate):
        pgrpguard_binary = 'pgrpguard' if use_pgrpguard else None

        start_time = str(time.time())
        msg = start_time + '\n'
        child_oom_adj = get_oom_adj() + 3

        start_process = rem.job.create_job_runner(runner, pgrpguard_binary, oom_adj=child_oom_adj)

        with NamedTemporaryFile('w') as parent_stdin:
            with NamedTemporaryFile('r') as parent_stderr:
                with NamedTemporaryFile('r') as parent_stdout:
                    parent_stdin.write(msg)
                    parent_stdin.flush()

                    with open(parent_stdin.name, 'r') as stdin:
                        with open(parent_stdout.name, 'w') as stdout:
                            with open(parent_stderr.name, 'w') as stderr:
                                p = start_process(
                                    ['sh', '-c', 'cat; echo $START_TIME; cat /proc/self/oom_adj; pwd >&2; sleep 3; exit 3'],
                                    cwd='/proc',
                                    stdin=stdin,
                                    stdout=stdout,
                                    stderr=stderr,
                                    env_update=[('START_TIME', start_time)],
                                    oom_adj=child_oom_adj,
                                )

                    time.sleep(1)
                    self.assertEqual(p.poll(), None)

                    if do_terminate:
                        p.terminate()

                    if do_terminate:
                        self.assertEqual(p.wait(), -15)
                    else:
                        self.assertEqual(p.wait(), 3)

                    self.assertEqual(p.was_signal_sent(), do_terminate)

                    self.assertEqual(parent_stdout.read(), (msg * 2) + str(child_oom_adj - 1) + '\n')
                    self.assertEqual(parent_stderr.read(), '/proc\n')