2018-06-29 12:44:16 +03:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/coredns/coredns/plugin/test"
|
2018-06-29 15:03:25 +01:00
|
|
|
"github.com/coredns/coredns/request"
|
2018-06-29 12:44:16 +03:00
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
2018-07-01 20:01:17 +01:00
|
|
|
type testProvider map[string]Func
|
2018-06-29 12:44:16 +03:00
|
|
|
|
2018-07-01 20:01:17 +01:00
|
|
|
func (tp testProvider) Metadata(ctx context.Context, state request.Request) context.Context {
|
|
|
|
|
for k, v := range tp {
|
|
|
|
|
SetValueFunc(ctx, k, v)
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
2018-07-01 20:01:17 +01:00
|
|
|
return ctx
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type testHandler struct{ ctx context.Context }
|
|
|
|
|
|
2018-07-01 20:01:17 +01:00
|
|
|
func (m *testHandler) Name() string { return "test" }
|
2018-06-29 12:44:16 +03:00
|
|
|
|
|
|
|
|
func (m *testHandler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
|
|
|
|
m.ctx = ctx
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 15:03:25 +01:00
|
|
|
func TestMetadataServeDNS(t *testing.T) {
|
2018-06-29 12:44:16 +03:00
|
|
|
expectedMetadata := []testProvider{
|
2018-07-01 20:01:17 +01:00
|
|
|
testProvider{"test/key1": func() string { return "testvalue1" }},
|
|
|
|
|
testProvider{"test/key2": func() string { return "two" }, "test/key3": func() string { return "testvalue3" }},
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
|
|
|
|
// Create fake Providers based on expectedMetadata
|
|
|
|
|
providers := []Provider{}
|
|
|
|
|
for _, e := range expectedMetadata {
|
|
|
|
|
providers = append(providers, e)
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 15:03:25 +01:00
|
|
|
next := &testHandler{} // fake handler which stores the resulting context
|
2018-07-01 20:01:17 +01:00
|
|
|
m := Metadata{
|
2018-06-29 12:44:16 +03:00
|
|
|
Zones: []string{"."},
|
|
|
|
|
Providers: providers,
|
|
|
|
|
Next: next,
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-01 20:01:17 +01:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
m.ServeDNS(ctx, &test.ResponseWriter{}, new(dns.Msg))
|
|
|
|
|
nctx := next.ctx
|
|
|
|
|
|
2018-06-29 12:44:16 +03:00
|
|
|
for _, expected := range expectedMetadata {
|
2018-07-01 20:01:17 +01:00
|
|
|
for label, expVal := range expected {
|
|
|
|
|
val := ValueFunc(nctx, label)
|
|
|
|
|
if val() != expVal() {
|
|
|
|
|
t.Errorf("Expected value %s for %s, but got %s", expVal(), label, val())
|
2018-06-29 12:44:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|