1919import subprocess
2020from tempfile import TemporaryFile
2121
22- import platform
22+ import pytest
2323import requests
2424import time
2525
26- import pytest
27-
2826from tests import get_binary_file_path , clear_octobot_previous_folders , get_log_file_content , is_on_windows
2927
3028logger = logging .getLogger ()
3129logger .setLevel (logging .DEBUG )
3230
3331BINARY_DISABLE_WEB_OPTION = "-nw"
32+ LOG_CHECKS_MAX_ATTEMPTS = 300
3433
3534
3635@pytest .fixture
3736def start_binary ():
3837 clear_octobot_previous_folders ()
3938 with TemporaryFile () as output , TemporaryFile () as err :
40- binary_process = create_binary ("" , output , err )
41- yield
42- terminate_binary (binary_process , output , err )
39+ binary_process = start_binary_process ("" , output , err )
40+ try :
41+ yield
42+ except Exception :
43+ pass
44+ finally :
45+ terminate_binary (binary_process , output , err )
4346
4447
4548@pytest .fixture
4649def start_binary_without_web_app ():
4750 clear_octobot_previous_folders ()
4851 with TemporaryFile () as output , TemporaryFile () as err :
49- binary_process = create_binary (BINARY_DISABLE_WEB_OPTION , output , err )
50- yield
51- terminate_binary (binary_process , output , err )
52+ binary_process = start_binary_process (BINARY_DISABLE_WEB_OPTION , output , err )
53+ logger .debug (err .read ())
54+ try :
55+ yield
56+ except Exception :
57+ pass
58+ finally :
59+ terminate_binary (binary_process , output , err )
5260
5361
54- def create_binary (binary_options , output_file , err_file ):
62+ def start_binary_process (binary_options , output_file , err_file ):
5563 logger .debug ("Starting binary process..." )
5664 return subprocess .Popen (f"{ get_binary_file_path ()} { binary_options } " ,
5765 shell = True ,
@@ -68,44 +76,71 @@ def terminate_binary(binary_process, output_file, err_file):
6876 raise ValueError (f"Error happened during process execution : { errors } " )
6977 logger .debug ("Killing binary process..." )
7078 if is_on_windows ():
71- binary_process .kill ()
79+ os .kill (binary_process . pid , signal . CTRL_C_EVENT )
7280 else :
7381 try :
7482 os .killpg (os .getpgid (binary_process .pid ), signal .SIGTERM ) # Send the signal to all the process groups
7583 except ProcessLookupError :
7684 binary_process .kill ()
7785
7886
79- def test_version_endpoint (start_binary ):
80- max_attempts = 10
87+ def multiple_checks (check_method , sleep_time = 1 , max_attempts = 10 , ** kwargs ):
8188 attempt = 1
8289 while max_attempts >= attempt > 0 :
8390 try :
84- requests .get ('http://localhost:5001/version' )
85- attempt = - 1 # success
86- except requests .exceptions .ConnectionError :
87- logger .warning (f"Failed to get http://localhost/version, retrying ({ attempt } /{ max_attempts } )..." )
91+ result = check_method (** kwargs )
92+ if result : # success
93+ return
94+ except Exception as e :
95+ logger .warning (f"Check ({ attempt } /{ max_attempts } ) failed : { e } " )
96+ finally :
8897 attempt += 1
89- time .sleep (1 )
90- assert attempt <= max_attempts
98+ try :
99+ time .sleep (sleep_time )
100+ except KeyboardInterrupt :
101+ # Fails when windows is stopping binary
102+ pass
103+ assert False # fail
91104
92105
93- def test_evaluation_state_created (start_binary_without_web_app ):
94- time .sleep (10 )
106+ def check_endpoint (endpoint_url , expected_code ):
107+ try :
108+ result = requests .get (endpoint_url )
109+ return result .status_code == expected_code
110+ except requests .exceptions .ConnectionError :
111+ logger .warning (f"Failed to get { endpoint_url } " )
112+ return False
113+
114+
115+ def check_logs_content (expected_content : str , should_appear : bool = True ):
95116 log_content = get_log_file_content ()
96117 logger .debug (log_content )
97- assert "new state:" in log_content
118+ if should_appear :
119+ return expected_content in log_content
120+ return expected_content not in log_content
121+
122+
123+ def test_terms_endpoint (start_binary ):
124+ multiple_checks (check_endpoint ,
125+ max_attempts = 100 ,
126+ endpoint_url = "http://localhost:5001/terms" ,
127+ expected_code = 200 )
128+
129+
130+ def test_evaluation_state_created (start_binary_without_web_app ):
131+ multiple_checks (check_logs_content ,
132+ max_attempts = LOG_CHECKS_MAX_ATTEMPTS ,
133+ expected_content = "new state:" )
98134
99135
100136def test_logs_content_has_no_errors (start_binary_without_web_app ):
101- time . sleep ( 10 )
102- log_content = get_log_file_content ()
103- logger . debug ( log_content )
104- assert "ERROR" not in log_content
137+ multiple_checks ( check_logs_content ,
138+ max_attempts = LOG_CHECKS_MAX_ATTEMPTS ,
139+ expected_content = "ERROR" ,
140+ should_appear = False )
105141
106142
107143def test_balance_profitability_updated (start_binary_without_web_app ):
108- time .sleep (10 )
109- log_content = get_log_file_content ()
110- logger .debug (log_content )
111- assert "BALANCE PROFITABILITY :" in log_content
144+ multiple_checks (check_logs_content ,
145+ max_attempts = LOG_CHECKS_MAX_ATTEMPTS ,
146+ expected_content = "BALANCE PROFITABILITY :" )
0 commit comments