Skip to content

Commit 2e70708

Browse files
author
Roman
committed
add stop_existing_test_containers - stops container if it's exits
1 parent 120ca3b commit 2e70708

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

tests/e2e_tests/conftest.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121

2222
LOCALNET_IMAGE_NAME = "ghcr.io/opentensor/subtensor-localnet:devnet-ready"
23+
CONTAINER_NAME_PREFIX = "test_local_chain_"
2324

2425

2526
def wait_for_node_start(process, timestamp=None):
@@ -163,7 +164,25 @@ def try_start_docker():
163164
print("Docker wasn't run. Manual start may be required.")
164165
return False
165166

166-
container_name = f"test_local_chain_{str(time.time()).replace('.', '_')}"
167+
def stop_existing_test_containers():
168+
"""Stop running Docker containers with names starting with 'test_local_chain_'."""
169+
try:
170+
existing_container_result = subprocess.run(
171+
["docker", "ps", "--filter", f"name={CONTAINER_NAME_PREFIX}", "--format", "{{.ID}}"],
172+
stdout=subprocess.PIPE,
173+
stderr=subprocess.PIPE,
174+
text=True,
175+
check=True
176+
)
177+
container_ids = existing_container_result.stdout.strip().splitlines()
178+
for cid in container_ids:
179+
if cid:
180+
print(f"Stopping existing container: {cid}")
181+
subprocess.run(["docker", "stop", cid], check=True)
182+
except subprocess.CalledProcessError as e:
183+
print(f"Failed to stop existing containers: {e}")
184+
185+
container_name = f"{CONTAINER_NAME_PREFIX}{str(time.time()).replace('.', '_')}"
167186

168187
# Command to start container
169188
cmds = [
@@ -182,6 +201,8 @@ def try_start_docker():
182201

183202
try_start_docker()
184203

204+
stop_existing_test_containers()
205+
185206
# Start container
186207
with subprocess.Popen(
187208
cmds,

0 commit comments

Comments
 (0)