builder/docker: validate export path is not a dir [GH-2105]

This commit is contained in:
Mitchell Hashimoto 2015-05-29 16:24:29 -07:00
parent 911a868ac5
commit 819986d19f
3 changed files with 22 additions and 0 deletions

View File

@ -30,6 +30,7 @@ BUG FIXES:
* builder/docker: Use `docker exec` for newer versions of Docker for
running scripts [GH-1993]
* builder/docker: Fix crash that could occur at certain timed ctrl-c [GH-1838]
* builder/docker: validate that `export_path` is not a directory [GH-2105]
* builder/qemu: Add `disk_discard` option [GH-2120]
* builder/virtualbox: Added SCSI support
* builder/vmware: Case-insensitive match of MAC address to find IP [GH-1989]

View File

@ -2,6 +2,7 @@ package docker
import (
"fmt"
"os"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/common"
@ -79,6 +80,13 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
fmt.Errorf("both commit and export_path cannot be set"))
}
if c.ExportPath != "" {
if fi, err := os.Stat(c.ExportPath); err == nil && fi.IsDir() {
errs = packer.MultiErrorAppend(errs, fmt.Errorf(
"export_path must be a file, not a directory"))
}
}
if errs != nil && len(errs.Errors) > 0 {
return nil, nil, errs
}

View File

@ -1,6 +1,8 @@
package docker
import (
"io/ioutil"
"os"
"testing"
)
@ -42,6 +44,12 @@ func testConfigOk(t *testing.T, warns []string, err error) {
}
func TestConfigPrepare_exportPath(t *testing.T) {
td, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(td)
raw := testConfig()
// No export path
@ -53,6 +61,11 @@ func TestConfigPrepare_exportPath(t *testing.T) {
raw["export_path"] = "good"
_, warns, errs = NewConfig(raw)
testConfigOk(t, warns, errs)
// Bad export path (directory)
raw["export_path"] = td
_, warns, errs = NewConfig(raw)
testConfigErr(t, warns, errs)
}
func TestConfigPrepare_exportPathAndCommit(t *testing.T) {