github.com/docker/cnab-to-oci@v0.3.0-beta4/remotes/mocks_test.go (about) 1 package remotes 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io" 8 "io/ioutil" 9 10 "github.com/containerd/containerd/content" 11 "github.com/containerd/containerd/remotes" 12 "github.com/docker/docker/api/types" 13 "github.com/opencontainers/go-digest" 14 ocischemav1 "github.com/opencontainers/image-spec/specs-go/v1" 15 ) 16 17 // Mock remote.Resolver interface 18 type mockResolver struct { 19 resolvedDescriptors []ocischemav1.Descriptor 20 pushedReferences []string 21 pusher *mockPusher 22 fetcher *mockFetcher 23 } 24 25 func (r *mockResolver) Resolve(_ context.Context, ref string) (string, ocischemav1.Descriptor, error) { 26 descriptor := r.resolvedDescriptors[0] 27 r.resolvedDescriptors = r.resolvedDescriptors[1:] 28 if descriptor.Size == -1 { 29 return "", descriptor, fmt.Errorf("empty descriptor") 30 } 31 return ref, descriptor, nil 32 } 33 func (r *mockResolver) Fetcher(_ context.Context, ref string) (remotes.Fetcher, error) { 34 return r.fetcher, nil 35 } 36 func (r *mockResolver) Pusher(_ context.Context, ref string) (remotes.Pusher, error) { 37 r.pushedReferences = append(r.pushedReferences, ref) 38 return r.pusher, nil 39 } 40 41 // Mock remotes.Pusher interface 42 type mockPusher struct { 43 pushedDescriptors []ocischemav1.Descriptor 44 buffers []*bytes.Buffer 45 returnErrorValues []error 46 } 47 48 func newMockPusher(ret []error) *mockPusher { 49 return &mockPusher{ 50 pushedDescriptors: []ocischemav1.Descriptor{}, 51 buffers: []*bytes.Buffer{}, 52 returnErrorValues: ret, 53 } 54 } 55 56 func (p *mockPusher) Push(ctx context.Context, d ocischemav1.Descriptor) (content.Writer, error) { 57 p.pushedDescriptors = append(p.pushedDescriptors, d) 58 buf := &bytes.Buffer{} 59 p.buffers = append(p.buffers, buf) 60 var err error 61 if p.returnErrorValues != nil { 62 err = p.returnErrorValues[0] 63 p.returnErrorValues = p.returnErrorValues[1:] 64 } 65 return &mockWriter{ 66 WriteCloser: nopWriteCloser{Buffer: buf}, 67 }, err 68 } 69 70 // Mock content.Writer interface 71 type mockWriter struct { 72 io.WriteCloser 73 } 74 75 func (w mockWriter) Digest() digest.Digest { return "" } 76 func (w mockWriter) Commit(ctx context.Context, size int64, expected digest.Digest, opts ...content.Opt) error { 77 return nil 78 } 79 func (w mockWriter) Status() (content.Status, error) { return content.Status{}, nil } 80 func (w mockWriter) Truncate(size int64) error { return nil } 81 82 type nopWriteCloser struct { 83 *bytes.Buffer 84 } 85 86 func (n nopWriteCloser) Close() error { return nil } 87 88 // Mock remotes.Fetcher interface 89 type mockFetcher struct { 90 indexBuffers []*bytes.Buffer 91 } 92 93 func (f *mockFetcher) Fetch(ctx context.Context, desc ocischemav1.Descriptor) (io.ReadCloser, error) { 94 rc := ioutil.NopCloser(f.indexBuffers[0]) 95 f.indexBuffers = f.indexBuffers[1:] 96 return rc, nil 97 } 98 99 type mockReadCloser struct { 100 } 101 102 func (rc mockReadCloser) Read(p []byte) (n int, err error) { 103 return 0, io.EOF 104 } 105 106 func (rc mockReadCloser) Close() error { 107 return nil 108 } 109 110 type mockImageClient struct { 111 pushedImages int 112 } 113 114 func newMockImageClient() *mockImageClient { 115 return &mockImageClient{} 116 } 117 118 func (c *mockImageClient) ImagePush(ctx context.Context, ref string, options types.ImagePushOptions) (io.ReadCloser, error) { 119 c.pushedImages++ 120 return mockReadCloser{}, nil 121 } 122 func (c *mockImageClient) ImageTag(ctx context.Context, image, ref string) error { 123 return nil 124 }