Files
3x-ui-web-src/internal/web/job/clear_logs_job_test.go
T
Sanaei f727d04f65
CI / go-test (push) Canceled after 0s
CI / postgres-durable-first (push) Canceled after 0s
CI / codegen (push) Canceled after 0s
CI / govulncheck (push) Canceled after 0s
CI / race (push) Canceled after 0s
CI / fuzz-smoke (push) Canceled after 0s
CI / golangci (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CodeQL Advanced / Analyze (go) (push) Canceled after 0s
CodeQL Advanced / Analyze (actions) (push) Canceled after 0s
CodeQL Advanced / Analyze (javascript-typescript) (push) Canceled after 0s
Docs CI / build (push) Canceled after 0s
Docs Deploy (GitHub Pages) / build (push) Canceled after 0s
Release 3X-UI / build (386) (push) Canceled after 0s
Release 3X-UI / build (amd64) (push) Canceled after 0s
Release 3X-UI / build (arm64) (push) Canceled after 0s
Release 3X-UI / build (armv5) (push) Canceled after 0s
Release 3X-UI / build (armv6) (push) Canceled after 0s
Release 3X-UI / build (armv7) (push) Canceled after 0s
Release 3X-UI / build (s390x) (push) Canceled after 0s
Release 3X-UI / Build for Windows (amd64) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-latest) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-latest) (push) Canceled after 0s
Docs Deploy (GitHub Pages) / deploy (push) Canceled after 0s
Release 3X-UI / Publish rolling dev release (push) Canceled after 0s
v3.7.0
2026-08-24 15:07:15 +02:00

93 lines
2.6 KiB
Go

package job
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
func writeLogConfig(t *testing.T, accessPath string, errorPath string) {
t.Helper()
binDir := t.TempDir()
t.Setenv("XUI_BIN_FOLDER", binDir)
configData, err := json.Marshal(map[string]any{
"log": map[string]any{"access": accessPath, "error": errorPath},
})
if err != nil {
t.Fatalf("marshal xray config: %v", err)
}
if err := os.WriteFile(filepath.Join(binDir, "config.json"), configData, 0o644); err != nil {
t.Fatalf("write xray config: %v", err)
}
}
func TestWipeXrayLogs_TruncatesEnabledLogs(t *testing.T) {
accessLog := filepath.Join(t.TempDir(), "access.log")
errorLog := filepath.Join(t.TempDir(), "error.log")
if err := os.WriteFile(accessLog, []byte("2026/06/23 12:00:00 from tcp:203.0.113.10:443 accepted\n"), 0o644); err != nil {
t.Fatalf("seed access log: %v", err)
}
if err := os.WriteFile(errorLog, []byte("xray warning\n"), 0o644); err != nil {
t.Fatalf("seed error log: %v", err)
}
writeLogConfig(t, accessLog, errorLog)
wipeXrayLogs()
for _, logPath := range []string{accessLog, errorLog} {
info, err := os.Stat(logPath)
if err != nil {
t.Fatalf("%s should still exist: %v", logPath, err)
}
if info.Size() != 0 {
t.Fatalf("%s should be truncated to 0, got %d bytes", logPath, info.Size())
}
}
}
func TestWipeXrayLogs_LeavesDisabledLogsAlone(t *testing.T) {
writeLogConfig(t, "none", "none")
wipeXrayLogs()
if _, err := os.Stat("none"); err == nil {
os.Remove("none")
t.Fatal(`wipeXrayLogs must not create a file named "none"`)
}
}
func TestPruneXrayLogs_TruncatesOnlyOversizedLogs(t *testing.T) {
oldMax := maxXrayLogBytes
maxXrayLogBytes = 8
defer func() { maxXrayLogBytes = oldMax }()
dir := t.TempDir()
accessLog := filepath.Join(dir, "access.log")
errorLog := filepath.Join(dir, "error.log")
if err := os.WriteFile(accessLog, []byte("small"), 0o644); err != nil {
t.Fatalf("seed access log: %v", err)
}
if err := os.WriteFile(errorLog, []byte("large log line"), 0o644); err != nil {
t.Fatalf("seed error log: %v", err)
}
writeLogConfig(t, accessLog, errorLog)
NewPruneXrayLogsJob().Run()
accessInfo, err := os.Stat(accessLog)
if err != nil {
t.Fatalf("access log should still exist: %v", err)
}
if accessInfo.Size() != 5 {
t.Fatalf("small access log should be left alone, got %d bytes", accessInfo.Size())
}
errorInfo, err := os.Stat(errorLog)
if err != nil {
t.Fatalf("error log should still exist: %v", err)
}
if errorInfo.Size() != 0 {
t.Fatalf("oversized error log should be truncated, got %d bytes", errorInfo.Size())
}
}