Skip to content

Commit

Permalink
Merge pull request #22 from yuuki/avoid-extra-environment
Browse files Browse the repository at this point in the history
Use Env of docker image insetad of printenv to generate drootenv
  • Loading branch information
yuuki authored Jul 15, 2017
2 parents 21224cb + ef4e7c2 commit fced94a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
17 changes: 13 additions & 4 deletions docker/docker.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package docker

import (
"fmt"
"io"
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"golang.org/x/net/context" // docker/docker don't use 'context' as standard package.

"github.com/yuuki/droot/environ"
"golang.org/x/net/context" // docker/docker don't use 'context' as standard package.
)

// dockerAPI is an interface for stub testing.
type dockerAPI interface {
ImageInspectWithRaw(ctx context.Context, imageID string) (types.ImageInspect, []byte, error)
ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error)
ContainerStart(ctx context.Context, containerID string, options types.ContainerStartOptions) error
ContainerWait(ctx context.Context, containerID string) (int64, error)
Expand Down Expand Up @@ -44,10 +46,17 @@ func New() (*Client, error) {
func (c *Client) ExportImage(imageID string) (io.ReadCloser, error) {
ctx := context.Background()

image, _, err := c.docker.ImageInspectWithRaw(ctx, imageID)
if err != nil {
return nil, errors.Wrapf(err, "Failed to inspect image imageID:%s", imageID)
}

//Put drootenv file into the filesystem.
cmd := fmt.Sprintf("echo \"%s\" > %s", strings.Join(image.ContainerConfig.Env, "\n"), environ.DROOT_ENV_FILE_PATH)
container, err := c.docker.ContainerCreate(ctx, &container.Config{
Image: imageID,
Entrypoint: []string{"/bin/sh"}, // Clear the exising entrypoint
Cmd: []string{"-c", "printenv", ">", environ.DROOT_ENV_FILE_PATH},
Entrypoint: []string{""}, // Clear the exising entrypoint
Cmd: []string{"/bin/sh", "-c", cmd},
}, nil, nil, "")
if err != nil {
return nil, errors.Wrapf(err, "Failed to create container imageID:%s", imageID)
Expand Down
7 changes: 7 additions & 0 deletions docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ func TestExportImage(t *testing.T) {
containerID := "container ID"

fakeClient := &fakeDocker{
FakeImageInspectWithRaw: func(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) {
return types.ImageInspect{
ContainerConfig: &container.Config{
Env: []string{"PATH=/usr/bin:/sbin:/bin"},
},
}, []byte{}, nil
},
FakeContainerCreate: func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) {
return container.ContainerCreateCreatedBody{ID: containerID}, nil
},
Expand Down
15 changes: 10 additions & 5 deletions docker/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import (

type fakeDocker struct {
dockerAPI
FakeContainerCreate func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error)
FakeContainerStart func(ctx context.Context, containerID string, options types.ContainerStartOptions) error
FakeContainerWait func(ctx context.Context, containerID string) (int64, error)
FakeContainerExport func(ctx context.Context, containerID string) (io.ReadCloser, error)
FakeContainerRemove func(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error
FakeImageInspectWithRaw func(ctx context.Context, imageID string) (types.ImageInspect, []byte, error)
FakeContainerCreate func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error)
FakeContainerStart func(ctx context.Context, containerID string, options types.ContainerStartOptions) error
FakeContainerWait func(ctx context.Context, containerID string) (int64, error)
FakeContainerExport func(ctx context.Context, containerID string) (io.ReadCloser, error)
FakeContainerRemove func(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error
}

func (d *fakeDocker) ImageInspectWithRaw(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) {
return d.FakeImageInspectWithRaw(ctx, imageID)
}

func (d *fakeDocker) ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error {
Expand Down

0 comments on commit fced94a

Please sign in to comment.