@@ -68,59 +68,85 @@ def redis_cluster_container(worker_id):
6868 # The DockerCompose helper isn't working with multiple services because the
6969 # subprocess command returns non-zero exit codes even on successful
7070 # completion. Here, we run the commands manually.
71+
72+ # First attempt the docker-compose up command and handle its errors directly
73+ docker_cmd = [
74+ "docker" ,
75+ "compose" ,
76+ "-f" ,
77+ compose_file ,
78+ "-p" , # Explicitly pass project name
79+ project_name ,
80+ "up" ,
81+ "--wait" , # Wait for healthchecks
82+ "-d" , # Detach
83+ ]
84+
7185 try :
72- # Use --wait to ensure services (especially redis-cluster-setup) are healthy
73- subprocess .run (
74- [
75- "docker" ,
76- "compose" ,
77- "-f" ,
78- compose_file ,
79- "-p" , # Explicitly pass project name
80- project_name ,
81- "up" ,
82- "--wait" , # Wait for healthchecks
83- "-d" , # Detach
84- ],
85- check = True , # check=True can be problematic if successful commands return non-zero, but `up --wait -d` should be 0 on success.
86+ result = subprocess .run (
87+ docker_cmd ,
88+ capture_output = True ,
89+ check = False , # Don't raise exception, we'll handle it ourselves
8690 )
91+
92+ if result .returncode != 0 :
93+ print (f"Docker Compose up failed with exit code { result .returncode } " )
94+ if result .stdout :
95+ print (f"STDOUT: { result .stdout .decode ('utf-8' , errors = 'replace' )} " )
96+ if result .stderr :
97+ print (f"STDERR: { result .stderr .decode ('utf-8' , errors = 'replace' )} " )
98+
99+ # Try to get logs for more details
100+ print ("Attempting to fetch container logs..." )
101+ try :
102+ logs_result = subprocess .run (
103+ [
104+ "docker" ,
105+ "compose" ,
106+ "-f" ,
107+ compose_file ,
108+ "-p" ,
109+ project_name ,
110+ "logs" ,
111+ ],
112+ capture_output = True ,
113+ text = True ,
114+ )
115+ print ("Docker Compose logs:\n " , logs_result .stdout )
116+ if logs_result .stderr :
117+ print ("Docker Compose logs stderr:\n " , logs_result .stderr )
118+ except Exception as log_e :
119+ print (f"Failed to get Docker Compose logs: { repr (log_e )} " )
120+
121+ # Now raise the exception with the original result
122+ raise subprocess .CalledProcessError (
123+ result .returncode ,
124+ docker_cmd ,
125+ output = result .stdout ,
126+ stderr = result .stderr ,
127+ )
128+
129+ # If we get here, setup was successful
87130 yield
88- except subprocess .CalledProcessError as e :
89- # Attempt to get logs if setup failed
90- print (f"Docker Compose up --wait failed: { e } " )
131+ finally :
132+ # Always clean up
91133 try :
92- logs_result = subprocess .run (
134+ subprocess .run (
93135 [
94136 "docker" ,
95137 "compose" ,
96138 "-f" ,
97139 compose_file ,
98140 "-p" ,
99141 project_name ,
100- "logs" ,
142+ "down" ,
143+ "-v" , # Remove volumes
101144 ],
145+ check = False , # Don't raise on cleanup failure
102146 capture_output = True ,
103- text = True ,
104147 )
105- print ("Docker Compose logs:\n " , logs_result .stdout )
106- if logs_result .stderr :
107- print ("Docker Compose logs stderr:\n " , logs_result .stderr )
108- except Exception as log_e :
109- print (f"Failed to get Docker Compose logs: { log_e } " )
110- raise # Re-raise the original error to fail the test setup
111- finally :
112- subprocess .run (
113- [
114- "docker" ,
115- "compose" ,
116- "-f" ,
117- compose_file ,
118- "-p" ,
119- project_name ,
120- "down" ,
121- "-v" , # Remove volumes
122- ]
123- )
148+ except Exception as e :
149+ print (f"Error during cleanup: { repr (e )} " )
124150
125151
126152@pytest .fixture (scope = "session" )
0 commit comments