| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | // +build etcd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package etcd | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 	"net" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/etcd/msg" | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnsrecorder" | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/test" | 
					
						
							| 
									
										
										
										
											2016-07-04 21:13:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | func fakeStubServerExampleNet(t *testing.T) (*dns.Server, string) { | 
					
						
							| 
									
										
										
										
											2016-10-08 18:19:42 +01:00
										 |  |  | 	server, addr, err := test.UDPServer("127.0.0.1:0") | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("failed to create a UDP server: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// add handler for example.net | 
					
						
							|  |  |  | 	dns.HandleFunc("example.net.", func(w dns.ResponseWriter, r *dns.Msg) { | 
					
						
							|  |  |  | 		m := new(dns.Msg) | 
					
						
							|  |  |  | 		m.SetReply(r) | 
					
						
							|  |  |  | 		m.Answer = []dns.RR{test.A("example.net.	86400	IN	A	93.184.216.34")} | 
					
						
							|  |  |  | 		w.WriteMsg(m) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return server, addr | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | func TestStubLookup(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 	server, addr := fakeStubServerExampleNet(t) | 
					
						
							|  |  |  | 	defer server.Shutdown() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	host, p, _ := net.SplitHostPort(addr) | 
					
						
							|  |  |  | 	port, _ := strconv.Atoi(p) | 
					
						
							|  |  |  | 	exampleNetStub := &msg.Service{Host: host, Port: port, Key: "a.example.net.stub.dns.skydns.test."} | 
					
						
							|  |  |  | 	servicesStub = append(servicesStub, exampleNetStub) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	etc := newEtcdMiddleware() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | 	for _, serv := range servicesStub { | 
					
						
							|  |  |  | 		set(t, etc, serv.Key, 0, serv) | 
					
						
							|  |  |  | 		defer delete(t, etc, serv.Key) | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | 	etc.updateStubZones() | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | 	for _, tc := range dnsTestCasesStub { | 
					
						
							|  |  |  | 		m := tc.Msg() | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		rec := dnsrecorder.New(&test.ResponseWriter{}) | 
					
						
							| 
									
										
										
										
											2016-06-11 08:00:47 +10:00
										 |  |  | 		_, err := etc.ServeDNS(ctxt, rec, m) | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 		if err != nil && m.Question[0].Name == "example.org." { | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | 			// This is OK, we expect this backend to *not* work. | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Errorf("expected no error, got %v for %s\n", err, m.Question[0].Name) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		resp := rec.Msg | 
					
						
							| 
									
										
										
										
											2016-04-15 20:22:37 +01:00
										 |  |  | 		if resp == nil { | 
					
						
							|  |  |  | 			// etcd not running? | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 15:30:58 +01:00
										 |  |  | 		test.SortAndCheck(t, resp, tc) | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | var servicesStub = []*msg.Service{ | 
					
						
							|  |  |  | 	// Two tests, ask a question that should return servfail because remote it no accessible | 
					
						
							|  |  |  | 	// and one with edns0 option added, that should return refused. | 
					
						
							|  |  |  | 	{Host: "127.0.0.1", Port: 666, Key: "b.example.org.stub.dns.skydns.test."}, | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | var dnsTestCasesStub = []test.Case{ | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		Qname: "example.org.", Qtype: dns.TypeA, Rcode: dns.RcodeServerFailure, | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		Qname: "example.net.", Qtype: dns.TypeA, | 
					
						
							|  |  |  | 		Answer: []dns.RR{test.A("example.net.	86400	IN	A	93.184.216.34")}, | 
					
						
							|  |  |  | 		Extra: []dns.RR{test.OPT(4096, false)}, // This will have an EDNS0 section, because *we* added our local stub forward to detect loops. | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | } |