github.com/kubesphere/s2irun@v3.2.1+incompatible/pkg/docker/fake_docker.go (about)

     1  package docker
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"io/ioutil"
     7  
     8  	dockertypes "github.com/docker/docker/api/types"
     9  
    10  	"github.com/kubesphere/s2irun/pkg/api"
    11  	"github.com/kubesphere/s2irun/pkg/tar"
    12  	"github.com/kubesphere/s2irun/pkg/utils/fs"
    13  )
    14  
    15  // FakeDocker provides a fake docker interface
    16  type FakeDocker struct {
    17  	LocalRegistryImage           string
    18  	LocalRegistryResult          bool
    19  	LocalRegistryError           error
    20  	RemoveContainerID            string
    21  	RemoveContainerError         error
    22  	DefaultURLImage              string
    23  	DefaultURLResult             string
    24  	DefaultURLError              error
    25  	AssembleInputFilesResult     string
    26  	AssembleInputFilesError      error
    27  	RunContainerOpts             RunContainerOptions
    28  	RunContainerError            error
    29  	RunContainerErrorBeforeStart bool
    30  	RunContainerContainerID      string
    31  	RunContainerCmd              []string
    32  	GetImageIDImage              string
    33  	GetImageIDResult             string
    34  	GetImageIDError              error
    35  	GetImageUserImage            string
    36  	GetImageUserResult           string
    37  	GetImageUserError            error
    38  	GetImageEntrypointResult     []string
    39  	GetImageEntrypointError      error
    40  	CommitContainerOpts          CommitContainerOptions
    41  	CommitContainerResult        string
    42  	CommitContainerError         error
    43  	RemoveImageName              string
    44  	RemoveImageError             error
    45  	BuildImageOpts               BuildImageOptions
    46  	BuildImageError              error
    47  	PullResult                   bool
    48  	PullError                    error
    49  	PushResult                   bool
    50  	PushError                    error
    51  	OnBuildImage                 string
    52  	OnBuildResult                []string
    53  	OnBuildError                 error
    54  	IsOnBuildResult              bool
    55  	IsOnBuildImage               string
    56  	Labels                       map[string]string
    57  	LabelsError                  error
    58  	GetInspectImage              *dockertypes.ImageInspect
    59  	GetInspectImageError         error
    60  }
    61  
    62  // IsImageInLocalRegistry checks if the image exists in the fake local registry
    63  func (f *FakeDocker) IsImageInLocalRegistry(imageName string) (bool, error) {
    64  	f.LocalRegistryImage = imageName
    65  	return f.LocalRegistryResult, f.LocalRegistryError
    66  }
    67  
    68  // IsImageOnBuild  returns true if the builder has onbuild instructions
    69  func (f *FakeDocker) IsImageOnBuild(imageName string) bool {
    70  	f.IsOnBuildImage = imageName
    71  	return f.IsOnBuildResult
    72  }
    73  
    74  // Version returns information of the docker client and server host
    75  func (f *FakeDocker) Version() (dockertypes.Version, error) {
    76  	return dockertypes.Version{}, nil
    77  }
    78  
    79  // GetImageWorkdir returns the workdir
    80  func (f *FakeDocker) GetImageWorkdir(name string) (string, error) {
    81  	return "/", nil
    82  }
    83  
    84  // GetOnBuild returns the list of onbuild instructions for the given image
    85  func (f *FakeDocker) GetOnBuild(imageName string) ([]string, error) {
    86  	f.OnBuildImage = imageName
    87  	return f.OnBuildResult, f.OnBuildError
    88  }
    89  
    90  // RemoveContainer removes a fake Docker container
    91  func (f *FakeDocker) RemoveContainer(id string) error {
    92  	f.RemoveContainerID = id
    93  	return f.RemoveContainerError
    94  }
    95  
    96  // KillContainer kills a fake container
    97  func (f *FakeDocker) KillContainer(id string) error {
    98  	return nil
    99  }
   100  
   101  // GetScriptsURL returns a default STI scripts URL
   102  func (f *FakeDocker) GetScriptsURL(image string) (string, error) {
   103  	f.DefaultURLImage = image
   104  	return f.DefaultURLResult, f.DefaultURLError
   105  }
   106  
   107  // GetAssembleInputFiles finds a io.openshift.s2i.assemble-input-files label on the given image.
   108  func (f *FakeDocker) GetAssembleInputFiles(image string) (string, error) {
   109  	return f.AssembleInputFilesResult, f.AssembleInputFilesError
   110  }
   111  
   112  // RunContainer runs a fake Docker container
   113  func (f *FakeDocker) RunContainer(opts RunContainerOptions) error {
   114  	f.RunContainerOpts = opts
   115  	if f.RunContainerErrorBeforeStart {
   116  		return f.RunContainerError
   117  	}
   118  	if opts.Stdout != nil {
   119  		opts.Stdout.Close()
   120  	}
   121  	if opts.Stderr != nil {
   122  		opts.Stderr.Close()
   123  	}
   124  	if opts.OnStart != nil {
   125  		if err := opts.OnStart(""); err != nil {
   126  			return err
   127  		}
   128  	}
   129  	if opts.Stdin != nil {
   130  		_, err := io.Copy(ioutil.Discard, opts.Stdin)
   131  		if err != nil {
   132  			return err
   133  		}
   134  	}
   135  	if opts.PostExec != nil {
   136  		opts.PostExec.PostExecute(f.RunContainerContainerID, string(opts.Command))
   137  	}
   138  	return f.RunContainerError
   139  }
   140  
   141  // UploadToContainer uploads artifacts to the container.
   142  func (f *FakeDocker) UploadToContainer(fs fs.FileSystem, srcPath, destPath, container string) error {
   143  	return nil
   144  }
   145  
   146  // UploadToContainerWithTarWriter uploads artifacts to the container.
   147  func (f *FakeDocker) UploadToContainerWithTarWriter(fs fs.FileSystem, srcPath, destPath, container string, makeTarWriter func(io.Writer) tar.Writer) error {
   148  	return errors.New("not implemented")
   149  }
   150  
   151  // DownloadFromContainer downloads file (or directory) from the container.
   152  func (f *FakeDocker) DownloadFromContainer(containerPath string, w io.Writer, container string) error {
   153  	return errors.New("not implemented")
   154  }
   155  
   156  // GetImageID returns a fake Docker image ID
   157  func (f *FakeDocker) GetImageID(image string) (string, error) {
   158  	f.GetImageIDImage = image
   159  	return f.GetImageIDResult, f.GetImageIDError
   160  }
   161  
   162  // GetImageUser returns a fake user
   163  func (f *FakeDocker) GetImageUser(image string) (string, error) {
   164  	f.GetImageUserImage = image
   165  	return f.GetImageUserResult, f.GetImageUserError
   166  }
   167  
   168  // InspectImage returns a fake Image info
   169  func (f *FakeDocker) InspectImage(image string) (*dockertypes.ImageInspect, error) {
   170  	return f.GetInspectImage, f.GetInspectImageError
   171  }
   172  
   173  // GetImageEntrypoint returns an empty entrypoint
   174  func (f *FakeDocker) GetImageEntrypoint(image string) ([]string, error) {
   175  	return f.GetImageEntrypointResult, f.GetImageEntrypointError
   176  }
   177  
   178  // CommitContainer commits a fake Docker container
   179  func (f *FakeDocker) CommitContainer(opts CommitContainerOptions) (string, error) {
   180  	f.CommitContainerOpts = opts
   181  	return f.CommitContainerResult, f.CommitContainerError
   182  }
   183  
   184  // RemoveImage removes a fake Docker image
   185  func (f *FakeDocker) RemoveImage(name string) error {
   186  	f.RemoveImageName = name
   187  	return f.RemoveImageError
   188  }
   189  
   190  // CheckImage checks image in local registry
   191  func (f *FakeDocker) CheckImage(name string) (*api.Image, error) {
   192  	return nil, nil
   193  }
   194  
   195  // PullImage pulls a fake docker image
   196  func (f *FakeDocker) PullImage(imageName string) (*api.Image, error) {
   197  	if f.PullResult {
   198  		return &api.Image{}, nil
   199  	}
   200  	return nil, f.PullError
   201  }
   202  func (f *FakeDocker) PushImage(name string) error {
   203  	if f.PushResult {
   204  		return nil
   205  	}
   206  	return f.PushError
   207  }
   208  
   209  // CheckAndPullImage pulls a fake docker image
   210  func (f *FakeDocker) CheckAndPullImage(name string) (*api.Image, error) {
   211  	if f.PullResult {
   212  		return &api.Image{}, nil
   213  	}
   214  	return nil, f.PullError
   215  }
   216  
   217  // BuildImage builds image
   218  func (f *FakeDocker) BuildImage(opts BuildImageOptions) error {
   219  	f.BuildImageOpts = opts
   220  	if opts.Stdin != nil {
   221  		_, err := io.Copy(ioutil.Discard, opts.Stdin)
   222  		if err != nil {
   223  			return err
   224  		}
   225  	}
   226  	return f.BuildImageError
   227  }
   228  
   229  // GetLabels returns the labels of the image
   230  func (f *FakeDocker) GetLabels(name string) (map[string]string, error) {
   231  	return f.Labels, f.LabelsError
   232  }
   233  
   234  // CheckReachable returns if the Docker daemon is reachable from s2i
   235  func (f *FakeDocker) CheckReachable() error {
   236  	return nil
   237  }