1212
1313import os
1414import socket
15- import subprocess
16- import time
1715from contextlib import closing
18- from uuid import uuid4
1916
2017import pytest
2118
2219import trino .logging
23- from trino . client import ClientSession , TrinoQuery , TrinoRequest
20+ from tests . development_server import TRINO_HOST , TRINO_VERSION , start_development_server
2421from trino .constants import DEFAULT_PORT
2522
2623logger = trino .logging .get_logger (__name__ )
2724
2825
29- TRINO_VERSION = os .environ .get ("TRINO_VERSION" ) or "latest"
30- TRINO_HOST = "127.0.0.1"
31- TRINO_PORT = 8080
32-
33-
34- def is_trino_available ():
26+ def is_trino_available (host , port ):
3527 with closing (socket .socket (socket .AF_INET , socket .SOCK_STREAM )) as sock :
3628 sock .settimeout (2 )
37- result = sock .connect_ex ((TRINO_HOST , DEFAULT_PORT ))
29+ result = sock .connect_ex ((host , port ))
3830 if result == 0 :
3931 return True
32+ return False
4033
4134
4235def get_local_port ():
@@ -45,115 +38,21 @@ def get_local_port():
4538 return s .getsockname ()[1 ]
4639
4740
48- def get_default_trino_image_tag ():
49- return "trinodb/trino:" + TRINO_VERSION
50-
51-
52- def start_trino (image_tag = None ):
53- if not image_tag :
54- image_tag = get_default_trino_image_tag ()
55-
56- container_id = "trino-python-client-tests-" + uuid4 ().hex [:7 ]
57- local_port = get_local_port ()
58- logger .info ("starting Docker container" )
59- docker_run = [
60- "docker" ,
61- "run" ,
62- "--rm" ,
63- "-p" ,
64- "{host_port}:{cont_port}" .format (host_port = local_port , cont_port = TRINO_PORT ),
65- "--name" ,
66- container_id ,
67- image_tag ,
68- ]
69- run = subprocess .Popen (docker_run , universal_newlines = True , stderr = subprocess .PIPE )
70- return (container_id , run , "localhost" , local_port )
71-
72-
73- def wait_for_trino_workers (host , port , timeout = 180 ):
74- request = TrinoRequest (
75- host = host ,
76- port = port ,
77- client_session = ClientSession (
78- user = "test_fixture"
79- )
80- )
81- sql = "SELECT state FROM system.runtime.nodes"
82- t0 = time .time ()
83- while True :
84- query = TrinoQuery (request , sql )
85- rows = list (query .execute ())
86- if any (row [0 ] == "active" for row in rows ):
87- break
88- if time .time () - t0 > timeout :
89- raise TimeoutError
90- time .sleep (1 )
91-
92-
93- def wait_for_trino_coordinator (stream , timeout = 180 ):
94- started_tag = "======== SERVER STARTED ========"
95- t0 = time .time ()
96- for line in iter (stream .readline , b"" ):
97- if line :
98- print (line )
99- if started_tag in line :
100- time .sleep (5 )
101- return True
102- if time .time () - t0 > timeout :
103- logger .error ("coordinator took longer than %s to start" , timeout )
104- raise TimeoutError
105- return False
106-
107-
108- def start_local_trino_server (image_tag ):
109- container_id , proc , host , port = start_trino (image_tag )
110- print ("trino.server.state starting" )
111- trino_ready = wait_for_trino_coordinator (proc .stderr )
112- if not trino_ready :
113- raise Exception ("Trino server did not start" )
114- wait_for_trino_workers (host , port )
115- print ("trino.server.state ready" )
116- return container_id , proc , host , port
117-
118-
119- def start_trino_and_wait (image_tag = None ):
120- container_id = None
121- proc = None
122- host = os .environ .get ("TRINO_RUNNING_HOST" , None )
123- if host :
124- port = os .environ .get ("TRINO_RUNNING_PORT" , DEFAULT_PORT )
125- else :
126- container_id , proc , host , port = start_local_trino_server (
127- image_tag
128- )
129-
130- print ("trino.server.hostname {}" .format (host ))
131- print ("trino.server.port {}" .format (port ))
132- if proc :
133- print ("trino.server.pid {}" .format (proc .pid ))
134- if container_id :
135- print ("trino.server.contained_id {}" .format (container_id ))
136- return container_id , proc , host , port
137-
138-
139- def stop_trino (container_id , proc ):
140- subprocess .check_call (["docker" , "kill" , container_id ])
141-
142-
143- @pytest .fixture (scope = "module" )
41+ @pytest .fixture (scope = "session" )
14442def run_trino ():
145- if is_trino_available ():
146- yield None , TRINO_HOST , DEFAULT_PORT
147- return
43+ host = os .environ .get ("TRINO_RUNNING_HOST" , TRINO_HOST )
44+ port = os .environ .get ("TRINO_RUNNING_PORT" , DEFAULT_PORT )
14845
149- image_tag = os .environ .get ("TRINO_IMAGE" )
150- if not image_tag :
151- image_tag = get_default_trino_image_tag ()
46+ # Is there any local Trino available
47+ if is_trino_available (host , port ):
48+ yield host , port
49+ return
15250
153- container_id , proc , host , port = start_trino_and_wait (image_tag )
154- yield proc , host , port
155- if container_id or proc :
156- stop_trino (container_id , proc )
51+ # Start Trino and MinIO server
52+ print (f"Could not connect to Trino at { host } :{ port } , starting server..." )
53+ local_port = get_local_port ()
54+ with start_development_server (port = local_port ):
55+ yield TRINO_HOST , local_port
15756
15857
15958def trino_version ():
0 commit comments