diff --git a/common/iso_config.go b/common/iso_config.go index ce7e7b223..655e92d98 100644 --- a/common/iso_config.go +++ b/common/iso_config.go @@ -23,18 +23,15 @@ type ISOConfig struct { RawSingleISOUrl string `mapstructure:"iso_url"` } -func (c *ISOConfig) Prepare(ctx *interpolate.Context) ([]string, []error) { - // Validation - var errs []error - var err error - var warnings []string - +func (c *ISOConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs []error) { if c.RawSingleISOUrl == "" && len(c.ISOUrls) == 0 { errs = append( errs, errors.New("One of iso_url or iso_urls must be specified.")) + return } else if c.RawSingleISOUrl != "" && len(c.ISOUrls) > 0 { errs = append( errs, errors.New("Only one of iso_url or iso_urls may be specified.")) + return } else if c.RawSingleISOUrl != "" { c.ISOUrls = []string{c.RawSingleISOUrl} } @@ -106,10 +103,12 @@ func (c *ISOConfig) Prepare(ctx *interpolate.Context) ([]string, []error) { c.ISOChecksum = strings.ToLower(c.ISOChecksum) for i, url := range c.ISOUrls { - c.ISOUrls[i], err = DownloadableURL(url) + url, err := DownloadableURL(url) if err != nil { errs = append( errs, fmt.Errorf("Failed to parse iso_url %d: %s", i+1, err)) + } else { + c.ISOUrls[i] = url } } diff --git a/common/iso_config_test.go b/common/iso_config_test.go index b7860d5f8..845e4e9dc 100644 --- a/common/iso_config_test.go +++ b/common/iso_config_test.go @@ -3,6 +3,8 @@ package common import ( "fmt" "io/ioutil" + "net/http" + "net/http/httptest" "reflect" "testing" ) @@ -216,6 +218,15 @@ func TestISOConfigPrepare_ISOUrl(t *testing.T) { t.Fatal("should have error") } + // Test iso_url not set but checksum url is + ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root"))) + defer ts.Close() + i = testISOConfig() + i.RawSingleISOUrl = "" + i.ISOChecksum = "" + i.ISOChecksumURL = ts.URL + "/basic.txt" + warns, err = i.Prepare(nil) + // Test iso_url set i = testISOConfig() i.RawSingleISOUrl = "http://www.packer.io/the-OS.iso"