* Run gostaticheck Run gostaticcheck on the codebase and fix almost all flagged items. Only keep * coremain/run.go:192:2: var appVersion is unused (U1000) * plugin/chaos/setup.go:54:3: the surrounding loop is unconditionally terminated (SA4004) * plugin/etcd/setup.go:103:3: the surrounding loop is unconditionally terminated (SA4004) * plugin/pkg/replacer/replacer.go:274:13: argument should be pointer-like to avoid allocations (SA6002) * plugin/route53/setup.go:124:28: session.New is deprecated: Use NewSession functions to create sessions instead. NewSession has the same functionality as New except an error can be returned when the func is called instead of waiting to receive an error until a request is made. (SA1019) * test/grpc_test.go:25:69: grpc.WithTimeout is deprecated: use DialContext and context.WithTimeout instead. Will be supported throughout 1.x. (SA1019) The first one isn't true, as this is set via ldflags. The rest is minor. The deprecation should be fixed at some point; I'll file some issues. Signed-off-by: Miek Gieben <miek@miek.nl> * Make sure to plug in the plugins import the plugins, that file that did this was removed, put it in the reload test as this requires an almost complete coredns server. Signed-off-by: Miek Gieben <miek@miek.nl>
96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
package trace
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
|
"github.com/coredns/coredns/plugin/pkg/rcode"
|
|
"github.com/coredns/coredns/plugin/test"
|
|
"github.com/coredns/coredns/request"
|
|
|
|
"github.com/caddyserver/caddy"
|
|
"github.com/miekg/dns"
|
|
"github.com/opentracing/opentracing-go/mocktracer"
|
|
)
|
|
|
|
func TestStartup(t *testing.T) {
|
|
m, err := traceParse(caddy.NewTestController("dns", `trace`))
|
|
if err != nil {
|
|
t.Errorf("Error parsing test input: %s", err)
|
|
return
|
|
}
|
|
if m.Name() != "trace" {
|
|
t.Errorf("Wrong name from GetName: %s", m.Name())
|
|
}
|
|
err = m.OnStartup()
|
|
if err != nil {
|
|
t.Errorf("Error starting tracing plugin: %s", err)
|
|
return
|
|
}
|
|
if m.Tracer() == nil {
|
|
t.Errorf("Error, no tracer created")
|
|
}
|
|
}
|
|
|
|
func TestTrace(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
rcode int
|
|
question *dns.Msg
|
|
server string
|
|
}{
|
|
{
|
|
name: "NXDOMAIN",
|
|
rcode: dns.RcodeNameError,
|
|
question: new(dns.Msg).SetQuestion("example.org.", dns.TypeA),
|
|
},
|
|
{
|
|
name: "NOERROR",
|
|
rcode: dns.RcodeSuccess,
|
|
question: new(dns.Msg).SetQuestion("example.net.", dns.TypeCNAME),
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
w := dnstest.NewRecorder(&test.ResponseWriter{})
|
|
m := mocktracer.New()
|
|
tr := &trace{
|
|
Next: test.HandlerFunc(func(_ context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
|
m := new(dns.Msg)
|
|
m.SetRcode(r, tc.rcode)
|
|
w.WriteMsg(m)
|
|
return tc.rcode, nil
|
|
}),
|
|
every: 1,
|
|
tracer: m,
|
|
}
|
|
ctx := context.TODO()
|
|
if _, err := tr.ServeDNS(ctx, w, tc.question); err != nil {
|
|
t.Fatalf("Error during tr.ServeDNS(ctx, w, %v): %v", tc.question, err)
|
|
}
|
|
|
|
fs := m.FinishedSpans()
|
|
// Each trace consists of two spans; the root and the Next function.
|
|
if len(fs) != 2 {
|
|
t.Fatalf("Unexpected span count: len(fs): want 2, got %v", len(fs))
|
|
}
|
|
|
|
rootSpan := fs[1]
|
|
req := request.Request{W: w, Req: tc.question}
|
|
if rootSpan.OperationName != spanName(ctx, req) {
|
|
t.Errorf("Unexpected span name: rootSpan.Name: want %v, got %v", spanName(ctx, req), rootSpan.OperationName)
|
|
}
|
|
if rootSpan.Tag(tagName) != req.Name() {
|
|
t.Errorf("Unexpected span tag: rootSpan.Tag(%v): want %v, got %v", tagName, req.Name(), rootSpan.Tag(tagName))
|
|
}
|
|
if rootSpan.Tag(tagType) != req.Type() {
|
|
t.Errorf("Unexpected span tag: rootSpan.Tag(%v): want %v, got %v", tagType, req.Type(), rootSpan.Tag(tagType))
|
|
}
|
|
if rootSpan.Tag(tagRcode) != rcode.ToString(tc.rcode) {
|
|
t.Errorf("Unexpected span tag: rootSpan.Tag(%v): want %v, got %v", tagRcode, rcode.ToString(tc.rcode), rootSpan.Tag(tagRcode))
|
|
}
|
|
})
|
|
}
|
|
}
|