Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
| ... | ... |
@@ -119,6 +119,12 @@ func (b *Builder) Run(context io.Reader) (string, error) {
|
| 119 | 119 |
return "", err |
| 120 | 120 |
} |
| 121 | 121 |
|
| 122 |
+ defer func() {
|
|
| 123 |
+ if err := os.RemoveAll(b.contextPath); err != nil {
|
|
| 124 |
+ log.Debugf("[BUILDER] failed to remove temporary context: %s", err)
|
|
| 125 |
+ } |
|
| 126 |
+ }() |
|
| 127 |
+ |
|
| 122 | 128 |
filename := path.Join(b.contextPath, "Dockerfile") |
| 123 | 129 |
|
| 124 | 130 |
fi, err := os.Stat(filename) |
| ... | ... |
@@ -164,10 +170,6 @@ func (b *Builder) Run(context io.Reader) (string, error) {
|
| 164 | 164 |
return "", fmt.Errorf("No image was generated. Is your Dockerfile empty?\n")
|
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 |
- if err := os.RemoveAll(b.contextPath); err != nil {
|
|
| 168 |
- log.Debugf("[BUILDER] failed to remove temporary context: %s", err)
|
|
| 169 |
- } |
|
| 170 |
- |
|
| 171 | 167 |
fmt.Fprintf(b.OutStream, "Successfully built %s\n", utils.TruncateID(b.image)) |
| 172 | 168 |
return b.image, nil |
| 173 | 169 |
} |
| ... | ... |
@@ -731,6 +731,31 @@ func TestBuildContextCleanup(t *testing.T) {
|
| 731 | 731 |
logDone("build - verify context cleanup works properly")
|
| 732 | 732 |
} |
| 733 | 733 |
|
| 734 |
+func TestBuildContextCleanupFailedBuild(t *testing.T) {
|
|
| 735 |
+ name := "testbuildcontextcleanup" |
|
| 736 |
+ defer deleteImages(name) |
|
| 737 |
+ entries, err := ioutil.ReadDir("/var/lib/docker/tmp")
|
|
| 738 |
+ if err != nil {
|
|
| 739 |
+ t.Fatalf("failed to list contents of tmp dir: %s", err)
|
|
| 740 |
+ } |
|
| 741 |
+ _, err = buildImage(name, |
|
| 742 |
+ `FROM scratch |
|
| 743 |
+ RUN /non/existing/command`, |
|
| 744 |
+ true) |
|
| 745 |
+ if err == nil {
|
|
| 746 |
+ t.Fatalf("expected build to fail, but it didn't")
|
|
| 747 |
+ } |
|
| 748 |
+ entriesFinal, err := ioutil.ReadDir("/var/lib/docker/tmp")
|
|
| 749 |
+ if err != nil {
|
|
| 750 |
+ t.Fatalf("failed to list contents of tmp dir: %s", err)
|
|
| 751 |
+ } |
|
| 752 |
+ if err = compareDirectoryEntries(entries, entriesFinal); err != nil {
|
|
| 753 |
+ t.Fatalf("context should have been deleted, but wasn't")
|
|
| 754 |
+ } |
|
| 755 |
+ |
|
| 756 |
+ logDone("build - verify context cleanup works properly after a failed build")
|
|
| 757 |
+} |
|
| 758 |
+ |
|
| 734 | 759 |
func TestBuildCmd(t *testing.T) {
|
| 735 | 760 |
name := "testbuildcmd" |
| 736 | 761 |
expected := "[/bin/echo Hello World]" |