opentofu/internal/providercache/cached_provider_test.go
Martin Atkins f6a7a4868b internal/providercache: Hashing of contents of cached packages
For the old-style provider cache directory model we hashed the individual
executable file for each provider. That's no longer appropriate because
we're giving each provider package a whole directory to itself where it
can potentially have many files.

This therefore introduces a new directory-oriented hashing algorithm, and
it's just using the Go Modules directory hashing algorithm directly
because that's already had its cross-platform quirks and other wrinkles
addressed during the Go Modules release process, and is now used
prolifically enough in Go codebases that breaking changes to the upstream
algorithm would be very expensive to the Go ecosystem.

This is also a bit of forward planning, anticipating that later we'll use
hashes in a top-level lock file intended to be checked in to user version
control, and then use those hashes also to verify packages _during_
installation, where we'd need to be able to hash unpacked zip files. The
Go Modules hashing algorithm is already implemented to consistently hash
both a zip file and an unpacked version of that zip file.
2020-04-06 09:24:23 -07:00

61 lines
1.8 KiB
Go

package providercache
import (
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/internal/getproviders"
)
func TestCachedProviderHash(t *testing.T) {
cp := &CachedProvider{
Provider: addrs.NewProvider(
addrs.DefaultRegistryHost,
"hashicorp", "null",
),
Version: getproviders.MustParseVersion("2.0.0"),
PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/darwin_amd64",
ExecutableFile: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/darwin_amd64/terraform-provider-null",
}
want := "h1:qjsREM4DqEWECD43FcPqddZ9oxCG+IaMTxvWPciS05g="
got, err := cp.Hash()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if got != want {
t.Errorf("wrong Hash result\ngot: %s\nwant: %s", got, want)
}
gotMatches, err := cp.MatchesHash(want)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if wantMatches := true; gotMatches != wantMatches {
t.Errorf("wrong MatchesHash result\ngot: %#v\nwant: %#v", gotMatches, wantMatches)
}
// The windows build has a different hash because its executable filename
// has a .exe suffix, but the darwin build (hashed above) does not.
cp2 := &CachedProvider{
Provider: addrs.NewProvider(
addrs.DefaultRegistryHost,
"hashicorp", "null",
),
Version: getproviders.MustParseVersion("2.0.0"),
PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64",
ExecutableFile: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64/terraform-provider-null",
}
gotMatches, err = cp2.MatchesHash(want)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if wantMatches := false; gotMatches != wantMatches {
t.Errorf("wrong MatchesHash result for other package\ngot: %#v\nwant: %#v", gotMatches, wantMatches)
}
}