def get_log_entry_kinds(): kind_map = {} for kind in LogEntryKind.select(): kind_map[kind.id] = kind.name kind_map[kind.name] = kind.id return kind_map
def test_yield_logs_for_export(logs_model): # Add some logs. kinds = list(LogEntryKind.select()) user = model.user.get_user('devtable') start_timestamp = datetime.utcnow() timestamp = start_timestamp for kind in kinds: for index in range(0, 10): logs_model.log_action(kind.name, namespace_name='devtable', repository_name='simple', performer=user, ip='1.2.3.4', timestamp=timestamp) timestamp = timestamp + timedelta(seconds=1) # Yield the logs. simple_repo = model.repository.get_repository('devtable', 'simple') logs_found = [] for logs in logs_model.yield_logs_for_export(start_timestamp, timestamp + timedelta(minutes=10), repository_id=simple_repo.id): logs_found.extend(logs) # Ensure we found all added logs. assert len(logs_found) == len(kinds) * 10
def test_yield_logs_for_export_timeout(logs_model): # Add some logs. kinds = list(LogEntryKind.select()) user = model.user.get_user("devtable") start_timestamp = datetime.utcnow() timestamp = start_timestamp for kind in kinds: for _ in range(0, 2): logs_model.log_action( kind.name, namespace_name="devtable", repository_name="simple", performer=user, ip="1.2.3.4", timestamp=timestamp, ) timestamp = timestamp + timedelta(seconds=1) # Yield the logs. Since we set the timeout to nothing, it should immediately fail. simple_repo = model.repository.get_repository("devtable", "simple") with pytest.raises(LogsIterationTimeout): list( logs_model.yield_logs_for_export( start_timestamp, timestamp + timedelta(minutes=1), repository_id=simple_repo.id, max_query_time=timedelta(seconds=0), ) )
def test_logs(namespace_name, repo_name, performer_name, check_args, expect_results, logs_model): # Add some logs. kinds = list(LogEntryKind.select()) user = model.user.get_user(performer_name) start_timestamp = datetime.utcnow() timestamp = start_timestamp for kind in kinds: for index in range(0, 3): logs_model.log_action(kind.name, namespace_name=namespace_name, repository_name=repo_name, performer=user, ip='1.2.3.4', timestamp=timestamp) timestamp = timestamp + timedelta(seconds=1) found = _lookup_logs(logs_model, start_timestamp, start_timestamp + timedelta(minutes=10), **check_args) if expect_results: assert len(found) == len(kinds) * 3 else: assert not found aggregated_counts = logs_model.get_aggregated_log_counts( start_timestamp, start_timestamp + timedelta(minutes=10), **check_args) if expect_results: assert len(aggregated_counts) == len(kinds) for ac in aggregated_counts: assert ac.count == 3 else: assert not aggregated_counts