diff --git a/paas_app_charmer/_gunicorn/logrotate.py b/paas_app_charmer/_gunicorn/logrotate.py index b009137..548b315 100644 --- a/paas_app_charmer/_gunicorn/logrotate.py +++ b/paas_app_charmer/_gunicorn/logrotate.py @@ -7,10 +7,13 @@ It will be injected into the container and does not depend on non-standard libraries. """ + import argparse import datetime import glob import os + +# this script runs similarly to a shell script, disable the warning for using subprocess import subprocess # nosec B404 import time diff --git a/tests/unit/flask/test_charm.py b/tests/unit/flask/test_charm.py index 4970d9e..a6234a2 100644 --- a/tests/unit/flask/test_charm.py +++ b/tests/unit/flask/test_charm.py @@ -97,9 +97,24 @@ def test_log_rotate(tmp_path): files = list(tmp_path.iterdir()) assert len(files) == 1 archive = files[0] + # if the filename doesn't match the pattern, a ValueError will be raised here datetime.datetime.strptime(archive.name, "access-%Y-%m-%d-%H-%M-%S.log") +def test_log_rotate_not_rotated(tmp_path): + """ + arrange: create a small log file + act: run the log rotate function + assert: the log file should not be rotated + """ + log_file = tmp_path / "access.log" + log_file.touch() + assert not logrotate.rotate(str(log_file.absolute())) + assert log_file.exists() + files = list(tmp_path.iterdir()) + assert len(files) == 1 + + def test_log_rotate_cleanup(tmp_path): """ arrange: create a log file larger than the max log file size, along with many archive files