@@ -10,8 +10,10 @@ import (
1010 "github.com/osbuild/blueprint/pkg/blueprint"
1111 "github.com/osbuild/images/internal/common"
1212 "github.com/osbuild/images/pkg/arch"
13+ "github.com/osbuild/images/pkg/container"
1314 "github.com/osbuild/images/pkg/distro"
1415 "github.com/osbuild/images/pkg/distro/defs"
16+ "github.com/osbuild/images/pkg/rpmmd"
1517)
1618
1719func isoTestImageType () * imageType {
@@ -134,3 +136,118 @@ func TestReplaceBasictemplate(t *testing.T) {
134136 assert .Equal (t , replaceBasicTemplate (tc .input , tc .arch ), tc .expected )
135137 }
136138}
139+
140+ func diskTestImageType () * imageType {
141+ return & imageType {
142+ arch : & architecture {
143+ distro : & distribution {},
144+ },
145+ ImageTypeYAML : defs.ImageTypeYAML {},
146+ }
147+ }
148+
149+ func ostreeTestImageType () * imageType {
150+ it := diskTestImageType ()
151+ it .ImageTypeYAML .OSTree .Ref = "rhel/9/x86_64/edge"
152+ return it
153+ }
154+
155+ func TestOSCustomizationsPodmanDefaultNetBackend (t * testing.T ) {
156+ netavark := container .NetworkBackendNetavark
157+
158+ tests := []struct {
159+ name string
160+ imageType func () * imageType
161+ backend * container.NetworkBackend
162+ containers []container.SourceSpec
163+ expectFile bool
164+ expectedPath string
165+ expectedVal string
166+ }{
167+ {
168+ name : "disk: backend set with containers creates file" ,
169+ imageType : diskTestImageType ,
170+ backend : & netavark ,
171+ containers : []container.SourceSpec {
172+ {Source : "registry.example.com/test:latest" },
173+ },
174+ expectFile : true ,
175+ expectedPath : "/var/lib/containers/storage/defaultNetworkBackend" ,
176+ expectedVal : "netavark" ,
177+ },
178+ {
179+ name : "disk: nil backend with containers does not create file" ,
180+ imageType : diskTestImageType ,
181+ backend : nil ,
182+ containers : []container.SourceSpec {
183+ {Source : "registry.example.com/test:latest" },
184+ },
185+ expectFile : false ,
186+ },
187+ {
188+ name : "disk: backend set without containers does not create file" ,
189+ imageType : diskTestImageType ,
190+ backend : & netavark ,
191+ containers : nil ,
192+ expectFile : false ,
193+ },
194+ {
195+ name : "disk: nil backend without containers does not create file" ,
196+ imageType : diskTestImageType ,
197+ backend : nil ,
198+ containers : nil ,
199+ expectFile : false ,
200+ },
201+ {
202+ name : "ostree: backend set with containers creates file in relocated path" ,
203+ imageType : ostreeTestImageType ,
204+ backend : & netavark ,
205+ containers : []container.SourceSpec {
206+ {Source : "registry.example.com/test:latest" },
207+ },
208+ expectFile : true ,
209+ expectedPath : "/usr/share/containers/storage/defaultNetworkBackend" ,
210+ expectedVal : "netavark" ,
211+ },
212+ {
213+ name : "ostree: nil backend with containers does not create file" ,
214+ imageType : ostreeTestImageType ,
215+ backend : nil ,
216+ containers : []container.SourceSpec {
217+ {Source : "registry.example.com/test:latest" },
218+ },
219+ expectFile : false ,
220+ },
221+ }
222+
223+ for _ , tt := range tests {
224+ t .Run (tt .name , func (t * testing.T ) {
225+ it := tt .imageType ()
226+ it .ImageConfigYAML .ImageConfig = & distro.ImageConfig {
227+ PodmanDefaultNetBackend : tt .backend ,
228+ }
229+
230+ bp := & blueprint.Blueprint {}
231+ osc , err := osCustomizations (it , rpmmd.PackageSet {}, distro.ImageOptions {}, tt .containers , bp )
232+ require .NoError (t , err )
233+
234+ if ! tt .expectFile {
235+ for _ , f := range osc .Files {
236+ assert .NotContains (t , f .Path (), "defaultNetworkBackend" ,
237+ "unexpected defaultNetworkBackend file found at %s" , f .Path ())
238+ }
239+ return
240+ }
241+
242+ var found bool
243+ for _ , f := range osc .Files {
244+ if f .Path () == tt .expectedPath {
245+ found = true
246+ assert .Equal (t , []byte (tt .expectedVal ), f .Data ())
247+ break
248+ }
249+ }
250+ assert .True (t , found , "expected file at %s" , tt .expectedPath )
251+ })
252+ }
253+ }
0 commit comments