aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/expression/expression_test.go
diff options
context:
space:
mode:
authorGravatar Chris O'Haver <cohaver@infoblox.com> 2022-09-08 14:56:27 -0400
committerGravatar GitHub <noreply@github.com> 2022-09-08 14:56:27 -0400
commitb56b080a7c1125cd97c0a2edd7ae21bc1bdcd2a5 (patch)
tree1494a9542db6d6f69db39113734c8518a6701daf /plugin/pkg/expression/expression_test.go
parent1f0a41a66597cb8ab4aace8ea5b5bad880bcd23b (diff)
downloadcoredns-b56b080a7c1125cd97c0a2edd7ae21bc1bdcd2a5.tar.gz
coredns-b56b080a7c1125cd97c0a2edd7ae21bc1bdcd2a5.tar.zst
coredns-b56b080a7c1125cd97c0a2edd7ae21bc1bdcd2a5.zip
plugin/view: Advanced routing interface and new 'view' plugin (#5538)
* introduce new interface "dnsserver.Viewer", that allows a plugin implementing it to decide if a query should be routed into its server block. * add new plugin "view", that uses the new interface to enable a user to define expression based conditions that must be met for a query to be routed to its server block. Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
Diffstat (limited to 'plugin/pkg/expression/expression_test.go')
-rw-r--r--plugin/pkg/expression/expression_test.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/plugin/pkg/expression/expression_test.go b/plugin/pkg/expression/expression_test.go
new file mode 100644
index 000000000..b39c67940
--- /dev/null
+++ b/plugin/pkg/expression/expression_test.go
@@ -0,0 +1,73 @@
+package expression
+
+import (
+ "context"
+ "testing"
+
+ "github.com/coredns/coredns/plugin/metadata"
+ "github.com/coredns/coredns/request"
+)
+
+func TestInCidr(t *testing.T) {
+ incidr := DefaultEnv(context.Background(), &request.Request{})["incidr"]
+
+ cases := []struct {
+ ip string
+ cidr string
+ expected bool
+ shouldErr bool
+ }{
+ // positive
+ {ip: "1.2.3.4", cidr: "1.2.0.0/16", expected: true, shouldErr: false},
+ {ip: "10.2.3.4", cidr: "1.2.0.0/16", expected: false, shouldErr: false},
+ {ip: "1:2::3:4", cidr: "1:2::/64", expected: true, shouldErr: false},
+ {ip: "A:2::3:4", cidr: "1:2::/64", expected: false, shouldErr: false},
+ // negative
+ {ip: "1.2.3.4", cidr: "invalid", shouldErr: true},
+ {ip: "invalid", cidr: "1.2.0.0/16", shouldErr: true},
+ }
+
+ for i, c := range cases {
+ r, err := incidr.(func(string, string) (bool, error))(c.ip, c.cidr)
+ if err != nil && !c.shouldErr {
+ t.Errorf("Test %d: unexpected error %v", i, err)
+ continue
+ }
+ if err == nil && c.shouldErr {
+ t.Errorf("Test %d: expected error", i)
+ continue
+ }
+ if c.shouldErr {
+ continue
+ }
+ if r != c.expected {
+ t.Errorf("Test %d: expected %v", i, c.expected)
+ continue
+ }
+ }
+}
+
+func TestMetadata(t *testing.T) {
+ ctx := metadata.ContextWithMetadata(context.Background())
+ metadata.SetValueFunc(ctx, "test/metadata", func() string {
+ return "success"
+ })
+ f := DefaultEnv(ctx, &request.Request{})["metadata"]
+
+ cases := []struct {
+ label string
+ expected string
+ shouldErr bool
+ }{
+ {label: "test/metadata", expected: "success"},
+ {label: "test/nonexistent", expected: ""},
+ }
+
+ for i, c := range cases {
+ r := f.(func(string) string)(c.label)
+ if r != c.expected {
+ t.Errorf("Test %d: expected %v", i, c.expected)
+ continue
+ }
+ }
+}