def test_tail_command(main_argument_parser, database_url, fake_system_event_record): args = main_argument_parser.parse_args([database_url, "-vv", "tail"]) fake_stdout = FakeStdout() fake_select_latest = FakeSelectLatest(5, fake_system_event_record) def breakout(*args, **kwargs): signal.alarm(0) raise AssertionError("Took too long!") signal.signal(signal.SIGALRM, breakout) signal.alarm(15) try: with patch("sys.stdout.write", fake_stdout.write): with patch( "rsyslog_postgres_tools.commands.tail.select_latest_events", fake_select_latest): with patch( "rsyslog_postgres_tools.commands.tail.regular_connect", lambda *args, **kwargs: None): main(args) except ExpectedException: signal.alarm(0) for record in fake_select_latest.generated_events: assert any(record.from_host in m for m in fake_stdout.msgs)
def test_run_http_server_command(initialized_db_connection, database_url, main_argument_parser): args = main_argument_parser.parse_args([database_url, "run_http_server"]) with patch("eventlet.wsgi.server") as wsgi_server: with patch("eventlet.listen") as eventlet_listen: main(args) assert wsgi_server.call_count > 0 assert eventlet_listen.call_count > 0
def test_clean_command(initialized_db_connection, database_url, main_argument_parser, fake_system_event_record): with initialized_db_connection: new_record = fake_system_event_record.copy( received_at=fake_system_event_record.received_at - datetime.timedelta(days=30)) new_record.id = insert_system_event(initialized_db_connection, new_record) assert search_system_events(initialized_db_connection) args = main_argument_parser.parse_args( [database_url, "clean", "-pi", "1 day"]) main(args) assert not search_system_events(initialized_db_connection)
def test_search_command_no_opts(initialized_db_connection, database_url, main_argument_parser, fake_system_event_record): with initialized_db_connection: new_record = fake_system_event_record.copy( message=_random_string(32), from_host=_random_string(8) ) new_record.id = insert_system_event(initialized_db_connection, new_record) assert search_system_events(initialized_db_connection) fake_stdout = FakeStdout() with patch("sys.stdout.write", fake_stdout.write): args = main_argument_parser.parse_args([database_url, "search"]) main(args) assert any(new_record.message in line for line in fake_stdout.msgs) assert any(new_record.from_host in line for line in fake_stdout.msgs)
def test_bootstrap_command(initialized_db_connection, database_url, main_argument_parser): with initialized_db_connection.cursor() as c: c.execute(""" select count(*) as t_count from information_schema.tables where table_schema = 'public'; """) table_count = c.fetchone()["t_count"] assert table_count == 0 args = main_argument_parser.parse_args([database_url, "bootstrap"]) main(args) with initialized_db_connection.cursor() as c: c.execute(""" select count(*) as t_count from information_schema.tables where table_schema = 'public'; """) table_count = c.fetchone()["t_count"] assert table_count > 0
#!/usr/bin/env python3 from rsyslog_postgres_tools.main import main if __name__ == "__main__": main()