Skip to content

Commit a6fd6d4

Browse files
whummerclaude
andcommitted
simplify insights query test to avoid indexing flakiness
Focus on verifying the proxy forwards StartQuery and GetQueryResults correctly rather than waiting for actual query results. The query ID format and matching status between proxy and direct AWS calls proves the operations are being proxied. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b017e90 commit a6fd6d4

1 file changed

Lines changed: 33 additions & 46 deletions

File tree

aws-proxy/tests/proxy/test_cloudwatch.py

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,11 @@ def test_logs_readonly_insights_query(start_aws_proxy, cleanups):
539539
StartQuery and GetQueryResults are read operations for CloudWatch Logs Insights
540540
but don't match standard prefixes. This test verifies they're correctly
541541
forwarded when read_only: true is configured.
542+
543+
Note: This test verifies the proxy correctly forwards these operations, not that
544+
CloudWatch Logs Insights returns results (which can be flaky due to indexing delays).
542545
"""
543546
log_group_name = f"/test/readonly-insights/{short_uid()}"
544-
log_stream_name = f"stream-{short_uid()}"
545547

546548
# start proxy - forwarding requests for CloudWatch Logs in read-only mode
547549
config = ProxyConfig(
@@ -558,58 +560,43 @@ def test_logs_readonly_insights_query(start_aws_proxy, cleanups):
558560
logs_client = connect_to().logs
559561
logs_client_aws = boto3.client("logs")
560562

561-
# create log group and stream in AWS
563+
# create log group in AWS
562564
logs_client_aws.create_log_group(logGroupName=log_group_name)
563565
cleanups.append(
564566
lambda: logs_client_aws.delete_log_group(logGroupName=log_group_name)
565567
)
566568

567-
logs_client_aws.create_log_stream(
568-
logGroupName=log_group_name, logStreamName=log_stream_name
569-
)
569+
# start_query through local client (proxied) - should work in read-only mode
570+
# The fact that we get a valid query ID proves the request was proxied to AWS
571+
start_time = int((time.time() - 300) * 1000) # 5 minutes ago
572+
end_time = int((time.time() + 60) * 1000) # 1 minute from now
570573

571-
# put log events
572-
timestamp = int(time.time() * 1000)
573-
logs_client_aws.put_log_events(
574+
query_response = logs_client.start_query(
574575
logGroupName=log_group_name,
575-
logStreamName=log_stream_name,
576-
logEvents=[
577-
{"timestamp": timestamp, "message": "Test log message for insights query"},
578-
],
579-
)
580-
581-
# start_query and get_query_results through local client (proxied)
582-
# Retry the entire query cycle if it completes with no results (events not indexed yet)
583-
def _run_insights_query():
584-
start_time = int((time.time() - 300) * 1000) # 5 minutes ago
585-
end_time = int((time.time() + 60) * 1000) # 1 minute from now
586-
587-
# start_query - should work in read-only mode
588-
query_response = logs_client.start_query(
589-
logGroupName=log_group_name,
590-
startTime=start_time,
591-
endTime=end_time,
592-
queryString="fields @timestamp, @message | limit 10",
593-
)
594-
query_id = query_response["queryId"]
595-
596-
# poll get_query_results until complete
597-
for _ in range(30):
598-
results = logs_client.get_query_results(queryId=query_id)
599-
if results["status"] in ["Complete", "Failed", "Cancelled"]:
600-
break
601-
time.sleep(1)
602-
603-
if results["status"] != "Complete":
604-
raise AssertionError(f"Query failed with status: {results['status']}")
605-
if len(results["results"]) < 1:
606-
# Query completed but no results - events may not be indexed yet, retry
607-
raise AssertionError("Query completed but no results found yet")
608-
return results
609-
610-
results = retry(_run_insights_query, retries=10, sleep=5)
611-
assert results["status"] == "Complete"
612-
assert len(results["results"]) >= 1
576+
startTime=start_time,
577+
endTime=end_time,
578+
queryString="fields @timestamp, @message | limit 10",
579+
)
580+
query_id = query_response["queryId"]
581+
assert query_id is not None
582+
# AWS query IDs are UUIDs, verify format to confirm this came from AWS
583+
assert len(query_id) == 36 and query_id.count("-") == 4
584+
585+
# get_query_results through local client (proxied) - should work in read-only mode
586+
# The fact that we get a valid status proves the request was proxied to AWS
587+
results = logs_client.get_query_results(queryId=query_id)
588+
assert "status" in results
589+
assert results["status"] in [
590+
"Scheduled",
591+
"Running",
592+
"Complete",
593+
"Failed",
594+
"Cancelled",
595+
]
596+
597+
# Verify via AWS client that the query exists (same query ID)
598+
results_aws = logs_client_aws.get_query_results(queryId=query_id)
599+
assert results_aws["status"] == results["status"]
613600

614601

615602
def test_logs_resource_name_matching(start_aws_proxy, cleanups):

0 commit comments

Comments
 (0)