get.porter.sh/porter@v1.3.0/pkg/cnab/cnab-to-oci/helpers.go (about)

     1  package cnabtooci
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"get.porter.sh/porter/pkg/cnab"
     8  	"github.com/docker/docker/api/types/image"
     9  	"github.com/opencontainers/go-digest"
    10  )
    11  
    12  var _ RegistryProvider = &TestRegistry{}
    13  
    14  type TestRegistry struct {
    15  	MockPullBundle        func(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (cnab.BundleReference, error)
    16  	MockPushBundle        func(ctx context.Context, ref cnab.BundleReference, opts RegistryOptions) (bundleReference cnab.BundleReference, err error)
    17  	MockPushImage         func(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (imageDigest digest.Digest, err error)
    18  	MockGetCachedImage    func(ctx context.Context, ref cnab.OCIReference) (ImageMetadata, error)
    19  	MockListTags          func(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) ([]string, error)
    20  	MockPullImage         func(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) error
    21  	MockGetBundleMetadata func(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (BundleMetadata, error)
    22  	MockGetImageMetadata  func(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (ImageMetadata, error)
    23  	cache                 map[string]ImageMetadata
    24  }
    25  
    26  func NewTestRegistry() *TestRegistry {
    27  	return &TestRegistry{
    28  		cache: make(map[string]ImageMetadata),
    29  	}
    30  }
    31  
    32  func (t TestRegistry) PullBundle(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (cnab.BundleReference, error) {
    33  	if t.MockPullBundle != nil {
    34  		return t.MockPullBundle(ctx, ref, opts)
    35  	}
    36  	sum, _ := NewImageSummaryFromInspect(ref, image.InspectResponse{ID: cnab.NewULID()})
    37  	t.cache[ref.String()] = sum
    38  
    39  	return cnab.BundleReference{Reference: ref}, nil
    40  }
    41  
    42  func (t *TestRegistry) PushBundle(ctx context.Context, ref cnab.BundleReference, opts RegistryOptions) (cnab.BundleReference, error) {
    43  	if t.MockPushBundle != nil {
    44  		return t.MockPushBundle(ctx, ref, opts)
    45  	}
    46  
    47  	return ref, nil
    48  }
    49  
    50  func (t *TestRegistry) PushImage(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (digest.Digest, error) {
    51  	if t.MockPushImage != nil {
    52  		return t.MockPushImage(ctx, ref, opts)
    53  	}
    54  	return "sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041", nil
    55  }
    56  
    57  func (t *TestRegistry) GetCachedImage(ctx context.Context, ref cnab.OCIReference) (ImageMetadata, error) {
    58  	if t.MockGetCachedImage != nil {
    59  		return t.MockGetCachedImage(ctx, ref)
    60  	}
    61  
    62  	img := ref.String()
    63  	sum, ok := t.cache[img]
    64  	if !ok {
    65  		return ImageMetadata{}, fmt.Errorf("failed to find image in docker cache: %w", ErrNotFound{Reference: ref})
    66  	}
    67  	return sum, nil
    68  }
    69  
    70  func (t TestRegistry) GetImageMetadata(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (ImageMetadata, error) {
    71  	meta, err := t.GetCachedImage(ctx, ref)
    72  	if err != nil {
    73  		if t.MockGetImageMetadata != nil {
    74  			return t.MockGetImageMetadata(ctx, ref, opts)
    75  		} else {
    76  			mockImg := ImageMetadata{
    77  				Reference:   ref,
    78  				RepoDigests: []string{fmt.Sprintf("%s@sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041", ref.Repository())},
    79  			}
    80  			return mockImg, nil
    81  		}
    82  	}
    83  	return meta, nil
    84  }
    85  
    86  func (t *TestRegistry) ListTags(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) ([]string, error) {
    87  	if t.MockListTags != nil {
    88  		return t.MockListTags(ctx, ref, opts)
    89  	}
    90  
    91  	return nil, nil
    92  }
    93  
    94  func (t *TestRegistry) PullImage(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) error {
    95  	if t.MockPullImage != nil {
    96  		return t.MockPullImage(ctx, ref, opts)
    97  	}
    98  
    99  	image_hash := ref.String()
   100  	sum, err := NewImageSummaryFromInspect(ref, image.InspectResponse{
   101  		ID:          cnab.NewULID(),
   102  		RepoDigests: []string{fmt.Sprintf("%s@sha256:75c495e5ce9c428d482973d72e3ce9925e1db304a97946c9aa0b540d7537e041", image_hash)},
   103  	})
   104  	if err != nil {
   105  		return err
   106  	}
   107  	t.cache[image_hash] = sum
   108  	return nil
   109  }
   110  
   111  func (t TestRegistry) GetBundleMetadata(ctx context.Context, ref cnab.OCIReference, opts RegistryOptions) (BundleMetadata, error) {
   112  	if t.MockGetBundleMetadata != nil {
   113  		return t.MockGetBundleMetadata(ctx, ref, opts)
   114  	}
   115  
   116  	return BundleMetadata{}, ErrNotFound{Reference: ref}
   117  }