| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | package test
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/proxy"
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/test"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const exampleOrg = `; example.org test file
 | 
					
						
							|  |  |  | example.org.		IN	SOA	sns.dns.icann.org. noc.dns.icann.org. 2015082541 7200 3600 1209600 3600
 | 
					
						
							|  |  |  | example.org.		IN	NS	b.iana-servers.net.
 | 
					
						
							|  |  |  | example.org.		IN	NS	a.iana-servers.net.
 | 
					
						
							|  |  |  | example.org.		IN	A	127.0.0.1
 | 
					
						
							| 
									
										
										
										
											2016-04-26 20:45:29 +01:00
										 |  |  | example.org.		IN	A	127.0.0.2
 | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | `
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestLookupProxy(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	name, rm, err := test.TempFile(t, ".", exampleOrg)
 | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("failed to created zone: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	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
										 |  |  | 
 | 
					
						
							|  |  |  | 	i, err := CoreDNSServer(corefile)
 | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		t.Fatalf("could not get CoreDNS serving instance: %s", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	udp, _ := CoreDNSServerPorts(i, 0)
 | 
					
						
							|  |  |  | 	if udp == "" {
 | 
					
						
							|  |  |  | 		t.Fatalf("could not get udp listening port")
 | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	defer i.Stop()
 | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	log.SetOutput(ioutil.Discard)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	p := proxy.New([]string{udp})
 | 
					
						
							|  |  |  | 	state := middleware.State{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
 | 
					
						
							|  |  |  | 	resp, err := p.Lookup(state, "example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		t.Fatal("Expected to receive reply, but didn't")
 | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	// expect answer section with A record in it
 | 
					
						
							|  |  |  | 	if len(resp.Answer) == 0 {
 | 
					
						
							|  |  |  | 		t.Error("Expected to at least one RR in the answer section, got none")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	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-08-20 23:03:36 +01:00
										 |  |  | 		t.Errorf("Expected 127.0.0.1, got: %d", resp.Answer[0].(*dns.A).A.String())
 | 
					
						
							| 
									
										
										
										
											2016-04-11 15:56:22 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |