def test_fork(mock_fork): pfile = create_file('test.pid', 123) daemon = Daemon(pfile) with pytest.raises(SystemExit) as err: daemon.fork() assert str(err.value) == '0' assert mock_fork.called is True
def test_delete_pid_exit(): pfile = create_file('test.pid', 123) with mock.patch('os.getpid', return_value=123), \ mock.patch('atexit.register'): daemon = Daemon(pfile) assert daemon.pid is 123 daemon._exit() assert daemon.pid is None
def test_fork_error(): pfile = create_file('test.pid', 123) with mock.patch('atexit.register'): daemon = Daemon(pfile) with mock.patch('os.fork', side_effect=OSError) as mock_fork, \ pytest.raises(DaemonException): daemon.fork() assert mock_fork.called is True
def test_delete_pid_exit(): pfile = create_file("test.pid", 123) with mock.patch("os.getpid", return_value=123), mock.patch("atexit.register"): daemon = Daemon(pfile) assert daemon.pid is 123 daemon._exit() assert daemon.pid is None
def test_fork(): pfile = create_file("test.pid", 123) with mock.patch("atexit.register"): daemon = Daemon(pfile) with mock.patch("os.fork", return_value=9999) as mock_fork, mock.patch( "os._exit") as mock_exit: daemon.fork() assert mock_fork.called is True assert mock_exit.called is True
def test_fork_error(): pfile = create_file("test.pid", 123) with mock.patch("atexit.register"): daemon = Daemon(pfile) with mock.patch( "os.fork", side_effect=OSError) as mock_fork, pytest.raises(DaemonException): daemon.fork() assert mock_fork.called is True
def test_delete_pid_exit(): pfile = create_file("test.pid", 123) with mock.patch("os.getpid", return_value=123), mock.patch( "atexit.register" ): daemon = Daemon(pfile) assert daemon.pid is 123 daemon._exit() assert daemon.pid is None
def test_fork(): pfile = create_file('test.pid', 123) with mock.patch('atexit.register'): daemon = Daemon(pfile) with mock.patch('os.fork', return_value=9999) as mock_fork, \ mock.patch('os._exit') as mock_exit: daemon.fork() assert mock_fork.called is True assert mock_exit.called is True
def test_fork(): pfile = create_file("test.pid", 123) with mock.patch("atexit.register"): daemon = Daemon(pfile) with mock.patch("os.fork", return_value=9999) as mock_fork, mock.patch( "os._exit" ) as mock_exit: daemon.fork() assert mock_fork.called is True assert mock_exit.called is True
def test_daemonise(mock_fork, mock_chdir, mock_setsid, mock_umask, mock_atexit, mock_getpid): daemon = Daemon('/fake/path.pid') m_open = mock.mock_open() with mock.patch('builtins.open', m_open, create=True): daemon.daemonize() assert mock_fork.called is True assert mock_chdir.called is True assert mock_setsid.called is True assert mock_umask.called is True assert mock_atexit.called is True
def test_daemonise(): pfile = create_file("test.pid", 123) with mock.patch("blackhole.daemon.Daemon.fork") as mock_fork, mock.patch( "os.chdir") as mock_chdir, mock.patch( "os.setsid") as mock_setsid, mock.patch( "os.umask") as mock_umask, mock.patch( "atexit.register") as mock_atexit, mock.patch( "os.getpid", return_value=123): daemon = Daemon(pfile) daemon.daemonize() assert mock_fork.called is True assert mock_chdir.called is True assert mock_setsid.called is True assert mock_umask.called is True assert mock_atexit.called is True
def test_daemonise(): pfile = create_file('test.pid', 123) with mock.patch('blackhole.daemon.Daemon.fork') as mock_fork, \ mock.patch('os.chdir') as mock_chdir, \ mock.patch('os.setsid') as mock_setsid, \ mock.patch('os.umask') as mock_umask, \ mock.patch('atexit.register') as mock_atexit, \ mock.patch('os.getpid', return_value=123): daemon = Daemon(pfile) daemon.daemonize() assert mock_fork.called is True assert mock_chdir.called is True assert mock_setsid.called is True assert mock_umask.called is True assert mock_atexit.called is True
def test_set_pid_valid_path(): pid = os.path.join(os.getcwd(), "fake.pid") with mock.patch("os.getpid", return_value=666), mock.patch("atexit.register"): daemon = Daemon(pid) assert daemon.pidfile == pid assert daemon.pid == 666
def test_get_pid_file_error(): with mock.patch("os.path.exists", return_value=True): with mock.patch("builtins.open", side_effect=FileNotFoundError), mock.patch( "atexit.register"), pytest.raises(DaemonException): Daemon("/fake/path.pid") with mock.patch("builtins.open", side_effect=IOError), mock.patch( "atexit.register"), pytest.raises(DaemonException): Daemon("/fake/path.pid") with mock.patch("builtins.open", side_effect=PermissionError), mock.patch( "atexit.register"), pytest.raises(DaemonException): Daemon("/fake/path.pid") with mock.patch("builtins.open", side_effect=OSError), mock.patch( "atexit.register"), pytest.raises(DaemonException): Daemon("/fake/path.pid")
def run(): """Create the asyncio loop and start the server.""" args = parse_cmd_args(sys.argv[1:]) configure_logs(args) logger = logging.getLogger('blackhole') if args.test: config_test(args) try: config = Config(args.config_file).load().test() except ConfigException as err: logger.fatal(err) raise SystemExit(os.EX_USAGE) if args.background and not config.pidfile: logger.fatal('Cannot run in the background without a pidfile.') raise SystemExit(os.EX_USAGE) loop = asyncio.get_event_loop() loop.add_signal_handler(signal.SIGINT, loop.stop) start_servers() setgid() setuid() if args.background: try: Daemon(config.pidfile).daemonize() except DaemonException as err: stop_servers() logger.fatal(err) raise SystemExit(os.EX_USAGE) try: loop.run_forever() except KeyboardInterrupt: pass stop_servers() raise SystemExit(os.EX_OK)
def test_delete_pid_no_exists(): pfile = create_file("test.pid", 123) daemon = Daemon(pfile) with mock.patch("os.remove") as mock_rm, mock.patch( "atexit.register"), mock.patch("os.path.exists", return_value=False): del daemon.pid assert mock_rm.called is False
def test_daemonise(): pfile = create_file("test.pid", 123) with mock.patch("blackhole.daemon.Daemon.fork") as mock_fork, mock.patch( "os.chdir" ) as mock_chdir, mock.patch("os.setsid") as mock_setsid, mock.patch( "os.umask" ) as mock_umask, mock.patch( "atexit.register" ) as mock_atexit, mock.patch( "os.getpid", return_value=123 ): daemon = Daemon(pfile) daemon.daemonize() assert mock_fork.called is True assert mock_chdir.called is True assert mock_setsid.called is True assert mock_umask.called is True assert mock_atexit.called is True
def test_get_pid_file_error(_): daemon = Daemon('/fake/path.pid') with mock.patch('builtins.open', side_effect=FileNotFoundError): with pytest.raises(DaemonException): _ = daemon.pid with mock.patch('builtins.open', side_effect=IOError): with pytest.raises(DaemonException): _ = daemon.pid with mock.patch('builtins.open', side_effect=PermissionError): with pytest.raises(DaemonException): _ = daemon.pid with mock.patch('builtins.open', side_effect=OSError): with pytest.raises(DaemonException): _ = daemon.pid
def stop_servers(): """ Stop the listeners. :raises: SystemExit """ loop = asyncio.get_event_loop() conf = Config() logger.debug('Stopping...') for _ in range(len(_servers)): server = _servers.pop() server.close() loop.run_until_complete(server.wait_closed()) loop.close() daemon = Daemon(conf.pidfile) if daemon.pid: del daemon.pid
def test_get_pid(): pfile = create_file('test.pid', 123) daemon = Daemon(pfile) assert daemon.pid is 123
def test_fork_error(mock_fork): pfile = create_file('test.pid', 123) daemon = Daemon(pfile) with pytest.raises(DaemonException): daemon.fork() assert mock_fork.called is True
def test_instantiated_but_not_daemonised(): daemon = Daemon('/fake/path.pid') assert daemon.pidfile == '/fake/path.pid' assert daemon.pid is None
def test_set_pid_invalid_path(_): daemon = Daemon('/fake/path.pid') with pytest.raises(DaemonException): daemon.pid = 123
def test_set_pid_valid_path(): daemon = Daemon('/fake/path.pid') m_open = mock.mock_open() with mock.patch('builtins.open', m_open, create=True): daemon.pid = 123 m_open.assert_called_once_with('/fake/path.pid', 'w+')
def test_get_pid_invalid_path(_): daemon = Daemon('/fake/path.pid') assert daemon.pid is None
def test_set_pid_invalid_path(): with mock.patch("os.path.exists", return_value=False), mock.patch( "atexit.register"), pytest.raises(DaemonException): Daemon("/fake/path.pid")
def test_instantiated_but_not_daemonised(): pid = os.path.join(os.getcwd(), "fake.pid") with mock.patch("os.getpid", return_value=666): daemon = Daemon(pid) assert daemon.pidfile == pid assert daemon.pid == 666
def test_delete_pid_no_exists(mock_rm): pfile = create_file('test.pid', 123) daemon = Daemon(pfile) with mock.patch('os.path.exists', return_value=False): del daemon.pid assert mock_rm.called is False
def test_delete_pid(): pfile = create_file('test.pid', 123) daemon = Daemon(pfile) assert daemon.pid is 123 del daemon.pid assert daemon.pid is None