Skip to content

Commit 0ac6ffc

Browse files
author
Roman
committed
fix comments + new logic
1 parent c117432 commit 0ac6ffc

File tree

1 file changed

+63
-21
lines changed

1 file changed

+63
-21
lines changed

tests/e2e_tests/conftest.py

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import os
22
import re
3-
import shutil
43
import shlex
4+
import shutil
55
import signal
66
import subprocess
77
import sys
88
import threading
99
import time
10-
from bittensor.utils.btlogging import logging
1110

1211
import pytest
1312
from async_substrate_interface import SubstrateInterface
1413

1514
from bittensor.core.async_subtensor import AsyncSubtensor
1615
from bittensor.core.subtensor import Subtensor
16+
from bittensor.utils.btlogging import logging
1717
from tests.e2e_tests.utils.e2e_test_utils import (
1818
Templates,
1919
setup_wallet,
@@ -55,24 +55,24 @@ def local_chain(request):
5555
"""Determines whether to run the localnet.sh script in a subprocess or a Docker container."""
5656
args = request.param if hasattr(request, "param") else None
5757
params = "" if args is None else f"{args}"
58-
if shutil.which("docker"):
58+
if shutil.which("docker") and not os.getenv("USE_DOCKER") == "0":
5959
yield from docker_runner(params)
60-
return
61-
62-
if sys.platform.startswith("linux"):
63-
docker_commend = (
64-
"Install docker with command "
65-
"[blue]sudo apt-get update && sudo apt-get install docker.io -y[/blue]"
66-
)
67-
elif sys.platform == "darwin":
68-
docker_commend = "Install docker with command [blue]brew install docker[/blue]"
6960
else:
70-
docker_commend = "[blue]Unknown OS, install Docker manually: https://docs.docker.com/get-docker/[/blue]"
71-
72-
logging.warning("Docker not found in the operating system!")
73-
logging.warning(docker_commend)
74-
logging.warning("Tests are run in legacy mode.")
75-
yield from legacy_runner(request)
61+
if not os.getenv("USE_DOCKER") == "0":
62+
if sys.platform.startswith("linux"):
63+
docker_command = (
64+
"Install docker with command "
65+
"[blue]sudo apt-get update && sudo apt-get install docker.io -y[/blue]"
66+
)
67+
elif sys.platform == "darwin":
68+
docker_command = "Install docker with command [blue]brew install docker[/blue] or use documentation [blue]https://docs.docker.com/engine/install/[/blue]"
69+
else:
70+
docker_command = "[blue]Unknown OS, install Docker manually: https://docs.docker.com/get-docker/[/blue]"
71+
72+
logging.warning("Docker not found in the operating system!")
73+
logging.warning(docker_command)
74+
logging.warning("Tests are run in legacy mode.")
75+
yield from legacy_runner(request)
7676

7777

7878
def legacy_runner(params):
@@ -103,7 +103,8 @@ def legacy_runner(params):
103103
except TimeoutError:
104104
raise
105105
else:
106-
yield SubstrateInterface(url="ws://127.0.0.1:9944")
106+
with SubstrateInterface(url="ws://127.0.0.1:9944") as substrate:
107+
yield substrate
107108
finally:
108109
# Terminate the process group (includes all child processes)
109110
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
@@ -119,6 +120,44 @@ def legacy_runner(params):
119120
def docker_runner(params):
120121
"""Starts a Docker container before tests and gracefully terminates it after."""
121122

123+
def is_docker_running():
124+
"""Check if Docker has been run."""
125+
try:
126+
subprocess.run(
127+
["docker", "info"],
128+
stdout=subprocess.DEVNULL,
129+
stderr=subprocess.DEVNULL,
130+
check=True,
131+
)
132+
return True
133+
except subprocess.CalledProcessError:
134+
return False
135+
136+
def try_start_docker():
137+
"""Run docker based on OS."""
138+
try:
139+
subprocess.run(["open", "-a", "Docker"], check=True) # macOS
140+
except FileNotFoundError:
141+
try:
142+
subprocess.run(["systemctl", "start", "docker"], check=True) # Linux
143+
except FileNotFoundError:
144+
try:
145+
subprocess.run(
146+
["sudo", "service", "docker", "start"], check=True
147+
) # Linux alternative
148+
except FileNotFoundError:
149+
print("Failed to start Docker. Manual start may be required.")
150+
return False
151+
152+
# Wait Docker run 10 attempts with 3 sec waits
153+
for _ in range(10):
154+
if is_docker_running():
155+
return True
156+
time.sleep(3)
157+
158+
print("Docker wasn't run. Manual start may be required.")
159+
return False
160+
122161
container_name = f"test_local_chain_{str(time.time()).replace(".", "_")}"
123162
image_name = "ghcr.io/opentensor/subtensor-localnet:latest"
124163

@@ -137,6 +176,8 @@ def docker_runner(params):
137176
params,
138177
]
139178

179+
try_start_docker()
180+
140181
# Start container
141182
with subprocess.Popen(
142183
cmds,
@@ -147,7 +188,7 @@ def docker_runner(params):
147188
) as process:
148189
try:
149190
try:
150-
wait_for_node_start(process, int(time.time()))
191+
wait_for_node_start(process, timestamp=int(time.time()))
151192
except TimeoutError:
152193
raise
153194

@@ -159,7 +200,8 @@ def docker_runner(params):
159200
if not result.stdout.strip():
160201
raise RuntimeError("Docker container failed to start.")
161202

162-
yield SubstrateInterface(url="ws://127.0.0.1:9944")
203+
with SubstrateInterface(url="ws://127.0.0.1:9944") as substrate:
204+
yield substrate
163205

164206
finally:
165207
try:

0 commit comments

Comments
 (0)