@@ -3,10 +3,12 @@ package registry
33import (
44 "os"
55 "path/filepath"
6+ "strings"
67 "testing"
78
89 "github.com/stretchr/testify/assert"
910 "github.com/stretchr/testify/require"
11+ "go.podman.io/image/v5/pkg/sysregistriesv2"
1012 "go.podman.io/image/v5/types"
1113)
1214
@@ -44,6 +46,74 @@ registries = ['registry.access.redhat.com']
4446 require .NoError (t , SetSystemRegistriesConfPath (sys ))
4547 assert .NotEqual (t , hostPath , sys .SystemRegistriesConfPath )
4648 assert .NotEmpty (t , sys .SystemRegistriesConfPath )
49+
50+ // Verify the generated v2 file preserves the search registry.
51+ ctx := & types.SystemContext {SystemRegistriesConfPath : sys .SystemRegistriesConfPath }
52+ v2 , err := sysregistriesv2 .TryUpdatingCache (ctx )
53+ require .NoError (t , err )
54+ assert .Contains (t , v2 .UnqualifiedSearchRegistries , "registry.access.redhat.com" )
55+ }
56+
57+ func TestSetSystemRegistriesConfPath_PreservesV1BlockedAndInsecureRegistries (t * testing.T ) {
58+ resetRegistriesConfPathOnce ()
59+
60+ dir := t .TempDir ()
61+ hostPath := filepath .Join (dir , "registries.conf" )
62+ content := `[registries.search]
63+ registries = ['registry.access.redhat.com']
64+
65+ [registries.block]
66+ registries = ['docker.io', 'registry.hub.docker.com']
67+
68+ [registries.insecure]
69+ registries = ['insecure-registry.example.com']
70+ `
71+ require .NoError (t , os .WriteFile (hostPath , []byte (content ), 0644 ))
72+
73+ t .Setenv ("CONTAINERS_REGISTRIES_CONF" , hostPath )
74+
75+ sys := & types.SystemContext {}
76+ require .NoError (t , SetSystemRegistriesConfPath (sys ))
77+ assert .NotEqual (t , hostPath , sys .SystemRegistriesConfPath )
78+ assert .NotEmpty (t , sys .SystemRegistriesConfPath )
79+
80+ ctx := & types.SystemContext {SystemRegistriesConfPath : sys .SystemRegistriesConfPath }
81+
82+ reg , err := sysregistriesv2 .FindRegistry (ctx , "docker.io/library/busybox" )
83+ require .NoError (t , err )
84+ require .NotNil (t , reg )
85+ assert .True (t , reg .Blocked , "expected docker.io to be blocked" )
86+
87+ reg , err = sysregistriesv2 .FindRegistry (ctx , "insecure-registry.example.com/app:latest" )
88+ require .NoError (t , err )
89+ require .NotNil (t , reg )
90+ assert .True (t , reg .Insecure , "expected insecure-registry.example.com to be insecure" )
91+
92+ v2 , err := sysregistriesv2 .TryUpdatingCache (ctx )
93+ require .NoError (t , err )
94+ assert .Contains (t , v2 .UnqualifiedSearchRegistries , "registry.access.redhat.com" )
95+ }
96+
97+ func TestSetSystemRegistriesConfPath_FallsBackToEmptyV2WhenV1ConfigIsEmpty (t * testing.T ) {
98+ resetRegistriesConfPathOnce ()
99+
100+ dir := t .TempDir ()
101+ hostPath := filepath .Join (dir , "registries.conf" )
102+ content := `[registries.search]
103+ registries = []
104+ `
105+ require .NoError (t , os .WriteFile (hostPath , []byte (content ), 0644 ))
106+
107+ t .Setenv ("CONTAINERS_REGISTRIES_CONF" , hostPath )
108+
109+ sys := & types.SystemContext {}
110+ require .NoError (t , SetSystemRegistriesConfPath (sys ))
111+ assert .NotEqual (t , hostPath , sys .SystemRegistriesConfPath )
112+ assert .NotEmpty (t , sys .SystemRegistriesConfPath )
113+
114+ data , err := os .ReadFile (sys .SystemRegistriesConfPath )
115+ require .NoError (t , err )
116+ assert .Empty (t , strings .TrimSpace (string (data )))
47117}
48118
49119func TestSetSystemRegistriesConfPath_FallsBackWhenHostConfigMissing (t * testing.T ) {
0 commit comments