| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | package test
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 15:31:32 +07:00
										 |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											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) {
 | 
					
						
							| 
									
										
										
										
											2019-01-28 15:25:14 +08:00
										 |  |  | 	name, rm, err := test.TempFile(".", exampleOrg)
 | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	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
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:08:36 +08:00
										 |  |  | 	corefile := `
 | 
					
						
							|  |  |  | 	example.org:0 {
 | 
					
						
							|  |  |  | 		file ` + name + ` {
 | 
					
						
							|  |  |  | 			reload 1s
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	example.net:0 {
 | 
					
						
							|  |  |  | 		file ` + name + `
 | 
					
						
							|  |  |  | 	}`
 | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetQuestion("example.org.", dns.TypeA)
 | 
					
						
							|  |  |  | 	resp, err := dns.Exchange(m, udp)
 | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	resp, err = dns.Exchange(m, udp)
 | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	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
 | 
					
						
							|  |  |  | `
 |