do validation in vmx config stage

This commit is contained in:
Megan Marsh 2017-11-13 11:44:59 -08:00
parent 0efcb1bba2
commit 0d18de2942
2 changed files with 11 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package ovf
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
@ -101,6 +102,13 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
if err != nil { if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is invalid: %s", err)) 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 validMode := false

View File

@ -65,14 +65,14 @@ func TestNewConfig_sourcePath(t *testing.T) {
t.Fatalf("should error with empty `source_path`") 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 = testConfig(t)
c["source_path"] = "/i/dont/exist" c["source_path"] = "/i/dont/exist"
_, warns, err = NewConfig(c) _, warns, err = NewConfig(c)
if len(warns) > 0 { if len(warns) > 0 {
t.Fatalf("bad: %#v", warns) t.Fatalf("bad: %#v", warns)
} }
if err != nil { if err == nil {
t.Fatalf("bad: %s", err) t.Fatalf("bad: %s", err)
} }
@ -84,7 +84,7 @@ func TestNewConfig_sourcePath(t *testing.T) {
t.Fatalf("bad: %#v", warns) t.Fatalf("bad: %#v", warns)
} }
if err == nil { if err == nil {
t.Fatal("should error") t.Fatal("Nonexistent source file should be caught in validation")
} }
// Good // Good