diff options
author | 2016-10-02 08:31:44 +0100 | |
---|---|---|
committer | 2016-10-02 08:31:44 +0100 | |
commit | e54c232c8c97fb163647c697e921e6f69846e304 (patch) | |
tree | b75fff81276e58a2ec4417c6c6742a22d6156f27 /test | |
parent | 9b6b8d276269cb1a36b7f78da4caa51106dff0ed (diff) | |
download | coredns-e54c232c8c97fb163647c697e921e6f69846e304.tar.gz coredns-e54c232c8c97fb163647c697e921e6f69846e304.tar.zst coredns-e54c232c8c97fb163647c697e921e6f69846e304.zip |
middleware/cache: split cache in positive and negative and use lru (#298)
Make the cache memory bounded, by using a LRU cache. Also split the
cache in a positive and negative one - each with its own controls.
Extend the cache stanza to allow for this:
cache {
positive limit [ttl]
negative limit [ttl]
}
is now possible. This also add a cache_test.go in the toplevel test/
directory that exercises the caching path.
Fixes #260
Diffstat (limited to 'test')
-rw-r--r-- | test/cache_test.go | 89 | ||||
-rw-r--r-- | test/etcd_test.go | 4 | ||||
-rw-r--r-- | test/kubernetes_test.go | 4 | ||||
-rw-r--r-- | test/middleware_dnssec_test.go | 2 | ||||
-rw-r--r-- | test/middleware_test.go | 2 | ||||
-rw-r--r-- | test/proxy_test.go | 4 | ||||
-rw-r--r-- | test/server_test.go | 4 |
7 files changed, 99 insertions, 10 deletions
diff --git a/test/cache_test.go b/test/cache_test.go new file mode 100644 index 000000000..8382aabba --- /dev/null +++ b/test/cache_test.go @@ -0,0 +1,89 @@ +package test + +import ( + "io/ioutil" + "log" + "testing" + "time" + + "github.com/miekg/coredns/middleware/proxy" + "github.com/miekg/coredns/middleware/test" + "github.com/miekg/coredns/request" + + "github.com/miekg/dns" +) + +// This tests uses the exampleOrg zone as defined in proxy_test.go + +func TestLookupCache(t *testing.T) { + // Start auth. CoreDNS holding the auth zone. + name, rm, err := test.TempFile(t, ".", exampleOrg) + if err != nil { + t.Fatalf("failed to created zone: %s", err) + } + defer rm() + + corefile := `example.org:0 { + file ` + name + ` +} +` + i, err := CoreDNSServer(corefile) + if err != nil { + t.Fatalf("Could not get CoreDNS serving instance: %s", err) + } + + udp, _ := CoreDNSServerPorts(i, 0) + if udp == "" { + t.Fatalf("Could not get UDP listening port") + } + defer i.Stop() + + // Start caching proxy CoreDNS that we want to test. + corefile = `example.org:0 { + proxy . ` + udp + ` + cache +} +` + i, err = CoreDNSServer(corefile) + if err != nil { + t.Fatalf("Could not get CoreDNS serving instance: %s", err) + } + + udp, _ = CoreDNSServerPorts(i, 0) + if udp == "" { + t.Fatalf("Could not get UDP listening port") + } + defer i.Stop() + + log.SetOutput(ioutil.Discard) + + p := proxy.New([]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.Error("Expected to at least one RR in the answer section, got none") + } + + 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) + } +} diff --git a/test/etcd_test.go b/test/etcd_test.go index 6f3c0b39a..265162a83 100644 --- a/test/etcd_test.go +++ b/test/etcd_test.go @@ -48,12 +48,12 @@ func TestEtcdStubAndProxyLookup(t *testing.T) { ex, err := CoreDNSServer(corefile) if err != nil { - t.Fatalf("could not get CoreDNS serving instance: %s", err) + t.Fatalf("Could not get CoreDNS serving instance: %s", err) } udp, _ := CoreDNSServerPorts(ex, 0) if udp == "" { - t.Fatalf("could not get udp listening port") + t.Fatalf("Could not get UDP listening port") } defer ex.Stop() diff --git a/test/kubernetes_test.go b/test/kubernetes_test.go index 555df7791..83a7b65d2 100644 --- a/test/kubernetes_test.go +++ b/test/kubernetes_test.go @@ -74,12 +74,12 @@ func TestKubernetesIntegration(t *testing.T) { func createTestServer(t *testing.T, corefile string) (*caddy.Instance, string) { server, err := CoreDNSServer(corefile) if err != nil { - t.Fatalf("could not get CoreDNS serving instance: %s", err) + t.Fatalf("Could not get CoreDNS serving instance: %s", err) } udp, _ := CoreDNSServerPorts(server, 0) if udp == "" { - t.Fatalf("could not get udp listening port") + t.Fatalf("Could not get UDP listening port") } return server, udp diff --git a/test/middleware_dnssec_test.go b/test/middleware_dnssec_test.go index afde72a54..238673925 100644 --- a/test/middleware_dnssec_test.go +++ b/test/middleware_dnssec_test.go @@ -31,7 +31,7 @@ func TestLookupBalanceRewriteCacheDnssec(t *testing.T) { ` ex, err := CoreDNSServer(corefile) if err != nil { - t.Fatalf("could not get CoreDNS serving instance: %s", err) + t.Fatalf("Could not get CoreDNS serving instance: %s", err) } udp, _ := CoreDNSServerPorts(ex, 0) diff --git a/test/middleware_test.go b/test/middleware_test.go index f7fefe78a..fb6fa2114 100644 --- a/test/middleware_test.go +++ b/test/middleware_test.go @@ -27,7 +27,7 @@ func benchmarkLookupBalanceRewriteCache(b *testing.B) { ex, err := CoreDNSServer(corefile) if err != nil { - t.Fatalf("could not get CoreDNS serving instance: %s", err) + t.Fatalf("Could not get CoreDNS serving instance: %s", err) } udp, _ := CoreDNSServerPorts(ex, 0) defer ex.Stop() diff --git a/test/proxy_test.go b/test/proxy_test.go index 9e2dd9aab..634fde888 100644 --- a/test/proxy_test.go +++ b/test/proxy_test.go @@ -34,12 +34,12 @@ func TestLookupProxy(t *testing.T) { i, err := CoreDNSServer(corefile) if err != nil { - t.Fatalf("could not get CoreDNS serving instance: %s", err) + t.Fatalf("Could not get CoreDNS serving instance: %s", err) } udp, _ := CoreDNSServerPorts(i, 0) if udp == "" { - t.Fatalf("could not get udp listening port") + t.Fatalf("Could not get UDP listening port") } defer i.Stop() diff --git a/test/server_test.go b/test/server_test.go index 807bfb643..58e929e96 100644 --- a/test/server_test.go +++ b/test/server_test.go @@ -14,7 +14,7 @@ func TestProxyToChaosServer(t *testing.T) { ` chaos, err := CoreDNSServer(corefile) if err != nil { - t.Fatalf("could not get CoreDNS serving instance: %s", err) + t.Fatalf("Could not get CoreDNS serving instance: %s", err) } udpChaos, _ := CoreDNSServerPorts(chaos, 0) @@ -26,7 +26,7 @@ func TestProxyToChaosServer(t *testing.T) { ` proxy, err := CoreDNSServer(corefileProxy) if err != nil { - t.Fatalf("could not get CoreDNS serving instance") + t.Fatalf("Could not get CoreDNS serving instance") } udp, _ := CoreDNSServerPorts(proxy, 0) |