gitea, notion webhook
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

63 lines
1.8 KiB

package logging
import (
"os"
"path/filepath"
"testing"
"time"
)
// 오래된 날짜 파일은 지우고 최근 retention일치만 남기는지 확인.
func TestDailyWriterCleanup(t *testing.T) {
dir := t.TempDir()
now := time.Date(2026, 6, 11, 10, 0, 0, 0, time.UTC)
// 14일치 더미 파일 생성 (app-YYYY-MM-DD.log)
for i := 0; i < 14; i++ {
d := now.AddDate(0, 0, -i).Format("2006-01-02")
if err := os.WriteFile(filepath.Join(dir, "app-"+d+".log"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
}
// 우리 형식이 아닌 파일은 보존돼야 함
keep := filepath.Join(dir, "other.txt")
_ = os.WriteFile(keep, []byte("x"), 0o644)
w := &dailyWriter{dir: dir, prefix: "app", ext: ".log", retention: 7}
w.cleanup(now)
// 오늘 포함 7일치(today..today-6)만 남아야 함
for i := 0; i < 14; i++ {
d := now.AddDate(0, 0, -i).Format("2006-01-02")
_, err := os.Stat(filepath.Join(dir, "app-"+d+".log"))
if i < 7 && err != nil {
t.Errorf("day-%d (%s) 가 삭제됨 — 보존돼야 함", i, d)
}
if i >= 7 && err == nil {
t.Errorf("day-%d (%s) 가 남아있음 — 삭제돼야 함", i, d)
}
}
if _, err := os.Stat(keep); err != nil {
t.Errorf("형식이 다른 파일 other.txt 가 삭제됨 — 보존돼야 함")
}
}
// Write가 날짜 파일을 만들고 거기에 기록하는지 확인.
func TestDailyWriterWrite(t *testing.T) {
dir := t.TempDir()
w, err := newDailyWriter(filepath.Join(dir, "app.log"), 7)
if err != nil {
t.Fatal(err)
}
if _, err := w.Write([]byte("hello\n")); err != nil {
t.Fatal(err)
}
today := time.Now().Format("2006-01-02")
data, err := os.ReadFile(filepath.Join(dir, "app-"+today+".log"))
if err != nil {
t.Fatalf("오늘 날짜 파일이 없음: %v", err)
}
if string(data) != "hello\n" {
t.Errorf("기록 내용 불일치: %q", string(data))
}
}