| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | package test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test" | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestLookupProxy(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-01-12 08:13:50 +00:00
										 |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2016-10-02 15:58:01 +01:00
										 |  |  | 	name, rm, err := test.TempFile(".", exampleOrg) | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-01 15:12:49 +01:00
										 |  |  | 		t.Fatalf("Failed to create zone: %s", err) | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	defer rm() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	corefile := `example.org:0 { | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  |        file ` + name + ` | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | } | 
					
						
							|  |  |  | ` | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 11:35:14 +01:00
										 |  |  | 	i, udp, _, err := CoreDNSServerAndPorts(corefile) | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	defer i.Stop() | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	m := new(dns.Msg) | 
					
						
							|  |  |  | 	m.SetQuestion("example.org.", dns.TypeA) | 
					
						
							|  |  |  | 	resp, err := dns.Exchange(m, udp) | 
					
						
							| 
									
										
										
										
											2017-03-14 21:32:21 +00:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Expected to receive reply, but didn't") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// expect answer section with A record in it | 
					
						
							|  |  |  | 	if len(resp.Answer) == 0 { | 
					
						
							|  |  |  | 		t.Fatalf("Expected to at least one RR in the answer section, got none: %s", resp) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if resp.Answer[0].Header().Rrtype != dns.TypeA { | 
					
						
							| 
									
										
										
										
											2016-08-20 23:03:36 +01:00
										 |  |  | 		t.Errorf("Expected RR to A, got: %d", resp.Answer[0].Header().Rrtype) | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if resp.Answer[0].(*dns.A).A.String() != "127.0.0.1" { | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | 		t.Errorf("Expected 127.0.0.1, got: %s", resp.Answer[0].(*dns.A).A.String()) | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-18 19:50:38 +01:00
										 |  |  | func BenchmarkProxyLookup(b *testing.B) { | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	t := new(testing.T) | 
					
						
							| 
									
										
										
										
											2016-10-08 15:07:07 +01:00
										 |  |  | 	name, rm, err := test.TempFile(".", exampleOrg) | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-01 15:12:49 +01:00
										 |  |  | 		t.Fatalf("Failed to created zone: %s", err) | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	defer rm() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	corefile := `example.org:0 { | 
					
						
							|  |  |  |        file ` + name + ` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	i, err := CoreDNSServer(corefile) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-01 15:12:49 +01:00
										 |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err) | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	udp, _ := CoreDNSServerPorts(i, 0) | 
					
						
							|  |  |  | 	if udp == "" { | 
					
						
							| 
									
										
										
										
											2018-06-01 15:12:49 +01:00
										 |  |  | 		t.Fatalf("Could not get udp listening port") | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	defer i.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	m := new(dns.Msg) | 
					
						
							|  |  |  | 	m.SetQuestion("example.org.", dns.TypeA) | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	b.ResetTimer() | 
					
						
							|  |  |  | 	for i := 0; i < b.N; i++ { | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 		if _, err := dns.Exchange(m, udp); err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 			b.Fatal("Expected to receive reply, but didn't") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |