|
| 1 | +/* |
| 2 | +Copyright 2025 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package injection |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/tektoncd/pipeline/pkg/remoteresolution/cache" |
| 24 | + "k8s.io/client-go/rest" |
| 25 | + logtesting "knative.dev/pkg/logging/testing" |
| 26 | +) |
| 27 | + |
| 28 | +func TestKey(t *testing.T) { |
| 29 | + // Test that Key is a distinct type that can be used as context key |
| 30 | + key1 := Key{} |
| 31 | + key2 := Key{} |
| 32 | + |
| 33 | + // Keys should be equivalent when used as context keys |
| 34 | + ctx := context.Background() |
| 35 | + ctx = context.WithValue(ctx, key1, "test-value") |
| 36 | + |
| 37 | + value := ctx.Value(key2) |
| 38 | + if value != "test-value" { |
| 39 | + t.Errorf("Expected Key{} to be usable as context key, got nil") |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestSharedCacheInitialization(t *testing.T) { |
| 44 | + // Test that sharedCache is properly initialized |
| 45 | + if sharedCache == nil { |
| 46 | + t.Fatal("sharedCache should be initialized") |
| 47 | + } |
| 48 | + |
| 49 | + // Test that it's a valid ResolverCache instance |
| 50 | + if sharedCache == nil { |
| 51 | + t.Fatal("sharedCache should be a valid ResolverCache instance") |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestWithCacheFromConfig(t *testing.T) { |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + ctx context.Context |
| 59 | + cfg *rest.Config |
| 60 | + expectCache bool |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "basic context with config", |
| 64 | + ctx: context.Background(), |
| 65 | + cfg: &rest.Config{}, |
| 66 | + expectCache: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "context with logger", |
| 70 | + ctx: logtesting.TestContextWithLogger(t), |
| 71 | + cfg: &rest.Config{}, |
| 72 | + expectCache: true, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + result := withCacheFromConfig(tt.ctx, tt.cfg) |
| 79 | + |
| 80 | + // Check that result context contains cache |
| 81 | + cache := result.Value(Key{}) |
| 82 | + if tt.expectCache && cache == nil { |
| 83 | + t.Errorf("Expected cache in context, got nil") |
| 84 | + } |
| 85 | + |
| 86 | + // Check that cache is functional (don't need type assertion for this test) |
| 87 | + if tt.expectCache && cache != nil { |
| 88 | + // The fact that it was stored and retrieved indicates correct type |
| 89 | + // Just check that we have a non-nil interface value |
| 90 | + if cache == nil { |
| 91 | + t.Errorf("Expected non-nil cache") |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestGet(t *testing.T) { |
| 99 | + tests := []struct { |
| 100 | + name string |
| 101 | + setupContext func() context.Context |
| 102 | + expectNotNil bool |
| 103 | + expectSameInstance bool |
| 104 | + }{ |
| 105 | + { |
| 106 | + name: "context without cache", |
| 107 | + setupContext: func() context.Context { |
| 108 | + return context.Background() |
| 109 | + }, |
| 110 | + expectNotNil: true, |
| 111 | + expectSameInstance: false, // Should get fallback instance |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "context with cache", |
| 115 | + setupContext: func() context.Context { |
| 116 | + ctx := context.Background() |
| 117 | + testCache := cache.NewResolverCache(100) |
| 118 | + return context.WithValue(ctx, Key{}, testCache) |
| 119 | + }, |
| 120 | + expectNotNil: true, |
| 121 | + expectSameInstance: false, // Should get the injected instance |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "context with logger but no cache", |
| 125 | + setupContext: func() context.Context { |
| 126 | + return logtesting.TestContextWithLogger(t) |
| 127 | + }, |
| 128 | + expectNotNil: true, |
| 129 | + expectSameInstance: false, // Should get fallback with logger |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + for _, tt := range tests { |
| 134 | + t.Run(tt.name, func(t *testing.T) { |
| 135 | + ctx := tt.setupContext() |
| 136 | + result := Get(ctx) |
| 137 | + |
| 138 | + if tt.expectNotNil && result == nil { |
| 139 | + t.Errorf("Expected non-nil cache, got nil") |
| 140 | + } |
| 141 | + |
| 142 | + // Test that we can call methods on the returned cache |
| 143 | + if result != nil { |
| 144 | + // This should not panic |
| 145 | + result.Clear() |
| 146 | + } |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func TestGetConsistency(t *testing.T) { |
| 152 | + // Test that Get returns consistent results for fallback case |
| 153 | + ctx := context.Background() |
| 154 | + |
| 155 | + cache1 := Get(ctx) |
| 156 | + cache2 := Get(ctx) |
| 157 | + |
| 158 | + if cache1 == nil || cache2 == nil { |
| 159 | + t.Fatal("Get should never return nil") |
| 160 | + } |
| 161 | + |
| 162 | + // Both should be valid cache instances |
| 163 | + cache1.Clear() |
| 164 | + cache2.Clear() |
| 165 | +} |
| 166 | + |
| 167 | +func TestGetWithInjectedCache(t *testing.T) { |
| 168 | + // Test that Get returns the injected cache when available |
| 169 | + ctx := context.Background() |
| 170 | + testCache := cache.NewResolverCache(50) |
| 171 | + ctx = context.WithValue(ctx, Key{}, testCache) |
| 172 | + |
| 173 | + result := Get(ctx) |
| 174 | + if result != testCache { |
| 175 | + t.Errorf("Expected injected cache instance, got different instance") |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func TestWithCacheFromConfigIntegration(t *testing.T) { |
| 180 | + // Test the integration between withCacheFromConfig and Get |
| 181 | + ctx := logtesting.TestContextWithLogger(t) |
| 182 | + cfg := &rest.Config{} |
| 183 | + |
| 184 | + // Inject cache into context |
| 185 | + ctxWithCache := withCacheFromConfig(ctx, cfg) |
| 186 | + |
| 187 | + // Retrieve cache using Get |
| 188 | + retrievedCache := Get(ctxWithCache) |
| 189 | + |
| 190 | + if retrievedCache == nil { |
| 191 | + t.Fatal("Expected cache from injected context") |
| 192 | + } |
| 193 | + |
| 194 | + // Test that the cache is functional |
| 195 | + retrievedCache.Clear() |
| 196 | +} |
| 197 | + |
| 198 | +func TestGetFallbackWithLogger(t *testing.T) { |
| 199 | + // Test that Get properly handles logger in fallback case |
| 200 | + ctx := logtesting.TestContextWithLogger(t) |
| 201 | + |
| 202 | + cache := Get(ctx) |
| 203 | + if cache == nil { |
| 204 | + t.Fatal("Expected cache with logger fallback") |
| 205 | + } |
| 206 | + |
| 207 | + // Cache should be functional |
| 208 | + cache.Clear() |
| 209 | +} |
0 commit comments