2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-03-27 17:36:07 -05:00
|
|
|
package providercache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-08-17 07:45:11 -05:00
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/addrs"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/getproviders"
|
2020-03-27 17:36:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCachedProviderHash(t *testing.T) {
|
|
|
|
cp := &CachedProvider{
|
|
|
|
Provider: addrs.NewProvider(
|
2021-05-27 13:09:28 -05:00
|
|
|
addrs.DefaultProviderRegistryHost,
|
2020-03-27 17:36:07 -05:00
|
|
|
"hashicorp", "null",
|
|
|
|
),
|
|
|
|
Version: getproviders.MustParseVersion("2.0.0"),
|
|
|
|
|
2020-07-07 13:36:04 -05:00
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/darwin_amd64",
|
2020-03-27 17:36:07 -05:00
|
|
|
}
|
|
|
|
|
2020-09-23 16:27:09 -05:00
|
|
|
want := getproviders.MustParseHash("h1:qjsREM4DqEWECD43FcPqddZ9oxCG+IaMTxvWPciS05g=")
|
2020-03-27 17:36:07 -05:00
|
|
|
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(
|
2021-05-27 13:09:28 -05:00
|
|
|
addrs.DefaultProviderRegistryHost,
|
2020-03-27 17:36:07 -05:00
|
|
|
"hashicorp", "null",
|
|
|
|
),
|
|
|
|
Version: getproviders.MustParseVersion("2.0.0"),
|
|
|
|
|
2020-07-07 13:36:04 -05:00
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64",
|
2020-03-27 17:36:07 -05:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-07-07 13:36:04 -05:00
|
|
|
|
|
|
|
func TestExecutableFile(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
cp *CachedProvider
|
|
|
|
file string
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
"linux": {
|
|
|
|
cp: &CachedProvider{
|
2021-05-27 13:09:28 -05:00
|
|
|
Provider: addrs.NewProvider(addrs.DefaultProviderRegistryHost, "hashicorp", "null"),
|
2020-07-07 13:36:04 -05:00
|
|
|
Version: getproviders.MustParseVersion("2.0.0"),
|
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/linux_amd64",
|
|
|
|
},
|
|
|
|
file: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/linux_amd64/terraform-provider-null",
|
|
|
|
},
|
|
|
|
"windows": {
|
|
|
|
cp: &CachedProvider{
|
2021-05-27 13:09:28 -05:00
|
|
|
Provider: addrs.NewProvider(addrs.DefaultProviderRegistryHost, "hashicorp", "null"),
|
2020-07-07 13:36:04 -05:00
|
|
|
Version: getproviders.MustParseVersion("2.0.0"),
|
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64",
|
|
|
|
},
|
|
|
|
file: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64/terraform-provider-null.exe",
|
|
|
|
},
|
|
|
|
"missing-executable": {
|
|
|
|
cp: &CachedProvider{
|
2021-05-27 13:09:28 -05:00
|
|
|
Provider: addrs.NewProvider(addrs.DefaultProviderRegistryHost, "missing", "executable"),
|
2020-07-07 13:36:04 -05:00
|
|
|
Version: getproviders.MustParseVersion("2.0.0"),
|
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/missing/executable/2.0.0/linux_amd64",
|
|
|
|
},
|
|
|
|
err: "could not find executable file starting with terraform-provider-executable",
|
|
|
|
},
|
|
|
|
"missing-dir": {
|
|
|
|
cp: &CachedProvider{
|
2021-05-27 13:09:28 -05:00
|
|
|
Provider: addrs.NewProvider(addrs.DefaultProviderRegistryHost, "missing", "packagedir"),
|
2020-07-07 13:36:04 -05:00
|
|
|
Version: getproviders.MustParseVersion("2.0.0"),
|
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/missing/packagedir/2.0.0/linux_amd64",
|
|
|
|
},
|
|
|
|
err: "could not read package directory: open testdata/cachedir/registry.terraform.io/missing/packagedir/2.0.0/linux_amd64: no such file or directory",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
file, err := tc.cp.ExecutableFile()
|
|
|
|
if file != tc.file {
|
|
|
|
t.Errorf("wrong file\n got: %q\nwant: %q", file, tc.file)
|
|
|
|
}
|
|
|
|
if err == nil && tc.err != "" {
|
|
|
|
t.Fatalf("no error returned, want: %q", tc.err)
|
|
|
|
} else if err != nil && err.Error() != tc.err {
|
|
|
|
t.Errorf("wrong error\n got: %q\nwant: %q", err, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|