Initial commit
This commit is contained in:
commit
2d98ed1b5d
33 changed files with 4644 additions and 0 deletions
65
checker/testhelpers_test.go
Normal file
65
checker/testhelpers_test.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
package checker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
sdk "git.happydns.org/checker-sdk-go/checker"
|
||||
)
|
||||
|
||||
// fakeObs is a tiny ObservationGetter that returns a single payload regardless
|
||||
// of the key requested. Tests use it to feed canned ResolverPropagationData
|
||||
// into rules.
|
||||
type fakeObs struct {
|
||||
payload any
|
||||
err error
|
||||
}
|
||||
|
||||
func newFakeObs(payload any) *fakeObs { return &fakeObs{payload: payload} }
|
||||
|
||||
func (f *fakeObs) Get(_ context.Context, _ sdk.ObservationKey, dest any) error {
|
||||
if f.err != nil {
|
||||
return f.err
|
||||
}
|
||||
raw, err := json.Marshal(f.payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fakeObs marshal: %w", err)
|
||||
}
|
||||
return json.Unmarshal(raw, dest)
|
||||
}
|
||||
|
||||
func (f *fakeObs) GetRelated(_ context.Context, _ sdk.ObservationKey) ([]sdk.RelatedObservation, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// errObs always fails Get; used to verify error-path branches in rules.
|
||||
type errObs struct{ err error }
|
||||
|
||||
func (e *errObs) Get(_ context.Context, _ sdk.ObservationKey, _ any) error { return e.err }
|
||||
func (e *errObs) GetRelated(_ context.Context, _ sdk.ObservationKey) ([]sdk.RelatedObservation, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// mkProbe is a small constructor used in many tests.
|
||||
func mkProbe(rcode, sig string, records ...string) *RRProbe {
|
||||
return &RRProbe{Rcode: rcode, Signature: sig, Records: records, Transport: TransportUDP}
|
||||
}
|
||||
|
||||
// mkResolver builds a ResolverView with a single probe.
|
||||
func mkResolver(id, region string, filtered, reachable bool, probes map[string]*RRProbe) *ResolverView {
|
||||
return &ResolverView{
|
||||
ID: id, Name: id, IP: "0.0.0.0", Region: region,
|
||||
Filtered: filtered, Reachable: reachable, Transport: TransportUDP,
|
||||
Probes: probes,
|
||||
}
|
||||
}
|
||||
|
||||
// statesByCode reorganises a slice of CheckState by Code for easy lookup.
|
||||
func statesByCode(states []sdk.CheckState) map[string][]sdk.CheckState {
|
||||
out := map[string][]sdk.CheckState{}
|
||||
for _, s := range states {
|
||||
out[s.Code] = append(out[s.Code], s)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue