Testing with a fake¶
Source · io.systemd.Resolve · None (runs anywhere)
Application code and its tests. lookup.go is a small piece of real code (an
IPv4 resolver built on resolve.Client); lookup_test.go tests it against a
varlinktest fake with canned answers. It runs anywhere go test runs, with no
root or systemd needed.
Run it¶
Output (real)
=== RUN TestIPv4
--- PASS: TestIPv4 (0.00s)
=== RUN TestNotFound
--- PASS: TestNotFound (0.00s)
=== RUN TestFeatureDetection
--- PASS: TestFeatureDetection (0.00s)
PASS
ok github.com/rossedman/sdvarlink/examples/testing-with-fake 0.008s
The code¶
How it works¶
- The seam.
Resolverholds a*resolve.Client. Production code builds it withresolve.Dial; the test builds it withresolve.DialPath(ctx, srv.Path()). - Typed fake handler.
srv.Handletakes a plain Go function fromResolveHostnameIntoResolveHostnameOut. Returning&resolve.NoSuchResourceRecordError{}sends exactly the error systemd would. - Call assertions.
srv.Calls()[0].Parameterschecks the JSON the code really sent, including thatfamilywas set. - Version-accurate introspection.
srv.AddInterface(resolve.InterfaceDescription)makes the fake answer like a v257 resolved, soSupports("BrowseServices")isfalse, just as on a real v257 host.