Skip to content

Commit

Permalink
Merge pull request moby#7842 from unclejack/build_delete_context
Browse files Browse the repository at this point in the history
clean up context on build completion & add test
  • Loading branch information
vieux committed Sep 2, 2014
2 parents ca24e91 + 1858746 commit 6eef7de
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions builder/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/docker/docker/builder/parser"
"github.com/docker/docker/daemon"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/log"
"github.com/docker/docker/pkg/tarsum"
"github.com/docker/docker/registry"
"github.com/docker/docker/runconfig"
Expand Down Expand Up @@ -163,6 +164,10 @@ func (b *Builder) Run(context io.Reader) (string, error) {
return "", fmt.Errorf("No image was generated. Is your Dockerfile empty?\n")
}

if err := os.RemoveAll(b.contextPath); err != nil {
log.Debugf("[BUILDER] failed to remove temporary context: %s", err)
}

fmt.Fprintf(b.OutStream, "Successfully built %s\n", utils.TruncateID(b.image))
return b.image, nil
}
Expand Down
25 changes: 25 additions & 0 deletions integration-cli/docker_cli_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,31 @@ func TestBuildEnv(t *testing.T) {
logDone("build - env")
}

func TestBuildContextCleanup(t *testing.T) {
name := "testbuildcontextcleanup"
defer deleteImages(name)
entries, err := ioutil.ReadDir("/var/lib/docker/tmp")
if err != nil {
t.Fatalf("failed to list contents of tmp dir: %s", err)
}
_, err = buildImage(name,
`FROM scratch
ENTRYPOINT ["/bin/echo"]`,
true)
if err != nil {
t.Fatal(err)
}
entriesFinal, err := ioutil.ReadDir("/var/lib/docker/tmp")
if err != nil {
t.Fatalf("failed to list contents of tmp dir: %s", err)
}
if err = compareDirectoryEntries(entries, entriesFinal); err != nil {
t.Fatalf("context should have been deleted, but wasn't")
}

logDone("build - verify context cleanup works properly")
}

func TestBuildCmd(t *testing.T) {
name := "testbuildcmd"
expected := "[/bin/echo Hello World]"
Expand Down
18 changes: 18 additions & 0 deletions integration-cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"reflect"
"strings"
Expand Down Expand Up @@ -175,3 +176,20 @@ func waitRun(contId string) error {

return nil
}

func compareDirectoryEntries(e1 []os.FileInfo, e2 []os.FileInfo) error {
var (
e1Entries = make(map[string]struct{})
e2Entries = make(map[string]struct{})
)
for _, e := range e1 {
e1Entries[e.Name()] = struct{}{}
}
for _, e := range e2 {
e2Entries[e.Name()] = struct{}{}
}
if !reflect.DeepEqual(e1Entries, e2Entries) {
return fmt.Errorf("entries differ")
}
return nil
}

0 comments on commit 6eef7de

Please sign in to comment.