diff --git a/infrastructure/docker/docker.go b/infrastructure/docker/docker.go index 179eb03a..1918addf 100644 --- a/infrastructure/docker/docker.go +++ b/infrastructure/docker/docker.go @@ -62,6 +62,7 @@ func (c *clientImpl) Build(ctx context.Context, file io.Reader, tag string, dock opts := types.ImageBuildOptions{ Tags: []string{tag}, Dockerfile: dockerfile, + Remove: true, } resp, err := c.moby.ImageBuild(ctx, file, opts) if err != nil { @@ -85,7 +86,7 @@ func (c *clientImpl) Run(ctx context.Context, opts RuntimeOptions, tag string, c } if err := c.moby.ContainerStart(ctx, con.ID, types.ContainerStartOptions{}); err != nil { - return "", nil, errors.WithStack(err) + return con.ID, nil, errors.WithStack(err) } log, err := c.moby.ContainerLogs(ctx, con.ID, types.ContainerLogsOptions{ @@ -94,7 +95,7 @@ func (c *clientImpl) Run(ctx context.Context, opts RuntimeOptions, tag string, c Follow: true, }) if err != nil { - return "", nil, errors.WithStack(err) + return con.ID, nil, errors.WithStack(err) } return con.ID, &runLogger{bufio.NewReader(log)}, nil diff --git a/infrastructure/docker/docker_test.go b/infrastructure/docker/docker_test.go index cd8c8a3b..caad31d1 100644 --- a/infrastructure/docker/docker_test.go +++ b/infrastructure/docker/docker_test.go @@ -67,6 +67,9 @@ func TestClientImpl_Build(t *testing.T) { if !contains(images, fmt.Sprintf("%s:latest", tag)) { t.Errorf("docker images must contains. images: %+v, tag: %+v", images, tag) } + + // cleanup + removeImage(t, tag) }) t.Run("with sub directory", func(t *testing.T) { @@ -93,6 +96,9 @@ func TestClientImpl_Build(t *testing.T) { if !contains(images, fmt.Sprintf("%s:latest", tag)) { t.Errorf("docker images must contains. images: %+v, tag: %+v", images, tag) } + + // cleanup + removeImage(t, tag) }) }) @@ -153,10 +159,10 @@ func TestClientImpl_Run(t *testing.T) { t.Parallel() // given - imagePull(t, "centos:latest") + imagePull(t, "alpine:latest") // when - containerId, _, err := cli.Run(context.New("test/task", uuid.New(), &url.URL{}), opts, "centos", "echo", "Hello-world") + containerId, _, err := cli.Run(context.New("test/task", uuid.New(), &url.URL{}), opts, "alpine", "echo", "Hello-world") if err != nil { t.Fatalf("error occured: %+v", err) } @@ -177,12 +183,16 @@ func TestClientImpl_Run(t *testing.T) { t.Parallel() // given - imagePull(t, "centos:latest") + imagePull(t, "alpine:latest") // expect - if _, _, err := cli.Run(context.New("test/task", uuid.New(), &url.URL{}), opts, "centos", "missing_command"); err == nil { + containerId, _, err := cli.Run(context.New("test/task", uuid.New(), &url.URL{}), opts, "alpine", "missing_command") + if err == nil { t.Error("error must occur") } + + // cleanup + removeContainer(t, containerId) }) }) @@ -190,7 +200,7 @@ func TestClientImpl_Run(t *testing.T) { t.Parallel() // given - imagePull(t, "centos:latest") + imagePull(t, "alpine:latest") // and opts := docker.RuntimeOptions{ @@ -198,7 +208,7 @@ func TestClientImpl_Run(t *testing.T) { } // when - containerId, _, err := cli.Run(context.New("test/task", uuid.New(), &url.URL{}), opts, "centos", "sh", "-c", "echo hello $ENV") + containerId, _, err := cli.Run(context.New("test/task", uuid.New(), &url.URL{}), opts, "alpine", "sh", "-c", "echo hello $ENV") if err != nil { t.Fatalf("error occured: %+v", err) } @@ -223,7 +233,7 @@ func TestClientImpl_Run(t *testing.T) { t.Parallel() // given - imagePull(t, "centos:latest") + imagePull(t, "alpine:latest") // and path, err := filepath.Abs("testdata") @@ -235,7 +245,7 @@ func TestClientImpl_Run(t *testing.T) { } // when - containerId, _, err := cli.Run(context.New("test/task", uuid.New(), &url.URL{}), opts, "centos", "cat", "/tmp/testdata/data") + containerId, _, err := cli.Run(context.New("test/task", uuid.New(), &url.URL{}), opts, "alpine", "cat", "/tmp/testdata/data") if err != nil { t.Fatalf("error occured: %+v", err) } @@ -261,7 +271,7 @@ func TestClientImpl_Rm(t *testing.T) { } // given - tag := "alpine:3.5" + tag := "alpine:latest" imagePull(t, tag) containerId := containerCreate(t, tag) @@ -564,6 +574,19 @@ func containerWait(t *testing.T, containerId string) { } } +func removeImage(t *testing.T, name string) { + t.Helper() + + cli, err := client.NewEnvClient() + if err != nil { + t.Fatalf("error occured. %+v", err) + } + + if _, err := cli.ImageRemove(context.New("test/task", uuid.New(), &url.URL{}), name, types.ImageRemoveOptions{}); err != nil { + t.Fatalf("error occured. %+v", err) + } +} + func removeContainer(t *testing.T, containerId string) { t.Helper() diff --git a/infrastructure/docker/testdata/correct_archive.tar b/infrastructure/docker/testdata/correct_archive.tar index 19a462b5..81c54d18 100644 Binary files a/infrastructure/docker/testdata/correct_archive.tar and b/infrastructure/docker/testdata/correct_archive.tar differ diff --git a/infrastructure/docker/testdata/correct_archive_subdir.tar b/infrastructure/docker/testdata/correct_archive_subdir.tar index 7ba0ab36..8f2344fa 100644 Binary files a/infrastructure/docker/testdata/correct_archive_subdir.tar and b/infrastructure/docker/testdata/correct_archive_subdir.tar differ