@@ -386,3 +386,79 @@ func TestRun_CorrectImage(t *testing.T) {
386386 })
387387 }
388388}
389+
390+ // TestRun_DirectOverride tests that an --image passed after a function has
391+ // already been build, the given --image with digest will override built function
392+ func TestRun_DirectOverride (t * testing.T ) {
393+ const overrideImage = "registry/myrepo/myimage@sha256:0000000000000000000000000000000000000000000000000000000000000000"
394+ root := FromTempDirectory (t )
395+ runner := mock .NewRunner ()
396+
397+ runner .RunFn = func (_ context.Context , f fn.Function , _ time.Duration ) (* fn.Job , error ) {
398+ if f .Build .Image != overrideImage {
399+ return nil , fmt .Errorf ("Expected image to be overridden with '%v' but got: '%v'" , overrideImage , f .Build .Image )
400+ }
401+ errs := make (chan error , 1 )
402+ stop := func () error { return nil }
403+ return fn .NewJob (f , "127.0.0.1" , "8080" , errs , stop , false )
404+ }
405+
406+ builder1 := mock .NewBuilder ()
407+
408+ // SETUP THE ENVIRONMENT & SITUATION
409+ // create function
410+ _ , err := fn .New ().Init (fn.Function {Root : root , Runtime : "go" })
411+ if err != nil {
412+ t .Fatal (err )
413+ }
414+
415+ // build function
416+ cmdBuild := NewBuildCmd (NewTestClient (fn .WithBuilder (builder1 ), fn .WithRegistry ("example.com/ns-to-override" )))
417+ if err := cmdBuild .Execute (); err != nil {
418+ t .Fatal (err )
419+ }
420+
421+ // fetch the functions state
422+ _ , err = fn .NewFunction (root )
423+ if err != nil {
424+ t .Fatal (err )
425+ }
426+
427+ // builder for 'func run' -- shall not be invoked
428+ builder2 := mock .NewBuilder ()
429+ builder2 .BuildFn = func (f fn.Function ) error {
430+ return fmt .Errorf ("should not be invoked" )
431+ }
432+
433+ // RUN THE ACTUAL TESTED COMMAND
434+ cmd := NewRunCmd (NewTestClient (
435+ fn .WithRunner (runner ),
436+ fn .WithBuilder (builder2 ),
437+ fn .WithRegistry ("ghcr.com/reg" ),
438+ ))
439+ cmd .SetArgs ([]string {fmt .Sprintf ("--image=%s" , overrideImage )})
440+
441+ // run function with above argument
442+ ctx , cancel := context .WithCancel (context .Background ())
443+ runErrCh := make (chan error , 1 )
444+ go func () {
445+ _ , err := cmd .ExecuteContextC (ctx )
446+ if err != nil {
447+ runErrCh <- err // error was not expected
448+ return
449+ }
450+
451+ // Ensure invocation doesnt happen for the second time as the image was
452+ // provided with a digest (should not build)
453+ if builder2 .BuildInvoked {
454+ runErrCh <- fmt .Errorf ("Function was not expected to build again but it did" )
455+ }
456+
457+ close (runErrCh ) // release the waiting parent process
458+ }()
459+ cancel () // trigger the return of cmd.ExecuteContextC in the routine
460+ <- ctx .Done ()
461+ if err := <- runErrCh ; err != nil { // wait for completion of assertions
462+ t .Fatal (err )
463+ }
464+ }
0 commit comments