Documentation ¶
Overview ¶
Package aghtest contains utilities for testing.
Index ¶
- Constants
- func MatchedResponse(req *dns.Msg, qt uint16, targ, answer string) (resp *dns.Msg)
- func ReplaceLogLevel(t testing.TB, l log.Level)
- func ReplaceLogWriter(t testing.TB, w io.Writer)
- type FS
- type FSWatcher
- type GlobFS
- type Listener
- type ServiceWithConfig
- type StatFS
- type TestResolver
- type Upstream
- type UpstreamMock
Constants ¶
const ErrUpstream errors.Error = "test upstream error"
ErrUpstream is the error returned from the *UpstreamMock created by NewErrorUpstream.
Variables ¶
This section is empty.
Functions ¶
func MatchedResponse ¶ added in v0.107.17
MatchedResponse is a test helper that returns a response with answer if req has question type qt, and target targ. Otherwise, it returns nil.
req must not be nil and req.Question must have a length of 1. Answer is interpreted in the following ways:
For A and AAAA queries, answer must be an IP address of the corresponding protocol version.
For PTR queries, answer should be a domain name in the response.
If the answer does not correspond to the question type, MatchedResponse panics. Panics are used instead of testing.TB, because the helper is intended to use in [UpstreamMock.OnExchange] callbacks, which are usually called in a separate goroutine.
TODO(a.garipov): Consider adding version with DNS class as well.
func ReplaceLogLevel ¶
ReplaceLogLevel sets logging level to l and uses Cleanup method of t to revert changes.
Types ¶
type FSWatcher ¶ added in v0.107.0
type FSWatcher struct { OnEvents func() (e <-chan struct{}) OnAdd func(name string) (err error) OnClose func() (err error) }
FSWatcher is a mock aghos.FSWatcher implementation for tests.
func (*FSWatcher) Add ¶ added in v0.107.0
Add implements the aghos.FSWatcher interface for *FSWatcher.
func (*FSWatcher) Close ¶ added in v0.107.0
Close implements the aghos.FSWatcher interface for *FSWatcher.
func (*FSWatcher) Events ¶ added in v0.107.0
func (w *FSWatcher) Events() (e <-chan struct{})
Events implements the aghos.FSWatcher interface for *FSWatcher.
type GlobFS ¶ added in v0.107.0
type GlobFS struct { // FS is embedded here to avoid implementing all it's methods. FS OnGlob func(pattern string) ([]string, error) }
GlobFS is a mock fs.GlobFS implementation for tests.
type Listener ¶ added in v0.107.10
type Listener struct { OnAccept func() (conn net.Conn, err error) OnAddr func() (addr net.Addr) OnClose func() (err error) }
Listener is a mock net.Listener implementation for tests.
func (*Listener) Accept ¶ added in v0.107.10
Accept implements the net.Listener interface for *Listener.
func (*Listener) Addr ¶ added in v0.107.10
Addr implements the net.Listener interface for *Listener.
func (*Listener) Close ¶ added in v0.107.10
Close implements the net.Listener interface for *Listener.
type ServiceWithConfig ¶ added in v0.107.34
type ServiceWithConfig[ConfigType any] struct { OnStart func() (err error) OnShutdown func(ctx context.Context) (err error) OnConfig func() (c ConfigType) }
ServiceWithConfig is a mock agh.ServiceWithConfig implementation for tests.
func (*ServiceWithConfig[ConfigType]) Config ¶ added in v0.107.34
func (s *ServiceWithConfig[ConfigType]) Config() (c ConfigType)
Config implements the agh.ServiceWithConfig interface for *ServiceWithConfig.
func (*ServiceWithConfig[_]) Shutdown ¶ added in v0.107.34
func (s *ServiceWithConfig[_]) Shutdown(ctx context.Context) (err error)
Shutdown implements the agh.ServiceWithConfig interface for *ServiceWithConfig.
func (*ServiceWithConfig[_]) Start ¶ added in v0.107.34
func (s *ServiceWithConfig[_]) Start() (err error)
Start implements the agh.ServiceWithConfig interface for *ServiceWithConfig.
type StatFS ¶ added in v0.107.0
type StatFS struct { // FS is embedded here to avoid implementing all it's methods. FS OnStat func(name string) (fs.FileInfo, error) }
StatFS is a mock fs.StatFS implementation for tests.
type TestResolver ¶
type TestResolver struct {
// contains filtered or unexported fields
}
TestResolver is a Resolver for tests.
func (*TestResolver) Counter ¶
func (r *TestResolver) Counter() int
Counter returns the number of requests handled.
func (*TestResolver) HostToIPs ¶
func (r *TestResolver) HostToIPs(host string) (ipv4, ipv6 net.IP)
HostToIPs generates IPv4 and IPv6 from host.
func (*TestResolver) LookupHost ¶
func (r *TestResolver) LookupHost(host string) (addrs []string, err error)
LookupHost implements Resolver interface for *testResolver. It returns the slice of IPv4 and IPv6 instances converted to strings.
type Upstream ¶ added in v0.107.7
type Upstream struct { // CName is a map of hostname to canonical name. CName map[string][]string // IPv4 is a map of hostname to IPv4. IPv4 map[string][]net.IP // IPv6 is a map of hostname to IPv6. IPv6 map[string][]net.IP }
Upstream is a mock implementation of upstream.Upstream.
TODO(a.garipov): Replace with UpstreamMock and rename it to just Upstream.
func (*Upstream) Address ¶ added in v0.107.7
Address implements upstream.Upstream interface for *Upstream.
func (*Upstream) Close ¶ added in v0.107.17
Close implements upstream.Upstream interface for *Upstream.
type UpstreamMock ¶ added in v0.107.10
type UpstreamMock struct { OnAddress func() (addr string) OnExchange func(req *dns.Msg) (resp *dns.Msg, err error) OnClose func() (err error) }
UpstreamMock is a mock upstream.Upstream implementation for tests.
TODO(a.garipov): Replace with all uses of Upstream with UpstreamMock and rename it to just Upstream.
func NewBlockUpstream ¶ added in v0.107.10
func NewBlockUpstream(hostname string, shouldBlock bool) (u *UpstreamMock)
NewBlockUpstream returns an *UpstreamMock that works like an upstream that supports hash-based safe-browsing/adult-blocking feature. If shouldBlock is true, hostname's actual hash is returned, blocking it. Otherwise, it returns a different hash.
func NewErrorUpstream ¶ added in v0.107.10
func NewErrorUpstream() (u *UpstreamMock)
NewErrorUpstream returns an *UpstreamMock that returns ErrUpstream from its Exchange method.
func NewUpstreamMock ¶ added in v0.107.17
NewUpstreamMock returns an *UpstreamMock, fields OnAddress and OnClose of which are set to stubs that return "upstream.example" and nil respectively. The field OnExchange is set to onExc.
func (*UpstreamMock) Address ¶ added in v0.107.10
func (u *UpstreamMock) Address() (addr string)
Address implements the upstream.Upstream interface for *UpstreamMock.
func (*UpstreamMock) Close ¶ added in v0.107.17
func (u *UpstreamMock) Close() (err error)
Close implements the upstream.Upstream interface for *UpstreamMock.
func (*UpstreamMock) Exchange ¶ added in v0.107.10
Exchange implements the upstream.Upstream interface for *UpstreamMock.