# -*- coding: utf-8 -*- """测试保留期清理:回填一条 40 天前的运行日志 + 一个 40 天前的设备壳日志后执行 cleanup_expired。""" import os import sys sys.path.insert(0, r"D:\work\code\37duAutomation\desktop") import pymysql from controlplane.core import config, logstore c = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456", database="37du_center_test") with c.cursor() as cur: cur.execute( "INSERT INTO run_logs(run_id, account, device_id, plugin_id, name, version, `trigger`, " "schedule_id, status, ok, error, summary, started, finished, duration_sec, log_count, " "logs, uploaded_at) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s," "DATE_SUB(NOW(), INTERVAL 40 DAY))", ("oldrun0000000000000000000000000001", "olduser", "dev-old-1", "XQ1", "老插件", "1.0", "manual", "", "error", 0, "x", "y", 0, 0, 0, 0, "[]")) cur.execute( "INSERT INTO device_logs(device_id, account, hostname, client_version, size_bytes, " "updated_at) VALUES (%s,%s,%s,%s,%s, DATE_SUB(NOW(), INTERVAL 40 DAY))", ("dev-old-1", "olduser", "OLD-PC", "1.0", 10)) c.commit() c.close() config.SHELL_LOGS_DIR.mkdir(parents=True, exist_ok=True) f = os.path.join(config.SHELL_LOGS_DIR, "dev-old-1.log") with open(f, "wb") as fh: fh.write(b"old content\n") r = logstore.cleanup_expired() print("cleanup result:", r) print("file exists after cleanup:", os.path.exists(f)) c = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456", database="37du_center_test") with c.cursor() as cur: cur.execute("SELECT COUNT(*) FROM run_logs WHERE run_id LIKE %s", ("oldrun%",)) print("old run_logs left:", cur.fetchone()[0]) cur.execute("SELECT COUNT(*) FROM device_logs WHERE device_id = %s", ("dev-old-1",)) print("old device_logs left:", cur.fetchone()[0]) c.close() print("CLEANUP OK")