test

package
v1.11.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package test contains helper functions for writing plugin tests.

Package test will scrape a target and you can inspect the variables. Basic usage:

result := Scrape("http://localhost:9153/metrics")
v := MetricValue("coredns_cache_capacity", result)

Index

Constants

View Source
const (
	// Answer is the answer section in an Msg.
	Answer sect = iota
	// Ns is the authoritative section in an Msg.
	Ns
	// Extra is the additional section in an Msg.
	Extra
)

Variables

This section is empty.

Functions

func A

func A(rr string) *dns.A

A returns an A record from rr. It panics on errors.

func AAAA

func AAAA(rr string) *dns.AAAA

AAAA returns an AAAA record from rr. It panics on errors.

func CAA

func CAA(rr string) *dns.CAA

CAA returns a CAA record from rr. It panics on errors.

func CNAME

func CNAME(rr string) *dns.CNAME

CNAME returns a CNAME record from rr. It panics on errors.

func CNAMEOrder

func CNAMEOrder(res *dns.Msg) error

CNAMEOrder makes sure that CNAMES do not appear after their target records.

func DNAME

func DNAME(rr string) *dns.DNAME

DNAME returns a DNAME record from rr. It panics on errors.

func DNSKEY

func DNSKEY(rr string) *dns.DNSKEY

DNSKEY returns a DNSKEY record from rr. It panics on errors.

func DS

func DS(rr string) *dns.DS

DS returns a DS record from rr. It panics on errors.

func HINFO

func HINFO(rr string) *dns.HINFO

HINFO returns a HINFO record from rr. It panics on errors.

func Header(tc Case, resp *dns.Msg) error

Header tests if the header in resp matches the header as defined in tc.

func MX

func MX(rr string) *dns.MX

MX returns an MX record from rr. It panics on errors.

func MetricValue

func MetricValue(name string, mfs []*MetricFamily) (string, map[string]string)

MetricValue returns the value associated with name as a string as well as the labels. It only returns the first metrics of the slice.

func MetricValueLabel

func MetricValueLabel(name, label string, mfs []*MetricFamily) (string, map[string]string)

MetricValueLabel returns the value for name *and* label *value*.

func NAPTR

func NAPTR(rr string) *dns.NAPTR

NAPTR returns a NAPTR record from rr. It panics on errors.

func NS

func NS(rr string) *dns.NS

NS returns an NS record from rr. It panics on errors.

func NSEC

func NSEC(rr string) *dns.NSEC

NSEC returns an NSEC record from rr. It panics on errors.

func OPT

func OPT(bufsize int, do bool) *dns.OPT

OPT returns an OPT record with UDP buffer size set to bufsize and the DO bit set to do.

func PTR

func PTR(rr string) *dns.PTR

PTR returns a PTR record from rr. It panics on errors.

func RRSIG

func RRSIG(rr string) *dns.RRSIG

RRSIG returns an RRSIG record from rr. It panics on errors.

func SOA

func SOA(rr string) *dns.SOA

SOA returns a SOA record from rr. It panics on errors.

func SRV

func SRV(rr string) *dns.SRV

SRV returns a SRV record from rr. It panics on errors.

func ScrapeMetricAsInt

func ScrapeMetricAsInt(addr string, name string, label string, nometricvalue int) int

ScrapeMetricAsInt provides a sum of all metrics collected for the name and label provided. if the metric is not a numeric value, it will be counted a 0.

func Section

func Section(tc Case, sec sect, rr []dns.RR) error

Section tests if the section in tc matches rr.

func SortAndCheck

func SortAndCheck(resp *dns.Msg, tc Case) error

SortAndCheck sorts resp and the checks the header and three sections against the testcase in tc.

func TXT

func TXT(rr string) *dns.TXT

TXT returns a TXT record from rr. It panics on errors.

func TempFile

func TempFile(dir, content string) (string, func(), error)

TempFile will create a temporary file on disk and returns the name and a cleanup function to remove it later.

func WritePEMFiles

func WritePEMFiles(t *testing.T) (string, error)

WritePEMFiles creates a tmp dir with ca.pem, cert.pem, and key.pem

Types

type Case

type Case struct {
	Qname             string
	Qtype             uint16
	Rcode             int
	Do                bool
	AuthenticatedData bool
	Answer            []dns.RR
	Ns                []dns.RR
	Extra             []dns.RR
	Error             error
}

Case represents a test case that encapsulates various data from a query and response. Note that is the TTL of a record is 303 we don't compare it with the TTL.

func (Case) Msg

func (c Case) Msg() *dns.Msg

Msg returns a *dns.Msg embedded in c.

type Handler

type Handler interface {
	ServeDNS(context.Context, dns.ResponseWriter, *dns.Msg) (int, error)
	Name() string
}

Handler interface defines a plugin.

func ErrorHandler

func ErrorHandler() Handler

ErrorHandler returns a Handler that returns ServerFailure error when called.

func NextHandler

func NextHandler(rcode int, err error) Handler

NextHandler returns a Handler that returns rcode and err.

type HandlerFunc

type HandlerFunc func(context.Context, dns.ResponseWriter, *dns.Msg) (int, error)

HandlerFunc is a convenience type like dns.HandlerFunc, except ServeDNS returns an rcode and an error.

func (HandlerFunc) Name

func (f HandlerFunc) Name() string

Name implements the Handler interface.

func (HandlerFunc) ServeDNS

func (f HandlerFunc) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error)

ServeDNS implements the Handler interface.

type MetricFamily

type MetricFamily struct {
	Name    string        `json:"name"`
	Help    string        `json:"help"`
	Type    string        `json:"type"`
	Metrics []interface{} `json:"metrics,omitempty"` // Either metric or summary.
}

MetricFamily holds a prometheus metric.

func Scrape

func Scrape(url string) []*MetricFamily

Scrape returns the all the vars a []*metricFamily.

type RRSet

type RRSet []dns.RR

RRSet represents a list of RRs.

func (RRSet) Len

func (p RRSet) Len() int

func (RRSet) Less

func (p RRSet) Less(i, j int) bool

func (RRSet) Swap

func (p RRSet) Swap(i, j int)

type ResponseWriter

type ResponseWriter struct {
	TCP      bool // if TCP is true we return an TCP connection instead of an UDP one.
	RemoteIP string
	Zone     string
}

ResponseWriter is useful for writing tests. It uses some fixed values for the client. The remote will always be 10.240.0.1 and port 40212. The local address is always 127.0.0.1 and port 53.

func (*ResponseWriter) Close

func (t *ResponseWriter) Close() error

Close implements dns.ResponseWriter interface.

func (*ResponseWriter) Hijack

func (t *ResponseWriter) Hijack()

Hijack implements dns.ResponseWriter interface.

func (*ResponseWriter) LocalAddr

func (t *ResponseWriter) LocalAddr() net.Addr

LocalAddr returns the local address, 127.0.0.1:53 (UDP, TCP if t.TCP is true).

func (*ResponseWriter) RemoteAddr

func (t *ResponseWriter) RemoteAddr() net.Addr

RemoteAddr returns the remote address, defaults to 10.240.0.1:40212 (UDP, TCP is t.TCP is true).

func (*ResponseWriter) TsigStatus

func (t *ResponseWriter) TsigStatus() error

TsigStatus implements dns.ResponseWriter interface.

func (*ResponseWriter) TsigTimersOnly

func (t *ResponseWriter) TsigTimersOnly(bool)

TsigTimersOnly implements dns.ResponseWriter interface.

func (*ResponseWriter) Write

func (t *ResponseWriter) Write(buf []byte) (int, error)

Write implements dns.ResponseWriter interface.

func (*ResponseWriter) WriteMsg

func (t *ResponseWriter) WriteMsg(m *dns.Msg) error

WriteMsg implements dns.ResponseWriter interface.

type ResponseWriter6

type ResponseWriter6 struct {
	ResponseWriter
}

ResponseWriter6 returns fixed client and remote address in IPv6. The remote address is always fe80::42:ff:feca:4c65 and port 40212. The local address is always ::1 and port 53.

func (*ResponseWriter6) LocalAddr

func (t *ResponseWriter6) LocalAddr() net.Addr

LocalAddr returns the local address, always ::1, port 53 (UDP, TCP is t.TCP is true).

func (*ResponseWriter6) RemoteAddr

func (t *ResponseWriter6) RemoteAddr() net.Addr

RemoteAddr returns the remote address, always fe80::42:ff:feca:4c65 port 40212 (UDP, TCP is t.TCP is true).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL