diff --git a/SABnzbd.py b/SABnzbd.py index f641dad..5d41f8b 100755 --- a/SABnzbd.py +++ b/SABnzbd.py @@ -61,7 +61,7 @@ import sabnzbd.newsunpack from sabnzbd.misc import check_latest_version, exit_sab, \ split_host, create_https_certificates, windows_variant, ip_extract, \ set_serv_parms, get_serv_parms, get_from_url -from sabnzbd.filesystem import get_ext, real_path, long_path, globber_full +from sabnzbd.filesystem import get_ext, real_path, long_path, globber_full, remove_file from sabnzbd.panic import panic_tmpl, panic_port, panic_host, panic, launch_a_browser import sabnzbd.scheduler as scheduler import sabnzbd.config as config @@ -221,8 +221,12 @@ def daemonize(): sys.stdout.flush() sys.stderr.flush() - # Replace file descriptors for stdin, stdout, and stderr + # Get log file path and remove the log file if it got too large log_path = os.path.join(sabnzbd.cfg.log_dir.get_path(), DEF_LOG_ERRFILE) + if os.path.exists(log_path) and os.path.getsize(log_path) > sabnzbd.cfg.log_size.get_int(): + remove_file(log_path) + + # Replace file descriptors for stdin, stdout, and stderr with open('/dev/null', 'rb', 0) as f: os.dup2(f.fileno(), sys.stdin.fileno()) with open(log_path, 'ab', 0) as f: diff --git a/tests/conftest.py b/tests/conftest.py index a1d0aa5..b1b19b4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -33,7 +33,7 @@ def start_sabnzbd(): shutil.rmtree(SAB_CACHE_DIR) # Copy basic config file with API key - os.mkdir(SAB_CACHE_DIR) + os.makedirs(SAB_CACHE_DIR, exist_ok=True) shutil.copyfile(os.path.join(SAB_BASE_DIR, "sabnzbd.basic.ini"), os.path.join(SAB_CACHE_DIR, "sabnzbd.ini")) # Check if we have language files diff --git a/tests/test_functional_misc.py b/tests/test_functional_misc.py index 993e900..a6e7537 100644 --- a/tests/test_functional_misc.py +++ b/tests/test_functional_misc.py @@ -18,9 +18,9 @@ """ tests.test_functional_misc - Functional tests of various functions """ - import sys import subprocess +import shutil import sabnzbd.encoding from tests.testhelper import * @@ -102,14 +102,26 @@ class TestExtractPot: @pytest.mark.skipif(sys.platform.startswith("win"), reason="Skipping on Windows") class TestDaemonizing(SABnzbdBaseTest): - def test_daemonizing_basic(self): - """ Simple test to see if daemon-mode still works + def test_daemonizing(self): + """ Simple test to see if daemon-mode still works. + Also test removal of large "sabnzbd.error.log" We inherit from SABnzbdBaseTest so we can use it's clean-up logic! """ daemon_host = "localhost" daemon_port = 23456 ini_location = os.path.join(SAB_CACHE_DIR, "daemon_test") + # Create large output-file + error_log_path = os.path.join(ini_location, sabnzbd.cfg.log_dir(), sabnzbd.constants.DEF_LOG_ERRFILE) + os.makedirs(os.path.dirname(error_log_path), exist_ok=True) + with open(error_log_path, "wb") as large_log: + large_log.seek(6 * 1024 * 1024) + large_log.write(b"\1") + + # We need the basic-config to set the API-key + # Otherwise we can't shut it down at the end + shutil.copyfile(os.path.join(SAB_BASE_DIR, "sabnzbd.basic.ini"), os.path.join(ini_location, "sabnzbd.ini")) + # Combine it all into the script call script_call = [ sys.executable, @@ -139,6 +151,10 @@ class TestDaemonizing(SABnzbdBaseTest): pid_file = os.path.join(ini_location, "sabnzbd-%d.pid" % daemon_port) assert os.path.exists(pid_file) + # Did it remove the bad log file? + assert os.path.exists(error_log_path) + assert os.path.getsize(error_log_path) < 1024 + # Let's shut it down and give it some time to do so - get_url_result("shutdown") + get_url_result("shutdown", daemon_host, daemon_port) time.sleep(3.0)