...

Source file src/edge-infra.dev/test/fixtures/layout.go

Documentation: edge-infra.dev/test/fixtures

     1  package fixtures
     2  
     3  import (
     4  	"embed"
     5  	"fmt"
     6  	"io/fs"
     7  	"path/filepath"
     8  
     9  	"edge-infra.dev/pkg/f8n/warehouse/oci/layout"
    10  	"edge-infra.dev/pkg/f8n/warehouse/oci/name"
    11  	"edge-infra.dev/pkg/f8n/warehouse/oci/remote"
    12  	"edge-infra.dev/pkg/lib/build/bazel"
    13  )
    14  
    15  // This file contains utilities for loading the vendored test fixtures for OCI
    16  // packages, so they can be used as test data or installing packages to K8s
    17  // clusters during testing.
    18  
    19  //go:embed warehouse/layout/**
    20  var warehouseLayout embed.FS
    21  
    22  type Path struct {
    23  	*layout.Path
    24  }
    25  
    26  // Layout copies the embedded Image Layout to a test's temp directory and then
    27  // instantiates it.
    28  func Layout() (*Path, error) {
    29  	testdir, err := bazel.NewTestTmpDir("edge-infra-warehouse-*")
    30  	if err != nil {
    31  		return nil, fmt.Errorf("failed to resolve tmp dir for image layout: %w", err)
    32  	}
    33  	f, err := fs.Sub(warehouseLayout, "warehouse/layout")
    34  	if err != nil {
    35  		return nil, fmt.Errorf("failed to create layout filesystem: %w", err)
    36  	}
    37  	p, err := layout.FromFS(f, filepath.Join(testdir, "warehouse/layout"))
    38  	return &Path{Path: p}, err
    39  }
    40  
    41  // Push pushes an artifact from the Image Layout identified by package name and
    42  // tag to a remote registry
    43  func (p *Path) Push(dst name.Reference, pkg, tag string, opts ...remote.Option) error {
    44  	ref, err := name.Tag(fmt.Sprintf("%s:%s", pkg, tag))
    45  	if err != nil {
    46  		return err
    47  	}
    48  	oart, err := p.Get(ref)
    49  	if err != nil {
    50  		return fmt.Errorf("%w", err)
    51  	}
    52  	return remote.Write(oart, dst, opts...)
    53  }
    54  

View as plain text