diff options
author | 2023-01-27 17:35:24 +0100 | |
---|---|---|
committer | 2023-01-27 11:35:24 -0500 | |
commit | bf7c2cf37bf35ba3a7596cb32c5a3607d896f95d (patch) | |
tree | 83a2dc9ae6ec2bd04074f42dd0d5f1962f916f68 /plugin/cache/cache_test.go | |
parent | d3e4fc78c3b80c1768b0f95ad849b07ba9968ba5 (diff) | |
download | coredns-bf7c2cf37bf35ba3a7596cb32c5a3607d896f95d.tar.gz coredns-bf7c2cf37bf35ba3a7596cb32c5a3607d896f95d.tar.zst coredns-bf7c2cf37bf35ba3a7596cb32c5a3607d896f95d.zip |
plugin/cache: add a new keepttl option (#5879)
adds a new option `keepttl` to the cache plugin
Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@proton.ch>
Diffstat (limited to 'plugin/cache/cache_test.go')
-rw-r--r-- | plugin/cache/cache_test.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/plugin/cache/cache_test.go b/plugin/cache/cache_test.go index 861e9b8fd..5a023f400 100644 --- a/plugin/cache/cache_test.go +++ b/plugin/cache/cache_test.go @@ -690,6 +690,44 @@ func TestCacheWildcardMetadata(t *testing.T) { } } +func TestCacheKeepTTL(t *testing.T) { + defaultTtl := 60 + + c := New() + c.Next = ttlBackend(defaultTtl) + + req := new(dns.Msg) + req.SetQuestion("cached.org.", dns.TypeA) + ctx := context.TODO() + + // Cache cached.org. with 60s TTL + rec := dnstest.NewRecorder(&test.ResponseWriter{}) + c.keepttl = true + c.ServeDNS(ctx, rec, req) + + tests := []struct { + name string + futureSeconds int + }{ + {"cached.org.", 0}, + {"cached.org.", 30}, + {"uncached.org.", 60}, + } + + for i, tt := range tests { + rec := dnstest.NewRecorder(&test.ResponseWriter{}) + c.now = func() time.Time { return time.Now().Add(time.Duration(tt.futureSeconds) * time.Second) } + r := req.Copy() + r.SetQuestion(tt.name, dns.TypeA) + c.ServeDNS(ctx, rec, r) + + recTtl := rec.Msg.Answer[0].Header().Ttl + if defaultTtl != int(recTtl) { + t.Errorf("Test %d: expecting TTL=%d, got TTL=%d", i, defaultTtl, recTtl) + } + } +} + // wildcardMetadataBackend mocks a backend that reponds with a response for qname synthesized by wildcard // and sets the zone/wildcard metadata value func wildcardMetadataBackend(qname, wildcard string) plugin.Handler { |