@@ -150,6 +150,172 @@ func TestBuildDockerDeps(t *testing.T) {
150
150
}
151
151
}
152
152
153
+ func TestDockerPkgConfig_ExportToCache (t * testing.T ) {
154
+ tests := []struct {
155
+ name string
156
+ config leeway.DockerPkgConfig
157
+ expectedExport bool
158
+ }{
159
+ {
160
+ name : "default behavior - push directly" ,
161
+ config : leeway.DockerPkgConfig {
162
+ Image : []string {"test:latest" },
163
+ },
164
+ expectedExport : false ,
165
+ },
166
+ {
167
+ name : "explicit export to cache" ,
168
+ config : leeway.DockerPkgConfig {
169
+ Image : []string {"test:latest" },
170
+ ExportToCache : true ,
171
+ },
172
+ expectedExport : true ,
173
+ },
174
+ {
175
+ name : "explicit push directly" ,
176
+ config : leeway.DockerPkgConfig {
177
+ Image : []string {"test:latest" },
178
+ ExportToCache : false ,
179
+ },
180
+ expectedExport : false ,
181
+ },
182
+ }
183
+
184
+ for _ , tt := range tests {
185
+ t .Run (tt .name , func (t * testing.T ) {
186
+ if tt .config .ExportToCache != tt .expectedExport {
187
+ t .Errorf ("ExportToCache = %v, want %v" , tt .config .ExportToCache , tt .expectedExport )
188
+ }
189
+ })
190
+ }
191
+ }
192
+
193
+ func TestBuildDocker_ExportToCache (t * testing.T ) {
194
+ if * testutil .Dut {
195
+ pth , err := os .MkdirTemp ("" , "" )
196
+ if err != nil {
197
+ t .Fatal (err )
198
+ }
199
+ err = os .WriteFile (filepath .Join (pth , "docker" ), []byte (dummyDocker ), 0755 )
200
+ if err != nil {
201
+ t .Fatal (err )
202
+ }
203
+ t .Cleanup (func () { os .RemoveAll (pth ) })
204
+
205
+ os .Setenv ("PATH" , pth + ":" + os .Getenv ("PATH" ))
206
+ log .WithField ("path" , os .Getenv ("PATH" )).Debug ("modified path to use dummy docker" )
207
+ }
208
+ testutil .RunDUT ()
209
+
210
+ tests := []* testutil.CommandFixtureTest {
211
+ {
212
+ Name : "docker export to cache" ,
213
+ T : t ,
214
+ Args : []string {"build" , "-v" , "-c" , "none" , "comp:pkg" },
215
+ StderrSub : "Exporting Docker image to cache" ,
216
+ ExitCode : 0 ,
217
+ Fixture : & testutil.Setup {
218
+ Components : []testutil.Component {
219
+ {
220
+ Location : "comp" ,
221
+ Files : map [string ]string {
222
+ "Dockerfile" : "FROM alpine:latest" ,
223
+ },
224
+ Packages : []leeway.Package {
225
+ {
226
+ PackageInternal : leeway.PackageInternal {
227
+ Name : "pkg" ,
228
+ Type : leeway .DockerPackage ,
229
+ },
230
+ Config : leeway.DockerPkgConfig {
231
+ Dockerfile : "Dockerfile" ,
232
+ Image : []string {"test:latest" },
233
+ ExportToCache : true ,
234
+ },
235
+ },
236
+ },
237
+ },
238
+ },
239
+ },
240
+ },
241
+ }
242
+
243
+ for _ , test := range tests {
244
+ test .Run ()
245
+ }
246
+ }
247
+
248
+ func TestDockerPackage_BuildContextOverride (t * testing.T ) {
249
+ tests := []struct {
250
+ name string
251
+ packageConfigValue bool
252
+ buildContextExportFlag bool
253
+ buildContextExportSet bool
254
+ expectedFinal bool
255
+ }{
256
+ {
257
+ name : "no override - use package config false" ,
258
+ packageConfigValue : false ,
259
+ buildContextExportFlag : false ,
260
+ buildContextExportSet : false ,
261
+ expectedFinal : false ,
262
+ },
263
+ {
264
+ name : "no override - use package config true" ,
265
+ packageConfigValue : true ,
266
+ buildContextExportFlag : false ,
267
+ buildContextExportSet : false ,
268
+ expectedFinal : true ,
269
+ },
270
+ {
271
+ name : "CLI flag enables export (overrides package false)" ,
272
+ packageConfigValue : false ,
273
+ buildContextExportFlag : true ,
274
+ buildContextExportSet : true ,
275
+ expectedFinal : true ,
276
+ },
277
+ {
278
+ name : "CLI flag keeps export enabled (package true)" ,
279
+ packageConfigValue : true ,
280
+ buildContextExportFlag : true ,
281
+ buildContextExportSet : true ,
282
+ expectedFinal : true ,
283
+ },
284
+ {
285
+ name : "CLI flag disables export (overrides package true) - CRITICAL TEST" ,
286
+ packageConfigValue : true ,
287
+ buildContextExportFlag : false ,
288
+ buildContextExportSet : true ,
289
+ expectedFinal : false ,
290
+ },
291
+ {
292
+ name : "CLI flag keeps export disabled (package false)" ,
293
+ packageConfigValue : false ,
294
+ buildContextExportFlag : false ,
295
+ buildContextExportSet : true ,
296
+ expectedFinal : false ,
297
+ },
298
+ }
299
+
300
+ for _ , tt := range tests {
301
+ t .Run (tt .name , func (t * testing.T ) {
302
+ cfg := leeway.DockerPkgConfig {
303
+ ExportToCache : tt .packageConfigValue ,
304
+ }
305
+
306
+ // Simulate the build context override logic from buildDocker
307
+ // This mimics: if buildctx.DockerExportSet { cfg.ExportToCache = buildctx.DockerExportToCache }
308
+ if tt .buildContextExportSet {
309
+ cfg .ExportToCache = tt .buildContextExportFlag
310
+ }
311
+
312
+ if cfg .ExportToCache != tt .expectedFinal {
313
+ t .Errorf ("ExportToCache = %v, want %v" , cfg .ExportToCache , tt .expectedFinal )
314
+ }
315
+ })
316
+ }
317
+ }
318
+
153
319
func TestDockerPostProcessing (t * testing.T ) {
154
320
if * testutil .Dut {
155
321
pth , err := os .MkdirTemp ("" , "" )
0 commit comments