mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-04 03:03:14 -05:00 
			
		
		
		
	Cache would let the first response through and would then cap subsequent ones to whatever the cache duration was. This would lead to huge drops in TTL values: 3600 -> 20 for instance, which is not only bad, but can mess up your careful TTL planning business. This PR fixes that and applies the cache duration to all replies. As a bonus I could remove a time.Sleep() from the cache test and just check for the cache duration as the TTL on the reply. Fixes #1038
		
			
				
	
	
		
			60 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package test
 | 
						|
 | 
						|
import (
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/coredns/coredns/plugin/proxy"
 | 
						|
	"github.com/coredns/coredns/plugin/test"
 | 
						|
	"github.com/coredns/coredns/request"
 | 
						|
 | 
						|
	"github.com/miekg/dns"
 | 
						|
)
 | 
						|
 | 
						|
func TestLookupCache(t *testing.T) {
 | 
						|
	// Start auth. CoreDNS holding the auth zone.
 | 
						|
	name, rm, err := test.TempFile(".", exampleOrg)
 | 
						|
	if err != nil {
 | 
						|
		t.Fatalf("failed to create zone: %s", err)
 | 
						|
	}
 | 
						|
	defer rm()
 | 
						|
 | 
						|
	corefile := `example.org:0 {
 | 
						|
       file ` + name + `
 | 
						|
}
 | 
						|
`
 | 
						|
	i, udp, _, err := CoreDNSServerAndPorts(corefile)
 | 
						|
	if err != nil {
 | 
						|
		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
						|
	}
 | 
						|
	defer i.Stop()
 | 
						|
 | 
						|
	// Start caching proxy CoreDNS that we want to test.
 | 
						|
	corefile = `example.org:0 {
 | 
						|
	proxy . ` + udp + `
 | 
						|
	cache 10
 | 
						|
}
 | 
						|
`
 | 
						|
	i, udp, _, err = CoreDNSServerAndPorts(corefile)
 | 
						|
	if err != nil {
 | 
						|
		t.Fatalf("Could not get CoreDNS serving instance: %s", err)
 | 
						|
	}
 | 
						|
	defer i.Stop()
 | 
						|
 | 
						|
	p := proxy.NewLookup([]string{udp})
 | 
						|
	state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
 | 
						|
 | 
						|
	resp, err := p.Lookup(state, "example.org.", dns.TypeA)
 | 
						|
	if err != nil {
 | 
						|
		t.Fatal("Expected to receive reply, but didn't")
 | 
						|
	}
 | 
						|
	// expect answer section with A record in it
 | 
						|
	if len(resp.Answer) == 0 {
 | 
						|
		t.Fatal("Expected to at least one RR in the answer section, got none")
 | 
						|
	}
 | 
						|
 | 
						|
	ttl := resp.Answer[0].Header().Ttl
 | 
						|
	if ttl != 10 { // as set in the Corefile
 | 
						|
		t.Errorf("Expected TTL to be %d, got %d", 10, ttl)
 | 
						|
	}
 | 
						|
}
 |