github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/command/image/client_test.go (about)

     1  package image
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/api/types/filters"
    11  	"github.com/docker/docker/api/types/image"
    12  	"github.com/docker/docker/client"
    13  )
    14  
    15  type fakeClient struct {
    16  	client.Client
    17  	imageTagFunc     func(string, string) error
    18  	imageSaveFunc    func(images []string) (io.ReadCloser, error)
    19  	imageRemoveFunc  func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error)
    20  	imagePushFunc    func(ref string, options types.ImagePushOptions) (io.ReadCloser, error)
    21  	infoFunc         func() (types.Info, error)
    22  	imagePullFunc    func(ref string, options types.ImagePullOptions) (io.ReadCloser, error)
    23  	imagesPruneFunc  func(pruneFilter filters.Args) (types.ImagesPruneReport, error)
    24  	imageLoadFunc    func(input io.Reader, quiet bool) (types.ImageLoadResponse, error)
    25  	imageListFunc    func(options types.ImageListOptions) ([]types.ImageSummary, error)
    26  	imageInspectFunc func(image string) (types.ImageInspect, []byte, error)
    27  	imageImportFunc  func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error)
    28  	imageHistoryFunc func(image string) ([]image.HistoryResponseItem, error)
    29  	imageBuildFunc   func(context.Context, io.Reader, types.ImageBuildOptions) (types.ImageBuildResponse, error)
    30  }
    31  
    32  func (cli *fakeClient) ImageTag(_ context.Context, image, ref string) error {
    33  	if cli.imageTagFunc != nil {
    34  		return cli.imageTagFunc(image, ref)
    35  	}
    36  	return nil
    37  }
    38  
    39  func (cli *fakeClient) ImageSave(_ context.Context, images []string) (io.ReadCloser, error) {
    40  	if cli.imageSaveFunc != nil {
    41  		return cli.imageSaveFunc(images)
    42  	}
    43  	return io.NopCloser(strings.NewReader("")), nil
    44  }
    45  
    46  func (cli *fakeClient) ImageRemove(_ context.Context, image string,
    47  	options types.ImageRemoveOptions,
    48  ) ([]types.ImageDeleteResponseItem, error) {
    49  	if cli.imageRemoveFunc != nil {
    50  		return cli.imageRemoveFunc(image, options)
    51  	}
    52  	return []types.ImageDeleteResponseItem{}, nil
    53  }
    54  
    55  func (cli *fakeClient) ImagePush(_ context.Context, ref string, options types.ImagePushOptions) (io.ReadCloser, error) {
    56  	if cli.imagePushFunc != nil {
    57  		return cli.imagePushFunc(ref, options)
    58  	}
    59  	return io.NopCloser(strings.NewReader("")), nil
    60  }
    61  
    62  func (cli *fakeClient) Info(_ context.Context) (types.Info, error) {
    63  	if cli.infoFunc != nil {
    64  		return cli.infoFunc()
    65  	}
    66  	return types.Info{}, nil
    67  }
    68  
    69  func (cli *fakeClient) ImagePull(_ context.Context, ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
    70  	if cli.imagePullFunc != nil {
    71  		cli.imagePullFunc(ref, options)
    72  	}
    73  	return io.NopCloser(strings.NewReader("")), nil
    74  }
    75  
    76  func (cli *fakeClient) ImagesPrune(_ context.Context, pruneFilter filters.Args) (types.ImagesPruneReport, error) {
    77  	if cli.imagesPruneFunc != nil {
    78  		return cli.imagesPruneFunc(pruneFilter)
    79  	}
    80  	return types.ImagesPruneReport{}, nil
    81  }
    82  
    83  func (cli *fakeClient) ImageLoad(_ context.Context, input io.Reader, quiet bool) (types.ImageLoadResponse, error) {
    84  	if cli.imageLoadFunc != nil {
    85  		return cli.imageLoadFunc(input, quiet)
    86  	}
    87  	return types.ImageLoadResponse{}, nil
    88  }
    89  
    90  func (cli *fakeClient) ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error) {
    91  	if cli.imageListFunc != nil {
    92  		return cli.imageListFunc(options)
    93  	}
    94  	return []types.ImageSummary{}, nil
    95  }
    96  
    97  func (cli *fakeClient) ImageInspectWithRaw(_ context.Context, image string) (types.ImageInspect, []byte, error) {
    98  	if cli.imageInspectFunc != nil {
    99  		return cli.imageInspectFunc(image)
   100  	}
   101  	return types.ImageInspect{}, nil, nil
   102  }
   103  
   104  func (cli *fakeClient) ImageImport(_ context.Context, source types.ImageImportSource, ref string,
   105  	options types.ImageImportOptions,
   106  ) (io.ReadCloser, error) {
   107  	if cli.imageImportFunc != nil {
   108  		return cli.imageImportFunc(source, ref, options)
   109  	}
   110  	return io.NopCloser(strings.NewReader("")), nil
   111  }
   112  
   113  func (cli *fakeClient) ImageHistory(_ context.Context, img string) ([]image.HistoryResponseItem, error) {
   114  	if cli.imageHistoryFunc != nil {
   115  		return cli.imageHistoryFunc(img)
   116  	}
   117  	return []image.HistoryResponseItem{{ID: img, Created: time.Now().Unix()}}, nil
   118  }
   119  
   120  func (cli *fakeClient) ImageBuild(ctx context.Context, context io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
   121  	if cli.imageBuildFunc != nil {
   122  		return cli.imageBuildFunc(ctx, context, options)
   123  	}
   124  	return types.ImageBuildResponse{Body: io.NopCloser(strings.NewReader(""))}, nil
   125  }