From 173efccd3cfe694d57cc8ef5be3ede2a5ceec2c8 Mon Sep 17 00:00:00 2001 From: Maciej Szulik Date: Tue, 21 Nov 2017 11:13:29 +0100 Subject: [PATCH] Code changes to match current dependecies --- pkg/docker/docker.go | 56 ++++++++++++++-------------- pkg/docker/docker_test.go | 8 ++-- pkg/docker/fake_docker.go | 3 +- pkg/docker/test/client.go | 40 +++++++++++++------- pkg/docker/util.go | 3 +- pkg/util/util.go | 2 +- pkg/util/util_test.go | 2 +- test/integration/integration_test.go | 35 ++++++++++------- 8 files changed, 87 insertions(+), 62 deletions(-) diff --git a/pkg/docker/docker.go b/pkg/docker/docker.go index 91b52d353..179eec80e 100644 --- a/pkg/docker/docker.go +++ b/pkg/docker/docker.go @@ -18,12 +18,12 @@ import ( "syscall" "time" + dockertypes "github.com/docker/docker/api/types" + dockercontainer "github.com/docker/docker/api/types/container" + dockernetwork "github.com/docker/docker/api/types/network" + dockerapi "github.com/docker/docker/client" dockermessage "github.com/docker/docker/pkg/jsonmessage" dockerstdcopy "github.com/docker/docker/pkg/stdcopy" - dockerapi "github.com/docker/engine-api/client" - dockertypes "github.com/docker/engine-api/types" - dockercontainer "github.com/docker/engine-api/types/container" - dockernetwork "github.com/docker/engine-api/types/network" "github.com/docker/go-connections/tlsconfig" "golang.org/x/net/context" @@ -142,19 +142,19 @@ type Docker interface { // Client contains all methods used when interacting directly with docker engine-api type Client interface { ContainerAttach(ctx context.Context, container string, options dockertypes.ContainerAttachOptions) (dockertypes.HijackedResponse, error) - ContainerCommit(ctx context.Context, container string, options dockertypes.ContainerCommitOptions) (dockertypes.ContainerCommitResponse, error) - ContainerCreate(ctx context.Context, config *dockercontainer.Config, hostConfig *dockercontainer.HostConfig, networkingConfig *dockernetwork.NetworkingConfig, containerName string) (dockertypes.ContainerCreateResponse, error) - ContainerInspect(ctx context.Context, containerID string) (dockertypes.ContainerJSON, error) - ContainerRemove(ctx context.Context, containerID string, options dockertypes.ContainerRemoveOptions) error - ContainerStart(ctx context.Context, containerID string) error - ContainerKill(ctx context.Context, containerID, signal string) error - ContainerWait(ctx context.Context, containerID string) (int, error) + ContainerCommit(ctx context.Context, container string, options dockertypes.ContainerCommitOptions) (dockertypes.IDResponse, error) + ContainerCreate(ctx context.Context, config *dockercontainer.Config, hostConfig *dockercontainer.HostConfig, networkingConfig *dockernetwork.NetworkingConfig, containerName string) (dockercontainer.ContainerCreateCreatedBody, error) + ContainerInspect(ctx context.Context, container string) (dockertypes.ContainerJSON, error) + ContainerRemove(ctx context.Context, container string, options dockertypes.ContainerRemoveOptions) error + ContainerStart(ctx context.Context, container string, options dockertypes.ContainerStartOptions) error + ContainerKill(ctx context.Context, container, signal string) error + ContainerWait(ctx context.Context, container string, condition dockercontainer.WaitCondition) (<-chan dockercontainer.ContainerWaitOKBody, <-chan error) CopyToContainer(ctx context.Context, container, path string, content io.Reader, opts dockertypes.CopyToContainerOptions) error CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, dockertypes.ContainerPathStat, error) ImageBuild(ctx context.Context, buildContext io.Reader, options dockertypes.ImageBuildOptions) (dockertypes.ImageBuildResponse, error) - ImageInspectWithRaw(ctx context.Context, imageID string, getSize bool) (dockertypes.ImageInspect, []byte, error) + ImageInspectWithRaw(ctx context.Context, image string) (dockertypes.ImageInspect, []byte, error) ImagePull(ctx context.Context, ref string, options dockertypes.ImagePullOptions) (io.ReadCloser, error) - ImageRemove(ctx context.Context, imageID string, options dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDelete, error) + ImageRemove(ctx context.Context, image string, options dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDeleteResponseItem, error) ServerVersion(ctx context.Context) (dockertypes.Version, error) } @@ -167,7 +167,7 @@ type stiDocker struct { func (d stiDocker) InspectImage(name string) (*dockertypes.ImageInspect, error) { ctx, cancel := getDefaultContext() defer cancel() - resp, _, err := d.client.ImageInspectWithRaw(ctx, name, false) + resp, _, err := d.client.ImageInspectWithRaw(ctx, name) if err != nil { return nil, err } @@ -819,7 +819,7 @@ func determineCommandBaseDir(opts RunContainerOptions, imageMetadata *api.Image, } // dumpContainerInfo dumps information about a running container (port/IP/etc). -func dumpContainerInfo(container dockertypes.ContainerCreateResponse, d *stiDocker, image string) { +func dumpContainerInfo(container dockercontainer.ContainerCreateCreatedBody, d *stiDocker, image string) { ctx, cancel := getDefaultContext() defer cancel() @@ -1024,7 +1024,7 @@ func (d *stiDocker) RunContainer(opts RunContainerOptions) error { glog.V(2).Infof("Starting container %q ...", container.ID) ctx, cancel = getDefaultContext() defer cancel() - err = d.client.ContainerStart(ctx, container.ID) + err = d.client.ContainerStart(ctx, container.ID, dockertypes.ContainerStartOptions{}) if err != nil { return err } @@ -1053,18 +1053,20 @@ func (d *stiDocker) RunContainer(opts RunContainerOptions) error { // Return an error if the exit code of the container is // non-zero. glog.V(4).Infof("Waiting for container %q to stop ...", container.ID) - exitCode, err := d.client.ContainerWait(context.Background(), container.ID) - if err != nil { - return fmt.Errorf("waiting for container %q to stop: %v", container.ID, err) - } - if exitCode != 0 { - var output string - json, _ := d.client.ContainerInspect(ctx, container.ID) - if err == nil && json.ContainerJSONBase != nil && json.ContainerJSONBase.State != nil { - state := json.ContainerJSONBase.State - output = fmt.Sprintf("Status: %s, Error: %s, OOMKilled: %v, Dead: %v", state.Status, state.Error, state.OOMKilled, state.Dead) + waitC, errC := d.client.ContainerWait(context.Background(), container.ID, dockercontainer.WaitConditionNextExit) + select { + case result := <-waitC: + if result.StatusCode != 0 { + var output string + json, _ := d.client.ContainerInspect(ctx, container.ID) + if err == nil && json.ContainerJSONBase != nil && json.ContainerJSONBase.State != nil { + state := json.ContainerJSONBase.State + output = fmt.Sprintf("Status: %s, Error: %s, OOMKilled: %v, Dead: %v", state.Status, state.Error, state.OOMKilled, state.Dead) + } + return s2ierr.NewContainerError(container.ID, int(result.StatusCode), output) } - return s2ierr.NewContainerError(container.ID, exitCode, output) + case err := <-errC: + return fmt.Errorf("waiting for container %q to stop: %v", container.ID, err) } // OnStart must be done before we move on. diff --git a/pkg/docker/docker_test.go b/pkg/docker/docker_test.go index 9af6f04ee..4a6f9b612 100644 --- a/pkg/docker/docker_test.go +++ b/pkg/docker/docker_test.go @@ -16,9 +16,9 @@ import ( "github.com/openshift/source-to-image/pkg/errors" testfs "github.com/openshift/source-to-image/pkg/test/fs" - dockertypes "github.com/docker/engine-api/types" - dockercontainer "github.com/docker/engine-api/types/container" - dockerstrslice "github.com/docker/engine-api/types/strslice" + dockertypes "github.com/docker/docker/api/types" + dockercontainer "github.com/docker/docker/api/types/container" + dockerstrslice "github.com/docker/docker/api/types/strslice" ) func TestContainerName(t *testing.T) { @@ -82,7 +82,7 @@ func TestCommitContainer(t *testing.T) { param := dockertypes.ContainerCommitOptions{ Reference: tst.containerTag, } - resp := dockertypes.ContainerCommitResponse{ + resp := dockertypes.IDResponse{ ID: tst.expectedImageID, } fakeDocker := &dockertest.FakeDockerClient{ diff --git a/pkg/docker/fake_docker.go b/pkg/docker/fake_docker.go index a52b67399..454d2092e 100644 --- a/pkg/docker/fake_docker.go +++ b/pkg/docker/fake_docker.go @@ -5,7 +5,8 @@ import ( "io" "io/ioutil" - dockertypes "github.com/docker/engine-api/types" + dockertypes "github.com/docker/docker/api/types" + "github.com/openshift/source-to-image/pkg/api" "github.com/openshift/source-to-image/pkg/tar" "github.com/openshift/source-to-image/pkg/util/fs" diff --git a/pkg/docker/test/client.go b/pkg/docker/test/client.go index bd9ac32be..dec543602 100644 --- a/pkg/docker/test/client.go +++ b/pkg/docker/test/client.go @@ -10,9 +10,9 @@ import ( "net" "time" - dockertypes "github.com/docker/engine-api/types" - dockercontainer "github.com/docker/engine-api/types/container" - dockernetwork "github.com/docker/engine-api/types/network" + dockertypes "github.com/docker/docker/api/types" + dockercontainer "github.com/docker/docker/api/types/container" + dockernetwork "github.com/docker/docker/api/types/network" "golang.org/x/net/context" ) @@ -79,7 +79,7 @@ type FakeDockerClient struct { ContainerCommitID string ContainerCommitOptions dockertypes.ContainerCommitOptions - ContainerCommitResponse dockertypes.ContainerCommitResponse + ContainerCommitResponse dockertypes.IDResponse ContainerCommitErr error BuildImageOpts dockertypes.ImageBuildOptions @@ -103,7 +103,7 @@ func NewFakeDockerClient() *FakeDockerClient { } // ImageInspectWithRaw returns the image information and its raw representation. -func (d *FakeDockerClient) ImageInspectWithRaw(ctx context.Context, imageID string, getSize bool) (dockertypes.ImageInspect, []byte, error) { +func (d *FakeDockerClient) ImageInspectWithRaw(ctx context.Context, imageID string) (dockertypes.ImageInspect, []byte, error) { d.Calls = append(d.Calls, "inspect_image") if _, exists := d.Images[imageID]; exists { @@ -129,13 +129,25 @@ func (d *FakeDockerClient) CopyFromContainer(ctx context.Context, container, src } // ContainerWait pauses execution until a container exits. -func (d *FakeDockerClient) ContainerWait(ctx context.Context, containerID string) (int, error) { +func (d *FakeDockerClient) ContainerWait(ctx context.Context, containerID string, condition dockercontainer.WaitCondition) (<-chan dockercontainer.ContainerWaitOKBody, <-chan error) { d.WaitContainerID = containerID - return d.WaitContainerResult, d.WaitContainerErr + resultC := make(chan dockercontainer.ContainerWaitOKBody) + errC := make(chan error, 1) + + go func() { + if d.WaitContainerErr != nil { + errC <- d.WaitContainerErr + return + } + + resultC <- dockercontainer.ContainerWaitOKBody{StatusCode: int64(d.WaitContainerResult)} + }() + + return resultC, errC } // ContainerCommit applies changes into a container and creates a new tagged image. -func (d *FakeDockerClient) ContainerCommit(ctx context.Context, container string, options dockertypes.ContainerCommitOptions) (dockertypes.ContainerCommitResponse, error) { +func (d *FakeDockerClient) ContainerCommit(ctx context.Context, container string, options dockertypes.ContainerCommitOptions) (dockertypes.IDResponse, error) { d.ContainerCommitID = container d.ContainerCommitOptions = options return d.ContainerCommitResponse, d.ContainerCommitErr @@ -156,11 +168,11 @@ func (d *FakeDockerClient) ImageBuild(ctx context.Context, buildContext io.Reade } // ContainerCreate creates a new container based in the given configuration. -func (d *FakeDockerClient) ContainerCreate(ctx context.Context, config *dockercontainer.Config, hostConfig *dockercontainer.HostConfig, networkingConfig *dockernetwork.NetworkingConfig, containerName string) (dockertypes.ContainerCreateResponse, error) { +func (d *FakeDockerClient) ContainerCreate(ctx context.Context, config *dockercontainer.Config, hostConfig *dockercontainer.HostConfig, networkingConfig *dockernetwork.NetworkingConfig, containerName string) (dockercontainer.ContainerCreateCreatedBody, error) { d.Calls = append(d.Calls, "create") d.Containers[containerName] = *config - return dockertypes.ContainerCreateResponse{}, nil + return dockercontainer.ContainerCreateCreatedBody{}, nil } // ContainerInspect returns the container information. @@ -186,7 +198,7 @@ func (d *FakeDockerClient) ContainerKill(ctx context.Context, containerID, signa } // ContainerStart sends a request to the docker daemon to start a container. -func (d *FakeDockerClient) ContainerStart(ctx context.Context, containerID string) error { +func (d *FakeDockerClient) ContainerStart(ctx context.Context, containerID string, options dockertypes.ContainerStartOptions) error { d.Calls = append(d.Calls, "start") return nil } @@ -203,14 +215,14 @@ func (d *FakeDockerClient) ImagePull(ctx context.Context, ref string, options do } // ImageRemove removes an image from the docker host. -func (d *FakeDockerClient) ImageRemove(ctx context.Context, imageID string, options dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDelete, error) { +func (d *FakeDockerClient) ImageRemove(ctx context.Context, imageID string, options dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDeleteResponseItem, error) { d.Calls = append(d.Calls, "remove_image") if _, exists := d.Images[imageID]; exists { delete(d.Images, imageID) - return []dockertypes.ImageDelete{}, nil + return []dockertypes.ImageDeleteResponseItem{}, nil } - return []dockertypes.ImageDelete{}, errors.New("image does not exist") + return []dockertypes.ImageDeleteResponseItem{}, errors.New("image does not exist") } // ServerVersion returns information of the docker client and server host. diff --git a/pkg/docker/util.go b/pkg/docker/util.go index 4f8603122..02ae68de9 100644 --- a/pkg/docker/util.go +++ b/pkg/docker/util.go @@ -16,7 +16,8 @@ import ( "github.com/docker/distribution/reference" cliconfig "github.com/docker/docker/cli/config" - "github.com/docker/engine-api/client" + "github.com/docker/docker/client" + "github.com/openshift/source-to-image/pkg/api" s2ierr "github.com/openshift/source-to-image/pkg/errors" utilglog "github.com/openshift/source-to-image/pkg/util/glog" diff --git a/pkg/util/util.go b/pkg/util/util.go index bd3dd64c2..3786f26e4 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -1,7 +1,7 @@ package util import ( - "github.com/docker/engine-api/types/container" + "github.com/docker/docker/api/types/container" utilglog "github.com/openshift/source-to-image/pkg/util/glog" ) diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 5962634e6..5836d671c 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - "github.com/docker/engine-api/types/container" + "github.com/docker/docker/api/types/container" ) func TestSafeForLoggingContainerConfig(t *testing.T) { diff --git a/test/integration/integration_test.go b/test/integration/integration_test.go index 3a4bc4d90..7e356451d 100644 --- a/test/integration/integration_test.go +++ b/test/integration/integration_test.go @@ -16,9 +16,9 @@ import ( "testing" "time" - dockerapi "github.com/docker/engine-api/client" - dockertypes "github.com/docker/engine-api/types" - dockercontainer "github.com/docker/engine-api/types/container" + dockertypes "github.com/docker/docker/api/types" + dockercontainer "github.com/docker/docker/api/types/container" + dockerapi "github.com/docker/docker/client" "github.com/golang/glog" "github.com/openshift/source-to-image/pkg/api" "github.com/openshift/source-to-image/pkg/build/strategies" @@ -125,7 +125,7 @@ type integrationTest struct { func (i integrationTest) InspectImage(name string) (*dockertypes.ImageInspect, error) { ctx, cancel := getDefaultContext() defer cancel() - resp, _, err := engineClient.ImageInspectWithRaw(ctx, name, false) + resp, _, err := engineClient.ImageInspectWithRaw(ctx, name) if err != nil { if dockerapi.IsErrImageNotFound(err) { return nil, fmt.Errorf("no such image :%q", name) @@ -541,7 +541,7 @@ func (i *integrationTest) createContainer(image string) string { ctx, cancel = getDefaultContext() defer cancel() - err = engineClient.ContainerStart(ctx, container.ID) + err = engineClient.ContainerStart(ctx, container.ID, dockertypes.ContainerStartOptions{}) if err != nil { i.t.Errorf("Couldn't start container: %s with error %+v", container.ID, err) return "" @@ -549,12 +549,17 @@ func (i *integrationTest) createContainer(image string) string { ctx, cancel = getDefaultContext() defer cancel() - exitCode, _ := engineClient.ContainerWait(ctx, container.ID) - if exitCode != 0 { - i.t.Errorf("Bad exit code from container: %d", exitCode) + waitC, errC := engineClient.ContainerWait(ctx, container.ID, dockercontainer.WaitConditionNextExit) + select { + case result := <-waitC: + if result.StatusCode != 0 { + i.t.Errorf("Bad exit code from container: %d", result.StatusCode) + return "" + } + case err := <-errC: + i.t.Errorf("Error waiting for container: %v", err) return "" } - return container.ID } @@ -570,15 +575,19 @@ func (i *integrationTest) runInContainer(image string, command []string) int { ctx, cancel = getDefaultContext() defer cancel() - err = engineClient.ContainerStart(ctx, container.ID) + err = engineClient.ContainerStart(ctx, container.ID, dockertypes.ContainerStartOptions{}) if err != nil { i.t.Errorf("Couldn't start container: %s", container.ID) } ctx, cancel = getDefaultContext() defer cancel() - exitCode, err := engineClient.ContainerWait(ctx, container.ID) - if err != nil { - i.t.Errorf("Couldn't wait for container: %s", container.ID) + waitC, errC := engineClient.ContainerWait(ctx, container.ID, dockercontainer.WaitConditionNextExit) + exitCode := -1 + select { + case result := <-waitC: + exitCode = int(result.StatusCode) + case err := <-errC: + i.t.Errorf("Couldn't wait for container: %s: %v", container.ID, err) } ctx, cancel = getDefaultContext() defer cancel()