def test_open_first_fork_child_second_fork_parent(self): self.os_mock.fork = MagicMock(side_effect=[0, 1123]) self.os_mock._exit.side_effect = LowLevelExit self.daemoncontext.pidfile = Mock() self.daemoncontext.signal_map = { self.signal_mock.SIGTSTP: None, self.signal_mock.SIGTTIN: None, self.signal_mock.SIGTTOU: None, self.signal_mock.SIGTERM: 'terminate' } with self.assertRaises(LowLevelExit) as err: self.daemoncontext.open() self.os_mock.assert_has_calls([ call.getuid(), call.getgid(), call.chdir('/'), call.setgid(54321), call.setuid(12345), call.umask(0), call.fork(), call.setsid(), call.fork() ]) self.resource_mock.assert_has_calls( [call.setrlimit(self.resource_mock.RLIMIT_CORE, (0, 0))])
def test_open_first_fork_child_second_fork_parent(self): self.os_mock.fork = MagicMock(side_effect=[0, 1123]) self.os_mock._exit.side_effect = LowLevelExit self.daemoncontext.pidfile = Mock() self.daemoncontext.signal_map = { self.signal_mock.SIGTSTP: None, self.signal_mock.SIGTTIN: None, self.signal_mock.SIGTTOU: None, self.signal_mock.SIGTERM: 'terminate'} with self.assertRaises(LowLevelExit) as err: self.daemoncontext.open() self.os_mock.assert_has_calls( [call.getuid(), call.getgid(), call.chdir('/'), call.setgid(54321), call.setuid(12345), call.umask(0), call.fork(), call.setsid(), call.fork()]) self.resource_mock.assert_has_calls( [call.setrlimit(self.resource_mock.RLIMIT_CORE, (0, 0))])
def test_open_first_fork_child_second_fork_child_and_pitfile(self): self.os_mock.fork = MagicMock(side_effect=[0, 0]) self.daemoncontext.pidfile = Mock() terminate = getattr(self.daemoncontext, 'terminate') self.daemoncontext.signal_map = { self.signal_mock.SIGTSTP: None, self.signal_mock.SIGTTIN: None, self.signal_mock.SIGTTOU: None, self.signal_mock.SIGTERM: 'terminate' } self.daemoncontext.open() self.os_mock.assert_has_calls([ call.getuid(), call.getgid(), call.chdir('/'), call.setgid(54321), call.setuid(12345), call.umask(0), call.fork(), call.setsid(), call.fork(), call.close(0), call.open(self.os_mock.devnull, self.os_mock.O_RDWR), call.dup2(self.os_mock.open(), self.sys_mock.stdin.fileno()), call.open(self.os_mock.devnull, self.os_mock.O_RDWR), call.dup2(self.os_mock.open(), self.sys_mock.stdout.fileno()), call.open(self.os_mock.devnull, self.os_mock.O_RDWR), call.dup2(self.os_mock.open(), self.sys_mock.stderr.fileno()) ]) self.resource_mock.assert_has_calls([ call.setrlimit(self.resource_mock.RLIMIT_CORE, (0, 0)), call.getrlimit(2048) ]) self.signal_mock.assert_has_calls([ call.signal(self.signal_mock.SIGTSTP, self.signal_mock.SIG_IGN), call.signal(self.signal_mock.SIGTERM, terminate), call.signal(self.signal_mock.SIGTTIN, self.signal_mock.SIG_IGN), call.signal(self.signal_mock.SIGTTOU, self.signal_mock.SIG_IGN) ], any_order=True) self.sys_mock.assert_has_calls([ call.stdin.fileno(), call.stdout.fileno(), call.stderr.fileno(), call.stdin.fileno(), call.stdout.fileno(), call.stderr.fileno() ]) self.daemoncontext.pidfile.assert_has_calls([call.acquire()])
def test_open_first_fork_child_second_fork_child_and_pitfile(self): self.os_mock.fork = MagicMock(side_effect=[0, 0]) self.daemoncontext.pidfile = Mock() terminate = getattr(self.daemoncontext, 'terminate') self.daemoncontext.signal_map = { self.signal_mock.SIGTSTP: None, self.signal_mock.SIGTTIN: None, self.signal_mock.SIGTTOU: None, self.signal_mock.SIGTERM: 'terminate'} self.daemoncontext.open() self.os_mock.assert_has_calls( [call.getuid(), call.getgid(), call.chdir('/'), call.setgid(54321), call.setuid(12345), call.umask(0), call.fork(), call.setsid(), call.fork(), call.close(0), call.open(self.os_mock.devnull, self.os_mock.O_RDWR), call.dup2(self.os_mock.open(), self.sys_mock.stdin.fileno()), call.open(self.os_mock.devnull, self.os_mock.O_RDWR), call.dup2(self.os_mock.open(), self.sys_mock.stdout.fileno()), call.open(self.os_mock.devnull, self.os_mock.O_RDWR), call.dup2(self.os_mock.open(), self.sys_mock.stderr.fileno())]) self.resource_mock.assert_has_calls( [call.setrlimit(self.resource_mock.RLIMIT_CORE, (0, 0)), call.getrlimit(2048)]) self.signal_mock.assert_has_calls( [call.signal(self.signal_mock.SIGTSTP, self.signal_mock.SIG_IGN), call.signal(self.signal_mock.SIGTERM, terminate), call.signal(self.signal_mock.SIGTTIN, self.signal_mock.SIG_IGN), call.signal(self.signal_mock.SIGTTOU, self.signal_mock.SIG_IGN)], any_order=True) self.sys_mock.assert_has_calls( [call.stdin.fileno(), call.stdout.fileno(), call.stderr.fileno(), call.stdin.fileno(), call.stdout.fileno(), call.stderr.fileno()]) self.daemoncontext.pidfile.assert_has_calls( [call.acquire()] )
def test_daemonize_call_sequence(self, pcheck_pidfile, pfork, pchdir, pumask, psetsid, pflush_buffers, preplace_io_descriptors, pcreate_pidfile, premove_pidfile_on_signal_or_exit, pset_signal_handler_for_termination): stdin_mock = MagicMock() stdout_mock = MagicMock() stderr_mock = MagicMock() manager = MagicMock() manager.attach_mock(pcheck_pidfile, "_check_pidfile") manager.attach_mock(pfork, "_fork") manager.attach_mock(pchdir, "chdir") manager.attach_mock(pumask, "umask") manager.attach_mock(psetsid, "setsid") manager.attach_mock(pcreate_pidfile, "_create_pidfile") manager.attach_mock(pflush_buffers, "_flush_buffers") manager.attach_mock(preplace_io_descriptors, "_replace_io_descriptors") manager.attach_mock(premove_pidfile_on_signal_or_exit, "_remove_pidfile_on_signal_or_exit") manager.attach_mock(pset_signal_handler_for_termination, "_set_signal_handler_for_termination") self.test_func(self.pidfile_mock, stdin=stdin_mock, stdout=stdout_mock, stderr=stderr_mock) expected_calls = [ call._check_pidfile(self.pidfile_mock), call._fork(1), call.chdir("/"), call.umask(0), call.setsid(), call._fork(2), call._create_pidfile(self.pidfile_mock), call._flush_buffers(), call._replace_io_descriptors(stdin_mock, stdout_mock, stderr_mock), call._remove_pidfile_on_signal_or_exit(self.pidfile_mock), call._set_signal_handler_for_termination(), ] manager.assert_has_calls(expected_calls)