-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to stabilize failing integration tests (#496)
* Attempt to stabilize failing integration tests * Run format * Address PR feedback + move log rotation test to separate file
- Loading branch information
1 parent
1abc450
commit 335674c
Showing
4 changed files
with
156 additions
and
108 deletions.
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
127 changes: 127 additions & 0 deletions
127
tests/integration/high_availability/test_log_rotation.py
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,127 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
import logging | ||
from pathlib import Path | ||
|
||
import pytest | ||
import yaml | ||
from pytest_operator.plugin import OpsTest | ||
|
||
from ..helpers import ( | ||
delete_file_or_directory_in_unit, | ||
dispatch_custom_event_for_logrotate, | ||
ls_in_unit, | ||
read_contents_from_file_in_unit, | ||
stop_running_flush_mysql_job, | ||
stop_running_log_rotate_dispatcher, | ||
write_content_to_file_in_unit, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
METADATA = yaml.safe_load(Path("./metadata.yaml").read_text()) | ||
APP_NAME = METADATA["name"] | ||
|
||
|
||
@pytest.mark.group(1) | ||
@pytest.mark.abort_on_fail | ||
async def test_log_rotation( | ||
ops_test: OpsTest, highly_available_cluster, continuous_writes | ||
) -> None: | ||
"""Test the log rotation of text files. | ||
Run continuous writes to ensure that audit log plugin is loaded and active | ||
when mysql-test-app runs start-continuous-writes (by logging into mysql). | ||
""" | ||
unit = ops_test.model.applications[APP_NAME].units[0] | ||
|
||
logger.info("Extending update-status-hook-interval to 60m") | ||
await ops_test.model.set_config({"update-status-hook-interval": "60m"}) | ||
|
||
# Exclude slowquery log files as slowquery logs are not enabled by default | ||
log_types = ["error", "general", "audit"] | ||
log_files = ["error.log", "general.log", "audit.log"] | ||
archive_directories = [ | ||
"archive_error", | ||
"archive_general", | ||
"archive_slowquery", | ||
"archive_audit", | ||
] | ||
|
||
logger.info("Overwriting the log rotate dispatcher script") | ||
unit_label = unit.name.replace("/", "-") | ||
await write_content_to_file_in_unit( | ||
ops_test, | ||
unit, | ||
f"/var/lib/juju/agents/unit-{unit_label}/charm/scripts/log_rotate_dispatcher.py", | ||
"exit(0)\n", | ||
container_name="charm", | ||
) | ||
|
||
logger.info("Stopping the log rotate dispatcher") | ||
await stop_running_log_rotate_dispatcher(ops_test, unit.name) | ||
|
||
logger.info("Stopping any running logrotate jobs") | ||
await stop_running_flush_mysql_job(ops_test, unit.name) | ||
|
||
logger.info("Removing existing archive directories") | ||
for archive_directory in archive_directories: | ||
await delete_file_or_directory_in_unit( | ||
ops_test, | ||
unit.name, | ||
f"/var/log/mysql/{archive_directory}/", | ||
) | ||
|
||
logger.info("Writing some data to the text log files") | ||
for log in log_types: | ||
log_path = f"/var/log/mysql/{log}.log" | ||
await write_content_to_file_in_unit(ops_test, unit, log_path, f"test {log} content\n") | ||
|
||
logger.info("Ensuring only log files exist") | ||
# Exclude archive directories, as handling any event would restart the | ||
# log_rotate_dispatcher (by the log_rotate_manager) | ||
ls_output = await ls_in_unit( | ||
ops_test, unit.name, "/var/log/mysql/", exclude_files=archive_directories | ||
) | ||
|
||
for file in log_files: | ||
# audit.log can be rotated and new file not created until access to db | ||
assert ( | ||
file in ls_output or file == "audit.log" | ||
), f"❌ files other than log files exist {ls_output}" | ||
|
||
logger.info("Dispatching custom event to rotate logs") | ||
await dispatch_custom_event_for_logrotate(ops_test, unit.name) | ||
|
||
logger.info("Ensuring log files and archive directories exist") | ||
ls_output = await ls_in_unit(ops_test, unit.name, "/var/log/mysql/") | ||
|
||
for file in log_files + archive_directories: | ||
# audit.log can be rotated and new file not created until access to db | ||
assert ( | ||
file in ls_output or file == "audit.log" | ||
), f"❌ unexpected files/directories in log directory: {ls_output}" | ||
|
||
logger.info("Ensuring log files were rotated") | ||
# Exclude checking slowquery log rotation as slowquery logs are disabled by default | ||
for log in set(log_types): | ||
file_contents = await read_contents_from_file_in_unit( | ||
ops_test, unit, f"/var/log/mysql/{log}.log" | ||
) | ||
assert f"test {log} content" not in file_contents, f"❌ log file {log}.log not rotated" | ||
|
||
ls_output = await ls_in_unit(ops_test, unit.name, f"/var/log/mysql/archive_{log}/") | ||
assert len(ls_output) != 0, f"❌ archive directory is empty: {ls_output}" | ||
|
||
rotated_file_content_exists = False | ||
for filename in ls_output: | ||
file_contents = await read_contents_from_file_in_unit( | ||
ops_test, | ||
unit, | ||
f"/var/log/mysql/archive_{log}/{filename}", | ||
) | ||
if f"test {log} content" in file_contents: | ||
rotated_file_content_exists = True | ||
assert rotated_file_content_exists, f"❌ log file {log}.log not rotated" |
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