From 853b04420c83fed3daa1de54404c94a80a63c3f6 Mon Sep 17 00:00:00 2001 From: Pavel Boldin Date: Mon, 6 Nov 2017 00:32:39 +0100 Subject: [PATCH] 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 --- common/iso_config.go | 18 +++++++++++--- common/iso_config_test.go | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/common/iso_config.go b/common/iso_config.go index 1ddb78d17..5216dc458 100644 --- a/common/iso_config.go +++ b/common/iso_config.go @@ -140,9 +140,20 @@ func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error { if err != nil { 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) - 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 { line, err := rd.ReadString('\n') if err != nil && line == "" { @@ -154,7 +165,8 @@ func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error { } if strings.ToLower(parts[0]) == c.ISOChecksumType { // 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] return nil } @@ -164,7 +176,7 @@ func (c *ISOConfig) parseCheckSumFile(rd *bufio.Reader) error { // Binary mode parts[1] = parts[1][1:] } - if parts[1] == filename { + if parts[1] == filename || parts[1] == relpath || parts[1] == "./"+relpath { c.ISOChecksum = parts[0] return nil } diff --git a/common/iso_config_test.go b/common/iso_config_test.go index 7a5a0b026..f31c687b2 100644 --- a/common/iso_config_test.go +++ b/common/iso_config_test.go @@ -24,11 +24,21 @@ MD5 (other.iso) = bAr MD5 (the-OS.iso) = baZ ` +var cs_bsd_style_subdir = ` +MD5 (other.iso) = bAr +MD5 (./subdir/the-OS.iso) = baZ +` + var cs_gnu_style = ` bAr0 *the-OS.iso baZ0 other.iso ` +var cs_gnu_style_subdir = ` +bAr0 *./subdir/the-OS.iso +baZ0 other.iso +` + var cs_bsd_style_no_newline = ` MD5 (other.iso) = bAr 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) } + // 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 i = testISOConfig() i.ISOChecksum = "" @@ -171,6 +202,26 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { if i.ISOChecksum != "bar0" { 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) {