| 
									
										
										
										
											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-10 02:23:27 -07:00
										 |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		query    string
 | 
					
						
							|  |  |  | 		expected string // output from r.String()
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							| 
									
										
										
										
											2017-08-22 20:44:42 +01:00
										 |  |  | 		// valid SRV request
 | 
					
						
							| 
									
										
										
										
											2018-08-25 20:53:41 +08:00
										 |  |  | 		{"_http._tcp.webs.mynamespace.svc.inter.webs.tests.", "http.tcp..webs.mynamespace.svc"},
 | 
					
						
							| 
									
										
										
										
											2017-08-22 20:44:42 +01:00
										 |  |  | 		// wildcard acceptance
 | 
					
						
							| 
									
										
										
										
											2018-08-25 20:53:41 +08:00
										 |  |  | 		{"*.any.*.any.svc.inter.webs.tests.", "*.any..*.any.svc"},
 | 
					
						
							| 
									
										
										
										
											2017-08-22 20:44:42 +01:00
										 |  |  | 		// A request of endpoint
 | 
					
						
							| 
									
										
										
										
											2018-08-25 20:53:41 +08:00
										 |  |  | 		{"1-2-3-4.webs.mynamespace.svc.inter.webs.tests.", "*.*.1-2-3-4.webs.mynamespace.svc"},
 | 
					
						
							| 
									
										
										
										
											2018-04-18 12:12:28 -04:00
										 |  |  | 		// bare zone
 | 
					
						
							| 
									
										
										
										
											2018-08-25 20:53:41 +08:00
										 |  |  | 		{"inter.webs.tests.", "....."},
 | 
					
						
							| 
									
										
										
										
											2018-04-18 12:12:28 -04:00
										 |  |  | 		// bare svc type
 | 
					
						
							| 
									
										
										
										
											2018-08-25 20:53:41 +08:00
										 |  |  | 		{"svc.inter.webs.tests.", "....."},
 | 
					
						
							| 
									
										
										
										
											2018-04-18 12:12:28 -04:00
										 |  |  | 		// bare pod type
 | 
					
						
							| 
									
										
										
										
											2018-08-25 20:53:41 +08:00
										 |  |  | 		{"pod.inter.webs.tests.", "....."},
 | 
					
						
							| 
									
										
										
										
											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)
 | 
					
						
							| 
									
										
										
										
											2017-08-22 20:44:42 +01:00
										 |  |  | 		m.SetQuestion(tc.query, dns.TypeA)
 | 
					
						
							| 
									
										
										
										
											2017-08-11 12:12:21 +01:00
										 |  |  | 		state := request.Request{Zone: zone, Req: m}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 07:19:41 +01:00
										 |  |  | 		r, e := 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 {
 | 
					
						
							| 
									
										
										
										
											2019-08-22 21:59:12 +08:00
										 |  |  | 			t.Errorf("Test %d, expected (stringified) recordRequest: %s, got %s", i, tc.expected, rs)
 | 
					
						
							| 
									
										
										
										
											2017-08-10 02:23:27 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											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-22 20:44:42 +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.pod.inter.webs.tests.", // Too long.
 | 
					
						
							| 
									
										
										
										
											2017-08-10 01:08:58 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 20:44:42 +01:00
										 |  |  | 	for i, query := range invalid {
 | 
					
						
							| 
									
										
										
										
											2017-08-11 12:12:21 +01:00
										 |  |  | 		m := new(dns.Msg)
 | 
					
						
							| 
									
										
										
										
											2017-08-22 20:44:42 +01:00
										 |  |  | 		m.SetQuestion(query, dns.TypeA)
 | 
					
						
							| 
									
										
										
										
											2017-08-11 12:12:21 +01:00
										 |  |  | 		state := request.Request{Zone: zone, Req: m}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 07:19:41 +01:00
										 |  |  | 		if _, e := parseRequest(state); e == nil {
 | 
					
						
							| 
									
										
										
										
											2017-08-22 20:44:42 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-18 12:12:28 -04:00
										 |  |  | const zone = "inter.webs.tests."
 |