Merge pull request #4004 from mitchellh/parseCheckSumFilePanic

fix parseCheckSumFile panic
This commit is contained in:
Matthew Hooker 2016-10-14 15:50:07 -07:00 committed by GitHub
commit 97688a96ba
2 changed files with 17 additions and 7 deletions

View File

@ -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
}
}

View File

@ -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"