@@ -2,6 +2,7 @@ package buildpacks
22
33import (
44 "context"
5+ "fmt"
56 "os"
67 "path/filepath"
78 "reflect"
@@ -10,6 +11,8 @@ import (
1011 pack "github.com/buildpacks/pack/pkg/client"
1112 "knative.dev/func/pkg/builders"
1213 fn "knative.dev/func/pkg/functions"
14+ "knative.dev/func/pkg/scaffolding"
15+ . "knative.dev/func/pkg/testing"
1316)
1417
1518// TestBuild_BuilderImageUntrusted ensures that only known builder images
@@ -83,12 +86,21 @@ func TestBuild_BuilderImageDefault(t *testing.T) {
8386// defined in-code, but none defined on the function, the defaults will be
8487// used.
8588func TestBuild_BuildpacksDefault (t * testing.T ) {
89+ root , done := Mktemp (t )
90+ defer done ()
91+
8692 var (
87- i = & mockImpl {}
88- b = NewBuilder (WithImpl (i ))
89- f = fn.Function {Runtime : "go" }
93+ i = & mockImpl {}
94+ b = NewBuilder (WithImpl (i ))
95+ f = fn.Function {Runtime : "go" , Root : root , Registry : "example.com/alice" }
96+ err error
9097 )
9198
99+ // Initialize the function to create proper source files
100+ if f , err = fn .New ().Init (f ); err != nil {
101+ t .Fatal (err )
102+ }
103+
92104 i .BuildFn = func (ctx context.Context , opts pack.BuildOptions ) error {
93105 expected := defaultBuildpacks ["go" ]
94106 if ! reflect .DeepEqual (expected , opts .Buildpacks ) {
@@ -136,23 +148,32 @@ func TestBuild_BuilderImageConfigurable(t *testing.T) {
136148// TestBuild_BuilderImageExclude ensures that ignored files are not added to the func
137149// image
138150func TestBuild_BuilderImageExclude (t * testing.T ) {
151+ root , done := Mktemp (t )
152+ defer done ()
153+
139154 var (
140155 i = & mockImpl {} // mock underlying implementation
141156 b = NewBuilder ( // Func Builder logic
142157 WithName (builders .Pack ), WithImpl (i ))
143158 f = fn.Function {
144- Runtime : "go" ,
159+ Runtime : "go" ,
160+ Root : root ,
161+ Registry : "example.com/alice" ,
145162 }
163+ err error
146164 )
165+
166+ // Initialize the function to create proper source files
167+ if f , err = fn .New ().Init (f ); err != nil {
168+ t .Fatal (err )
169+ }
170+
147171 funcIgnoreContent := []byte (`#testing comments
148172hello.txt` )
149173 expected := []string {"hello.txt" }
150174
151- tempdir := t .TempDir ()
152- f .Root = tempdir
153-
154175 //create a .funcignore file containing the details of the files to be ignored
155- err : = os .WriteFile (filepath .Join (f .Root , ".funcignore" ), funcIgnoreContent , 0644 )
176+ err = os .WriteFile (filepath .Join (f .Root , ".funcignore" ), funcIgnoreContent , 0644 )
156177 if err != nil {
157178 t .Fatal (err )
158179 }
@@ -204,6 +225,57 @@ func TestBuild_Envs(t *testing.T) {
204225 }
205226}
206227
228+ // TestBuild_MiddlewareLabel ensures that the middleware-version label is set
229+ // on the build options for runtimes that support scaffolding.
230+ func TestBuild_MiddlewareLabel (t * testing.T ) {
231+ root , done := Mktemp (t )
232+ defer done ()
233+
234+ var (
235+ i = & mockImpl {}
236+ b = NewBuilder (WithImpl (i ))
237+ f = fn.Function {
238+ Name : "test-middleware-label" ,
239+ Root : root ,
240+ Runtime : "go" ,
241+ Registry : "example.com/alice" ,
242+ }
243+ err error
244+ )
245+
246+ // Initialize the function to create proper source files
247+ if f , err = fn .New ().Init (f ); err != nil {
248+ t .Fatal (err )
249+ }
250+
251+ // Get expected middleware version
252+ expectedVersion , err := scaffolding .MiddlewareVersion (f .Root , f .Runtime , f .Invoke , fn .EmbeddedTemplatesFS )
253+ if err != nil {
254+ t .Fatalf ("failed to get expected middleware version: %v" , err )
255+ }
256+ if expectedVersion == "" {
257+ t .Fatal ("expected middleware version to be non-empty for go runtime" )
258+ }
259+
260+ expectedLabel := fmt .Sprintf ("%s=%s" , fn .MiddlewareVersionLabelKey , expectedVersion )
261+
262+ i .BuildFn = func (ctx context.Context , opts pack.BuildOptions ) error {
263+ bpLabels , ok := opts .Env ["BP_IMAGE_LABELS" ]
264+ if ! ok {
265+ t .Fatal ("expected BP_IMAGE_LABELS to be set" )
266+ }
267+ if bpLabels != expectedLabel {
268+ t .Fatalf ("expected BP_IMAGE_LABELS to be %q, got: %q" , expectedLabel , bpLabels )
269+ }
270+ t .Logf ("BP_IMAGE_LABELS: %s" , bpLabels )
271+ return nil
272+ }
273+
274+ if err := b .Build (context .Background (), f , nil ); err != nil {
275+ t .Fatal (err )
276+ }
277+ }
278+
207279// TestBuild_Errors confirms error scenarios.
208280func TestBuild_Errors (t * testing.T ) {
209281 testCases := []struct {
0 commit comments