-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CI] Add binary run tests #22
Open
Herklos
wants to merge
10
commits into
master
Choose a base branch
from
feature/add-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ecc3899
[CI] Add binary run tests
Herklos c65f7c0
[Tests] Fix Windows process killing
Herklos 71ff383
[Tests] Add disable web interface option
Herklos 7d59870
[Tests] Add balance profitability update
Herklos 1ee6651
[Tests] Fix binary path
Herklos 1aa8766
[Tests] Update sleep time to read logs content
Herklos 9176895
[CI] Fix nltk_data env variable usage
b81f3cb
[Tests] Fix windows tests and add timeout management
da2b75b
[CI] Fix test job name
Herklos 1ddf20d
[CI] Fix downloaded assets name
Herklos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,4 +97,7 @@ venv.bak/ | |
# mypy | ||
.mypy_cache/ | ||
|
||
\.idea/ | ||
.idea | ||
tentacles | ||
user | ||
logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pytest | ||
pytest-timeout | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Drakkar-Software OctoBot-Binary | ||
# Copyright (c) Drakkar-Software, All rights reserved. | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 3.0 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. | ||
import os | ||
import platform | ||
import shutil | ||
|
||
|
||
def is_on_windows(): | ||
return platform.system() == "Windows" | ||
|
||
|
||
def get_binary_file_path() -> str: | ||
if is_on_windows(): | ||
return "OctoBot_windows_x64.exe\\OctoBot_windows.exe" | ||
elif platform.system() == "Darwin": | ||
return "./OctoBot_macos_x64/OctoBot_x64" | ||
else: | ||
return "./OctoBot_linux_x64/OctoBot_x64" | ||
|
||
|
||
def delete_folder_if_exists(folder_path): | ||
if os.path.exists(folder_path) and os.path.isdir(folder_path): | ||
shutil.rmtree(folder_path) | ||
|
||
|
||
def clear_octobot_previous_folders(): | ||
try: | ||
for folder_path in [ | ||
"logs", | ||
"tentacles", | ||
"user" | ||
]: | ||
delete_folder_if_exists(folder_path) | ||
except PermissionError: | ||
# Windows file conflict | ||
pass | ||
|
||
|
||
def get_log_file_content(log_file_path="logs/OctoBot.log"): | ||
with open(log_file_path, "r") as log_file: | ||
return log_file.read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# Drakkar-Software OctoBot-Binary | ||
# Copyright (c) Drakkar-Software, All rights reserved. | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 3.0 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. | ||
import logging | ||
import os | ||
import signal | ||
import subprocess | ||
from tempfile import TemporaryFile | ||
|
||
import pytest | ||
import requests | ||
import time | ||
|
||
from tests import get_binary_file_path, clear_octobot_previous_folders, get_log_file_content, is_on_windows | ||
|
||
logger = logging.getLogger() | ||
logger.setLevel(logging.DEBUG) | ||
|
||
BINARY_DISABLE_WEB_OPTION = "-nw" | ||
LOG_CHECKS_MAX_ATTEMPTS = 300 | ||
DEFAULT_TIMEOUT_WINDOW = -95 | ||
|
||
|
||
@pytest.fixture | ||
def start_binary(): | ||
clear_octobot_previous_folders() | ||
with TemporaryFile() as output, TemporaryFile() as err: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
binary_process = start_binary_process("", output, err) | ||
try: | ||
yield | ||
except Exception: | ||
pass | ||
finally: | ||
terminate_binary(binary_process, output, err) | ||
|
||
|
||
@pytest.fixture | ||
def start_binary_without_web_app(): | ||
clear_octobot_previous_folders() | ||
with TemporaryFile() as output, TemporaryFile() as err: | ||
binary_process = start_binary_process(BINARY_DISABLE_WEB_OPTION, output, err) | ||
logger.debug(err.read()) | ||
try: | ||
yield | ||
except Exception: | ||
pass | ||
finally: | ||
terminate_binary(binary_process, output, err) | ||
|
||
|
||
def start_binary_process(binary_options, output_file, err_file): | ||
logger.debug("Starting binary process...") | ||
return subprocess.Popen(f"{get_binary_file_path()}{f' {binary_options}' if binary_options else ''}", | ||
shell=True, | ||
stdout=output_file, | ||
stderr=err_file, | ||
preexec_fn=os.setsid if not is_on_windows() else None) | ||
|
||
|
||
def terminate_binary(binary_process, output_file, err_file): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
try: | ||
logger.info(output_file.read()) | ||
errors = err_file.read() | ||
if errors: | ||
logger.error(errors) | ||
raise ValueError(f"Error happened during process execution : {errors}") | ||
finally: | ||
logger.debug("Killing binary process...") | ||
if is_on_windows(): | ||
subprocess.call(["taskkill", "/F", "/IM", "OctoBot_windows.exe"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
else: | ||
try: | ||
os.killpg(os.getpgid(binary_process.pid), signal.SIGTERM) # Send the signal to all the process groups | ||
except ProcessLookupError: | ||
binary_process.kill() | ||
|
||
|
||
def multiple_checks(check_method, sleep_time=1, max_attempts=10, **kwargs): | ||
attempt = 1 | ||
while max_attempts >= attempt > 0: | ||
try: | ||
result = check_method(**kwargs) | ||
if result: # success | ||
return | ||
except Exception as e: | ||
logger.warning(f"Check ({attempt}/{max_attempts}) failed : {e}") | ||
finally: | ||
attempt += 1 | ||
try: | ||
time.sleep(sleep_time) | ||
except KeyboardInterrupt: | ||
# Fails when windows is stopping binary | ||
pass | ||
assert False # fail | ||
|
||
|
||
def check_endpoint(endpoint_url, expected_code): | ||
try: | ||
result = requests.get(endpoint_url) | ||
return result.status_code == expected_code | ||
except requests.exceptions.ConnectionError: | ||
logger.warning(f"Failed to get {endpoint_url}") | ||
return False | ||
|
||
|
||
def check_logs_content(expected_content: str, should_appear: bool = True): | ||
log_content = get_log_file_content() | ||
logger.debug(log_content) | ||
if should_appear: | ||
return expected_content in log_content | ||
return expected_content not in log_content | ||
|
||
|
||
@pytest.mark.timeout(100 + DEFAULT_TIMEOUT_WINDOW) | ||
def test_terms_endpoint(start_binary): | ||
multiple_checks(check_endpoint, | ||
max_attempts=100, | ||
endpoint_url="http://localhost:5001/terms", | ||
expected_code=200) | ||
|
||
|
||
@pytest.mark.timeout(LOG_CHECKS_MAX_ATTEMPTS + DEFAULT_TIMEOUT_WINDOW) | ||
def test_evaluation_state_created(start_binary_without_web_app): | ||
multiple_checks(check_logs_content, | ||
max_attempts=LOG_CHECKS_MAX_ATTEMPTS, | ||
expected_content="new state:") | ||
|
||
|
||
@pytest.mark.timeout(LOG_CHECKS_MAX_ATTEMPTS + DEFAULT_TIMEOUT_WINDOW) | ||
def test_logs_content_has_no_errors(start_binary_without_web_app): | ||
multiple_checks(check_logs_content, | ||
max_attempts=LOG_CHECKS_MAX_ATTEMPTS, | ||
expected_content="ERROR", | ||
should_appear=False) | ||
|
||
|
||
@pytest.mark.timeout(LOG_CHECKS_MAX_ATTEMPTS + DEFAULT_TIMEOUT_WINDOW) | ||
def test_balance_profitability_updated(start_binary_without_web_app): | ||
multiple_checks(check_logs_content, | ||
max_attempts=LOG_CHECKS_MAX_ATTEMPTS, | ||
expected_content="BALANCE PROFITABILITY :") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍