2015-11-03 14:54:15 +01:00
|
|
|
package docker
|
2014-10-10 11:00:59 -04:00
|
|
|
|
2016-05-20 15:52:05 +02:00
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"io"
|
2016-10-23 17:40:25 +01:00
|
|
|
"io/ioutil"
|
2016-10-06 19:36:28 +01:00
|
|
|
|
2017-11-21 11:13:29 +01:00
|
|
|
dockertypes "github.com/docker/docker/api/types"
|
|
|
|
|
|
2016-10-06 19:36:28 +01:00
|
|
|
"github.com/openshift/source-to-image/pkg/api"
|
2016-10-23 17:40:25 +01:00
|
|
|
"github.com/openshift/source-to-image/pkg/tar"
|
2017-07-11 10:33:36 +01:00
|
|
|
"github.com/openshift/source-to-image/pkg/util/fs"
|
2016-05-20 15:52:05 +02:00
|
|
|
)
|
2014-10-10 11:00:59 -04:00
|
|
|
|
2014-12-04 15:01:52 +01:00
|
|
|
// FakeDocker provides a fake docker interface
|
2014-10-10 11:00:59 -04:00
|
|
|
type FakeDocker struct {
|
|
|
|
|
LocalRegistryImage string
|
|
|
|
|
LocalRegistryResult bool
|
|
|
|
|
LocalRegistryError error
|
|
|
|
|
RemoveContainerID string
|
|
|
|
|
RemoveContainerError error
|
2014-12-17 22:27:34 +01:00
|
|
|
DefaultURLImage string
|
|
|
|
|
DefaultURLResult string
|
|
|
|
|
DefaultURLError error
|
2016-05-20 15:52:05 +02:00
|
|
|
AssembleInputFilesResult string
|
|
|
|
|
AssembleInputFilesError error
|
2015-11-03 14:54:15 +01:00
|
|
|
RunContainerOpts RunContainerOptions
|
2014-10-10 11:00:59 -04:00
|
|
|
RunContainerError error
|
|
|
|
|
RunContainerErrorBeforeStart bool
|
|
|
|
|
RunContainerContainerID string
|
|
|
|
|
RunContainerCmd []string
|
2014-12-04 15:01:52 +01:00
|
|
|
GetImageIDImage string
|
|
|
|
|
GetImageIDResult string
|
|
|
|
|
GetImageIDError error
|
2015-02-04 11:12:01 -05:00
|
|
|
GetImageUserImage string
|
|
|
|
|
GetImageUserResult string
|
|
|
|
|
GetImageUserError error
|
2016-07-15 13:27:15 -04:00
|
|
|
GetImageEntrypointResult []string
|
|
|
|
|
GetImageEntrypointError error
|
2015-11-03 14:54:15 +01:00
|
|
|
CommitContainerOpts CommitContainerOptions
|
2014-10-10 11:00:59 -04:00
|
|
|
CommitContainerResult string
|
|
|
|
|
CommitContainerError error
|
|
|
|
|
RemoveImageName string
|
|
|
|
|
RemoveImageError error
|
2015-11-03 14:54:15 +01:00
|
|
|
BuildImageOpts BuildImageOptions
|
2014-12-15 14:23:15 +01:00
|
|
|
BuildImageError error
|
2015-02-20 10:37:43 +01:00
|
|
|
PullResult bool
|
|
|
|
|
PullError error
|
2015-08-03 11:34:26 -04:00
|
|
|
OnBuildImage string
|
|
|
|
|
OnBuildResult []string
|
|
|
|
|
OnBuildError error
|
|
|
|
|
IsOnBuildResult bool
|
|
|
|
|
IsOnBuildImage string
|
2015-09-27 21:32:07 -04:00
|
|
|
Labels map[string]string
|
|
|
|
|
LabelsError error
|
2014-10-10 11:00:59 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-04 15:01:52 +01:00
|
|
|
// IsImageInLocalRegistry checks if the image exists in the fake local registry
|
2014-10-10 11:00:59 -04:00
|
|
|
func (f *FakeDocker) IsImageInLocalRegistry(imageName string) (bool, error) {
|
|
|
|
|
f.LocalRegistryImage = imageName
|
|
|
|
|
return f.LocalRegistryResult, f.LocalRegistryError
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-03 11:34:26 -04:00
|
|
|
// IsImageOnBuild returns true if the builder has onbuild instructions
|
2015-02-13 09:32:41 +01:00
|
|
|
func (f *FakeDocker) IsImageOnBuild(imageName string) bool {
|
2015-08-03 11:34:26 -04:00
|
|
|
f.IsOnBuildImage = imageName
|
|
|
|
|
return f.IsOnBuildResult
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 19:36:28 +01:00
|
|
|
// Version returns information of the docker client and server host
|
|
|
|
|
func (f *FakeDocker) Version() (dockertypes.Version, error) {
|
|
|
|
|
return dockertypes.Version{}, nil
|
2015-11-10 14:35:54 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-13 14:04:14 +01:00
|
|
|
// GetImageWorkdir returns the workdir
|
|
|
|
|
func (f *FakeDocker) GetImageWorkdir(name string) (string, error) {
|
|
|
|
|
return "/", nil
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-03 11:34:26 -04:00
|
|
|
// GetOnBuild returns the list of onbuild instructions for the given image
|
|
|
|
|
func (f *FakeDocker) GetOnBuild(imageName string) ([]string, error) {
|
|
|
|
|
f.OnBuildImage = imageName
|
|
|
|
|
return f.OnBuildResult, f.OnBuildError
|
2015-02-13 09:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
2014-12-04 15:01:52 +01:00
|
|
|
// RemoveContainer removes a fake Docker container
|
2014-10-10 11:00:59 -04:00
|
|
|
func (f *FakeDocker) RemoveContainer(id string) error {
|
|
|
|
|
f.RemoveContainerID = id
|
|
|
|
|
return f.RemoveContainerError
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-14 13:21:03 +00:00
|
|
|
// KillContainer kills a fake container
|
|
|
|
|
func (f *FakeDocker) KillContainer(id string) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-15 14:23:15 +01:00
|
|
|
// GetScriptsURL returns a default STI scripts URL
|
|
|
|
|
func (f *FakeDocker) GetScriptsURL(image string) (string, error) {
|
2014-12-17 22:27:34 +01:00
|
|
|
f.DefaultURLImage = image
|
|
|
|
|
return f.DefaultURLResult, f.DefaultURLError
|
2014-10-10 11:00:59 -04:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 15:52:05 +02:00
|
|
|
// GetAssembleInputFiles finds a io.openshift.s2i.assemble-input-files label on the given image.
|
|
|
|
|
func (f *FakeDocker) GetAssembleInputFiles(image string) (string, error) {
|
|
|
|
|
return f.AssembleInputFilesResult, f.AssembleInputFilesError
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 15:01:52 +01:00
|
|
|
// RunContainer runs a fake Docker container
|
2015-11-03 14:54:15 +01:00
|
|
|
func (f *FakeDocker) RunContainer(opts RunContainerOptions) error {
|
2014-10-10 11:00:59 -04:00
|
|
|
f.RunContainerOpts = opts
|
|
|
|
|
if f.RunContainerErrorBeforeStart {
|
|
|
|
|
return f.RunContainerError
|
|
|
|
|
}
|
2016-10-28 12:45:03 +01:00
|
|
|
if opts.Stdout != nil {
|
|
|
|
|
opts.Stdout.Close()
|
|
|
|
|
}
|
|
|
|
|
if opts.Stderr != nil {
|
|
|
|
|
opts.Stderr.Close()
|
|
|
|
|
}
|
2014-10-28 11:54:02 -04:00
|
|
|
if opts.OnStart != nil {
|
2016-01-05 10:58:03 +01:00
|
|
|
if err := opts.OnStart(""); err != nil {
|
2014-10-28 11:54:02 -04:00
|
|
|
return err
|
|
|
|
|
}
|
2014-10-10 11:00:59 -04:00
|
|
|
}
|
2016-10-24 19:49:12 +01:00
|
|
|
if opts.Stdin != nil {
|
|
|
|
|
_, err := io.Copy(ioutil.Discard, opts.Stdin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-10 11:00:59 -04:00
|
|
|
if opts.PostExec != nil {
|
2014-12-15 14:23:15 +01:00
|
|
|
opts.PostExec.PostExecute(f.RunContainerContainerID, string(opts.Command))
|
2014-10-10 11:00:59 -04:00
|
|
|
}
|
|
|
|
|
return f.RunContainerError
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 11:21:52 +01:00
|
|
|
// UploadToContainer uploads artifacts to the container.
|
2017-07-11 10:33:36 +01:00
|
|
|
func (f *FakeDocker) UploadToContainer(fs fs.FileSystem, srcPath, destPath, container string) error {
|
2016-01-05 10:58:03 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-23 17:40:25 +01:00
|
|
|
// UploadToContainerWithTarWriter uploads artifacts to the container.
|
2017-07-11 10:33:36 +01:00
|
|
|
func (f *FakeDocker) UploadToContainerWithTarWriter(fs fs.FileSystem, srcPath, destPath, container string, makeTarWriter func(io.Writer) tar.Writer) error {
|
2016-05-20 15:52:05 +02:00
|
|
|
return errors.New("not implemented")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DownloadFromContainer downloads file (or directory) from the container.
|
|
|
|
|
func (f *FakeDocker) DownloadFromContainer(containerPath string, w io.Writer, container string) error {
|
|
|
|
|
return errors.New("not implemented")
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 15:01:52 +01:00
|
|
|
// GetImageID returns a fake Docker image ID
|
|
|
|
|
func (f *FakeDocker) GetImageID(image string) (string, error) {
|
|
|
|
|
f.GetImageIDImage = image
|
|
|
|
|
return f.GetImageIDResult, f.GetImageIDError
|
2014-10-10 11:00:59 -04:00
|
|
|
}
|
|
|
|
|
|
2015-02-04 11:12:01 -05:00
|
|
|
// GetImageUser returns a fake user
|
|
|
|
|
func (f *FakeDocker) GetImageUser(image string) (string, error) {
|
|
|
|
|
f.GetImageUserImage = image
|
|
|
|
|
return f.GetImageUserResult, f.GetImageUserError
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-15 13:27:15 -04:00
|
|
|
// GetImageEntrypoint returns an empty entrypoint
|
|
|
|
|
func (f *FakeDocker) GetImageEntrypoint(image string) ([]string, error) {
|
|
|
|
|
return f.GetImageEntrypointResult, f.GetImageEntrypointError
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 15:01:52 +01:00
|
|
|
// CommitContainer commits a fake Docker container
|
2015-11-03 14:54:15 +01:00
|
|
|
func (f *FakeDocker) CommitContainer(opts CommitContainerOptions) (string, error) {
|
2014-10-10 11:00:59 -04:00
|
|
|
f.CommitContainerOpts = opts
|
|
|
|
|
return f.CommitContainerResult, f.CommitContainerError
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 15:01:52 +01:00
|
|
|
// RemoveImage removes a fake Docker image
|
2014-10-10 11:00:59 -04:00
|
|
|
func (f *FakeDocker) RemoveImage(name string) error {
|
|
|
|
|
f.RemoveImageName = name
|
|
|
|
|
return f.RemoveImageError
|
|
|
|
|
}
|
2014-11-21 15:19:08 -05:00
|
|
|
|
2015-07-03 20:54:14 +02:00
|
|
|
// CheckImage checks image in local registry
|
2016-07-25 12:38:15 -04:00
|
|
|
func (f *FakeDocker) CheckImage(name string) (*api.Image, error) {
|
2015-07-03 20:54:14 +02:00
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 15:01:52 +01:00
|
|
|
// PullImage pulls a fake docker image
|
2016-07-25 12:38:15 -04:00
|
|
|
func (f *FakeDocker) PullImage(imageName string) (*api.Image, error) {
|
2015-02-20 10:37:43 +01:00
|
|
|
if f.PullResult {
|
2016-07-25 12:38:15 -04:00
|
|
|
return &api.Image{}, nil
|
2015-02-20 10:37:43 +01:00
|
|
|
}
|
|
|
|
|
return nil, f.PullError
|
2014-11-21 15:19:08 -05:00
|
|
|
}
|
2014-12-04 16:19:46 +01:00
|
|
|
|
2015-07-03 20:54:14 +02:00
|
|
|
// CheckAndPullImage pulls a fake docker image
|
2016-07-25 12:38:15 -04:00
|
|
|
func (f *FakeDocker) CheckAndPullImage(name string) (*api.Image, error) {
|
2016-02-18 17:45:18 +01:00
|
|
|
if f.PullResult {
|
2016-07-25 12:38:15 -04:00
|
|
|
return &api.Image{}, nil
|
2016-02-18 17:45:18 +01:00
|
|
|
}
|
|
|
|
|
return nil, f.PullError
|
2014-12-04 16:19:46 +01:00
|
|
|
}
|
2014-12-15 14:23:15 +01:00
|
|
|
|
|
|
|
|
// BuildImage builds image
|
2015-11-03 14:54:15 +01:00
|
|
|
func (f *FakeDocker) BuildImage(opts BuildImageOptions) error {
|
2014-12-15 14:23:15 +01:00
|
|
|
f.BuildImageOpts = opts
|
2016-10-23 17:40:25 +01:00
|
|
|
if opts.Stdin != nil {
|
|
|
|
|
_, err := io.Copy(ioutil.Discard, opts.Stdin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-15 14:23:15 +01:00
|
|
|
return f.BuildImageError
|
|
|
|
|
}
|
2015-09-27 21:32:07 -04:00
|
|
|
|
2016-10-18 11:21:52 +01:00
|
|
|
// GetLabels returns the labels of the image
|
2015-09-27 21:32:07 -04:00
|
|
|
func (f *FakeDocker) GetLabels(name string) (map[string]string, error) {
|
|
|
|
|
return f.Labels, f.LabelsError
|
|
|
|
|
}
|
2017-04-25 15:00:09 +01:00
|
|
|
|
|
|
|
|
// CheckReachable returns if the Docker daemon is reachable from s2i
|
|
|
|
|
func (f *FakeDocker) CheckReachable() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|