diff options
author | 2017-08-10 01:08:58 -0700 | |
---|---|---|
committer | 2017-08-10 01:08:58 -0700 | |
commit | 7e56cc74e5420bfc1670ddf858420909fdaf3fee (patch) | |
tree | b8d2992c476a57c11082239c9501761f19f52ef8 /middleware/kubernetes/parse_test.go | |
parent | fefc4374d72086d6325019471677a6ae73622a63 (diff) | |
download | coredns-7e56cc74e5420bfc1670ddf858420909fdaf3fee.tar.gz coredns-7e56cc74e5420bfc1670ddf858420909fdaf3fee.tar.zst coredns-7e56cc74e5420bfc1670ddf858420909fdaf3fee.zip |
WIP: Parserequest2 cutback (#868)
* middleware/kubernetes: pull TXT out of parseRequest
Put the TXT handling one layer higher and remove it from parseRequest.
Also rename the podsvc field in there to podOrSvc. Now that it isn't
used anymore for TXT record (dns-version) that was put in there. We can
make this a boolean (in a future PR).
Make parseRequest get an optional Zone that is from state.Zone and use
that instead of its own code. Removed some tests and other smaller
cleanups.
Fixes #836
* add this reverse
* another check
* readd
* Rename to kPod and kService for some clarity
Diffstat (limited to 'middleware/kubernetes/parse_test.go')
-rw-r--r-- | middleware/kubernetes/parse_test.go | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/middleware/kubernetes/parse_test.go b/middleware/kubernetes/parse_test.go new file mode 100644 index 000000000..06c3711cd --- /dev/null +++ b/middleware/kubernetes/parse_test.go @@ -0,0 +1,131 @@ +package kubernetes + +import ( + "reflect" + "testing" + + "github.com/miekg/dns" +) + +func expectString(t *testing.T, function, qtype, query string, r *recordRequest, field, expected string) { + ref := reflect.ValueOf(r) + refField := reflect.Indirect(ref).FieldByName(field) + got := refField.String() + if got != expected { + t.Errorf("Expected %v(%v, \"%v\") to get %v == \"%v\". Instead got \"%v\".", function, query, qtype, field, expected, got) + } +} + +func TestParseRequest(t *testing.T) { + + var tcs map[string]string + + zone := "intern.webs.tests." + k := Kubernetes{Zones: []string{zone}} + f := "parseRequest" + + // Test a valid SRV request + // + query := "_http._tcp.webs.mynamespace.svc.inter.webs.test." + r, e := k.parseRequest(query, dns.TypeSRV, zone) + if e != nil { + t.Errorf("Expected no error from parseRequest(%v, \"SRV\"). Instead got '%v'.", query, e) + } + + tcs = map[string]string{ + "port": "http", + "protocol": "tcp", + "endpoint": "", + "service": "webs", + "namespace": "mynamespace", + "podOrSvc": Svc, + "zone": zone, + } + for field, expected := range tcs { + expectString(t, f, "SRV", query, &r, field, expected) + } + + // Test wildcard acceptance + // + query = "*.any.*.any.svc.inter.webs.test." + r, e = k.parseRequest(query, dns.TypeSRV, zone) + if e != nil { + t.Errorf("Expected no error from parseRequest(\"%v\", \"SRV\"). Instead got '%v'.", query, e) + } + + tcs = map[string]string{ + "port": "*", + "protocol": "any", + "endpoint": "", + "service": "*", + "namespace": "any", + "podOrSvc": Svc, + "zone": zone, + } + for field, expected := range tcs { + expectString(t, f, "SRV", query, &r, field, expected) + } + + // Test A request of endpoint + query = "1-2-3-4.webs.mynamespace.svc.inter.webs.test." + r, e = k.parseRequest(query, dns.TypeA, zone) + if e != nil { + t.Errorf("Expected no error from parseRequest(\"%v\", \"A\"). Instead got '%v'.", query, e) + } + tcs = map[string]string{ + "port": "", + "protocol": "", + "endpoint": "1-2-3-4", + "service": "webs", + "namespace": "mynamespace", + "podOrSvc": Svc, + "zone": zone, + } + for field, expected := range tcs { + expectString(t, f, "A", query, &r, field, expected) + } + + // Test NS request + query = "inter.webs.test." + r, e = k.parseRequest(query, dns.TypeNS, zone) + if e != nil { + t.Errorf("Expected no error from parseRequest(\"%v\", \"NS\"). Instead got '%v'.", query, e) + } + tcs = map[string]string{ + "port": "", + "protocol": "", + "endpoint": "", + "service": "", + "namespace": "", + "podOrSvc": "", + "zone": zone, + } + for field, expected := range tcs { + expectString(t, f, "NS", query, &r, field, expected) + } + + // Invalid query tests + invalidAQueries := []string{ + "_http._tcp.webs.mynamespace.svc.inter.webs.test.", // A requests cannot have port or protocol TODO(miek): this must return NODATA + + } + for _, q := range invalidAQueries { + _, e = k.parseRequest(q, dns.TypeA, zone) + if e == nil { + t.Errorf("Expected error from %v(\"%v\", \"A\").", f, q) + } + } + + invalidSRVQueries := []string{ + "_http._pcp.webs.mynamespace.svc.inter.webs.test.", // SRV protocol must be tcp or udp + "_http._tcp.ep.webs.ns.svc.inter.webs.test.", // SRV requests cannot have an endpoint + "_*._*.webs.mynamespace.svc.inter.webs.test.", // SRV request with invalid wildcards + } + + for _, q := range invalidSRVQueries { + _, e = k.parseRequest(q, dns.TypeSRV, zone) + if e == nil { + t.Errorf("Expected error from %v(\"%v\", \"SRV\").", f, q) + } + } +} |