@@ -18,8 +18,10 @@ package injection
1818
1919import (
2020 "context"
21+ "sync"
2122 "testing"
2223
24+ resolverconfig "github.com/tektoncd/pipeline/pkg/apis/config/resolver"
2325 "github.com/tektoncd/pipeline/pkg/remoteresolution/cache"
2426 "k8s.io/client-go/rest"
2527 logtesting "knative.dev/pkg/logging/testing"
@@ -41,14 +43,16 @@ func TestKey(t *testing.T) {
4143}
4244
4345func TestSharedCacheInitialization (t * testing.T ) {
44- // Test that sharedCache is properly initialized
45- if sharedCache == nil {
46- t .Fatal ("sharedCache should be initialized" )
47- }
46+ // Reset globals and create context with resolvers enabled
47+ resetCacheGlobals ()
48+ ctx := createContextWithResolverConfig (t , true )
4849
49- // Test that it's a valid ResolverCache instance
50+ // Initialize cache by calling Get
51+ Get (ctx )
52+
53+ // Test that sharedCache is properly initialized when resolvers are enabled
5054 if sharedCache == nil {
51- t .Fatal ("sharedCache should be a valid ResolverCache instance " )
55+ t .Fatal ("sharedCache should be initialized when resolvers are enabled " )
5256 }
5357}
5458
@@ -204,3 +208,75 @@ func TestGetFallbackWithLogger(t *testing.T) {
204208 // Cache should be functional
205209 cache .Clear ()
206210}
211+
212+ // TestConditionalCacheLoading tests that cache is only loaded when resolvers are enabled
213+ func TestConditionalCacheLoading (t * testing.T ) {
214+ testCases := []struct {
215+ name string
216+ resolversEnabled bool
217+ expectCacheNil bool
218+ }{
219+ {
220+ name : "Cache loaded when resolvers enabled" ,
221+ resolversEnabled : true ,
222+ expectCacheNil : false ,
223+ },
224+ {
225+ name : "Cache not loaded when resolvers disabled" ,
226+ resolversEnabled : false ,
227+ expectCacheNil : true ,
228+ },
229+ }
230+
231+ for _ , tc := range testCases {
232+ t .Run (tc .name , func (t * testing.T ) {
233+ // Reset global state for each test
234+ resetCacheGlobals ()
235+
236+ // Create context with resolver config
237+ ctx := createContextWithResolverConfig (t , tc .resolversEnabled )
238+
239+ // Test Get function
240+ cache := Get (ctx )
241+
242+ if tc .expectCacheNil && cache != nil {
243+ t .Errorf ("Expected cache to be nil when resolvers disabled, got non-nil cache" )
244+ }
245+ if ! tc .expectCacheNil && cache == nil {
246+ t .Errorf ("Expected cache to be non-nil when resolvers enabled, got nil" )
247+ }
248+ })
249+ }
250+ }
251+
252+ // resetCacheGlobals resets the global cache state for testing
253+ // This is necessary because sync.Once prevents re-initialization
254+ func resetCacheGlobals () {
255+ // Reset the sync.Once variables by creating new instances
256+ cacheOnce = sync.Once {}
257+ injectionOnce = sync.Once {}
258+ sharedCache = nil
259+ resolversEnabled = false
260+ }
261+
262+ // createContextWithResolverConfig creates a test context with resolver configuration
263+ func createContextWithResolverConfig (t * testing.T , anyResolverEnabled bool ) context.Context {
264+ t .Helper ()
265+
266+ ctx := t .Context ()
267+
268+ // Create feature flags based on test case
269+ featureFlags := & resolverconfig.FeatureFlags {
270+ EnableGitResolver : anyResolverEnabled ,
271+ EnableHubResolver : false ,
272+ EnableBundleResolver : false ,
273+ EnableClusterResolver : false ,
274+ EnableHttpResolver : false ,
275+ }
276+
277+ config := & resolverconfig.Config {
278+ FeatureFlags : featureFlags ,
279+ }
280+
281+ return resolverconfig .ToContext (ctx , config )
282+ }
0 commit comments