| 
									
										
										
										
											2017-08-10 01:08:58 -07:00
										 |  |  | package kubernetes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 12:12:21 +01:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 01:08:58 -07:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestParseRequest(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 	k := New([]string{zone})
 | 
					
						
							| 
									
										
										
										
											2017-08-10 01:08:58 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 02:23:27 -07:00
										 |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		query    string
 | 
					
						
							|  |  |  | 		qtype    uint16
 | 
					
						
							|  |  |  | 		expected string // output from r.String()
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			// valid SRV request
 | 
					
						
							|  |  |  | 			"_http._tcp.webs.mynamespace.svc.inter.webs.test.", dns.TypeSRV,
 | 
					
						
							| 
									
										
										
										
											2017-08-19 07:18:35 +01:00
										 |  |  | 			"http.tcp..webs.mynamespace.svc",
 | 
					
						
							| 
									
										
										
										
											2017-08-10 02:23:27 -07:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			// wildcard acceptance
 | 
					
						
							|  |  |  | 			"*.any.*.any.svc.inter.webs.test.", dns.TypeSRV,
 | 
					
						
							| 
									
										
										
										
											2017-08-18 19:40:12 +01:00
										 |  |  | 			"*.*.any.*.any.svc",
 | 
					
						
							| 
									
										
										
										
											2017-08-10 02:23:27 -07:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			// A request of endpoint
 | 
					
						
							|  |  |  | 			"1-2-3-4.webs.mynamespace.svc.inter.webs.test.", dns.TypeA,
 | 
					
						
							| 
									
										
										
										
											2017-08-18 19:40:12 +01:00
										 |  |  | 			"*.*.1-2-3-4.webs.mynamespace.svc",
 | 
					
						
							| 
									
										
										
										
											2017-08-10 02:23:27 -07:00
										 |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for i, tc := range tests {
 | 
					
						
							| 
									
										
										
										
											2017-08-11 12:12:21 +01:00
										 |  |  | 		m := new(dns.Msg)
 | 
					
						
							|  |  |  | 		m.SetQuestion(tc.query, tc.qtype)
 | 
					
						
							|  |  |  | 		state := request.Request{Zone: zone, Req: m}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		r, e := k.parseRequest(state)
 | 
					
						
							| 
									
										
										
										
											2017-08-10 02:23:27 -07:00
										 |  |  | 		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)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-08-10 01:08:58 -07:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-08-10 02:23:27 -07:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-08-10 01:08:58 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 02:23:27 -07:00
										 |  |  | func TestParseInvalidRequest(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  | 	k := New([]string{zone})
 | 
					
						
							| 
									
										
										
										
											2017-08-10 01:08:58 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 19:40:12 +01:00
										 |  |  | 	invalid := []string{
 | 
					
						
							|  |  |  | 		"webs.mynamespace.pood.inter.webs.test.",                // Request must be for pod or svc subdomain.
 | 
					
						
							|  |  |  | 		"too.long.for.what.I.am.trying.to.do.inter.webs.tests.", // Too long.
 | 
					
						
							| 
									
										
										
										
											2017-08-10 01:08:58 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 19:40:12 +01:00
										 |  |  | 	for i, query := range invalid {
 | 
					
						
							| 
									
										
										
										
											2017-08-11 12:12:21 +01:00
										 |  |  | 		m := new(dns.Msg)
 | 
					
						
							| 
									
										
										
										
											2017-08-18 19:40:12 +01:00
										 |  |  | 		m.SetQuestion(query, dns.TypeA)
 | 
					
						
							| 
									
										
										
										
											2017-08-11 12:12:21 +01:00
										 |  |  | 		state := request.Request{Zone: zone, Req: m}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if _, e := k.parseRequest(state); e == nil {
 | 
					
						
							| 
									
										
										
										
											2017-08-18 19:40:12 +01:00
										 |  |  | 			t.Errorf("Test %d: expected error from %s, got none", i, query)
 | 
					
						
							| 
									
										
										
										
											2017-08-10 01:08:58 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-08-10 02:23:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const zone = "intern.webs.tests."
 |