Files
magpie/internal/usage/log_unreadable_test.go
T
yetone d5f6159109 usage: a test that a failed stat keeps the log's last read (f2039c13f)
f2039c13f kept the usage ledger's last good read when the log's stat
failed but for absence (@simonzyzy on X), and shipped with no test.
TestUnreadableLogKeepsItsLastRead takes search permission off the log's
folder, so the stat fails with EACCES, and checks that the last read
stands. It also checks that a log removed for real drops it.

Run: on mote, the new test -race -count=3 ok, and ./internal/usage ok
(37.7s), go vet ok, gofmt clean. On Linux (wsl-run, magpie-desktop),
-v shows the test run and pass, not skipped.
Red on the old code: with f2039c13f^'s log_index.go, it fails with
"a failed stat replaced the last read" (an empty snapshot, info nil).
Not tried: Windows. The test is !windows because its stat goes through
CreateFile there, and folder permissions don't fail it the same way.
2026-11-11 09:32:03 +08:00

47 lines
1.3 KiB
Go

//go:build !windows
package usage
import (
"os"
"path/filepath"
"testing"
)
// A stat of the log that fails for any reason but its absence (a busy
// disk, a share that blinked; here a folder without search permission)
// says nothing of the log: the last good read stands, and the ledger
// doesn't read empty until the next good stat (f2039c13f, @simonzyzy).
// Once the log is gone for real, the read is dropped.
func TestUnreadableLogKeepsItsLastRead(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("root reads through folder permissions")
}
pageHome(t)
historyLog(t, 3)
warm := readLogSnapshot()
if warm == nil || warm.info == nil || len(warm.blocks) == 0 {
t.Fatalf("fixture not read: %+v", warm)
}
dir := filepath.Dir(Path())
if err := os.Chmod(dir, 0); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Chmod(dir, 0700) })
if _, err := statLogFile(Path()); err == nil || os.IsNotExist(err) {
t.Fatalf("fixture: stat said %v, not a failure other than absence", err)
}
if got := readLogSnapshot(); got != warm {
t.Fatalf("a failed stat replaced the last read: %+v", got)
}
if err := os.Chmod(dir, 0700); err != nil {
t.Fatal(err)
}
if err := os.Remove(Path()); err != nil {
t.Fatal(err)
}
if got := readLogSnapshot(); got == warm || len(got.blocks) != 0 {
t.Fatalf("a removed log kept its last read: %+v", got)
}
}