aboutsummaryrefslogtreecommitdiff
path: root/plugin/kubernetes/handler_test.go
diff options
context:
space:
mode:
authorGravatar Chris O'Haver <cohaver@infoblox.com> 2022-02-22 09:38:57 -0500
committerGravatar GitHub <noreply@github.com> 2022-02-22 09:38:57 -0500
commit66dc74caebd4f4bdb8bd38d03b52611488424594 (patch)
tree41c8e56a785c45c138776b5d0d622507dd1726b6 /plugin/kubernetes/handler_test.go
parentd3a118e1c175eb00a0c171c2d2616dbaf39cdbba (diff)
downloadcoredns-66dc74caebd4f4bdb8bd38d03b52611488424594.tar.gz
coredns-66dc74caebd4f4bdb8bd38d03b52611488424594.tar.zst
coredns-66dc74caebd4f4bdb8bd38d03b52611488424594.zip
plugin/etcd+kubernetes: Persist truncated state to client if CNAME lookup response is truncated (#4715)
Persist the TC bit to client response for truncated CNAME lookups. Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
Diffstat (limited to 'plugin/kubernetes/handler_test.go')
-rw-r--r--plugin/kubernetes/handler_test.go236
1 files changed, 155 insertions, 81 deletions
diff --git a/plugin/kubernetes/handler_test.go b/plugin/kubernetes/handler_test.go
index 4ff931ba1..523341fda 100644
--- a/plugin/kubernetes/handler_test.go
+++ b/plugin/kubernetes/handler_test.go
@@ -8,48 +8,56 @@ import (
"github.com/coredns/coredns/plugin/kubernetes/object"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
+ "github.com/coredns/coredns/request"
"github.com/miekg/dns"
api "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)
-var dnsTestCases = []test.Case{
+type kubeTestCase struct {
+ Upstream Upstreamer
+ Truncated bool
+ test.Case
+}
+
+var dnsTestCases = []kubeTestCase{
// A Service
- {
+ {Case: test.Case{
Qname: "svc1.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("svc1.testns.svc.cluster.local. 5 IN A 10.0.0.1"),
},
- },
- {
+ }},
+ {Case: test.Case{
Qname: "svcempty.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("svcempty.testns.svc.cluster.local. 5 IN A 10.0.0.1"),
},
- },
- {
+ }},
+ {Case: test.Case{
Qname: "svc1.testns.svc.cluster.local.", Qtype: dns.TypeSRV,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{test.SRV("svc1.testns.svc.cluster.local. 5 IN SRV 0 100 80 svc1.testns.svc.cluster.local.")},
Extra: []dns.RR{test.A("svc1.testns.svc.cluster.local. 5 IN A 10.0.0.1")},
- },
- {
+ }},
+ {Case: test.Case{
Qname: "svcempty.testns.svc.cluster.local.", Qtype: dns.TypeSRV,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{test.SRV("svcempty.testns.svc.cluster.local. 5 IN SRV 0 100 80 svcempty.testns.svc.cluster.local.")},
Extra: []dns.RR{test.A("svcempty.testns.svc.cluster.local. 5 IN A 10.0.0.1")},
- },
- {
+ }},
+ {Case: test.Case{
Qname: "svc6.testns.svc.cluster.local.", Qtype: dns.TypeSRV,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{test.SRV("svc6.testns.svc.cluster.local. 5 IN SRV 0 100 80 svc6.testns.svc.cluster.local.")},
Extra: []dns.RR{test.AAAA("svc6.testns.svc.cluster.local. 5 IN AAAA 1234:abcd::1")},
- },
+ }},
// SRV Service
- {
+ {Case: test.Case{
+
Qname: "_http._tcp.svc1.testns.svc.cluster.local.", Qtype: dns.TypeSRV,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
@@ -58,8 +66,9 @@ var dnsTestCases = []test.Case{
Extra: []dns.RR{
test.A("svc1.testns.svc.cluster.local. 5 IN A 10.0.0.1"),
},
- },
- {
+ }},
+ {Case: test.Case{
+
Qname: "_http._tcp.svcempty.testns.svc.cluster.local.", Qtype: dns.TypeSRV,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
@@ -68,9 +77,9 @@ var dnsTestCases = []test.Case{
Extra: []dns.RR{
test.A("svcempty.testns.svc.cluster.local. 5 IN A 10.0.0.1"),
},
- },
+ }},
// A Service (Headless)
- {
+ {Case: test.Case{
Qname: "hdls1.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
@@ -79,50 +88,50 @@ var dnsTestCases = []test.Case{
test.A("hdls1.testns.svc.cluster.local. 5 IN A 172.0.0.4"),
test.A("hdls1.testns.svc.cluster.local. 5 IN A 172.0.0.5"),
},
- },
+ }},
// A Service (Headless and Portless)
- {
+ {Case: test.Case{
Qname: "hdlsprtls.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("hdlsprtls.testns.svc.cluster.local. 5 IN A 172.0.0.20"),
},
- },
+ }},
// An Endpoint with no port
- {
+ {Case: test.Case{
Qname: "172-0-0-20.hdlsprtls.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("172-0-0-20.hdlsprtls.testns.svc.cluster.local. 5 IN A 172.0.0.20"),
},
- },
+ }},
// An Endpoint ip
- {
+ {Case: test.Case{
Qname: "172-0-0-2.hdls1.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("172-0-0-2.hdls1.testns.svc.cluster.local. 5 IN A 172.0.0.2"),
},
- },
+ }},
// A Endpoint ip
- {
+ {Case: test.Case{
Qname: "172-0-0-3.hdls1.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("172-0-0-3.hdls1.testns.svc.cluster.local. 5 IN A 172.0.0.3"),
},
- },
+ }},
// An Endpoint by name
- {
+ {Case: test.Case{
Qname: "dup-name.hdls1.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("dup-name.hdls1.testns.svc.cluster.local. 5 IN A 172.0.0.4"),
test.A("dup-name.hdls1.testns.svc.cluster.local. 5 IN A 172.0.0.5"),
},
- },
+ }},
// SRV Service (Headless)
- {
+ {Case: test.Case{
Qname: "_http._tcp.hdls1.testns.svc.cluster.local.", Qtype: dns.TypeSRV,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
@@ -140,8 +149,8 @@ var dnsTestCases = []test.Case{
test.A("dup-name.hdls1.testns.svc.cluster.local. 5 IN A 172.0.0.4"),
test.A("dup-name.hdls1.testns.svc.cluster.local. 5 IN A 172.0.0.5"),
},
- },
- { // An A record query for an existing headless service should return a record for each of its ipv4 endpoints
+ }},
+ {Case: test.Case{ // An A record query for an existing headless service should return a record for each of its ipv4 endpoints
Qname: "hdls1.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
@@ -150,96 +159,120 @@ var dnsTestCases = []test.Case{
test.A("hdls1.testns.svc.cluster.local. 5 IN A 172.0.0.4"),
test.A("hdls1.testns.svc.cluster.local. 5 IN A 172.0.0.5"),
},
- },
+ }},
// AAAA
- {
+ {Case: test.Case{
Qname: "5678-abcd--2.hdls1.testns.svc.cluster.local", Qtype: dns.TypeAAAA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{test.AAAA("5678-abcd--2.hdls1.testns.svc.cluster.local. 5 IN AAAA 5678:abcd::2")},
- },
+ }},
// CNAME External
- {
+ {Case: test.Case{
Qname: "external.testns.svc.cluster.local.", Qtype: dns.TypeCNAME,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.CNAME("external.testns.svc.cluster.local. 5 IN CNAME ext.interwebs.test."),
},
+ }},
+ // CNAME External Truncated Lookup
+ {
+ Case: test.Case{
+ Qname: "external.testns.svc.cluster.local.", Qtype: dns.TypeA,
+ Rcode: dns.RcodeSuccess,
+ Answer: []dns.RR{
+ test.A("ext.interwebs.test. 5 IN A 1.2.3.4"),
+ test.CNAME("external.testns.svc.cluster.local. 5 IN CNAME ext.interwebs.test."),
+ },
+ },
+ Upstream: &Upstub{
+ Truncated: true,
+ Qclass: dns.ClassINET,
+ Case: test.Case{
+ Qname: "external.testns.svc.cluster.local.",
+ Qtype: dns.TypeA,
+ Answer: []dns.RR{
+ test.A("ext.interwebs.test. 5 IN A 1.2.3.4"),
+ test.CNAME("external.testns.svc.cluster.local. 5 IN CNAME ext.interwebs.test."),
+ },
+ },
+ },
+ Truncated: true,
},
// CNAME External To Internal Service
- {
+ {Case: test.Case{
Qname: "external-to-service.testns.svc.cluster.local", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.CNAME("external-to-service.testns.svc.cluster.local. 5 IN CNAME svc1.testns.svc.cluster.local."),
test.A("svc1.testns.svc.cluster.local. 5 IN A 10.0.0.1"),
},
- },
+ }},
// AAAA Service (with an existing A record, but no AAAA record)
- {
+ {Case: test.Case{
Qname: "svc1.testns.svc.cluster.local.", Qtype: dns.TypeAAAA,
Rcode: dns.RcodeSuccess,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// AAAA Service (non-existing service)
- {
+ {Case: test.Case{
Qname: "svc0.testns.svc.cluster.local.", Qtype: dns.TypeAAAA,
Rcode: dns.RcodeNameError,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// A Service (non-existing service)
- {
+ {Case: test.Case{
Qname: "svc0.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeNameError,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// A Service (non-existing namespace)
- {
+ {Case: test.Case{
Qname: "svc0.svc-nons.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeNameError,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// TXT Schema
- {
+ {Case: test.Case{
Qname: "dns-version.cluster.local.", Qtype: dns.TypeTXT,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.TXT("dns-version.cluster.local 28800 IN TXT 1.1.0"),
},
- },
+ }},
// A Service (Headless) does not exist
- {
+ {Case: test.Case{
Qname: "bogusendpoint.hdls1.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeNameError,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// A Service does not exist
- {
+ {Case: test.Case{
Qname: "bogusendpoint.svc0.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeNameError,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// AAAA Service
- {
+ {Case: test.Case{
Qname: "svc6.testns.svc.cluster.local.", Qtype: dns.TypeAAAA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.AAAA("svc6.testns.svc.cluster.local. 5 IN AAAA 1234:abcd::1"),
},
- },
+ }},
// SRV
- {
+ {Case: test.Case{
Qname: "_http._tcp.svc6.testns.svc.cluster.local.", Qtype: dns.TypeSRV,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
@@ -248,94 +281,94 @@ var dnsTestCases = []test.Case{
Extra: []dns.RR{
test.AAAA("svc6.testns.svc.cluster.local. 5 IN AAAA 1234:abcd::1"),
},
- },
+ }},
// AAAA Service (Headless)
- {
+ {Case: test.Case{
Qname: "hdls1.testns.svc.cluster.local.", Qtype: dns.TypeAAAA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.AAAA("hdls1.testns.svc.cluster.local. 5 IN AAAA 5678:abcd::1"),
test.AAAA("hdls1.testns.svc.cluster.local. 5 IN AAAA 5678:abcd::2"),
},
- },
+ }},
// AAAA Endpoint
- {
+ {Case: test.Case{
Qname: "5678-abcd--1.hdls1.testns.svc.cluster.local.", Qtype: dns.TypeAAAA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.AAAA("5678-abcd--1.hdls1.testns.svc.cluster.local. 5 IN AAAA 5678:abcd::1"),
},
- },
+ }},
- {
+ {Case: test.Case{
Qname: "svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
- {
+ }},
+ {Case: test.Case{
Qname: "pod.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
- {
+ }},
+ {Case: test.Case{
Qname: "testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// NS query for qname != zone (existing domain)
- {
+ {Case: test.Case{
Qname: "svc.cluster.local.", Qtype: dns.TypeNS,
Rcode: dns.RcodeSuccess,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// NS query for qname != zone (existing domain)
- {
+ {Case: test.Case{
Qname: "testns.svc.cluster.local.", Qtype: dns.TypeNS,
Rcode: dns.RcodeSuccess,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// NS query for qname != zone (non existing domain)
- {
+ {Case: test.Case{
Qname: "foo.cluster.local.", Qtype: dns.TypeNS,
Rcode: dns.RcodeNameError,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// NS query for qname != zone (non existing domain)
- {
+ {Case: test.Case{
Qname: "foo.svc.cluster.local.", Qtype: dns.TypeNS,
Rcode: dns.RcodeNameError,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
// Dual Stack ClusterIP Services
- {
+ {Case: test.Case{
Qname: "svc-dual-stack.testns.svc.cluster.local.", Qtype: dns.TypeA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.A("svc-dual-stack.testns.svc.cluster.local. 5 IN A 10.0.0.3"),
},
- },
- {
+ }},
+ {Case: test.Case{
Qname: "svc-dual-stack.testns.svc.cluster.local.", Qtype: dns.TypeAAAA,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.AAAA("svc-dual-stack.testns.svc.cluster.local. 5 IN AAAA 10::3"),
},
- },
- {
+ }},
+ {Case: test.Case{
Qname: "svc-dual-stack.testns.svc.cluster.local.", Qtype: dns.TypeSRV,
Rcode: dns.RcodeSuccess,
Answer: []dns.RR{test.SRV("svc-dual-stack.testns.svc.cluster.local. 5 IN SRV 0 50 80 svc-dual-stack.testns.svc.cluster.local.")},
@@ -343,14 +376,14 @@ var dnsTestCases = []test.Case{
test.A("svc-dual-stack.testns.svc.cluster.local. 5 IN A 10.0.0.3"),
test.AAAA("svc-dual-stack.testns.svc.cluster.local. 5 IN AAAA 10::3"),
},
- },
- {
+ }},
+ {Case: test.Case{
Qname: "svc1.testns.svc.cluster.local.", Qtype: dns.TypeSOA,
Rcode: dns.RcodeSuccess,
Ns: []dns.RR{
test.SOA("cluster.local. 5 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 5"),
},
- },
+ }},
}
func TestServeDNS(t *testing.T) {
@@ -361,6 +394,8 @@ func TestServeDNS(t *testing.T) {
ctx := context.TODO()
for i, tc := range dnsTestCases {
+ k.Upstream = tc.Upstream
+
r := tc.Msg()
w := dnstest.NewRecorder(&test.ResponseWriter{})
@@ -379,12 +414,16 @@ func TestServeDNS(t *testing.T) {
t.Fatalf("Test %d, got nil message and no error for %q", i, r.Question[0].Name)
}
+ if tc.Truncated != resp.Truncated {
+ t.Errorf("Expected truncation %t, got truncation %t", tc.Truncated, resp.Truncated)
+ }
+
// Before sorting, make sure that CNAMES do not appear after their target records
if err := test.CNAMEOrder(resp); err != nil {
t.Errorf("Test %d, %v", i, err)
}
- if err := test.SortAndCheck(resp, tc); err != nil {
+ if err := test.SortAndCheck(resp, tc.Case); err != nil {
t.Errorf("Test %d, %v", i, err)
}
}
@@ -740,3 +779,38 @@ func (APIConnServeTest) GetNamespaceByName(name string) (*object.Namespace, erro
Name: name,
}, nil
}
+
+// Upstub implements an Upstreamer that returns a set response for test purposes
+type Upstub struct {
+ test.Case
+ Truncated bool
+ Qclass uint16
+}
+
+// Lookup returns a set response
+func (t *Upstub) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
+ var answer []dns.RR
+ // if query type is not CNAME, remove any CNAME with same name as qname from the answer
+ if t.Qtype != dns.TypeCNAME {
+ for _, a := range t.Answer {
+ if c, ok := a.(*dns.CNAME); ok && c.Header().Name == t.Qname {
+ continue
+ }
+ answer = append(answer, a)
+ }
+ } else {
+ answer = t.Answer
+ }
+
+ return &dns.Msg{
+ MsgHdr: dns.MsgHdr{
+ Response: true,
+ Truncated: t.Truncated,
+ Rcode: t.Rcode,
+ },
+ Question: []dns.Question{{Name: t.Qname, Qtype: t.Qtype, Qclass: t.Qclass}},
+ Answer: answer,
+ Extra: t.Extra,
+ Ns: t.Ns,
+ }, nil
+}