c9cabbb5fa
Test / Create distribution (push) Successful in 1m3s
Test / Sandbox (push) Successful in 2m49s
Test / Hakurei (push) Successful in 3m49s
Test / ShareFS (push) Successful in 3m43s
Test / Sandbox (race detector) (push) Successful in 5m22s
Test / Hakurei (race detector) (push) Successful in 6m32s
Test / Flake checks (push) Successful in 1m17s
This is useful for reporting errors from processes that must never terminate. Signed-off-by: Ophestra <cat@gensokyo.uk>
169 lines
4.0 KiB
Go
169 lines
4.0 KiB
Go
package report_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"errors"
|
|
"log"
|
|
"os"
|
|
"reflect"
|
|
"slices"
|
|
"testing"
|
|
|
|
"hakurei.app/check"
|
|
"hakurei.app/internal/report"
|
|
"hakurei.app/internal/stub"
|
|
)
|
|
|
|
func init() { gob.Register(stub.UniqueError(0)) }
|
|
|
|
func TestDispatch(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
flag uint64
|
|
errs []report.Error
|
|
|
|
persist bool
|
|
wantLog string
|
|
wantPanic error
|
|
}{
|
|
{"default", 0, []report.Error{
|
|
{
|
|
Severity: report.Fatal,
|
|
Message: "rejecting coldboot loop",
|
|
Err: stub.UniqueError(0xcafe),
|
|
},
|
|
}, true, "dispatch: rejecting coldboot loop: unique error 51966 injected by the test suite\n", nil},
|
|
|
|
{"strict", report.DStrict, []report.Error{
|
|
{
|
|
Severity: report.Fatal,
|
|
Message: "rejecting coldboot loop",
|
|
Err: stub.UniqueError(0xcafe),
|
|
},
|
|
}, true, "dispatch: rejecting coldboot loop: unique error 51966 injected by the test suite\n", report.Error{
|
|
Severity: report.Fatal,
|
|
Message: "rejecting coldboot loop",
|
|
Err: stub.UniqueError(0xcafe),
|
|
}},
|
|
|
|
{"early", 0, []report.Error{
|
|
{
|
|
Severity: report.Fatal,
|
|
Message: "rejecting coldboot loop",
|
|
Err: stub.UniqueError(0xcafe),
|
|
},
|
|
}, false, "dispatch: rejecting coldboot loop: unique error 51966 injected by the test suite\n", report.Error{
|
|
Severity: report.Fatal,
|
|
Message: "rejecting coldboot loop",
|
|
Err: stub.UniqueError(0xcafe),
|
|
}},
|
|
|
|
{"bypass early", report.DBypassEarly, []report.Error{
|
|
{
|
|
Severity: report.Fatal,
|
|
Message: "rejecting coldboot loop",
|
|
Err: stub.UniqueError(0xcafe),
|
|
},
|
|
}, false, "dispatch: rejecting coldboot loop: unique error 51966 injected by the test suite\n", nil},
|
|
}
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var r report.Reporter
|
|
r.SetFlags(tc.flag)
|
|
var buf bytes.Buffer
|
|
l := log.New(&buf, "dispatch: ", 0)
|
|
r.SetOutput(l)
|
|
|
|
a := check.MustAbs(t.TempDir())
|
|
if tc.persist {
|
|
r.SetPathname(a)
|
|
}
|
|
|
|
var got []report.Error
|
|
r.Notify(func(p report.Error) { got = append(got, p) })
|
|
|
|
var gotPanic any
|
|
func() {
|
|
defer func() { gotPanic = recover() }()
|
|
for _, p := range tc.errs {
|
|
r.Dispatch(p.Severity, p.Message, p.Err)
|
|
}
|
|
}()
|
|
|
|
if gotPanic == nil && !reflect.DeepEqual(got, tc.errs) {
|
|
t.Errorf("Dispatch: %#v, want %#v", got, tc.errs)
|
|
}
|
|
if !reflect.DeepEqual(gotPanic, tc.wantPanic) {
|
|
t.Errorf("Dispatch: panic = %v, want %v", gotPanic, tc.wantPanic)
|
|
}
|
|
if gotLog := buf.String(); gotLog != tc.wantLog {
|
|
t.Errorf("Dispatch: log =\n%s\nwant\n%s", gotLog, tc.wantLog)
|
|
}
|
|
|
|
if tc.persist {
|
|
var names []string
|
|
if dents, err := os.ReadDir(a.String()); err != nil {
|
|
t.Fatal(err)
|
|
} else {
|
|
names = make([]string, len(dents))
|
|
for i, dent := range dents {
|
|
names[i] = dent.Name()
|
|
}
|
|
slices.Sort(names)
|
|
}
|
|
|
|
gotPersist := make([]report.Error, len(names))
|
|
for i, name := range names {
|
|
f, err := os.Open(a.Append(name).String())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err = gob.NewDecoder(f).Decode(&gotPersist[i]); err != nil {
|
|
_ = f.Close()
|
|
t.Fatal(err)
|
|
} else if err = f.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
if !reflect.DeepEqual(gotPersist, tc.errs) {
|
|
t.Errorf("Dispatch: persist = %#v, want %#v", gotPersist, tc.errs)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBadPersist(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var r report.Reporter
|
|
r.SetFlags(report.DNoRecover)
|
|
r.SetPathname(check.MustAbs("/proc/nonexistent"))
|
|
|
|
var pathError *os.PathError
|
|
func() {
|
|
defer func() {
|
|
if !errors.As(recover().(error), &pathError) {
|
|
t.Fatal("invalid panic kind")
|
|
}
|
|
}()
|
|
r.Dispatch(report.Fatal, "\x00", stub.UniqueError(0xbad))
|
|
}()
|
|
if !errors.Is(pathError.Err, os.ErrNotExist) {
|
|
t.Fatalf("Dispatch: panic = %v", pathError)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
l := log.New(&buf, "persist: ", 0)
|
|
r.SetOutput(l)
|
|
r.SetFlags(0)
|
|
r.Dispatch(report.Fatal, "\x00", stub.UniqueError(0xbad))
|
|
}
|