mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-04 03:03:14 -05:00 
			
		
		
		
	mw/kubernetes: fix parseTests (#875)
* mw/kubernetes: fix parseTests Make parse tests table driven to remove a duplicate code, i.e. most of the contents of parse_test.go can be removed as well as the expectString helper function. * cleanup parse tests
This commit is contained in:
		@@ -1,131 +1,67 @@
 | 
				
			|||||||
package kubernetes
 | 
					package kubernetes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"reflect"
 | 
					 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/miekg/dns"
 | 
						"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) {
 | 
					func TestParseRequest(t *testing.T) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tcs map[string]string
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	zone := "intern.webs.tests."
 | 
					 | 
				
			||||||
	k := Kubernetes{Zones: []string{zone}}
 | 
						k := Kubernetes{Zones: []string{zone}}
 | 
				
			||||||
	f := "parseRequest"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Test a valid SRV request
 | 
						tests := []struct {
 | 
				
			||||||
	//
 | 
							query    string
 | 
				
			||||||
	query := "_http._tcp.webs.mynamespace.svc.inter.webs.test."
 | 
							qtype    uint16
 | 
				
			||||||
	r, e := k.parseRequest(query, dns.TypeSRV, zone)
 | 
							expected string // output from r.String()
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								// valid SRV request
 | 
				
			||||||
 | 
								"_http._tcp.webs.mynamespace.svc.inter.webs.test.", dns.TypeSRV,
 | 
				
			||||||
 | 
								"http.tcp..webs.mynamespace.svc.intern.webs.tests..",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								// wildcard acceptance
 | 
				
			||||||
 | 
								"*.any.*.any.svc.inter.webs.test.", dns.TypeSRV,
 | 
				
			||||||
 | 
								"*.any..*.any.svc.intern.webs.tests..",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								// A request of endpoint
 | 
				
			||||||
 | 
								"1-2-3-4.webs.mynamespace.svc.inter.webs.test.", dns.TypeA,
 | 
				
			||||||
 | 
								"..1-2-3-4.webs.mynamespace.svc.intern.webs.tests..",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								"inter.webs.test.", dns.TypeNS,
 | 
				
			||||||
 | 
								"......intern.webs.tests..",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for i, tc := range tests {
 | 
				
			||||||
 | 
							r, e := k.parseRequest(tc.query, tc.qtype, zone)
 | 
				
			||||||
		if e != nil {
 | 
							if e != nil {
 | 
				
			||||||
		t.Errorf("Expected no error from parseRequest(%v, \"SRV\"). Instead got '%v'.", query, e)
 | 
								t.Errorf("Test %d, expected no error, got '%v'.", i, e)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							rs := r.String()
 | 
				
			||||||
	tcs = map[string]string{
 | 
							if rs != tc.expected {
 | 
				
			||||||
		"port":      "http",
 | 
								t.Errorf("Test %d, expected (stringyfied) recordRequest: %s, got %s", i, tc.expected, rs)
 | 
				
			||||||
		"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{
 | 
					func TestParseInvalidRequest(t *testing.T) {
 | 
				
			||||||
		"_http._pcp.webs.mynamespace.svc.inter.webs.test.", // SRV protocol must be tcp or udp
 | 
						k := Kubernetes{Zones: []string{zone}}
 | 
				
			||||||
		"_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
 | 
						invalid := map[string]uint16{
 | 
				
			||||||
 | 
							"_http._tcp.webs.mynamespace.svc.inter.webs.test.": dns.TypeA,   // A requests cannot have port or protocol
 | 
				
			||||||
 | 
							"_http._pcp.webs.mynamespace.svc.inter.webs.test.": dns.TypeSRV, // SRV protocol must be tcp or udp
 | 
				
			||||||
 | 
							"_http._tcp.ep.webs.ns.svc.inter.webs.test.":       dns.TypeSRV, // SRV requests cannot have an endpoint
 | 
				
			||||||
 | 
							"_*._*.webs.mynamespace.svc.inter.webs.test.":      dns.TypeSRV, // SRV request with invalid wildcards
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, q := range invalidSRVQueries {
 | 
						for query, qtype := range invalid {
 | 
				
			||||||
		_, e = k.parseRequest(q, dns.TypeSRV, zone)
 | 
							if _, e := k.parseRequest(query, qtype, zone); e == nil {
 | 
				
			||||||
		if e == nil {
 | 
								t.Errorf("Expected error from %s:%d, got none", query, qtype)
 | 
				
			||||||
			t.Errorf("Expected error from %v(\"%v\", \"SRV\").", f, q)
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const zone = "intern.webs.tests."
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user