From 2319521aa538de2916e2ee692f3a51592e77cff1 Mon Sep 17 00:00:00 2001 From: Sylvia Moss Date: Wed, 18 Mar 2020 11:32:51 +0100 Subject: [PATCH] Add iso config test for checksum from file specific case (#8897) --- common/iso_config_test.go | 34 ++++++++++++++++++- common/test-fixtures/root/subfolder.sum | 2 ++ go.mod | 2 +- go.sum | 2 ++ .../hashicorp/go-getter/v2/checksum.go | 7 ++++ vendor/modules.txt | 2 +- 6 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 common/test-fixtures/root/subfolder.sum diff --git a/common/iso_config_test.go b/common/iso_config_test.go index 9ffe6fecf..a9f13b6fb 100644 --- a/common/iso_config_test.go +++ b/common/iso_config_test.go @@ -5,6 +5,7 @@ package common import ( "net/http" "net/http/httptest" + "path/filepath" "reflect" "testing" ) @@ -158,7 +159,7 @@ func TestISOConfigPrepare_ISOUrl(t *testing.T) { } // Test iso_url not set but checksum url is - ts := httptest.NewServer(http.FileServer(http.Dir("./test-fixtures/root"))) + ts := httpTestModule("root") defer ts.Close() i = testISOConfig() i.RawSingleISOUrl = "" @@ -258,3 +259,34 @@ func TestISOConfigPrepare_TargetExtension(t *testing.T) { t.Fatalf("should've lowercased: %s", i.TargetExtension) } } + +func TestISOConfigPrepare_ISOChecksumURLMyTest(t *testing.T) { + httpChecksums := httpTestModule("root") + defer httpChecksums.Close() + i := ISOConfig{ + ISOChecksumURL: httpChecksums.URL + "/subfolder.sum", + ISOChecksumType: "sha256", + ISOUrls: []string{"http://hashicorp.com/ubuntu/dists/bionic-updates/main/installer-amd64/current/images/netboot/mini.iso"}, + } + + // Test ISOChecksum overrides url + warns, err := i.Prepare(nil) + if len(warns) > 0 { + t.Fatalf("Bad: should not have warnings") + } + if len(err) > 0 { + t.Fatalf("Bad; should not have errored.") + } +} + +const fixtureDir = "./test-fixtures" + +func httpTestModule(n string) *httptest.Server { + p := filepath.Join(fixtureDir, n) + p, err := filepath.Abs(p) + if err != nil { + panic(err) + } + + return httptest.NewServer(http.FileServer(http.Dir(p))) +} diff --git a/common/test-fixtures/root/subfolder.sum b/common/test-fixtures/root/subfolder.sum new file mode 100644 index 000000000..5c765fb66 --- /dev/null +++ b/common/test-fixtures/root/subfolder.sum @@ -0,0 +1,2 @@ +a264b6b009dfaa16286fdfd046a156a43587333b ./hwe-netboot/mini.iso +27c39bac2cf4640c00cacfc8982b0ba39e7b7f96 ./netboot/mini.iso diff --git a/go.mod b/go.mod index abc115616..cb8f7db9c 100644 --- a/go.mod +++ b/go.mod @@ -72,7 +72,7 @@ require ( github.com/hashicorp/go-cty-funcs/filesystem v0.0.0-20200203151509-c92509f48b18 github.com/hashicorp/go-cty-funcs/uuid v0.0.0-20200203151509-c92509f48b18 github.com/hashicorp/go-getter v1.3.1-0.20190906090232-a0f878cb75da // indirect - github.com/hashicorp/go-getter/v2 v2.0.0-20200206160058-e2a28063d6e7 + github.com/hashicorp/go-getter/v2 v2.0.0-20200318090939-0b1d527d9793 github.com/hashicorp/go-multierror v1.0.0 github.com/hashicorp/go-oracle-terraform v0.0.0-20181016190316-007121241b79 github.com/hashicorp/go-retryablehttp v0.5.2 // indirect diff --git a/go.sum b/go.sum index 1bf247abf..477bbe43d 100644 --- a/go.sum +++ b/go.sum @@ -245,6 +245,8 @@ github.com/hashicorp/go-getter v1.3.1-0.20190906090232-a0f878cb75da h1:HAasZmyRr github.com/hashicorp/go-getter v1.3.1-0.20190906090232-a0f878cb75da/go.mod h1:7qxyCd8rBfcShwsvxgIguu4KbS3l8bUCwg2Umn7RjeY= github.com/hashicorp/go-getter/v2 v2.0.0-20200206160058-e2a28063d6e7 h1:ODZKizgWGz4diUEZwCgf8qgIn/D+qVW/JOdVVV/z7k8= github.com/hashicorp/go-getter/v2 v2.0.0-20200206160058-e2a28063d6e7/go.mod h1:jlmxRRjTpY0KdWrV1Uq38GUVskrjIZUrjOAybo0OArw= +github.com/hashicorp/go-getter/v2 v2.0.0-20200318090939-0b1d527d9793 h1:jH222Ag2I+p5tq5IagFwCfj5CnRzqcGW8RgcQ8jEPMs= +github.com/hashicorp/go-getter/v2 v2.0.0-20200318090939-0b1d527d9793/go.mod h1:jlmxRRjTpY0KdWrV1Uq38GUVskrjIZUrjOAybo0OArw= github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0= github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= github.com/hashicorp/go-msgpack v0.5.3 h1:zKjpN5BK/P5lMYrLmBHdBULWbJ0XpYR+7NGzqkZzoD4= diff --git a/vendor/github.com/hashicorp/go-getter/v2/checksum.go b/vendor/github.com/hashicorp/go-getter/v2/checksum.go index d10ab2c0b..94e137435 100644 --- a/vendor/github.com/hashicorp/go-getter/v2/checksum.go +++ b/vendor/github.com/hashicorp/go-getter/v2/checksum.go @@ -274,6 +274,13 @@ func (c *Client) ChecksumFromFile(ctx context.Context, checksumURL, checksummedP return checksum, nil } } + // The checksum filename can contain a sub folder to differ versions. + // e.g. ./netboot/mini.iso and ./hwe-netboot/mini.iso + // In this case we remove root folder characters to compare with the checksummed path + fn := strings.TrimLeft(checksum.Filename, "./") + if strings.Contains(checksummedPath, fn) { + return checksum, nil + } } return nil, fmt.Errorf("no checksum found in: %s", checksumURL) } diff --git a/vendor/modules.txt b/vendor/modules.txt index 42b1a7e22..81cbb28d8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -321,7 +321,7 @@ github.com/hashicorp/go-cty-funcs/filesystem github.com/hashicorp/go-cty-funcs/uuid # github.com/hashicorp/go-getter v1.3.1-0.20190906090232-a0f878cb75da github.com/hashicorp/go-getter/helper/url -# github.com/hashicorp/go-getter/v2 v2.0.0-20200206160058-e2a28063d6e7 +# github.com/hashicorp/go-getter/v2 v2.0.0-20200318090939-0b1d527d9793 github.com/hashicorp/go-getter/v2 github.com/hashicorp/go-getter/v2/helper/url # github.com/hashicorp/go-immutable-radix v1.0.0