Skip to content

Commit 60329e7

Browse files
authored
fix: remove fallback in count_running_containers causing flaky warm container tests (aws#8651)
The count_running_containers method fell back to counting ALL SAM CLI containers when MODE env var filtering found no matches. This caused AssertionError: 3 != 2 when stale containers from other tests were present. Changes: - Remove fallback that returned len(sam_containers) - now strictly returns only containers matching this test's unique MODE UUID - Use exact string matching instead of substring matching on env var
1 parent f92e9df commit 60329e7

2 files changed

Lines changed: 6 additions & 24 deletions

File tree

tests/integration/local/start_api/test_start_api.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,35 +2220,26 @@ def setUp(self):
22202220

22212221
def count_running_containers(self):
22222222
"""Count containers created by this test using Docker client directly."""
2223-
# Use Docker client to find containers with SAM CLI labels
22242223
try:
2225-
# Get running containers with SAM CLI lambda container label
22262224
sam_containers = self.docker_client.containers.list(
22272225
all=False, filters={"label": "sam.cli.container.type=lambda"}
22282226
)
22292227

2230-
# Filter by our test's mode environment variable if possible
22312228
test_containers = []
22322229
for container in sam_containers:
22332230
try:
22342231
container.reload()
22352232
env_vars = container.attrs.get("Config", {}).get("Env", [])
22362233
for env_var in env_vars:
2237-
if env_var.startswith("MODE=") and self.mode_env_variable in env_var:
2234+
if env_var == f"MODE={self.mode_env_variable}":
22382235
test_containers.append(container)
22392236
break
22402237
except Exception:
22412238
continue
22422239

2243-
# If we found containers with our mode variable, return that count
2244-
if test_containers:
2245-
return len(test_containers)
2240+
return len(test_containers)
22462241

2247-
# Otherwise, return all SAM containers (fallback)
2248-
return len(sam_containers)
2249-
2250-
except Exception as e:
2251-
# If we can't access Docker client, fall back to 0
2242+
except Exception:
22522243
return 0
22532244

22542245
def _parse_container_ids_from_output(self):

tests/integration/local/start_lambda/test_start_lambda.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,35 +403,26 @@ def setUp(self):
403403

404404
def count_running_containers(self):
405405
"""Count containers created by this test using Docker client directly."""
406-
# Use Docker client to find containers with SAM CLI labels
407406
try:
408-
# Get running containers with SAM CLI lambda container label
409407
sam_containers = self.docker_client.containers.list(
410408
all=False, filters={"label": "sam.cli.container.type=lambda"}
411409
)
412410

413-
# Filter by our test's mode environment variable if possible
414411
test_containers = []
415412
for container in sam_containers:
416413
try:
417414
container.reload()
418415
env_vars = container.attrs.get("Config", {}).get("Env", [])
419416
for env_var in env_vars:
420-
if env_var.startswith("MODE=") and self.mode_env_variable in env_var:
417+
if env_var == f"MODE={self.mode_env_variable}":
421418
test_containers.append(container)
422419
break
423420
except Exception:
424421
continue
425422

426-
# If we found containers with our mode variable, return that count
427-
if test_containers:
428-
return len(test_containers)
423+
return len(test_containers)
429424

430-
# Otherwise, return all SAM containers (fallback)
431-
return len(sam_containers)
432-
433-
except Exception as e:
434-
# If we can't access Docker client, fall back to 0
425+
except Exception:
435426
return 0
436427

437428
def _parse_container_ids_from_output(self):

0 commit comments

Comments
 (0)