11"""Integration tests for Unleash provider using testcontainers."""
22
3+ import logging
34import time
45from datetime import datetime , timezone
56
1516
1617# Configuration for the running Unleash instance (will be set by fixtures)
1718UNLEASH_URL = None
18- API_TOKEN = "default:development.unleash-insecure-api-token"
19- ADMIN_TOKEN = "user:76672ac99726f8e48a1bbba16b7094a50d1eee3583d1e8457e12187a"
19+ API_TOKEN = "default:development.unleash-insecure-api-token" # noqa: S105
20+ ADMIN_TOKEN = "user:76672ac99726f8e48a1bbba16b7094a50d1eee3583d1e8457e12187a" # noqa: S105
21+
22+ logger = logging .getLogger ("openfeature.contrib.tests" )
2023
2124
2225class UnleashContainer (DockerContainer ):
@@ -33,7 +36,7 @@ def _configure(self):
3336 self .with_env ("DATABASE_SSL_REJECT_UNAUTHORIZED" , "false" )
3437 self .with_env ("LOG_LEVEL" , "info" )
3538 self .with_env ("PORT" , "4242" )
36- self .with_env ("HOST" , "0.0.0.0" )
39+ self .with_env ("HOST" , "0.0.0.0" ) # noqa: S104
3740 self .with_env ("ADMIN_AUTHENTICATION" , "none" )
3841 self .with_env ("AUTH_ENABLE" , "false" )
3942 self .with_env ("INIT_CLIENT_API_TOKENS" , API_TOKEN )
@@ -66,9 +69,9 @@ def insert_admin_token(postgres_container):
6669 ),
6770 )
6871 conn .commit ()
69- print ("Admin token inserted successfully" )
72+ logger . info ("Admin token inserted successfully" )
7073 except Exception as e :
71- print (f"Error inserting admin token: { e } " )
74+ logger . error (f"Error inserting admin token: { e } " )
7275 conn .rollback ()
7376 finally :
7477 conn .close ()
@@ -126,13 +129,15 @@ def create_test_flags():
126129 timeout = 10 ,
127130 )
128131 if response .status_code in [200 , 201 , 409 ]:
129- print (f"Flag '{ flag ['name' ]} ' created" )
132+ logger . info (f"Flag '{ flag ['name' ]} ' created" )
130133 add_strategy_with_variants (flag ["name" ], headers )
131134 enable_flag (flag ["name" ], headers )
132135 else :
133- print (f"Failed to create flag '{ flag ['name' ]} ': { response .status_code } " )
134- except Exception as e :
135- print (f"Error creating flag '{ flag ['name' ]} ': { e } " )
136+ logger .error (
137+ f"Failed to create flag '{ flag ['name' ]} ': { response .status_code } "
138+ )
139+ except Exception as e : # noqa: PERF203
140+ logger .error (f"Error creating flag '{ flag ['name' ]} ': { e } " )
136141
137142
138143def add_strategy_with_variants (flag_name : str , headers : dict ):
@@ -225,9 +230,9 @@ def add_strategy_with_variants(flag_name: str, headers: dict):
225230 timeout = 10 ,
226231 )
227232 if strategy_response .status_code in [200 , 201 ]:
228- print (f"Strategy with variants added to '{ flag_name } '" )
233+ logger . info (f"Strategy with variants added to '{ flag_name } '" )
229234 else :
230- print (
235+ logger . error (
231236 f"Failed to add strategy to '{ flag_name } ': { strategy_response .status_code } "
232237 )
233238
@@ -241,11 +246,13 @@ def enable_flag(flag_name: str, headers: dict):
241246 timeout = 10 ,
242247 )
243248 if enable_response .status_code in [200 , 201 ]:
244- print (f"Flag '{ flag_name } ' enabled in development environment" )
249+ logger . info (f"Flag '{ flag_name } ' enabled in development environment" )
245250 else :
246- print (f"Failed to enable flag '{ flag_name } ': { enable_response .status_code } " )
251+ logger .error (
252+ f"Failed to enable flag '{ flag_name } ': { enable_response .status_code } "
253+ )
247254 except Exception as e :
248- print (f"Error enabling flag '{ flag_name } ': { e } " )
255+ logger . error (f"Error enabling flag '{ flag_name } ': { e } " )
249256
250257
251258@pytest .fixture (scope = "session" )
@@ -254,13 +261,13 @@ def postgres_container():
254261 with PostgresContainer ("postgres:15" , driver = None ) as postgres :
255262 postgres .start ()
256263 postgres_url = postgres .get_connection_url ()
257- print (f"PostgreSQL started at: { postgres_url } " )
264+ logger . info (f"PostgreSQL started at: { postgres_url } " )
258265
259266 yield postgres
260267
261268
262269@pytest .fixture (scope = "session" )
263- def unleash_container (postgres_container ):
270+ def unleash_container (postgres_container ): # noqa: PLR0915
264271 """Create and start Unleash container with PostgreSQL dependency."""
265272 global UNLEASH_URL
266273
@@ -278,12 +285,12 @@ def unleash_container(postgres_container):
278285 unleash = UnleashContainer (internal_url )
279286
280287 with unleash as container :
281- print ("Starting Unleash container..." )
288+ logger . info ("Starting Unleash container..." )
282289 container .start ()
283- print ("Unleash container started" )
290+ logger . info ("Unleash container started" )
284291
285292 # Wait for health check to pass
286- print ("Waiting for Unleash container to be healthy..." )
293+ logger . info ("Waiting for Unleash container to be healthy..." )
287294 max_wait_time = 60 # 1 minute max wait
288295 start_time = time .time ()
289296
@@ -292,42 +299,42 @@ def unleash_container(postgres_container):
292299 try :
293300 exposed_port = container .get_exposed_port (4242 )
294301 unleash_url = f"http://localhost:{ exposed_port } "
295- print (f"Trying health check at: { unleash_url } " )
302+ logger . info (f"Trying health check at: { unleash_url } " )
296303 except Exception as port_error :
297- print (f"Port not ready yet: { port_error } " )
304+ logger . error (f"Port not ready yet: { port_error } " )
298305 time .sleep (2 )
299306 continue
300307
301308 response = requests .get (f"{ unleash_url } /health" , timeout = 5 )
302309 if response .status_code == 200 :
303- print ("Unleash container is healthy!" )
310+ logger . info ("Unleash container is healthy!" )
304311 break
305312
306- print (f"Health check failed, status: { response .status_code } " )
313+ logger . error (f"Health check failed, status: { response .status_code } " )
307314 time .sleep (2 )
308315
309316 except Exception as e :
310- print (f"Health check error: { e } " )
317+ logger . error (f"Health check error: { e } " )
311318 time .sleep (2 )
312319 else :
313320 raise Exception ("Unleash container did not become healthy within timeout" )
314321
315322 # Get the exposed port and set global URL
316323 UNLEASH_URL = f"http://localhost:{ container .get_exposed_port (4242 )} "
317- print (f"Unleash started at: { unleash_url } " )
324+ logger . info (f"Unleash started at: { unleash_url } " )
318325
319326 insert_admin_token (postgres_container )
320- print ("Admin token inserted into database" )
327+ logger . info ("Admin token inserted into database" )
321328
322329 yield container , unleash_url
323330
324331
325332@pytest .fixture (scope = "session" , autouse = True )
326333def setup_test_flags (unleash_container ):
327334 """Setup test flags before running any tests."""
328- print ("Creating test flags in Unleash..." )
335+ logger . info ("Creating test flags in Unleash..." )
329336 create_test_flags ()
330- print ("Test flags setup completed" )
337+ logger . info ("Test flags setup completed" )
331338
332339
333340@pytest .fixture (scope = "session" )
0 commit comments