diff --git a/common/iso_config.go b/common/iso_config.go index fddea5bb3..ce7e7b223 100644 --- a/common/iso_config.go +++ b/common/iso_config.go @@ -4,7 +4,6 @@ import ( "bufio" "errors" "fmt" - "io" "net/http" "net/url" "os" @@ -125,14 +124,11 @@ func (c *ISOConfig) Prepare(ctx *interpolate.Context) ([]string, []error) { } func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error { + errNotFound := fmt.Errorf("No checksum for %q found at: %s", filepath.Base(c.ISOUrls[0]), c.ISOChecksumURL) for { line, err := rd.ReadString('\n') - if err != nil { - if err == io.EOF { - return fmt.Errorf("No checksum for \"%s\" found at: %s", filepath.Base(c.ISOUrls[0]), c.ISOChecksumURL) - } else { - return fmt.Errorf("Error getting checksum from url: %s , %s", c.ISOChecksumURL, err.Error()) - } + if err != nil && line == "" { + break } parts := strings.Fields(line) if len(parts) < 2 { @@ -142,7 +138,7 @@ func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error { // BSD-style checksum if parts[1] == fmt.Sprintf("(%s)", filepath.Base(c.ISOUrls[0])) { c.ISOChecksum = parts[3] - break + return nil } } else { // Standard checksum @@ -152,9 +148,9 @@ func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error { } if parts[1] == filepath.Base(c.ISOUrls[0]) { c.ISOChecksum = parts[0] - break + return nil } } } - return nil + return errNotFound } diff --git a/common/iso_config_test.go b/common/iso_config_test.go index baaeea2b6..b7860d5f8 100644 --- a/common/iso_config_test.go +++ b/common/iso_config_test.go @@ -26,6 +26,14 @@ bAr0 *the-OS.iso baZ0 other.iso ` +var cs_bsd_style_no_newline = ` +MD5 (other.iso) = bAr +MD5 (the-OS.iso) = baZ` + +var cs_gnu_style_no_newline = ` +bAr0 *the-OS.iso +baZ0 other.iso` + func TestISOConfigPrepare_ISOChecksum(t *testing.T) { i := testISOConfig() @@ -100,6 +108,43 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { if i.ISOChecksum != "bar0" { t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum) } + + // Test good - ISOChecksumURL BSD style no newline + i = testISOConfig() + i.ISOChecksum = "" + cs_file, _ = ioutil.TempFile("", "packer-test-") + ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_no_newline), 0666) + i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) + warns, err = i.Prepare(nil) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + if i.ISOChecksum != "baz" { + t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum) + } + + // Test good - ISOChecksumURL GNU style no newline + i = testISOConfig() + i.ISOChecksum = "" + cs_file, _ = ioutil.TempFile("", "packer-test-") + ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_no_newline), 0666) + i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) + warns, err = i.Prepare(nil) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + if i.ISOChecksum != "bar0" { + t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum) + } + } func TestISOConfigPrepare_ISOChecksumType(t *testing.T) {