Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #75 from duck8823/maintenance/change_container_image
Browse files Browse the repository at this point in the history
Change docker images in test
  • Loading branch information
duck8823 authored Aug 26, 2018
2 parents b542366 + 5e46fda commit b0fadcf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
5 changes: 3 additions & 2 deletions infrastructure/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand All @@ -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
Expand Down
41 changes: 32 additions & 9 deletions infrastructure/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
})
})

Expand Down Expand Up @@ -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)
}
Expand All @@ -177,28 +183,32 @@ 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)
})
})

t.Run("with environments", func(t *testing.T) {
t.Parallel()

// given
imagePull(t, "centos:latest")
imagePull(t, "alpine:latest")

// and
opts := docker.RuntimeOptions{
Environments: docker.Environments{"ENV": "hello-world"},
}

// 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)
}
Expand All @@ -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")
Expand All @@ -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)
}
Expand All @@ -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)

Expand Down Expand Up @@ -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()

Expand Down
Binary file modified infrastructure/docker/testdata/correct_archive.tar
Binary file not shown.
Binary file modified infrastructure/docker/testdata/correct_archive_subdir.tar
Binary file not shown.

0 comments on commit b0fadcf

Please sign in to comment.