coredns/middleware/kubernetes/parse_test.go
Miek Gieben 6a4e69eb9f mw/kubernetes: Rewrite parseRequest and Readability improvements ()
* mw/kubernetes: rewrite parseRequest

Stop looking at the qtype in parseRequest and make k.Namespace a map.
Fallout from this is that pkg/strings as it is not used anymore. Also
add a few helper functions to make unexposed namespaces easier to see in
the code.

Add wildcard tests to the middleware tests.

* Fix tests

Add a whole bunch of comments to document what we are trying to do.

* This is now answered

* up coverage

* duh

* Update testcase

* Make it nodata
2017-08-22 12:44:42 -07:00

60 lines
1.5 KiB
Go

package kubernetes
import (
"testing"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
func TestParseRequest(t *testing.T) {
k := New([]string{zone})
tests := []struct {
query string
expected string // output from r.String()
}{
// valid SRV request
{"_http._tcp.webs.mynamespace.svc.inter.webs.test.", "http.tcp..webs.mynamespace.svc"},
// wildcard acceptance
{"*.any.*.any.svc.inter.webs.test.", "*.any..*.any.svc"},
// A request of endpoint
{"1-2-3-4.webs.mynamespace.svc.inter.webs.test.", "*.*.1-2-3-4.webs.mynamespace.svc"},
}
for i, tc := range tests {
m := new(dns.Msg)
m.SetQuestion(tc.query, dns.TypeA)
state := request.Request{Zone: zone, Req: m}
r, e := k.parseRequest(state)
if e != nil {
t.Errorf("Test %d, expected no error, got '%v'.", i, e)
}
rs := r.String()
if rs != tc.expected {
t.Errorf("Test %d, expected (stringyfied) recordRequest: %s, got %s", i, tc.expected, rs)
}
}
}
func TestParseInvalidRequest(t *testing.T) {
k := New([]string{zone})
invalid := []string{
"webs.mynamespace.pood.inter.webs.test.", // Request must be for pod or svc subdomain.
"too.long.for.what.I.am.trying.to.pod.inter.webs.tests.", // Too long.
}
for i, query := range invalid {
m := new(dns.Msg)
m.SetQuestion(query, dns.TypeA)
state := request.Request{Zone: zone, Req: m}
if _, e := k.parseRequest(state); e == nil {
t.Errorf("Test %d: expected error from %s, got none", i, query)
}
}
}
const zone = "intern.webs.tests."