mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	* plugin/file: make non-existent file non-fatal If the zone file being loaded doesn't exist *and* reload is enabled, just wait the file to pop up in the normal Reload routine. If reload is set to 0s; we keep this a fatal error on startup. Aslo fix the ticker in z.Reload(): remove the per second ticks and just use the reload interval for the ticker. Brush up the documentation a bit as well. Fixes: #2951 Signed-off-by: Miek Gieben <miek@miek.nl> * Stickler and test compile Signed-off-by: Miek Gieben <miek@miek.nl> * Remove there too Signed-off-by: Miek Gieben <miek@miek.nl> * Cant README test these because zone files dont exist Signed-off-by: Miek Gieben <miek@miek.nl>
		
			
				
	
	
		
			84 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package file
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"io/ioutil"
 | |
| 	"os"
 | |
| 	"strings"
 | |
| 	"testing"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/coredns/coredns/plugin/test"
 | |
| 	"github.com/coredns/coredns/request"
 | |
| 
 | |
| 	"github.com/miekg/dns"
 | |
| )
 | |
| 
 | |
| func TestZoneReload(t *testing.T) {
 | |
| 	fileName, rm, err := test.TempFile(".", reloadZoneTest)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Failed to create zone: %s", err)
 | |
| 	}
 | |
| 	defer rm()
 | |
| 	reader, err := os.Open(fileName)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Failed to open zone: %s", err)
 | |
| 	}
 | |
| 	z, err := Parse(reader, "miek.nl", fileName, 0)
 | |
| 	if err != nil {
 | |
| 		t.Fatalf("Failed to parse zone: %s", err)
 | |
| 	}
 | |
| 
 | |
| 	z.ReloadInterval = 500 * time.Millisecond
 | |
| 	z.Reload()
 | |
| 	time.Sleep(time.Second)
 | |
| 
 | |
| 	ctx := context.TODO()
 | |
| 	r := new(dns.Msg)
 | |
| 	r.SetQuestion("miek.nl", dns.TypeSOA)
 | |
| 	state := request.Request{W: &test.ResponseWriter{}, Req: r}
 | |
| 	if _, _, _, res := z.Lookup(ctx, state, "miek.nl."); res != Success {
 | |
| 		t.Fatalf("Failed to lookup, got %d", res)
 | |
| 	}
 | |
| 
 | |
| 	r = new(dns.Msg)
 | |
| 	r.SetQuestion("miek.nl", dns.TypeNS)
 | |
| 	state = request.Request{W: &test.ResponseWriter{}, Req: r}
 | |
| 	if _, _, _, res := z.Lookup(ctx, state, "miek.nl."); res != Success {
 | |
| 		t.Fatalf("Failed to lookup, got %d", res)
 | |
| 	}
 | |
| 
 | |
| 	if len(z.All()) != 5 {
 | |
| 		t.Fatalf("Expected 5 RRs, got %d", len(z.All()))
 | |
| 	}
 | |
| 	if err := ioutil.WriteFile(fileName, []byte(reloadZone2Test), 0644); err != nil {
 | |
| 		t.Fatalf("Failed to write new zone data: %s", err)
 | |
| 	}
 | |
| 	// Could still be racy, but we need to wait a bit for the event to be seen
 | |
| 	time.Sleep(1 * time.Second)
 | |
| 
 | |
| 	if len(z.All()) != 3 {
 | |
| 		t.Fatalf("Expected 3 RRs, got %d", len(z.All()))
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func TestZoneReloadSOAChange(t *testing.T) {
 | |
| 	_, err := Parse(strings.NewReader(reloadZoneTest), "miek.nl.", "stdin", 1460175181)
 | |
| 	if err == nil {
 | |
| 		t.Fatalf("Zone should not have been re-parsed")
 | |
| 	}
 | |
| 
 | |
| }
 | |
| 
 | |
| const reloadZoneTest = `miek.nl.		1627	IN	SOA	linode.atoom.net. miek.miek.nl. 1460175181 14400 3600 604800 14400
 | |
| miek.nl.		1627	IN	NS	ext.ns.whyscream.net.
 | |
| miek.nl.		1627	IN	NS	omval.tednet.nl.
 | |
| miek.nl.		1627	IN	NS	linode.atoom.net.
 | |
| miek.nl.		1627	IN	NS	ns-ext.nlnetlabs.nl.
 | |
| `
 | |
| 
 | |
| const reloadZone2Test = `miek.nl.		1627	IN	SOA	linode.atoom.net. miek.miek.nl. 1460175182 14400 3600 604800 14400
 | |
| miek.nl.		1627	IN	NS	ext.ns.whyscream.net.
 | |
| miek.nl.		1627	IN	NS	omval.tednet.nl.
 | |
| `
 |