Skip to content

Commit 3498f73

Browse files
committed
fix(test/multi-ec2-fleet): filter fleet count by test session to ignore stale fleets
Add an optional fleet_ids parameter to verify_ec2_fleet_instances_detached so the membership check is scoped to the specific fleets created in the current test run. Without this, the helper listed every active EC2 Fleet in the region and made a describe_fleet_instances call for each one — slow, and vulnerable to stale fleets from prior runs. Both call sites now pass ec2_fleet_ids, which is already populated by Step 4 from the test's own instance tags.
1 parent 410d4e9 commit 3498f73

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

tests/providers/aws/live/test_multi_ec2_fleet_termination.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,29 @@ def get_ec2_fleet_instances(fleet_id: str) -> List[str]:
9595
return []
9696

9797

98-
def verify_ec2_fleet_instances_detached(instance_ids: List[str]) -> bool:
99-
"""Verify that instances are no longer part of any EC2 Fleet."""
98+
def verify_ec2_fleet_instances_detached(
99+
instance_ids: List[str],
100+
fleet_ids: List[str] | None = None,
101+
) -> bool:
102+
"""Verify that instances are no longer part of any EC2 Fleet.
103+
104+
When *fleet_ids* is supplied the check is scoped to those specific fleets
105+
(the ones created by this test run). Without it the call falls back to
106+
listing every active fleet in the region, which can be slow and may
107+
include stale fleets from earlier test runs.
108+
"""
100109
try:
101110
if not instance_ids:
102111
return True
103112

104-
# Get all active EC2 fleets
105-
response = _get_ec2_client().describe_fleets(
106-
Filters=[{"Name": "fleet-state", "Values": ["active", "modifying"]}]
107-
)
113+
if fleet_ids:
114+
# Fast path: only inspect the fleets we created in this test.
115+
response = _get_ec2_client().describe_fleets(FleetIds=list(fleet_ids))
116+
else:
117+
# Fallback: scan every active fleet in the region.
118+
response = _get_ec2_client().describe_fleets(
119+
Filters=[{"Name": "fleet-state", "Values": ["active", "modifying"]}]
120+
)
108121

109122
# Check each fleet for our instances
110123
for fleet in response.get("Fleets", []):
@@ -612,7 +625,7 @@ def test_multi_ec2_fleet_termination(setup_multi_ec2_fleet_templates):
612625

613626
# Check if instances are detached from EC2 Fleets
614627
if not termination_started:
615-
detached = verify_ec2_fleet_instances_detached(all_instance_ids)
628+
detached = verify_ec2_fleet_instances_detached(all_instance_ids, list(ec2_fleet_ids))
616629
if detached:
617630
log.info("All instances successfully detached from EC2 Fleets")
618631
termination_started = True
@@ -635,7 +648,7 @@ def test_multi_ec2_fleet_termination(setup_multi_ec2_fleet_templates):
635648
assert final_terminating, "Some instances are not in terminating/terminated state"
636649

637650
# Verify instances are detached from EC2 Fleets
638-
final_detached = verify_ec2_fleet_instances_detached(all_instance_ids)
651+
final_detached = verify_ec2_fleet_instances_detached(all_instance_ids, list(ec2_fleet_ids))
639652
if not final_detached:
640653
log.info(
641654
"Note: Some instances still show EC2 Fleet attachment while terminating - this is expected AWS behavior"

0 commit comments

Comments
 (0)