aboutsummaryrefslogtreecommitdiff
path: root/plugin/rewrite/rcode_test.go
diff options
context:
space:
mode:
authorGravatar pschou <pschou@users.noreply.github.com> 2023-08-26 22:20:12 -0400
committerGravatar GitHub <noreply@github.com> 2023-08-26 22:20:12 -0400
commit5ace19d4557cc441e63768247175cd753e62b55c (patch)
tree3ffab2d90ea3a42789e8d265523f85fed22a88f8 /plugin/rewrite/rcode_test.go
parent07c7dc82f0a76ef5438e878aaf5c7b319135ac7e (diff)
downloadcoredns-5ace19d4557cc441e63768247175cd753e62b55c.tar.gz
coredns-5ace19d4557cc441e63768247175cd753e62b55c.tar.zst
coredns-5ace19d4557cc441e63768247175cd753e62b55c.zip
plugin/rewrite: add rcode as a rewrite option (#6204)
* plugin/forward add ignore_server_failure for masking upstream server faults Signed-off-by: schou <pschou@users.noreply.github.com> * Switch from a ignore_server_fail to a rewrite rcode type. Signed-off-by: schou <pschou@users.noreply.github.com> * trim down the tests Signed-off-by: schou <pschou@users.noreply.github.com> * fixing readme TTL and using map for rcode Signed-off-by: schou <pschou@users.noreply.github.com> * add newline Signed-off-by: schou <pschou@users.noreply.github.com> --------- Signed-off-by: schou <pschou@users.noreply.github.com>
Diffstat (limited to 'plugin/rewrite/rcode_test.go')
-rw-r--r--plugin/rewrite/rcode_test.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/plugin/rewrite/rcode_test.go b/plugin/rewrite/rcode_test.go
new file mode 100644
index 000000000..e40260719
--- /dev/null
+++ b/plugin/rewrite/rcode_test.go
@@ -0,0 +1,72 @@
+package rewrite
+
+import (
+ "testing"
+
+ "github.com/coredns/coredns/plugin/test"
+ "github.com/coredns/coredns/request"
+
+ "github.com/miekg/dns"
+)
+
+func TestNewRCodeRule(t *testing.T) {
+ tests := []struct {
+ next string
+ args []string
+ expectedFail bool
+ }{
+ {"stop", []string{"numeric.rcode.coredns.rocks", "2", "0"}, false},
+ {"stop", []string{"too.few.rcode.coredns.rocks", "2"}, true},
+ {"stop", []string{"exact", "too.many.rcode.coredns.rocks", "2", "1", "0"}, true},
+ {"stop", []string{"exact", "match.string.rcode.coredns.rocks", "SERVFAIL", "NOERROR"}, false},
+ {"continue", []string{"regex", `(regex)\.rcode\.(coredns)\.(rocks)`, "FORMERR", "NOERROR"}, false},
+ {"stop", []string{"invalid.rcode.coredns.rocks", "random", "nothing"}, true},
+ }
+ for i, tc := range tests {
+ failed := false
+ rule, err := newRCodeRule(tc.next, tc.args...)
+ if err != nil {
+ failed = true
+ }
+ if !failed && !tc.expectedFail {
+ continue
+ }
+ if failed && tc.expectedFail {
+ continue
+ }
+ t.Fatalf("Test %d: FAIL, expected fail=%t, but received fail=%t: (%s) %s, rule=%v, err=%v", i, tc.expectedFail, failed, tc.next, tc.args, rule, err)
+ }
+ for i, tc := range tests {
+ failed := false
+ tc.args = append([]string{tc.next, "rcode"}, tc.args...)
+ rule, err := newRule(tc.args...)
+ if err != nil {
+ failed = true
+ }
+ if !failed && !tc.expectedFail {
+ continue
+ }
+ if failed && tc.expectedFail {
+ continue
+ }
+ t.Fatalf("Test %d: FAIL, expected fail=%t, but received fail=%t: (%s) %s, rule=%v, err=%v", i, tc.expectedFail, failed, tc.next, tc.args, rule, err)
+ }
+}
+
+func TestRCodeRewrite(t *testing.T) {
+ rule, err := newRCodeRule("stop", []string{"exact", "srv1.coredns.rocks", "SERVFAIL", "FORMERR"}...)
+
+ m := new(dns.Msg)
+ m.SetQuestion("srv1.coredns.rocks.", dns.TypeA)
+ m.Question[0].Qclass = dns.ClassINET
+ m.Answer = []dns.RR{test.A("srv1.coredns.rocks. 5 IN A 10.0.0.1")}
+ m.MsgHdr.Rcode = dns.RcodeServerFailure
+ request := request.Request{Req: m}
+
+ rcRule, _ := rule.(*exactRCodeRule)
+ var rr dns.RR
+ rcRule.response.RewriteResponse(request.Req, rr)
+ if request.Req.MsgHdr.Rcode != dns.RcodeFormatError {
+ t.Fatalf("RCode rewrite did not apply changes, request=%#v, err=%v", request.Req, err)
+ }
+}