19
19
import subprocess
20
20
from tempfile import TemporaryFile
21
21
22
- import platform
22
+ import pytest
23
23
import requests
24
24
import time
25
25
26
- import pytest
27
-
28
26
from tests import get_binary_file_path , clear_octobot_previous_folders , get_log_file_content , is_on_windows
29
27
30
28
logger = logging .getLogger ()
31
29
logger .setLevel (logging .DEBUG )
32
30
33
31
BINARY_DISABLE_WEB_OPTION = "-nw"
32
+ LOG_CHECKS_MAX_ATTEMPTS = 300
34
33
35
34
36
35
@pytest .fixture
37
36
def start_binary ():
38
37
clear_octobot_previous_folders ()
39
38
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 )
43
46
44
47
45
48
@pytest .fixture
46
49
def start_binary_without_web_app ():
47
50
clear_octobot_previous_folders ()
48
51
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 )
52
60
53
61
54
- def create_binary (binary_options , output_file , err_file ):
62
+ def start_binary_process (binary_options , output_file , err_file ):
55
63
logger .debug ("Starting binary process..." )
56
64
return subprocess .Popen (f"{ get_binary_file_path ()} { binary_options } " ,
57
65
shell = True ,
@@ -68,44 +76,64 @@ def terminate_binary(binary_process, output_file, err_file):
68
76
raise ValueError (f"Error happened during process execution : { errors } " )
69
77
logger .debug ("Killing binary process..." )
70
78
if is_on_windows ():
71
- binary_process .kill ()
79
+ os .kill (binary_process . pid , signal . CTRL_C_EVENT )
72
80
else :
73
81
try :
74
82
os .killpg (os .getpgid (binary_process .pid ), signal .SIGTERM ) # Send the signal to all the process groups
75
83
except ProcessLookupError :
76
84
binary_process .kill ()
77
85
78
86
79
- def test_version_endpoint (start_binary ):
80
- max_attempts = 10
87
+ def multiple_checks (check_method , sleep_time = 1 , max_attempts = 10 , ** kwargs ):
81
88
attempt = 1
82
89
while max_attempts >= attempt > 0 :
83
90
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 :
88
97
attempt += 1
89
- time .sleep (1 )
90
- assert attempt <= max_attempts
98
+ time .sleep (sleep_time )
99
+ assert False # fail
91
100
92
101
93
- def test_evaluation_state_created (start_binary_without_web_app ):
94
- time .sleep (10 )
102
+ def check_endpoint (endpoint_url , expected_code ):
103
+ try :
104
+ result = requests .get (endpoint_url )
105
+ return result .status_code == expected_code
106
+ except requests .exceptions .ConnectionError :
107
+ logger .warning (f"Failed to get { endpoint_url } " )
108
+ return False
109
+
110
+
111
+ def check_logs_content (expected_content : str , should_appear : bool = True ):
95
112
log_content = get_log_file_content ()
96
113
logger .debug (log_content )
97
- assert "new state:" in log_content
114
+ if should_appear :
115
+ return expected_content in log_content
116
+ return expected_content not in log_content
117
+
118
+
119
+ def test_terms_endpoint (start_binary ):
120
+ multiple_checks (check_endpoint , endpoint_url = "http://localhost:5001/terms" , expected_code = 200 )
121
+
122
+
123
+ def test_evaluation_state_created (start_binary_without_web_app ):
124
+ multiple_checks (check_logs_content ,
125
+ max_attempts = LOG_CHECKS_MAX_ATTEMPTS ,
126
+ expected_content = "new state:" )
98
127
99
128
100
129
def 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
130
+ multiple_checks ( check_logs_content ,
131
+ max_attempts = LOG_CHECKS_MAX_ATTEMPTS ,
132
+ expected_content = "ERROR" ,
133
+ should_appear = False )
105
134
106
135
107
136
def 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
137
+ multiple_checks (check_logs_content ,
138
+ max_attempts = LOG_CHECKS_MAX_ATTEMPTS ,
139
+ expected_content = "BALANCE PROFITABILITY :" )
0 commit comments