diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index d71426785..ac8b574d5 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -8,8 +8,10 @@ import ( "github.com/mitchellh/packer/packer" "log" "math/rand" + "net/url" "os" "path/filepath" + "strings" "time" ) @@ -93,6 +95,36 @@ func (b *Builder) Prepare(raw interface{}) (err error) { if b.config.ISOUrl == "" { errs = append(errs, errors.New("An iso_url must be specified.")) + } else { + url, err := url.Parse(b.config.ISOUrl) + if err != nil { + errs = append(errs, fmt.Errorf("iso_url is not a valid URL: %s", err)) + } else { + if url.Scheme == "" { + url.Scheme = "file" + } + + if url.Scheme == "file" { + if _, err := os.Stat(b.config.ISOUrl); err != nil { + errs = append(errs, fmt.Errorf("iso_url points to bad file: %s", err)) + } + } else { + supportedSchemes := []string{"file", "http", "https"} + scheme := strings.ToLower(url.Scheme) + + found := false + for _, supported := range supportedSchemes { + if scheme == supported { + found = true + break + } + } + + if !found { + errs = append(errs, fmt.Errorf("Unsupported URL scheme in iso_url: %s", scheme)) + } + } + } } if b.config.SSHUser == "" { diff --git a/builder/vmware/builder_test.go b/builder/vmware/builder_test.go index bb45b6241..090097a6f 100644 --- a/builder/vmware/builder_test.go +++ b/builder/vmware/builder_test.go @@ -8,7 +8,7 @@ import ( func testConfig() map[string]interface{} { return map[string]interface{}{ - "iso_url": "foo", + "iso_url": "http://www.packer.io", "ssh_username": "foo", } } @@ -104,10 +104,22 @@ func TestBuilderPrepare_ISOUrl(t *testing.T) { t.Fatal("should have error") } - config["iso_url"] = "exists" + config["iso_url"] = "i/am/a/file/that/doesnt/exist" + err = b.Prepare(config) + if err == nil { + t.Error("should have error") + } + + config["iso_url"] = "file:i/am/a/file/that/doesnt/exist" + err = b.Prepare(config) + if err == nil { + t.Error("should have error") + } + + config["iso_url"] = "http://www.packer.io" err = b.Prepare(config) if err != nil { - t.Fatalf("should not have error: %s", err) + t.Errorf("should not have error: %s", err) } }