@@ -330,3 +330,92 @@ type fileInfo struct {
330
330
Executable bool
331
331
Linkname string
332
332
}
333
+
334
+ // TestBuilder_StaticEnvs ensures that certain "static" environment variables
335
+ // comprising Function metadata are added to the config.
336
+ func TestBuilder_StaticEnvs (t * testing.T ) {
337
+ root , done := Mktemp (t )
338
+ defer done ()
339
+
340
+ staticEnvs := []string {
341
+ "FUNC_CREATED" ,
342
+ "FUNC_VERSION" ,
343
+ }
344
+
345
+ f , err := fn .New ().Init (fn.Function {Root : root , Runtime : "go" })
346
+ if err != nil {
347
+ t .Fatal (err )
348
+ }
349
+
350
+ if err := NewBuilder ("" , true ).Build (context .Background (), f , TestPlatforms ); err != nil {
351
+ t .Fatal (err )
352
+ }
353
+
354
+ // Assert
355
+ // Check if the OCI container defines at least one of the static
356
+ // variables on each of the constituent containers.
357
+ // ---
358
+ // Get the images list (manifest descripors) from the index
359
+ ociPath := path (f .Root , fn .RunDataDir , "builds" , "last" , "oci" )
360
+ data , err := os .ReadFile (filepath .Join (ociPath , "index.json" ))
361
+ if err != nil {
362
+ t .Fatal (err )
363
+ }
364
+ var index struct {
365
+ Manifests []struct {
366
+ Digest string `json:"digest"`
367
+ } `json:"manifests"`
368
+ }
369
+ if err := json .Unmarshal (data , & index ); err != nil {
370
+ t .Fatal (err )
371
+ }
372
+ for _ , manifestDesc := range index .Manifests {
373
+
374
+ // Dereference the manifest descriptor into the referenced image manifest
375
+ manifestHash := strings .TrimPrefix (manifestDesc .Digest , "sha256:" )
376
+ data , err := os .ReadFile (filepath .Join (ociPath , "blobs" , "sha256" , manifestHash ))
377
+ if err != nil {
378
+ t .Fatal (err )
379
+ }
380
+ var manifest struct {
381
+ Config struct {
382
+ Digest string `json:"digest"`
383
+ } `json:"config"`
384
+ }
385
+ if err := json .Unmarshal (data , & manifest ); err != nil {
386
+ t .Fatal (err )
387
+ }
388
+
389
+ // From the image manifest get the image's config.json
390
+ configHash := strings .TrimPrefix (manifest .Config .Digest , "sha256:" )
391
+ data , err = os .ReadFile (filepath .Join (ociPath , "blobs" , "sha256" , configHash ))
392
+ if err != nil {
393
+ t .Fatal (err )
394
+ }
395
+ var config struct {
396
+ Config struct {
397
+ Env []string `json:"Env"`
398
+ } `json:"config"`
399
+ }
400
+ if err := json .Unmarshal (data , & config ); err != nil {
401
+ panic (err )
402
+ }
403
+
404
+ containsEnv := func (ss []string , name string ) bool {
405
+ for _ , s := range ss {
406
+ if strings .HasPrefix (s , name ) {
407
+ return true
408
+ }
409
+ }
410
+ return false
411
+ }
412
+
413
+ for _ , expected := range staticEnvs {
414
+ t .Logf ("checking for %q in slice %v" , expected , config .Config .Env )
415
+ if containsEnv (config .Config .Env , expected ) {
416
+ continue // to check the rest
417
+ }
418
+ t .Fatalf ("static env %q not found in resultant container" , expected )
419
+ }
420
+ }
421
+ }
0 commit comments