iso_checksum: fix parsing with absent newline
Signed-off-by: Vasiliy Tolstov <v.tolstov@selfip.ru>
This commit is contained in:
parent
443ffd68f2
commit
82c63bd723
|
@ -4,7 +4,6 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -125,14 +124,11 @@ func (c *ISOConfig) Prepare(ctx *interpolate.Context) ([]string, []error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) 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 {
|
for {
|
||||||
line, err := rd.ReadString('\n')
|
line, err := rd.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil && line == "" {
|
||||||
if err == io.EOF {
|
break
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
parts := strings.Fields(line)
|
parts := strings.Fields(line)
|
||||||
if len(parts) < 2 {
|
if len(parts) < 2 {
|
||||||
|
@ -142,7 +138,7 @@ func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error {
|
||||||
// BSD-style checksum
|
// BSD-style checksum
|
||||||
if parts[1] == fmt.Sprintf("(%s)", filepath.Base(c.ISOUrls[0])) {
|
if parts[1] == fmt.Sprintf("(%s)", filepath.Base(c.ISOUrls[0])) {
|
||||||
c.ISOChecksum = parts[3]
|
c.ISOChecksum = parts[3]
|
||||||
break
|
return nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Standard checksum
|
// Standard checksum
|
||||||
|
@ -152,9 +148,9 @@ func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error {
|
||||||
}
|
}
|
||||||
if parts[1] == filepath.Base(c.ISOUrls[0]) {
|
if parts[1] == filepath.Base(c.ISOUrls[0]) {
|
||||||
c.ISOChecksum = parts[0]
|
c.ISOChecksum = parts[0]
|
||||||
break
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return errNotFound
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,14 @@ bAr0 *the-OS.iso
|
||||||
baZ0 other.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) {
|
func TestISOConfigPrepare_ISOChecksum(t *testing.T) {
|
||||||
i := testISOConfig()
|
i := testISOConfig()
|
||||||
|
|
||||||
|
@ -100,6 +108,43 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
|
||||||
if i.ISOChecksum != "bar0" {
|
if i.ISOChecksum != "bar0" {
|
||||||
t.Fatalf("should've found \"bar0\" got: %s", i.ISOChecksum)
|
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) {
|
func TestISOConfigPrepare_ISOChecksumType(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue