Skip to content

Commit 3a510cb

Browse files
committed
Fix race in tests
1 parent 3c90719 commit 3a510cb

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/confcom/azext_confcom/tests/latest/test_confcom_arm.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
55

6+
import fcntl
67
import os
78
import unittest
89
import json
910
import deepdiff
11+
import docker
12+
import requests
1013
from unittest.mock import patch
1114

1215
from azext_confcom.security_policy import (
@@ -24,6 +27,7 @@
2427
)
2528

2629
TEST_DIR = os.path.abspath(os.path.join(os.path.abspath(__file__), ".."))
30+
PRUNE_LOCK_PATH = "/tmp/confcom_docker_prune.lock"
2731

2832

2933
class PolicyGeneratingArm(unittest.TestCase):
@@ -5007,7 +5011,18 @@ def setUpClass(cls):
50075011

50085012
@classmethod
50095013
def tearDownClass(cls):
5010-
cls.client.containers.prune()
5014+
# Coordinate cleanup across xdist workers to avoid prune conflicts.
5015+
with open(PRUNE_LOCK_PATH, "w") as lock_file:
5016+
fcntl.flock(lock_file, fcntl.LOCK_EX)
5017+
try:
5018+
cls.client.containers.prune()
5019+
except (docker.errors.APIError, requests.exceptions.ReadTimeout) as exc:
5020+
# Ignore conflicts (another prune in flight) or slow daemon timeouts.
5021+
status = getattr(getattr(exc, "response", None), "status_code", None)
5022+
if status not in (409, None) or not isinstance(exc, requests.exceptions.ReadTimeout):
5023+
raise
5024+
finally:
5025+
fcntl.flock(lock_file, fcntl.LOCK_UN)
50115026
cls.client.close()
50125027

50135028
def test_arm_template_security_context_no_run_as_group(self):

0 commit comments

Comments
 (0)