Deploy with Docker Compose / deploy (push) Successful in 45s
Two admin-only REST endpoints and a Diagnostics tab in the admin panel: - GET /admin/config reports which known environment variables are set, what each is for, and the value only for non-credential vars. Secrets are never returned, only their presence. - GET /admin/logs returns the tail of backend/logs/go.log (Gin traffic plus application lines), bounded to 256 KiB and 500 lines so the read is constant-size however large the file has grown. tailLogFile is split out from the handler and tested for the line cap, the partial-first-line rule, and the empty/missing cases. Co-Authored-By: Claude Code <[email protected]>
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// tailLogFile's rules that are easy to get wrong: the line cap, and dropping
|
|
// the partial first line when the read started mid-file.
|
|
|
|
func writeLog(t *testing.T, content string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "go.log")
|
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
|
t.Fatalf("writing fixture: %v", err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestTailLogFileCapsLines(t *testing.T) {
|
|
var b strings.Builder
|
|
for range logTailLines + 50 {
|
|
b.WriteString("line\n")
|
|
}
|
|
lines, _, err := tailLogFile(writeLog(t, b.String()))
|
|
if err != nil {
|
|
t.Fatalf("tailLogFile: %v", err)
|
|
}
|
|
if len(lines) != logTailLines {
|
|
t.Fatalf("got %d lines, want the cap of %d", len(lines), logTailLines)
|
|
}
|
|
}
|
|
|
|
func TestTailLogFileDropsPartialFirstLine(t *testing.T) {
|
|
// A file larger than the byte window: the read starts mid-file, so the
|
|
// first line it sees is a fragment and must be discarded.
|
|
filler := strings.Repeat("x", 100) + "\n"
|
|
var b strings.Builder
|
|
for b.Len() < logTailBytes+len(filler) {
|
|
b.WriteString(filler)
|
|
}
|
|
b.WriteString("FINAL\n")
|
|
|
|
lines, _, err := tailLogFile(writeLog(t, b.String()))
|
|
if err != nil {
|
|
t.Fatalf("tailLogFile: %v", err)
|
|
}
|
|
if len(lines) == 0 {
|
|
t.Fatal("expected some lines")
|
|
}
|
|
if lines[len(lines)-1] != "FINAL" {
|
|
t.Fatalf("last line = %q, want FINAL", lines[len(lines)-1])
|
|
}
|
|
// Every surviving line must be whole, i.e. a full filler line.
|
|
if got := lines[0]; got != strings.TrimSuffix(filler, "\n") {
|
|
t.Fatalf("first line = %q, want a complete filler line", got)
|
|
}
|
|
}
|
|
|
|
func TestTailLogFileEmpty(t *testing.T) {
|
|
lines, size, err := tailLogFile(writeLog(t, ""))
|
|
if err != nil {
|
|
t.Fatalf("tailLogFile: %v", err)
|
|
}
|
|
if len(lines) != 0 || size != 0 {
|
|
t.Fatalf("got %d lines / %d bytes, want 0 / 0", len(lines), size)
|
|
}
|
|
}
|
|
|
|
func TestTailLogFileMissing(t *testing.T) {
|
|
if _, _, err := tailLogFile(filepath.Join(t.TempDir(), "absent.log")); err == nil {
|
|
t.Fatal("expected an error for a missing log file")
|
|
}
|
|
}
|