diff options
author | 2021-08-31 04:34:26 -0400 | |
---|---|---|
committer | 2021-08-31 10:34:26 +0200 | |
commit | 9b3b2679f5020f10cb8ef4c0bad33831333f09fa (patch) | |
tree | 93b01bc4fb1d22bb80281727d339ea98262fba89 /plugin | |
parent | b143cd49b8f0213e3b5cc3086039b463c735e0b1 (diff) | |
download | coredns-9b3b2679f5020f10cb8ef4c0bad33831333f09fa.tar.gz coredns-9b3b2679f5020f10cb8ef4c0bad33831333f09fa.tar.zst coredns-9b3b2679f5020f10cb8ef4c0bad33831333f09fa.zip |
restore 1.8.3 question revert logic; add tests (#4840)
Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
Diffstat (limited to 'plugin')
-rw-r--r-- | plugin/rewrite/rewrite.go | 5 | ||||
-rw-r--r-- | plugin/rewrite/rewrite_test.go | 53 |
2 files changed, 57 insertions, 1 deletions
diff --git a/plugin/rewrite/rewrite.go b/plugin/rewrite/rewrite.go index 188418ca2..a1e474c29 100644 --- a/plugin/rewrite/rewrite.go +++ b/plugin/rewrite/rewrite.go @@ -54,7 +54,10 @@ func (rw Rewrite) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg } wr.ResponseRules = append(wr.ResponseRules, respRules...) if rule.Mode() == Stop { - break + if !rw.RevertPolicy.DoRevert() { + return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, w, r) + } + return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, wr, r) } } } diff --git a/plugin/rewrite/rewrite_test.go b/plugin/rewrite/rewrite_test.go index 260b65c82..ae5576ab5 100644 --- a/plugin/rewrite/rewrite_test.go +++ b/plugin/rewrite/rewrite_test.go @@ -163,6 +163,59 @@ func TestNewRule(t *testing.T) { } } +func TestRewriteDefaultRevertPolicy(t *testing.T) { + rules := []Rule{} + + r, _ := newNameRule("stop", "prefix", "prefix", "to") + rules = append(rules, r) + r, _ = newNameRule("stop", "suffix", ".suffix.", ".nl.") + rules = append(rules, r) + r, _ = newNameRule("stop", "substring", "from.substring", "to") + rules = append(rules, r) + r, _ = newNameRule("stop", "regex", "(f.*m)\\.regex\\.(nl)", "to.{2}") + rules = append(rules, r) + + rw := Rewrite{ + Next: plugin.HandlerFunc(msgPrinter), + Rules: rules, + // use production (default) RevertPolicy + } + + tests := []struct { + from string + fromT uint16 + fromC uint16 + to string + toT uint16 + toC uint16 + }{ + {"prefix.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, + {"to.suffix.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, + {"from.substring.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, + {"from.regex.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET}, + } + + ctx := context.TODO() + for i, tc := range tests { + m := new(dns.Msg) + m.SetQuestion(tc.from, tc.fromT) + m.Question[0].Qclass = tc.fromC + + rec := dnstest.NewRecorder(&test.ResponseWriter{}) + rw.ServeDNS(ctx, rec, m) + + resp := rec.Msg + + if resp.Question[0].Name != tc.from { + t.Errorf("Test %d: Expected Name in Question to be %q but was %q", i, tc.from, resp.Question[0].Name) + } + + if resp.Answer[0].Header().Name != tc.to { + t.Errorf("Test %d: Expected Name in Answer to be %q but was %q", i, tc.to, resp.Answer[0].Header().Name) + } + } +} + func TestRewrite(t *testing.T) { rules := []Rule{} r, _ := newNameRule("stop", "from.nl.", "to.nl.") |