Skip to content

Commit 4ea7304

Browse files
committed
[Tests] Update sleep time to read logs content
1 parent dd9eea7 commit 4ea7304

File tree

3 files changed

+79
-40
lines changed

3 files changed

+79
-40
lines changed

.github/workflows/main.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: OctoBot-Binary-CI
22
on: push
33

44
jobs:
5-
builds:
5+
build:
66
name: ${{ matrix.os }} - ${{ matrix.arch }} - Python 3.8 - build
77
runs-on: ${{ matrix.os }}
88
strategy:
@@ -59,7 +59,7 @@ jobs:
5959
env:
6060
GH_REPO: Drakkar-Software/OctoBot-Tentacles
6161
OCTOBOT_GH_REPO: https://github.com/Drakkar-Software/OctoBot.git
62-
OCTOBOT_DEFAULT_BRANCH: dev
62+
OCTOBOT_DEFAULT_BRANCH: master
6363
OCTOBOT_REPOSITORY_DIR: OctoBot
6464
NLTK_DATA: nltk_data
6565
BUILD_ARCH: ${{ matrix.arch }}
@@ -70,7 +70,7 @@ jobs:
7070
env:
7171
GH_REPO: Drakkar-Software/OctoBot-Tentacles
7272
OCTOBOT_GH_REPO: https://github.com/Drakkar-Software/OctoBot.git
73-
OCTOBOT_DEFAULT_BRANCH: dev
73+
OCTOBOT_DEFAULT_BRANCH: master
7474
OCTOBOT_REPOSITORY_DIR: OctoBot
7575
NLTK_DATA: nltk_data
7676
run: .\build_scripts\windows.ps1
@@ -122,7 +122,7 @@ jobs:
122122
- name: Test OctoBot Binary on Windows
123123
if: matrix.os == 'windows-latest'
124124
run: |
125-
pytest tests
125+
pytest tests --full-trace
126126
127127
- name: Download Linux x64 artifact
128128
if: matrix.os == 'ubuntu-latest'

tests/__init__.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ def delete_folder_if_exists(folder_path):
3737

3838

3939
def clear_octobot_previous_folders():
40-
for folder_path in [
41-
"logs",
42-
"tentacles",
43-
"user"
44-
]:
45-
delete_folder_if_exists(folder_path)
40+
try:
41+
for folder_path in [
42+
"logs",
43+
"tentacles",
44+
"user"
45+
]:
46+
delete_folder_if_exists(folder_path)
47+
except PermissionError:
48+
# Windows file conflict
49+
pass
4650

4751

4852
def get_log_file_content(log_file_path="logs/OctoBot.log"):

tests/test_binary.py

+65-30
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,47 @@
1919
import subprocess
2020
from tempfile import TemporaryFile
2121

22-
import platform
22+
import pytest
2323
import requests
2424
import time
2525

26-
import pytest
27-
2826
from tests import get_binary_file_path, clear_octobot_previous_folders, get_log_file_content, is_on_windows
2927

3028
logger = logging.getLogger()
3129
logger.setLevel(logging.DEBUG)
3230

3331
BINARY_DISABLE_WEB_OPTION = "-nw"
32+
LOG_CHECKS_MAX_ATTEMPTS = 300
3433

3534

3635
@pytest.fixture
3736
def 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
4649
def 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

100136
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
137+
multiple_checks(check_logs_content,
138+
max_attempts=LOG_CHECKS_MAX_ATTEMPTS,
139+
expected_content="ERROR",
140+
should_appear=False)
105141

106142

107143
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
144+
multiple_checks(check_logs_content,
145+
max_attempts=LOG_CHECKS_MAX_ATTEMPTS,
146+
expected_content="BALANCE PROFITABILITY :")

0 commit comments

Comments
 (0)