diff options
author | 2018-07-08 03:18:01 -0400 | |
---|---|---|
committer | 2018-07-08 08:18:01 +0100 | |
commit | 774546243036c52ff6e3ad524594e68b59c3a919 (patch) | |
tree | 22f466b2d07e92faf91d1eba7d7e716f7d6783c1 /plugin/rewrite/rewrite_test.go | |
parent | 6ec1978340de48e25ff77b032040e0b39805d84c (diff) | |
download | coredns-774546243036c52ff6e3ad524594e68b59c3a919.tar.gz coredns-774546243036c52ff6e3ad524594e68b59c3a919.tar.zst coredns-774546243036c52ff6e3ad524594e68b59c3a919.zip |
plugin/rewrite - extend edns0 local variable support with metadata (#1928)
* - add support of metadata values for edns0 local variables
* - comments from review.
* - simplify label check. Add UT
* - enhance check for Labels, add UT
- remove IsMetadataSet
* - edns0 variable - if variable is not found just ignore the rewrite.
Diffstat (limited to 'plugin/rewrite/rewrite_test.go')
-rw-r--r-- | plugin/rewrite/rewrite_test.go | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/plugin/rewrite/rewrite_test.go b/plugin/rewrite/rewrite_test.go index d67f8d0bf..f70933c3c 100644 --- a/plugin/rewrite/rewrite_test.go +++ b/plugin/rewrite/rewrite_test.go @@ -7,8 +7,10 @@ import ( "testing" "github.com/coredns/coredns/plugin" + "github.com/coredns/coredns/plugin/metadata" "github.com/coredns/coredns/plugin/pkg/dnstest" "github.com/coredns/coredns/plugin/test" + "github.com/coredns/coredns/request" "github.com/miekg/dns" ) @@ -434,12 +436,32 @@ func optsEqual(a, b []dns.EDNS0) bool { return true } +type testProvider map[string]metadata.Func + +func (tp testProvider) Metadata(ctx context.Context, state request.Request) context.Context { + for k, v := range tp { + metadata.SetValueFunc(ctx, k, v) + } + return ctx +} + func TestRewriteEDNS0LocalVariable(t *testing.T) { rw := Rewrite{ Next: plugin.HandlerFunc(msgPrinter), noRevert: true, } + expectedMetadata := []metadata.Provider{ + testProvider{"test/label": func() string { return "my-value" }}, + testProvider{"test/empty": func() string { return "" }}, + } + + meta := metadata.Metadata{ + Zones: []string{"."}, + Providers: expectedMetadata, + Next: &rw, + } + // test.ResponseWriter has the following values: // The remote will always be 10.240.0.1 and port 40212. // The local address is always 127.0.0.1 and port 53. @@ -492,9 +514,26 @@ func TestRewriteEDNS0LocalVariable(t *testing.T) { []dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte{0x7F, 0x00, 0x00, 0x01}}}, true, }, + { + []dns.EDNS0{}, + []string{"local", "set", "0xffee", "{test/label}"}, + []dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte("my-value")}}, + true, + }, + { + []dns.EDNS0{}, + []string{"local", "set", "0xffee", "{test/empty}"}, + []dns.EDNS0{&dns.EDNS0_LOCAL{Code: 0xffee, Data: []byte("")}}, + true, + }, + { + []dns.EDNS0{}, + []string{"local", "set", "0xffee", "{test/does-not-exist}"}, + nil, + false, + }, } - ctx := context.TODO() for i, tc := range tests { m := new(dns.Msg) m.SetQuestion("example.com.", dns.TypeA) @@ -506,16 +545,19 @@ func TestRewriteEDNS0LocalVariable(t *testing.T) { } rw.Rules = []Rule{r} + ctx := context.TODO() rec := dnstest.NewRecorder(&test.ResponseWriter{}) - rw.ServeDNS(ctx, rec, m) + meta.ServeDNS(ctx, rec, m) resp := rec.Msg o := resp.IsEdns0() - o.SetDo(tc.doBool) if o == nil { - t.Errorf("Test %d: EDNS0 options not set", i) + if tc.toOpts != nil { + t.Errorf("Test %d: EDNS0 options not set", i) + } continue } + o.SetDo(tc.doBool) if o.Do() != tc.doBool { t.Errorf("Test %d: Expected %v but got %v", i, tc.doBool, o.Do()) } |