Golden-file testing against real systemd. health.go checks that journald
answers over Varlink. Its test replaystestdata/journal.json, recorded
from a real systemd-journald v255, so it runs anywhere. Pass -update on a
systemd host to record a fresh golden file through a forwarding fake.
// Package health checks that systemd-journald answers over Varlink. Its// test replays a golden file recorded against a real journald, so it runs// anywhere; pass -update on a systemd host to re-record://// go test ./examples/record-replay/ # replay// sudo go test ./examples/record-replay/ -update # record against journaldpackagehealthimport("context""fmt""slices""github.com/rossedman/sdvarlink/systemd/v257/journal""github.com/rossedman/sdvarlink/varlink")// Report describes the journald instance that answered.typeReportstruct{ProductstringVersionstring}// CheckJournal verifies journald implements io.systemd.Journal and that a// Synchronize round trip succeeds.funcCheckJournal(ctxcontext.Context,c*journal.Client)(*Report,error){info,err:=varlink.GetInfo(ctx,c.Caller())iferr!=nil{returnnil,err}if!slices.Contains(info.Interfaces,journal.InterfaceName){returnnil,fmt.Errorf("health: %s does not implement %s",info.Product,journal.InterfaceName)}iferr:=c.Synchronize(ctx);err!=nil{returnnil,fmt.Errorf("health: synchronize: %w",err)}return&Report{Product:info.Product,Version:info.Version},nil}
packagehealthimport("context""flag""strings""testing""github.com/rossedman/sdvarlink/systemd/v257/journal""github.com/rossedman/sdvarlink/varlinktest")varupdate=flag.Bool("update",false,"record testdata/journal.json against the live journald")funcTestCheckJournal(t*testing.T){varsrv*varlinktest.Serverif*update{srv=varlinktest.Record(t,journal.DefaultSocket,"testdata/journal.json")}else{srv=varlinktest.Replay(t,"testdata/journal.json")}c,err:=journal.DialPath(context.Background(),srv.Path())iferr!=nil{t.Fatal(err)}deferc.Close()rep,err:=CheckJournal(context.Background(),c)iferr!=nil{t.Fatal(err)}if!strings.Contains(rep.Product,"journald"){t.Fatalf("report = %+v",rep)}}
varlinktest.Record(t, journal.DefaultSocket, golden) starts a fake that
forwards every call to the real journald and writes the exchanges to the
golden file when the test ends.
varlinktest.Replay(t, golden) answers from the file, failing the test
if calls arrive out of order or with different parameters.
The code under test doesn't know. It gets a *journal.Client dialed at
srv.Path() either way.
Reviewable fixtures. The golden file is indented JSON: one entry per
call, with its parameters and replies.