-
Notifications
You must be signed in to change notification settings - Fork 2
Fixed env variable LOG_ENV_VARIABLE_NAME #360
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9c7c417
added test cases, moved existing tests, fixed directory bug
MarleneKress79789 7c4bf34
fix test cleanup
MarleneKress79789 c7c61b4
[run all tests]
MarleneKress79789 8952058
[run all tests] changes from code review
MarleneKress79789 2d17e0f
[run all tests] added matcher for env var tests
MarleneKress79789 7fbbbb9
[run all tests] updated matcher
MarleneKress79789 2794d66
Apply suggestions from code review
MarleneKress79789 a8cbacd
[run all tests]
MarleneKress79789 bcc8aef
[run all tests]
MarleneKress79789 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 hidden or 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 hidden or 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 hidden or 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
94 changes: 0 additions & 94 deletions
94
exasol_integration_test_docker_environment/test/test_common_run_task_subprocess.py
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,214 @@ | ||
import pytest | ||
import os.path | ||
from pathlib import Path | ||
from unittest import mock | ||
|
||
import luigi | ||
|
||
from exasol_integration_test_docker_environment.lib.base.luigi_log_config import LOG_ENV_VARIABLE_NAME, get_log_path | ||
from exasol_integration_test_docker_environment.lib.api.common import generate_root_task, run_task | ||
from exasol_integration_test_docker_environment.lib.base.dependency_logger_base_task import DependencyLoggerBaseTask | ||
from exasol_integration_test_docker_environment.lib.config.build_config import build_config | ||
|
||
|
||
@pytest.fixture | ||
def set_tempdir(tmp_path): | ||
path_old = os.getcwd() | ||
os.chdir(tmp_path) | ||
yield tmp_path | ||
os.chdir(path_old) | ||
|
||
|
||
@pytest.fixture() | ||
def mock_settings_env_vars(): | ||
with mock.patch.dict(os.environ, {}): | ||
yield | ||
|
||
|
||
class UsedLogPath: | ||
def __init__(self, task): | ||
self.log_path = Path(task["log_path"]) | ||
self.task_input_parameter = task["in_parameter"] | ||
|
||
def __repr__(self): | ||
return f"log path: {str(self.log_path)}" \ | ||
f"\nlog content: '{self.log_path.read_text()}'" | ||
|
||
|
||
class LogPathCorrectnessMatcher: | ||
"""Assert that a given path meets some expectations.""" | ||
|
||
def __init__(self, expected_log_path: Path): | ||
self.expected_log_path = expected_log_path | ||
|
||
def __eq__(self, used_log_path: UsedLogPath): | ||
if not isinstance(used_log_path, UsedLogPath): | ||
return False | ||
log_path = used_log_path.log_path | ||
if not (log_path == self.expected_log_path | ||
and log_path.exists() | ||
and log_path.is_file()): | ||
return False | ||
|
||
log_content = log_path.read_text() | ||
return f"Logging: {used_log_path.task_input_parameter}" in log_content | ||
|
||
def __repr__(self): | ||
return f"log path: {str(self.expected_log_path)}" \ | ||
f"\nlog content: '.*Logging: {self.task_input_parameter}.*'" | ||
|
||
|
||
def default_log_path(job_id): | ||
return Path(build_config().output_directory) / "jobs" / job_id / "logs" / "main.log" | ||
|
||
|
||
class TestTask(DependencyLoggerBaseTask): | ||
x = luigi.Parameter() | ||
|
||
def run_task(self): | ||
self.logger.info(f"Logging: {self.x}") | ||
self.return_object({"job_id": self.job_id, "parameter": self.x}) | ||
|
||
|
||
def run_n_simple_tasks(task_number): | ||
NUMBER_TASK = task_number | ||
task_id_generator = (x for x in range(NUMBER_TASK)) | ||
tasks = [] | ||
|
||
def create_task(): | ||
return generate_root_task(task_class=TestTask, x=f"{next(task_id_generator)}") | ||
|
||
for j in range(NUMBER_TASK): | ||
output = run_task(create_task, workers=5, task_dependencies_dot_file=None, use_job_specific_log_file=True) | ||
jobid = output["job_id"] | ||
log_path = get_log_path(jobid) | ||
tasks.append({"jobid": jobid, "log_path": log_path, "in_parameter": output["parameter"]}) | ||
|
||
return tasks | ||
|
||
MarleneKress79789 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def use_specific_log_file(task_creator, temp_dir, test_name): | ||
MarleneKress79789 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log_path = Path(temp_dir) / (test_name + ".log") | ||
os.environ[LOG_ENV_VARIABLE_NAME] = str(log_path) | ||
run_task(task_creator, workers=5, task_dependencies_dot_file=None, use_job_specific_log_file=True) | ||
return log_path | ||
|
||
|
||
def test_var_not_set(set_tempdir): | ||
""" | ||
Integration test which verifies that not setting the env var LOG_ENV_VARIABLE_NAME works and uses the | ||
default log path | ||
""" | ||
tasks = run_n_simple_tasks(1) | ||
|
||
log_path_matcher = LogPathCorrectnessMatcher(default_log_path(tasks[0]["jobid"])) | ||
log_path = UsedLogPath(tasks[0]) | ||
assert log_path == log_path_matcher | ||
|
||
|
||
def test_var_not_set_same_logging_file(set_tempdir): | ||
""" | ||
Integration test which verifies that re-using the same logging for multiple tasks works as expected, | ||
using the default log path | ||
""" | ||
tasks = run_n_simple_tasks(5) | ||
for task in tasks: | ||
log_path_matcher = LogPathCorrectnessMatcher(default_log_path(task["jobid"])) | ||
log_path = UsedLogPath(task) | ||
assert log_path == log_path_matcher | ||
|
||
|
||
def test_custom_log_path_points_at_file(set_tempdir, mock_settings_env_vars): | ||
""" | ||
Integration test which verifies that using a custom log path for a single task works if the path points to a file | ||
""" | ||
temp_dir = set_tempdir | ||
custom_log_path = Path(temp_dir) / "main.log" | ||
log_path_matcher = LogPathCorrectnessMatcher(custom_log_path) | ||
os.environ[LOG_ENV_VARIABLE_NAME] = str(custom_log_path) | ||
|
||
tasks = run_n_simple_tasks(1) | ||
|
||
log_path = UsedLogPath(tasks[0]) | ||
assert log_path == log_path_matcher | ||
|
||
|
||
def test_preexisting_custom_log_file(set_tempdir, mock_settings_env_vars): | ||
""" | ||
Integration test which verifies that using a custom log path for a single task works if file already exists. | ||
""" | ||
temp_dir = set_tempdir | ||
custom_log_path = Path(temp_dir) / "main.log" | ||
log_path_matcher = LogPathCorrectnessMatcher(custom_log_path) | ||
os.environ[LOG_ENV_VARIABLE_NAME] = str(custom_log_path) | ||
file_content = "This existing file has content." | ||
with open(custom_log_path, "a") as f: | ||
f.write(file_content) | ||
|
||
tasks = run_n_simple_tasks(1) | ||
|
||
log_path = UsedLogPath(tasks[0]) | ||
assert log_path == log_path_matcher | ||
|
||
with open(custom_log_path, "r") as f: | ||
log_content = f.read() | ||
assert file_content in log_content | ||
|
||
|
||
def test_same_logging_file_custom_log_path(set_tempdir, mock_settings_env_vars): | ||
""" | ||
Integration test which verifies that re-using the same log file for multiple tasks works as expected, | ||
using a custom log path | ||
""" | ||
temp_dir = set_tempdir | ||
custom_log_path = Path(temp_dir) / "main.log" | ||
log_path_matcher = LogPathCorrectnessMatcher(custom_log_path) | ||
os.environ[LOG_ENV_VARIABLE_NAME] = str(custom_log_path) | ||
tasks = run_n_simple_tasks(5) | ||
|
||
for task in tasks: | ||
log_path = UsedLogPath(task) | ||
assert log_path == log_path_matcher | ||
|
||
|
||
def test_different_custom_logging_file(set_tempdir, mock_settings_env_vars): | ||
""" | ||
Integration test which verifies that changing the log path from one invocation of run_task to the next will work | ||
""" | ||
temp_dir = set_tempdir | ||
task_creator = lambda: generate_root_task(task_class=TestTask, x="Test") | ||
|
||
log_path_1 = use_specific_log_file(task_creator, temp_dir, "first") | ||
log_path_2 = use_specific_log_file(task_creator, temp_dir, "second") | ||
|
||
for log_path in [log_path_1, log_path_2]: | ||
assert log_path.exists() | ||
with open(log_path, "r") as f: | ||
tkilias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log_content = f.read() | ||
assert f"Logging: Test" in log_content | ||
|
||
|
||
def test_custom_log_path_points_at_dir(set_tempdir, mock_settings_env_vars): | ||
""" | ||
Integration test which verifies that using a custom log path for a single task fails if the path points to a directory instead of a file | ||
""" | ||
temp_dir = set_tempdir | ||
custom_log_path = Path(temp_dir) | ||
os.environ[LOG_ENV_VARIABLE_NAME] = str(custom_log_path) | ||
|
||
with pytest.raises(IsADirectoryError): | ||
run_n_simple_tasks(1) | ||
|
||
|
||
def test_missing_dir_in_custom_log_path(set_tempdir, mock_settings_env_vars): | ||
""" | ||
Integration test which verifies that all not preexisting dirs in a custom log path are created correctly. | ||
""" | ||
temp_dir = set_tempdir | ||
custom_log_path = Path(temp_dir) / "another_dir" / "main.log" | ||
log_path_matcher = LogPathCorrectnessMatcher(custom_log_path) | ||
os.environ[LOG_ENV_VARIABLE_NAME] = str(custom_log_path) | ||
tasks = run_n_simple_tasks(1) | ||
|
||
log_path = UsedLogPath(tasks[0]) | ||
assert log_path == log_path_matcher |
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.
Uh oh!
There was an error while loading. Please reload this page.