예제 #1
0
def test_delete_old_events(initialized_db_connection,
                           fake_system_event_record):
    assert not select_distinct_hosts_from_system_events(
        initialized_db_connection)
    assert not search_system_events(initialized_db_connection)

    old_record = fake_system_event_record.copy(
        received_at=fake_system_event_record.received_at -
        datetime.timedelta(days=30))

    with initialized_db_connection:
        fake_system_event_record.id = insert_system_event(
            initialized_db_connection, fake_system_event_record)
        old_record.id = insert_system_event(initialized_db_connection,
                                            old_record)

    assert len(search_system_events(initialized_db_connection)) == 2
    assert select_system_event_by_id(initialized_db_connection,
                                     fake_system_event_record.id)
    assert select_system_event_by_id(initialized_db_connection, old_record.id)

    with initialized_db_connection:
        delete_old_system_events(initialized_db_connection, "21 days")

    assert len(search_system_events(initialized_db_connection)) == 1
    assert select_system_event_by_id(initialized_db_connection,
                                     fake_system_event_record.id)
    assert not select_system_event_by_id(initialized_db_connection,
                                         old_record.id)
예제 #2
0
def test_basic_system_event_crud(initialized_db_connection,
                                 fake_system_event_record):
    assert not select_distinct_hosts_from_system_events(
        initialized_db_connection)
    assert not search_system_events(initialized_db_connection)
    assert not select_latest_events(initialized_db_connection)

    with initialized_db_connection:
        fake_system_event_record.id = insert_system_event(
            initialized_db_connection, fake_system_event_record)

    assert "foobar" in select_distinct_hosts_from_system_events(
        initialized_db_connection)
    assert len(search_system_events(initialized_db_connection)) == 1
    assert select_system_event_by_id(initialized_db_connection,
                                     fake_system_event_record.id)
    assert len(select_latest_events(initialized_db_connection)) == 1

    with initialized_db_connection:
        delete_system_event(initialized_db_connection,
                            fake_system_event_record)

    assert not select_distinct_hosts_from_system_events(
        initialized_db_connection)
    assert not search_system_events(initialized_db_connection)
    assert not select_system_event_by_id(initialized_db_connection,
                                         fake_system_event_record.id)
    assert not select_latest_events(initialized_db_connection)
예제 #3
0
 def search_system_events_api():
     with get_pool_connection(database_pool) as conn:
         with conn:
             swagger_data = request.swagger_data
             facilities = swagger_data.get("facilities")
             priorities = swagger_data.get("priorities")
             from_hosts = swagger_data.get("fromHosts")
             event_sources = swagger_data.get("eventSources")
             message_filter = swagger_data.get("messageFilter")
             syslog_tag_filter = swagger_data.get("syslogTagFilter")
             start_received_at = swagger_data.get("startReceivedAt")
             end_receieved_at = swagger_data.get("endReceivedAt")
             start_device_reported_time = swagger_data.get(
                 "startDeviceReportedTime")
             end_device_reported_time = swagger_data.get(
                 "endDeviceReportedTime")
             limit = swagger_data.get('limit')
             offset = swagger_data.get('offset')
             system_events = search_system_events(
                 conn,
                 opt_facility=facilities,
                 opt_priority=priorities,
                 opt_from_host=from_hosts,
                 opt_event_source=event_sources,
                 opt_message_search=message_filter,
                 opt_syslog_tag_search=syslog_tag_filter,
                 opt_start_received_at=start_received_at,
                 opt_end_received_at=end_receieved_at,
                 opt_start_device_reported_time=start_device_reported_time,
                 opt_end_device_reported_time=end_device_reported_time,
                 limit=limit,
                 offset=offset)
         return [s.to_api_response() for s in system_events]
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)
예제 #5
0
 def func(args):
     connection = regular_connect(args.postgres_connection_url)
     logging.info("Search Results:")
     for event in reversed(search_system_events(
         connection,
         opt_facility=args.facility,
         opt_priority=args.priority,
         opt_from_host=args.from_host,
         opt_event_source=args.event_source,
         opt_syslog_tag_search=args.syslog_tag,
         opt_message_search=args.message,
         opt_start_received_at=args.start_received_at,
         opt_end_received_at=args.end_received_at,
         opt_start_device_reported_time=args.start_device_reported_time,
         opt_end_device_reported_time=args.end_device_reported_time,
         limit=args.limit,
         offset=args.offset
     )):
         print(event.format(args.format_str))
예제 #6
0
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)
예제 #7
0
def test_search_parameters(initialized_db_connection,
                           fake_system_event_record):
    assert not select_distinct_hosts_from_system_events(
        initialized_db_connection)
    assert not search_system_events(initialized_db_connection)
    assert not select_latest_events(initialized_db_connection)

    records = [
        fake_system_event_record,
        fake_system_event_record.copy(
            from_host="other-host",
            message="other-host test",
            device_reported_time=datetime.datetime.now() -
            datetime.timedelta(days=5)),
        fake_system_event_record.copy(from_host="router",
                                      message="router test",
                                      received_at=datetime.datetime.now() -
                                      datetime.timedelta(days=5))
    ]

    with initialized_db_connection:
        for record in records:
            record.id = insert_system_event(initialized_db_connection, record)

    assert len(search_system_events(initialized_db_connection)) == len(records)
    assert len(
        search_system_events(initialized_db_connection,
                             opt_start_received_at=datetime.datetime.now() -
                             datetime.timedelta(days=6),
                             opt_end_received_at=datetime.datetime.now() -
                             datetime.timedelta(days=4))) == 1
    assert len(
        search_system_events(
            initialized_db_connection,
            opt_start_device_reported_time=datetime.datetime.now() -
            datetime.timedelta(days=6),
            opt_end_device_reported_time=datetime.datetime.now() -
            datetime.timedelta(days=4))) == 1
    assert len(
        search_system_events(initialized_db_connection,
                             opt_from_host=["router", "other-host"])) == 2
    assert len(
        search_system_events(initialized_db_connection,
                             opt_message_search="%other-host%")) == 1
예제 #8
0
def test_select_from_empty_database(initialized_db_connection):
    assert not select_distinct_hosts_from_system_events(
        initialized_db_connection)
    assert not search_system_events(initialized_db_connection)