|
| 1 | +package image |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "sort" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/docker/cli/cli/command" |
| 13 | + "github.com/docker/cli/cli/internal/test" |
| 14 | + "github.com/docker/docker/api/types" |
| 15 | + "github.com/docker/docker/pkg/archive" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + "golang.org/x/net/context" |
| 19 | +) |
| 20 | + |
| 21 | +func TestRunBuildDockerfileFromStdinWithCompress(t *testing.T) { |
| 22 | + dest, err := ioutil.TempDir("", "test-build-compress-dest") |
| 23 | + require.NoError(t, err) |
| 24 | + defer os.RemoveAll(dest) |
| 25 | + |
| 26 | + var dockerfileName string |
| 27 | + fakeImageBuild := func(_ context.Context, context io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) { |
| 28 | + buffer := new(bytes.Buffer) |
| 29 | + tee := io.TeeReader(context, buffer) |
| 30 | + |
| 31 | + assert.NoError(t, archive.Untar(tee, dest, nil)) |
| 32 | + dockerfileName = options.Dockerfile |
| 33 | + |
| 34 | + header := buffer.Bytes()[:10] |
| 35 | + assert.Equal(t, archive.Gzip, archive.DetectCompression(header)) |
| 36 | + |
| 37 | + body := new(bytes.Buffer) |
| 38 | + return types.ImageBuildResponse{Body: ioutil.NopCloser(body)}, nil |
| 39 | + } |
| 40 | + |
| 41 | + cli := test.NewFakeCli(&fakeClient{imageBuildFunc: fakeImageBuild}, ioutil.Discard) |
| 42 | + dockerfile := bytes.NewBufferString(` |
| 43 | + FROM alpine:3.6 |
| 44 | + COPY foo / |
| 45 | + `) |
| 46 | + cli.SetIn(command.NewInStream(ioutil.NopCloser(dockerfile))) |
| 47 | + |
| 48 | + dir, err := ioutil.TempDir("", "test-build-compress") |
| 49 | + require.NoError(t, err) |
| 50 | + defer os.RemoveAll(dir) |
| 51 | + |
| 52 | + ioutil.WriteFile(filepath.Join(dir, "foo"), []byte("some content"), 0644) |
| 53 | + |
| 54 | + options := newBuildOptions() |
| 55 | + options.compress = true |
| 56 | + options.dockerfileName = "-" |
| 57 | + options.context = dir |
| 58 | + |
| 59 | + err = runBuild(cli, options) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + files, err := ioutil.ReadDir(dest) |
| 63 | + require.NoError(t, err) |
| 64 | + actual := []string{} |
| 65 | + for _, fileInfo := range files { |
| 66 | + actual = append(actual, fileInfo.Name()) |
| 67 | + } |
| 68 | + sort.Strings(actual) |
| 69 | + assert.Equal(t, []string{dockerfileName, ".dockerignore", "foo"}, actual) |
| 70 | +} |
0 commit comments