MCPcopy
hub / github.com/coredns/coredns / TestCacheWildcardMetadata

Function TestCacheWildcardMetadata

plugin/cache/cache_test.go:835–873  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

833}
834
835func TestCacheWildcardMetadata(t *testing.T) {
836 c := New()
837 qname := "foo.bar.example.org."
838 wildcard := "*.bar.example.org."
839 c.Next = wildcardMetadataBackend(qname, wildcard)
840
841 req := new(dns.Msg)
842 req.SetQuestion(qname, dns.TypeA)
843 state := request.Request{W: &test.ResponseWriter{}, Req: req}
844
845 // 1. Test writing wildcard metadata retrieved from backend to the cache
846
847 ctx := metadata.ContextWithMetadata(context.TODO())
848 w := dnstest.NewRecorder(&test.ResponseWriter{})
849 c.ServeDNS(ctx, w, req)
850 if c.pcache.Len() != 1 {
851 t.Errorf("Msg should have been cached")
852 }
853 _, k := key(qname, w.Msg, response.NoError, state.Do(), state.Req.CheckingDisabled)
854 i, _ := c.pcache.Get(k)
855 if i.wildcard != wildcard {
856 t.Errorf("expected wildcard response to enter cache with cache item's wildcard = %q, got %q", wildcard, i.wildcard)
857 }
858
859 // 2. Test retrieving the cached item from cache and writing its wildcard value to metadata
860
861 // reset context and response writer
862 ctx = metadata.ContextWithMetadata(context.TODO())
863 w = dnstest.NewRecorder(&test.ResponseWriter{})
864
865 c.ServeDNS(ctx, w, req)
866 f := metadata.ValueFunc(ctx, "zone/wildcard")
867 if f == nil {
868 t.Fatal("expected metadata func for wildcard response retrieved from cache, got nil")
869 }
870 if f() != wildcard {
871 t.Errorf("after retrieving wildcard item from cache, expected \"zone/wildcard\" metadata value to be %q, got %q", wildcard, i.wildcard)
872 }
873}
874
875func TestCacheKeepTTL(t *testing.T) {
876 defaultTtl := 60

Callers

nothing calls this directly

Calls 12

DoMethod · 0.95
ContextWithMetadataFunction · 0.92
NewRecorderFunction · 0.92
ValueFuncFunction · 0.92
wildcardMetadataBackendFunction · 0.85
NewFunction · 0.70
keyFunction · 0.70
ServeDNSMethod · 0.65
ErrorfMethod · 0.65
FatalMethod · 0.65
LenMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…