clean up context on build completion & add test

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
This commit is contained in:
unclejack 2014-09-02 17:35:25 +03:00
Родитель 5f6b420f91
Коммит 1858746978
3 изменённых файлов: 48 добавлений и 0 удалений

Просмотреть файл

@ -30,6 +30,7 @@ import (
"github.com/docker/docker/builder/parser" "github.com/docker/docker/builder/parser"
"github.com/docker/docker/daemon" "github.com/docker/docker/daemon"
"github.com/docker/docker/engine" "github.com/docker/docker/engine"
"github.com/docker/docker/pkg/log"
"github.com/docker/docker/pkg/tarsum" "github.com/docker/docker/pkg/tarsum"
"github.com/docker/docker/registry" "github.com/docker/docker/registry"
"github.com/docker/docker/runconfig" "github.com/docker/docker/runconfig"
@ -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") 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)) fmt.Fprintf(b.OutStream, "Successfully built %s\n", utils.TruncateID(b.image))
return b.image, nil return b.image, nil
} }

Просмотреть файл

@ -706,6 +706,31 @@ func TestBuildEnv(t *testing.T) {
logDone("build - env") 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) { func TestBuildCmd(t *testing.T) {
name := "testbuildcmd" name := "testbuildcmd"
expected := "[/bin/echo Hello World]" expected := "[/bin/echo Hello World]"

Просмотреть файл

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"os"
"os/exec" "os/exec"
"reflect" "reflect"
"strings" "strings"
@ -175,3 +176,20 @@ func waitRun(contId string) error {
return nil 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
}