| 
									
										
										
										
											2016-10-18 12:05:19 +01:00
										 |  |  | // +build etcd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-06-01 15:12:49 +01:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2016-10-18 12:05:19 +01:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/etcd/msg" | 
					
						
							| 
									
										
										
										
											2016-10-18 12:05:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // uses some stuff from etcd_tests.go | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 10:52:43 +01:00
										 |  |  | func TestEtcdCache(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2016-10-18 12:05:19 +01:00
										 |  |  | 	corefile := `.:0 { | 
					
						
							|  |  |  |     etcd skydns.test { | 
					
						
							|  |  |  |         path /skydns | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     cache skydns.test | 
					
						
							|  |  |  | }` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-24 11:35:14 +01:00
										 |  |  | 	ex, udp, _, err := CoreDNSServerAndPorts(corefile) | 
					
						
							| 
									
										
										
										
											2016-10-18 12:05:19 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer ex.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 14:13:28 +01:00
										 |  |  | 	etc := etcdPlugin() | 
					
						
							| 
									
										
										
										
											2016-10-18 12:05:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var ctx = context.TODO() | 
					
						
							|  |  |  | 	for _, serv := range servicesCacheTest { | 
					
						
							|  |  |  | 		set(ctx, t, etc, serv.Key, 0, serv) | 
					
						
							|  |  |  | 		defer delete(ctx, t, etc, serv.Key) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	m := new(dns.Msg) | 
					
						
							|  |  |  | 	m.SetQuestion("b.example.skydns.test.", dns.TypeA) | 
					
						
							|  |  |  | 	resp, err := dns.Exchange(m, udp) | 
					
						
							| 
									
										
										
										
											2016-10-18 12:05:19 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Expected to receive reply, but didn't: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	checkResponse(t, resp) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	resp, err = dns.Exchange(m, udp) | 
					
						
							| 
									
										
										
										
											2016-10-18 12:05:19 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Errorf("Expected to receive reply, but didn't: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	checkResponse(t, resp) | 
					
						
							|  |  |  | 	if len(resp.Extra) != 0 { | 
					
						
							|  |  |  | 		t.Errorf("Expected no RRs in additional section, got: %d", len(resp.Extra)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func checkResponse(t *testing.T, resp *dns.Msg) { | 
					
						
							|  |  |  | 	if len(resp.Answer) == 0 { | 
					
						
							|  |  |  | 		t.Fatal("Expected to at least one RR in the answer section, got none") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if resp.Answer[0].Header().Rrtype != dns.TypeA { | 
					
						
							|  |  |  | 		t.Errorf("Expected RR to A, got: %d", resp.Answer[0].Header().Rrtype) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if resp.Answer[0].(*dns.A).A.String() != "127.0.0.1" { | 
					
						
							| 
									
										
										
										
											2017-09-24 03:59:04 -07:00
										 |  |  | 		t.Errorf("Expected 127.0.0.1, got: %s", resp.Answer[0].(*dns.A).A.String()) | 
					
						
							| 
									
										
										
										
											2016-10-18 12:05:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var servicesCacheTest = []*msg.Service{ | 
					
						
							|  |  |  | 	{Host: "127.0.0.1", Port: 666, Key: "b.example.skydns.test."}, | 
					
						
							|  |  |  | } |