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