@@ -368,6 +368,108 @@ func TestRewriteReference(t *testing.T) {
368368 // condition fires → registry reverts to host, no library/ to strip
369369 assertAnnotationsInStore (t , s , "newrepo/img:v2" , host + "/newrepo/img:v2" )
370370 })
371+
372+ // The library/-detection must look at rawRewrite's path (not the
373+ // go-containerregistry-normalized newRepo, which always carries "library/" for
374+ // single-segment repos), so that a rewrite which explicitly asks for
375+ // "library/..." is honored instead of being unconditionally stripped.
376+
377+ t .Run ("path-only rewrite with explicit library/ prefix is preserved" , func (t * testing.T ) {
378+ s := newTestStore (t )
379+ seedStoreDescriptor (t , s , map [string ]string {
380+ ocispec .AnnotationRefName : "library/nginx:latest" ,
381+ consts .ContainerdImageNameKey : "index.docker.io/library/nginx:latest" ,
382+ })
383+
384+ oldRef , _ := name .NewTag ("nginx:latest" )
385+ newRef , _ := name .NewTag ("library/nginx:v2" )
386+ rawRewrite := "library/nginx:v2"
387+
388+ if err := rewriteReference (ctx , s , oldRef , newRef , rawRewrite ); err != nil {
389+ t .Fatalf ("rewriteReference: %v" , err )
390+ }
391+ // rewriteRepo (derived from rawRewrite) starts with "library/" → must be kept
392+ assertAnnotationsInStore (t , s , "library/nginx:v2" , "index.docker.io/library/nginx:v2" )
393+ })
394+
395+ t .Run ("leading slash rewrite with explicit library/ prefix is preserved" , func (t * testing.T ) {
396+ s := newTestStore (t )
397+ seedStoreDescriptor (t , s , map [string ]string {
398+ ocispec .AnnotationRefName : "library/nginx:latest" ,
399+ consts .ContainerdImageNameKey : "index.docker.io/library/nginx:latest" ,
400+ })
401+
402+ oldRef , _ := name .NewTag ("nginx:latest" )
403+ newRef , _ := name .NewTag ("library/nginx:v2" )
404+ // AddImageCmd passes the pre-trim rewrite string through as rawRewrite, so a
405+ // leading "/" must still be handled correctly here.
406+ rawRewrite := "/library/nginx:v2"
407+
408+ if err := rewriteReference (ctx , s , oldRef , newRef , rawRewrite ); err != nil {
409+ t .Fatalf ("rewriteReference: %v" , err )
410+ }
411+ assertAnnotationsInStore (t , s , "library/nginx:v2" , "index.docker.io/library/nginx:v2" )
412+ })
413+ }
414+
415+ func TestRewriteChartReference (t * testing.T ) {
416+ ctx := newTestContext (t )
417+
418+ // A chart rewritten to a bare single-segment name must not keep an erroneous
419+ // "library/" prefix picked up from go-containerregistry's docker hub
420+ // normalization, unless the rewrite explicitly asked for one.
421+
422+ t .Run ("path-only rewrite strips library/ prefix from docker hub normalization" , func (t * testing.T ) {
423+ s := newTestStore (t )
424+ seedStoreDescriptor (t , s , map [string ]string {
425+ ocispec .AnnotationRefName : "library/mychart:1.0.0" ,
426+ })
427+
428+ ref , _ := name .NewTag ("mychart:1.0.0" )
429+ if err := rewriteChartReference (ctx , s , ref , "mychart:2.0.0" ); err != nil {
430+ t .Fatalf ("rewriteChartReference: %v" , err )
431+ }
432+ assertArtifactInStore (t , s , "mychart:2.0.0" )
433+ })
434+
435+ t .Run ("explicit library/ prefix in rewrite is preserved" , func (t * testing.T ) {
436+ s := newTestStore (t )
437+ seedStoreDescriptor (t , s , map [string ]string {
438+ ocispec .AnnotationRefName : "library/mychart:1.0.0" ,
439+ })
440+
441+ ref , _ := name .NewTag ("mychart:1.0.0" )
442+ if err := rewriteChartReference (ctx , s , ref , "library/mychart:2.0.0" ); err != nil {
443+ t .Fatalf ("rewriteChartReference: %v" , err )
444+ }
445+ assertArtifactInStore (t , s , "library/mychart:2.0.0" )
446+ })
447+
448+ t .Run ("leading slash rewrite with explicit library/ prefix is preserved" , func (t * testing.T ) {
449+ s := newTestStore (t )
450+ seedStoreDescriptor (t , s , map [string ]string {
451+ ocispec .AnnotationRefName : "library/mychart:1.0.0" ,
452+ })
453+
454+ ref , _ := name .NewTag ("mychart:1.0.0" )
455+ if err := rewriteChartReference (ctx , s , ref , "/library/mychart:2.0.0" ); err != nil {
456+ t .Fatalf ("rewriteChartReference: %v" , err )
457+ }
458+ assertArtifactInStore (t , s , "library/mychart:2.0.0" )
459+ })
460+
461+ t .Run ("rewrite omitting tag inherits the source tag" , func (t * testing.T ) {
462+ s := newTestStore (t )
463+ seedStoreDescriptor (t , s , map [string ]string {
464+ ocispec .AnnotationRefName : "library/mychart:1.0.0" ,
465+ })
466+
467+ ref , _ := name .NewTag ("mychart:1.0.0" )
468+ if err := rewriteChartReference (ctx , s , ref , "myneworg/mychart" ); err != nil {
469+ t .Fatalf ("rewriteChartReference: %v" , err )
470+ }
471+ assertArtifactInStore (t , s , "myneworg/mychart:1.0.0" )
472+ })
371473}
372474
373475// --------------------------------------------------------------------------
0 commit comments