From 7fa5ebb68b503eb84ef8adf91b8139ea09d8a160 Mon Sep 17 00:00:00 2001 From: shunsuke maeda Date: Sun, 26 Aug 2018 14:53:06 +0900 Subject: [PATCH 1/4] Remove random image --- infrastructure/docker/docker_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/infrastructure/docker/docker_test.go b/infrastructure/docker/docker_test.go index cd8c8a3b..8b4767eb 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) }) }) @@ -564,6 +570,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() From a4078e46051e8fdcbc4b14a8bc96b25d67a68fe3 Mon Sep 17 00:00:00 2001 From: shunsuke maeda Date: Sun, 26 Aug 2018 15:09:17 +0900 Subject: [PATCH 2/4] Return container when even error --- infrastructure/docker/docker.go | 4 ++-- infrastructure/docker/docker_test.go | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/infrastructure/docker/docker.go b/infrastructure/docker/docker.go index 179eb03a..2ca0fddc 100644 --- a/infrastructure/docker/docker.go +++ b/infrastructure/docker/docker.go @@ -85,7 +85,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 +94,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 8b4767eb..6d7e28e6 100644 --- a/infrastructure/docker/docker_test.go +++ b/infrastructure/docker/docker_test.go @@ -186,9 +186,13 @@ func TestClientImpl_Run(t *testing.T) { imagePull(t, "centos: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, "centos", "missing_command") + if err == nil { t.Error("error must occur") } + + // cleanup + removeContainer(t, containerId) }) }) From aa8f39c46b3dfc22f6279063854d194c8202d63d Mon Sep 17 00:00:00 2001 From: shunsuke maeda Date: Sun, 26 Aug 2018 15:58:05 +0900 Subject: [PATCH 3/4] Add build option to remove container --- infrastructure/docker/docker.go | 1 + 1 file changed, 1 insertion(+) diff --git a/infrastructure/docker/docker.go b/infrastructure/docker/docker.go index 2ca0fddc..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 { From 5e46fda5146f97576dfaaf23938f4737c25d1cec Mon Sep 17 00:00:00 2001 From: shunsuke maeda Date: Sun, 26 Aug 2018 16:16:25 +0900 Subject: [PATCH 4/4] Change container base image in test --- infrastructure/docker/docker_test.go | 18 +++++++++--------- .../docker/testdata/correct_archive.tar | Bin 2048 -> 2048 bytes .../testdata/correct_archive_subdir.tar | Bin 2560 -> 2560 bytes 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/infrastructure/docker/docker_test.go b/infrastructure/docker/docker_test.go index 6d7e28e6..caad31d1 100644 --- a/infrastructure/docker/docker_test.go +++ b/infrastructure/docker/docker_test.go @@ -159,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) } @@ -183,10 +183,10 @@ func TestClientImpl_Run(t *testing.T) { t.Parallel() // given - imagePull(t, "centos:latest") + imagePull(t, "alpine:latest") // expect - containerId, _, err := cli.Run(context.New("test/task", uuid.New(), &url.URL{}), opts, "centos", "missing_command") + containerId, _, err := cli.Run(context.New("test/task", uuid.New(), &url.URL{}), opts, "alpine", "missing_command") if err == nil { t.Error("error must occur") } @@ -200,7 +200,7 @@ func TestClientImpl_Run(t *testing.T) { t.Parallel() // given - imagePull(t, "centos:latest") + imagePull(t, "alpine:latest") // and opts := docker.RuntimeOptions{ @@ -208,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) } @@ -233,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") @@ -245,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) } @@ -271,7 +271,7 @@ func TestClientImpl_Rm(t *testing.T) { } // given - tag := "alpine:3.5" + tag := "alpine:latest" imagePull(t, tag) containerId := containerCreate(t, tag) diff --git a/infrastructure/docker/testdata/correct_archive.tar b/infrastructure/docker/testdata/correct_archive.tar index 19a462b56c75eccdba04cbce50e7af0ec4602107..81c54d189084b5d1eaf49bdb70f7b8d401152c4b 100644 GIT binary patch delta 84 zcmZn=Xb_lCp=xBJz+eCaW(Ep|#>OTFCZ>i4X66b8hQ=mlrVI)OlM@+JCNnZ>uojmj hrln03l%DL#xQ90}ryw&g)hZ{kB(=C?GcVISRsciW7G?kd delta 82 zcmZn=Xb_lCp=w~Dz+eCarp5|}#>R#w2Igj_#)b+8hQ=mlMhprDlM@+JCNnZ>u$E_} frshl(l%DL#xQ8P-HLoPU*vf3P5Ys!>i3+R$5&aa> diff --git a/infrastructure/docker/testdata/correct_archive_subdir.tar b/infrastructure/docker/testdata/correct_archive_subdir.tar index 7ba0ab36de85766665cf3baf57f14bae3029658a..8f2344fa20751f427417201c79fe6bc724cca229 100644 GIT binary patch delta 87 zcmZn=X%LyvEo5R~Vrpn)XsTdfXk=n;!k}O}Igv4CGb3XQqlB4(f}yc7k_r