2016-10-02 08:31:44 +01:00
|
|
|
package test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2017-02-21 22:51:47 -08:00
|
|
|
"github.com/coredns/coredns/middleware/proxy"
|
|
|
|
|
"github.com/coredns/coredns/middleware/test"
|
|
|
|
|
"github.com/coredns/coredns/request"
|
2016-10-02 08:31:44 +01:00
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestLookupCache(t *testing.T) {
|
2017-01-12 08:13:50 +00:00
|
|
|
t.Parallel()
|
2016-10-02 08:31:44 +01:00
|
|
|
// Start auth. CoreDNS holding the auth zone.
|
2016-10-02 15:58:01 +01:00
|
|
|
name, rm, err := test.TempFile(".", exampleOrg)
|
2016-10-02 08:31:44 +01:00
|
|
|
if err != nil {
|
2017-03-02 19:35:44 +00:00
|
|
|
t.Fatalf("failed to create zone: %s", err)
|
2016-10-02 08:31:44 +01:00
|
|
|
}
|
|
|
|
|
defer rm()
|
|
|
|
|
|
|
|
|
|
corefile := `example.org:0 {
|
|
|
|
|
file ` + name + `
|
|
|
|
|
}
|
|
|
|
|
`
|
2017-08-24 11:35:14 +01:00
|
|
|
i, udp, _, err := CoreDNSServerAndPorts(corefile)
|
2016-10-02 08:31:44 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
`
|
2017-08-24 11:35:14 +01:00
|
|
|
i, udp, _, err = CoreDNSServerAndPorts(corefile)
|
2016-10-02 08:31:44 +01:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
|
|
|
|
|
}
|
|
|
|
|
defer i.Stop()
|
|
|
|
|
|
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
|
|
2017-01-15 08:12:58 +00:00
|
|
|
p := proxy.NewLookup([]string{udp})
|
2016-10-02 08:31:44 +01:00
|
|
|
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 {
|
2017-01-15 08:12:58 +00:00
|
|
|
t.Fatal("Expected to at least one RR in the answer section, got none")
|
2016-10-02 08:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ttl := resp.Answer[0].Header().Ttl
|
|
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second) // TODO(miek): meh.
|
|
|
|
|
|
|
|
|
|
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.Error("Expected to at least one RR in the answer section, got none")
|
|
|
|
|
}
|
|
|
|
|
newTTL := resp.Answer[0].Header().Ttl
|
|
|
|
|
if newTTL >= ttl {
|
|
|
|
|
t.Errorf("Expected TTL to be lower than: %d, got %d", ttl, newTTL)
|
|
|
|
|
}
|
|
|
|
|
}
|