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