From 0d18de29428094689e3909ce21fd62b4e51ec06d Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 13 Nov 2017 11:44:59 -0800 Subject: [PATCH] do validation in vmx config stage --- builder/virtualbox/ovf/config.go | 8 ++++++++ builder/virtualbox/ovf/config_test.go | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/builder/virtualbox/ovf/config.go b/builder/virtualbox/ovf/config.go index 77b7ffa0e..ff8080dd7 100644 --- a/builder/virtualbox/ovf/config.go +++ b/builder/virtualbox/ovf/config.go @@ -2,6 +2,7 @@ package ovf import ( "fmt" + "os" "strings" vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" @@ -101,6 +102,13 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { if err != nil { errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is invalid: %s", err)) } + // file must exist now. + if (len(c.SourcePath) > 7) && (c.SourcePath[:7] == "file://") { + if _, err := os.Stat(c.SourcePath[7:]); err != nil { + errs = packer.MultiErrorAppend(errs, + fmt.Errorf("source file needs to exist at time of config validation: %s", err)) + } + } } validMode := false diff --git a/builder/virtualbox/ovf/config_test.go b/builder/virtualbox/ovf/config_test.go index 980c4d4ef..bdab9b763 100644 --- a/builder/virtualbox/ovf/config_test.go +++ b/builder/virtualbox/ovf/config_test.go @@ -65,14 +65,14 @@ func TestNewConfig_sourcePath(t *testing.T) { t.Fatalf("should error with empty `source_path`") } - // Okay, because it gets caught during download + // Want this to fail on validation c = testConfig(t) c["source_path"] = "/i/dont/exist" _, warns, err = NewConfig(c) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) } - if err != nil { + if err == nil { t.Fatalf("bad: %s", err) } @@ -84,7 +84,7 @@ func TestNewConfig_sourcePath(t *testing.T) { t.Fatalf("bad: %#v", warns) } if err == nil { - t.Fatal("should error") + t.Fatal("Nonexistent source file should be caught in validation") } // Good