1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package metadata
import (
"context"
"testing"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
type testProvider map[string]Func
func (tp testProvider) Metadata(ctx context.Context, state request.Request) context.Context {
for k, v := range tp {
SetValueFunc(ctx, k, v)
}
return ctx
}
type testHandler struct{ ctx context.Context }
func (m *testHandler) Name() string { return "test" }
func (m *testHandler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
m.ctx = ctx
return 0, nil
}
func TestMetadataServeDNS(t *testing.T) {
expectedMetadata := []testProvider{
{"test/key1": func() string { return "testvalue1" }},
{"test/key2": func() string { return "two" }, "test/key3": func() string { return "testvalue3" }},
}
// Create fake Providers based on expectedMetadata
providers := []Provider{}
for _, e := range expectedMetadata {
providers = append(providers, e)
}
next := &testHandler{} // fake handler which stores the resulting context
m := Metadata{
Zones: []string{"."},
Providers: providers,
Next: next,
}
ctx := context.TODO()
m.ServeDNS(ctx, &test.ResponseWriter{}, new(dns.Msg))
nctx := next.ctx
for _, expected := range expectedMetadata {
for label, expVal := range expected {
if !IsLabel(label) {
t.Errorf("Expected label %s is not considered a valid label", label)
}
val := ValueFunc(nctx, label)
if val() != expVal() {
t.Errorf("Expected value %s for %s, but got %s", expVal(), label, val())
}
}
}
}
func TestLabelFormat(t *testing.T) {
labels := []struct {
label string
isValid bool
}{
// ok
{"plugin/LABEL", true},
{"p/LABEL", true},
{"plugin/L", true},
{"PLUGIN/LABEL/SUB-LABEL", true},
// fails
{"LABEL", false},
{"plugin.LABEL", false},
{"/NO-PLUGIN-NOT-ACCEPTED", false},
{"ONLY-PLUGIN-NOT-ACCEPTED/", false},
{"/", false},
{"//", false},
}
for _, test := range labels {
if x := IsLabel(test.label); x != test.isValid {
t.Errorf("Label %v expected %v, got: %v", test.label, test.isValid, x)
}
}
}
|