@@ -39,6 +39,7 @@ func TestHashSandbox(t *testing.T) {
3939 expectedHash string
4040 expectedHashWithoutImageResources string
4141 validateDifferentHashes bool
42+ validateDifferentMetadataHashes bool
4243 }{
4344 {
4445 name : "basic sandbox with containers" ,
@@ -165,11 +166,6 @@ func TestHashSandbox(t *testing.T) {
165166 Spec : agentsv1alpha1.SandboxSpec {
166167 EmbeddedSandboxTemplate : agentsv1alpha1.EmbeddedSandboxTemplate {
167168 Template : & corev1.PodTemplateSpec {
168- ObjectMeta : metav1.ObjectMeta {
169- Labels : map [string ]string {
170- "app" : "test" ,
171- },
172- },
173169 Spec : corev1.PodSpec {
174170 Containers : []corev1.Container {
175171 {
@@ -195,11 +191,37 @@ func TestHashSandbox(t *testing.T) {
195191 },
196192 validateDifferentHashes : true ,
197193 },
194+ {
195+ name : "sandbox with metadata" ,
196+ sandbox : & agentsv1alpha1.Sandbox {
197+ Spec : agentsv1alpha1.SandboxSpec {
198+ EmbeddedSandboxTemplate : agentsv1alpha1.EmbeddedSandboxTemplate {
199+ Template : & corev1.PodTemplateSpec {
200+ ObjectMeta : metav1.ObjectMeta {
201+ Labels : map [string ]string {
202+ "app" : "test" ,
203+ },
204+ },
205+ Spec : corev1.PodSpec {
206+ Containers : []corev1.Container {
207+ {
208+ Name : "test-container" ,
209+ Image : "nginx:latest" ,
210+ },
211+ },
212+ },
213+ },
214+ },
215+ },
216+ },
217+ validateDifferentHashes : true ,
218+ validateDifferentMetadataHashes : true ,
219+ },
198220 }
199221
200222 for _ , tt := range tests {
201223 t .Run (tt .name , func (t * testing.T ) {
202- hash , hashWithoutImageResources := HashSandbox (tt .sandbox )
224+ hash , hashWithoutImageResources , hashWithoutImageResourcesMetadata := HashSandbox (tt .sandbox )
203225
204226 // Verify both hashes are not empty
205227 if hash == "" {
@@ -210,12 +232,18 @@ func TestHashSandbox(t *testing.T) {
210232 }
211233
212234 // Verify consistency - same input should always produce same output
213- hash2 , hashWithoutImageResources2 := HashSandbox (tt .sandbox )
235+ hash2 , hashWithoutImageResources2 , hashWithoutImageResourcesMetadata2 := HashSandbox (tt .sandbox )
214236 if hash != hash2 {
215237 t .Errorf ("HashSandbox() is not consistent for hash: got %s, want %s" , hash , hash2 )
216238 }
217239 if hashWithoutImageResources != hashWithoutImageResources2 {
218- t .Errorf ("HashSandbox() is not consistent for hashWithoutImageResources: got %s, want %s" , hashWithoutImageResources , hashWithoutImageResources2 )
240+ t .Errorf ("HashSandbox() is not consistent for hashWithoutImageResources: got %s, want %s" ,
241+ hashWithoutImageResources , hashWithoutImageResources2 )
242+ }
243+
244+ if hashWithoutImageResourcesMetadata != hashWithoutImageResourcesMetadata2 {
245+ t .Errorf ("HashSandbox() is not consistent for hashWithoutImageResourcesMetadata: got %s, want %s" ,
246+ hashWithoutImageResourcesMetadata , hashWithoutImageResourcesMetadata2 )
219247 }
220248
221249 // Validate that hashes have expected format (from HashData function)
@@ -228,10 +256,16 @@ func TestHashSandbox(t *testing.T) {
228256 if hash == hashWithoutImageResources {
229257 t .Errorf ("Expected different hashes when image/resources are present, but got same: %s" , hash )
230258 }
231- } else {
232- if hash != hashWithoutImageResources {
233- t .Errorf ("Expected same hashes when no image/resources differences, but got different: %s vs %s" , hash , hashWithoutImageResources )
259+ } else if hash != hashWithoutImageResources {
260+ t .Errorf ("Expected same hashes when no image/resources differences, but got different: %s vs %s" , hash , hashWithoutImageResources )
261+ }
262+
263+ if tt .validateDifferentMetadataHashes {
264+ if hashWithoutImageResources == hashWithoutImageResourcesMetadata {
265+ t .Errorf ("Expected different hashes when sandbox metadata are present, but got same: %s" , hashWithoutImageResources )
234266 }
267+ } else if hashWithoutImageResourcesMetadata != hashWithoutImageResources {
268+ t .Errorf ("Expected same hashes when no metadata differences, but got different: %s vs %s" , hash , hashWithoutImageResources )
235269 }
236270 })
237271 }
@@ -273,8 +307,8 @@ func TestHashSandboxWithDifferentImages(t *testing.T) {
273307 },
274308 }
275309
276- hash1 , hashWithoutImageResources1 := HashSandbox (sandbox1 )
277- hash2 , hashWithoutImageResources2 := HashSandbox (sandbox2 )
310+ hash1 , hashWithoutImageResources1 , _ := HashSandbox (sandbox1 )
311+ hash2 , hashWithoutImageResources2 , _ := HashSandbox (sandbox2 )
278312
279313 // Full hashes should be different because images are different
280314 if hash1 == hash2 {
@@ -288,6 +322,69 @@ func TestHashSandboxWithDifferentImages(t *testing.T) {
288322 }
289323}
290324
325+ func TestHashSandboxWithDifferentHash (t * testing.T ) {
326+ // Test that changing only image results in different full hash but same hash without image/resources
327+ sandbox1 := & agentsv1alpha1.Sandbox {
328+ Spec : agentsv1alpha1.SandboxSpec {
329+ EmbeddedSandboxTemplate : agentsv1alpha1.EmbeddedSandboxTemplate {
330+ Template : & corev1.PodTemplateSpec {
331+ ObjectMeta : metav1.ObjectMeta {
332+ Labels : map [string ]string {
333+ "app" : "test" ,
334+ },
335+ },
336+ Spec : corev1.PodSpec {
337+ Containers : []corev1.Container {
338+ {
339+ Name : "test-container" ,
340+ Image : "nginx:1.19" , // Different image
341+ },
342+ },
343+ },
344+ },
345+ },
346+ },
347+ }
348+
349+ sandbox2 := & agentsv1alpha1.Sandbox {
350+ Spec : agentsv1alpha1.SandboxSpec {
351+ EmbeddedSandboxTemplate : agentsv1alpha1.EmbeddedSandboxTemplate {
352+ Template : & corev1.PodTemplateSpec {
353+ Spec : corev1.PodSpec {
354+ Containers : []corev1.Container {
355+ {
356+ Name : "test-container" ,
357+ Image : "nginx:1.20" , // Different image
358+ },
359+ },
360+ },
361+ },
362+ },
363+ },
364+ }
365+
366+ hash1 , hashWithoutImageResources1 , hashWithoutImageResourcesMetadata1 := HashSandbox (sandbox1 )
367+ hash2 , hashWithoutImageResources2 , hashWithoutImageResourcesMetadata2 := HashSandbox (sandbox2 )
368+
369+ // Full hashes should be different because images are different
370+ if hash1 == hash2 {
371+ t .Errorf ("Expected different full hashes for different images, but got same: %s" , hash1 )
372+ }
373+
374+ // Hashes without images/resources should be the different
375+ if hashWithoutImageResources1 == hashWithoutImageResources2 {
376+ t .Errorf ("Expected different hashes without images/resources, but got same: %s" ,
377+ hashWithoutImageResources1 )
378+ }
379+
380+ // Hashes without images/resources/metadata should be the same
381+ if hashWithoutImageResourcesMetadata1 != hashWithoutImageResourcesMetadata2 {
382+ t .Errorf ("Expected same hashes without images/resources/metadata, but got different:" +
383+ "%s vs %s" ,
384+ hashWithoutImageResourcesMetadata1 , hashWithoutImageResourcesMetadata2 )
385+ }
386+ }
387+
291388func TestHashSandboxWithDifferentResources (t * testing.T ) {
292389 // Test that changing only resources results in different full hash but same hash without image/resources
293390 sandbox1 := & agentsv1alpha1.Sandbox {
@@ -336,8 +433,8 @@ func TestHashSandboxWithDifferentResources(t *testing.T) {
336433 },
337434 }
338435
339- hash1 , hashWithoutImageResources1 := HashSandbox (sandbox1 )
340- hash2 , hashWithoutImageResources2 := HashSandbox (sandbox2 )
436+ hash1 , hashWithoutImageResources1 , _ := HashSandbox (sandbox1 )
437+ hash2 , hashWithoutImageResources2 , _ := HashSandbox (sandbox2 )
341438
342439 // Full hashes should be different because resources are different
343440 if hash1 == hash2 {
0 commit comments