"""Unit: what the hourly cleanup prunes (#5155).""" import os from datetime import datetime, timedelta, timezone from steward.core import cleanup def test_run_logs_older_than_the_window_are_pruned(tmp_path): now = datetime.now(timezone.utc) old, new, other = tmp_path / "old.log", tmp_path / "new.log", tmp_path / "notes.txt" for f in (old, new, other): f.write_text("x") aged = (now - timedelta(days=100)).timestamp() os.utime(old, (aged, aged)) os.utime(other, (aged, aged)) assert cleanup.prune_run_logs(str(tmp_path), now - timedelta(days=90)) == 1 assert sorted(p.name for p in tmp_path.iterdir()) == ["new.log", "notes.txt"] def test_no_run_log_directory_prunes_nothing(tmp_path): assert cleanup.prune_run_logs(str(tmp_path / "missing"), datetime.now(timezone.utc)) == 0 def test_core_time_series_are_on_the_data_window(): from steward.models.alerts import AlertEvent from steward.models.ansible import AnsibleRun from steward.models.audit import AuditEvent from steward.models.monitors import MonitorResult tables = {c.class_.__tablename__ for c in cleanup._AGED} assert {MonitorResult.__tablename__, AnsibleRun.__tablename__, AlertEvent.__tablename__} <= tables # The audit log has its own, longer window, not the data one. assert AuditEvent.__tablename__ not in tables def test_a_column_registers_once(monkeypatch): from steward.models.monitors import MonitorResult monkeypatch.setattr(cleanup, "_AGED", []) cleanup.prune_by_age(MonitorResult.checked_at) cleanup.prune_by_age(MonitorResult.checked_at) # a plugin set up twice assert len(cleanup._AGED) == 1