| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | package test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-20 17:28:23 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/file" | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/proxy" | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/test" | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request" | 
					
						
							| 
									
										
										
										
											2017-03-02 19:35:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestZoneReload(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-09-20 17:28:23 +01:00
										 |  |  | 	file.TickTime = 1 * time.Second | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	name, rm, err := TempFile(".", exampleOrg) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:35:44 +00:00
										 |  |  | 		t.Fatalf("Failed to create zone: %s", err) | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	defer rm() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Corefile with two stanzas | 
					
						
							|  |  |  | 	corefile := `example.org:0 { | 
					
						
							|  |  |  |        file ` + name + ` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | example.net:0 { | 
					
						
							|  |  |  | 	file ` + name + ` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ` | 
					
						
							| 
									
										
										
										
											2017-08-24 11:35:14 +01:00
										 |  |  | 	i, udp, _, err := CoreDNSServerAndPorts(corefile) | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("Could not get CoreDNS serving instance: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer i.Stop() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 	p := proxy.NewLookup([]string{udp}) | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	resp, err := p.Lookup(state, "example.org.", dns.TypeA) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:35:44 +00:00
										 |  |  | 		t.Fatalf("Expected to receive reply, but didn't: %s", err) | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if len(resp.Answer) != 2 { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:59:18 +01:00
										 |  |  | 		t.Fatalf("Expected two RR in answer section got %d", len(resp.Answer)) | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Remove RR from the Apex | 
					
						
							|  |  |  | 	ioutil.WriteFile(name, []byte(exampleOrgUpdated), 0644) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-20 17:28:23 +01:00
										 |  |  | 	time.Sleep(2 * time.Second) // reload time | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	resp, err = p.Lookup(state, "example.org.", dns.TypeA) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal("Expected to receive reply, but didn't") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(resp.Answer) != 1 { | 
					
						
							| 
									
										
										
										
											2016-10-28 12:59:18 +01:00
										 |  |  | 		t.Fatalf("Expected two RR in answer section got %d", len(resp.Answer)) | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const exampleOrgUpdated = `; example.org test file | 
					
						
							|  |  |  | example.org.		IN	SOA	sns.dns.icann.org. noc.dns.icann.org. 2016082541 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.2 | 
					
						
							|  |  |  | ` |