1
1
import os
2
2
import re
3
- import shutil
4
3
import shlex
4
+ import shutil
5
5
import signal
6
6
import subprocess
7
7
import sys
8
8
import threading
9
9
import time
10
- from bittensor .utils .btlogging import logging
11
10
12
11
import pytest
13
12
from async_substrate_interface import SubstrateInterface
14
13
15
14
from bittensor .core .async_subtensor import AsyncSubtensor
16
15
from bittensor .core .subtensor import Subtensor
16
+ from bittensor .utils .btlogging import logging
17
17
from tests .e2e_tests .utils .e2e_test_utils import (
18
18
Templates ,
19
19
setup_wallet ,
@@ -55,24 +55,24 @@ def local_chain(request):
55
55
"""Determines whether to run the localnet.sh script in a subprocess or a Docker container."""
56
56
args = request .param if hasattr (request , "param" ) else None
57
57
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" :
59
59
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]"
69
60
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 )
76
76
77
77
78
78
def legacy_runner (params ):
@@ -103,7 +103,8 @@ def legacy_runner(params):
103
103
except TimeoutError :
104
104
raise
105
105
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
107
108
finally :
108
109
# Terminate the process group (includes all child processes)
109
110
os .killpg (os .getpgid (process .pid ), signal .SIGTERM )
@@ -119,6 +120,44 @@ def legacy_runner(params):
119
120
def docker_runner (params ):
120
121
"""Starts a Docker container before tests and gracefully terminates it after."""
121
122
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
+
122
161
container_name = f"test_local_chain_{ str (time .time ()).replace ("." , "_" )} "
123
162
image_name = "ghcr.io/opentensor/subtensor-localnet:latest"
124
163
@@ -137,6 +176,8 @@ def docker_runner(params):
137
176
params ,
138
177
]
139
178
179
+ try_start_docker ()
180
+
140
181
# Start container
141
182
with subprocess .Popen (
142
183
cmds ,
@@ -147,7 +188,7 @@ def docker_runner(params):
147
188
) as process :
148
189
try :
149
190
try :
150
- wait_for_node_start (process , int (time .time ()))
191
+ wait_for_node_start (process , timestamp = int (time .time ()))
151
192
except TimeoutError :
152
193
raise
153
194
@@ -159,7 +200,8 @@ def docker_runner(params):
159
200
if not result .stdout .strip ():
160
201
raise RuntimeError ("Docker container failed to start." )
161
202
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
163
205
164
206
finally :
165
207
try :
0 commit comments