コード例 #1
0
    def test_shell_scipt_with_fork(self):
        with TemporaryDirectory() as tmpdir:
            script = Path(tmpdir, 'hello.sh')
            with script.open('w') as f:
                f.write(self.shell_script_with_fork)
            script.chmod(0o755)
            argv = [script.as_posix()]
            actual = self.run_trace(argv)
            # split into two lists, one for each PID
            pids = {}
            for pid, event, args in actual:
                pids.setdefault(pid, []).append((pid, event, args))
            self.assertEqual(len(pids), 2)
            ppid, cpid = sorted(pids.keys())  # parent PID < child PID

            dmesg_checks = []
            for p in test_utils.do_sh_path_lookup('dmesg'):
                dmesg_checks.append(('check', (p.as_posix(), p.exists())))
            last = dmesg_checks.pop()
            assert last[1][1] is True
            dmesg_checks.extend([last] * 10)

            # Check events generated by sh process
            self.check_events(pids[ppid], ppid, argv, INIT_SH + [
                ('read', (script.as_posix(),)),
                ('check', ('.', True)),
            ] + dmesg_checks + [
                ('fork', (cpid,)),
            ])

            # Check events generated by dmesg process
            # sh applies the following env changes to its subprocesses
            env = test_utils.modified_env(os.environ, {
                '_': p.as_posix(),
                'SHLVL': str(int(os.environ.get('SHLVL', 0)) + 1),
                'OLDPWD': None,  # delete
                'PS1': None,  # delete
            })
            self.check_events(
                pids[cpid], cpid, ['dmesg'], INIT_C_LOCALE + [
                    ('read', ('/dev/kmsg',)),
                ], env=env)
コード例 #2
0
 def path_lookup(self, name, only_missing=False):
     for path in test_utils.do_sh_path_lookup(name, self.env['PATH']):
         if not only_missing or not path.exists():
             self.check(path, path.exists())
     return path