iso_config: allow for subdirs in hash sum files

Allow hash sum files and ISOs to be in different directories as
Ubuntu does.

Signed-off-by: Pavel Boldin <boldin.pavel@gmail.com>
This commit is contained in:
Pavel Boldin 2017-11-06 00:32:39 +01:00 committed by Pavel Boldin
parent ec6d6098de
commit 853b04420c
2 changed files with 66 additions and 3 deletions

View File

@ -140,9 +140,20 @@ func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error {
if err != nil { if err != nil {
return err return err
} }
checksumurl, err := url.Parse(c.ISOChecksumURL)
if err != nil {
return err
}
relpath, err := filepath.Rel(filepath.Dir(checksumurl.Path), u.Path)
if err != nil {
return err
}
filename := filepath.Base(u.Path) filename := filepath.Base(u.Path)
errNotFound := fmt.Errorf("No checksum for %q found at: %s", filename, c.ISOChecksumURL) errNotFound := fmt.Errorf("No checksum for %q or %q found at: %s", filename, relpath, c.ISOChecksumURL)
for { for {
line, err := rd.ReadString('\n') line, err := rd.ReadString('\n')
if err != nil && line == "" { if err != nil && line == "" {
@ -154,7 +165,8 @@ func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error {
} }
if strings.ToLower(parts[0]) == c.ISOChecksumType { if strings.ToLower(parts[0]) == c.ISOChecksumType {
// BSD-style checksum // BSD-style checksum
if parts[1] == fmt.Sprintf("(%s)", filename) { if parts[1] == fmt.Sprintf("(%s)", filename) || parts[1] == fmt.Sprintf("(%s)", relpath) ||
parts[1] == fmt.Sprintf("(./%s)", relpath) {
c.ISOChecksum = parts[3] c.ISOChecksum = parts[3]
return nil return nil
} }
@ -164,7 +176,7 @@ func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error {
// Binary mode // Binary mode
parts[1] = parts[1][1:] parts[1] = parts[1][1:]
} }
if parts[1] == filename { if parts[1] == filename || parts[1] == relpath || parts[1] == "./"+relpath {
c.ISOChecksum = parts[0] c.ISOChecksum = parts[0]
return nil return nil
} }

View File

@ -24,11 +24,21 @@ MD5 (other.iso) = bAr
MD5 (the-OS.iso) = baZ MD5 (the-OS.iso) = baZ
` `
var cs_bsd_style_subdir = `
MD5 (other.iso) = bAr
MD5 (./subdir/the-OS.iso) = baZ
`
var cs_gnu_style = ` var cs_gnu_style = `
bAr0 *the-OS.iso bAr0 *the-OS.iso
baZ0 other.iso baZ0 other.iso
` `
var cs_gnu_style_subdir = `
bAr0 *./subdir/the-OS.iso
baZ0 other.iso
`
var cs_bsd_style_no_newline = ` var cs_bsd_style_no_newline = `
MD5 (other.iso) = bAr MD5 (other.iso) = bAr
MD5 (the-OS.iso) = baZ` MD5 (the-OS.iso) = baZ`
@ -134,6 +144,27 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) {
t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum) t.Fatalf("should've found \"baz\" got: %s", i.ISOChecksum)
} }
// Test good - ISOChecksumURL BSD style with relative path
i = testISOConfig()
i.ISOChecksum = ""
cs_dir, _ := ioutil.TempDir("", "packer-testdir-")
cs_file, _ = ioutil.TempFile(cs_dir, "packer-test-")
ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style_subdir), 0666)
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso")
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 // Test good - ISOChecksumURL GNU style no newline
i = testISOConfig() i = testISOConfig()
i.ISOChecksum = "" i.ISOChecksum = ""
@ -171,6 +202,26 @@ 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 GNU style with relative path
i = testISOConfig()
i.ISOChecksum = ""
cs_file, _ = ioutil.TempFile(cs_dir, "packer-test-")
ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style_subdir), 0666)
i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name())
i.RawSingleISOUrl = fmt.Sprintf("%s%s", cs_dir, "/subdir/the-OS.iso")
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) {