2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-03-11 17:26:18 -05:00
|
|
|
package providercache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/apparentlymart/go-versions/versions"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2023-08-17 07:45:11 -05:00
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/addrs"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/getproviders"
|
2020-03-11 17:26:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDirReading(t *testing.T) {
|
|
|
|
testDir := "testdata/cachedir"
|
|
|
|
|
|
|
|
// We'll force using particular platforms for unit testing purposes,
|
|
|
|
// so that we'll get consistent results on all platforms.
|
|
|
|
windowsPlatform := getproviders.Platform{ // only null 2.0.0 is cached
|
|
|
|
OS: "windows",
|
|
|
|
Arch: "amd64",
|
|
|
|
}
|
|
|
|
linuxPlatform := getproviders.Platform{ // various provider versions are cached
|
|
|
|
OS: "linux",
|
|
|
|
Arch: "amd64",
|
|
|
|
}
|
|
|
|
|
|
|
|
nullProvider := addrs.NewProvider(
|
2021-05-27 13:09:28 -05:00
|
|
|
addrs.DefaultProviderRegistryHost, "hashicorp", "null",
|
2020-03-11 17:26:18 -05:00
|
|
|
)
|
|
|
|
randomProvider := addrs.NewProvider(
|
2021-05-27 13:09:28 -05:00
|
|
|
addrs.DefaultProviderRegistryHost, "hashicorp", "random",
|
2020-03-11 17:26:18 -05:00
|
|
|
)
|
2020-04-16 14:54:33 -05:00
|
|
|
randomBetaProvider := addrs.NewProvider(
|
2021-05-27 13:09:28 -05:00
|
|
|
addrs.DefaultProviderRegistryHost, "hashicorp", "random-beta",
|
2020-04-16 14:54:33 -05:00
|
|
|
)
|
2020-03-11 17:26:18 -05:00
|
|
|
nonExistProvider := addrs.NewProvider(
|
2021-05-27 13:09:28 -05:00
|
|
|
addrs.DefaultProviderRegistryHost, "bloop", "nonexist",
|
2020-03-11 17:26:18 -05:00
|
|
|
)
|
|
|
|
legacyProvider := addrs.NewLegacyProvider("legacy")
|
2020-07-07 13:36:04 -05:00
|
|
|
missingExecutableProvider := addrs.NewProvider(
|
2021-05-27 13:09:28 -05:00
|
|
|
addrs.DefaultProviderRegistryHost, "missing", "executable",
|
2020-07-07 13:36:04 -05:00
|
|
|
)
|
2020-03-11 17:26:18 -05:00
|
|
|
|
|
|
|
t.Run("ProviderLatestVersion", func(t *testing.T) {
|
|
|
|
t.Run("exists", func(t *testing.T) {
|
2020-04-21 16:09:29 -05:00
|
|
|
dir := NewDirWithPlatform(testDir, windowsPlatform)
|
2020-03-11 17:26:18 -05:00
|
|
|
|
|
|
|
got := dir.ProviderLatestVersion(nullProvider)
|
|
|
|
want := &CachedProvider{
|
|
|
|
Provider: nullProvider,
|
|
|
|
|
|
|
|
// We want 2.0.0 rather than 2.1.0 because the 2.1.0 package is
|
|
|
|
// still packed and thus not considered to be a cache member.
|
|
|
|
Version: versions.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-11 17:26:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong result\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("no package for current platform", func(t *testing.T) {
|
2020-04-21 16:09:29 -05:00
|
|
|
dir := NewDirWithPlatform(testDir, windowsPlatform)
|
2020-03-11 17:26:18 -05:00
|
|
|
|
|
|
|
// random provider is only cached for linux_amd64 in our fixtures dir
|
|
|
|
got := dir.ProviderLatestVersion(randomProvider)
|
|
|
|
var want *CachedProvider
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong result\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("no versions available at all", func(t *testing.T) {
|
2020-04-21 16:09:29 -05:00
|
|
|
dir := NewDirWithPlatform(testDir, windowsPlatform)
|
2020-03-11 17:26:18 -05:00
|
|
|
|
|
|
|
// nonexist provider is not present in our fixtures dir at all
|
|
|
|
got := dir.ProviderLatestVersion(nonExistProvider)
|
|
|
|
var want *CachedProvider
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong result\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ProviderVersion", func(t *testing.T) {
|
|
|
|
t.Run("exists", func(t *testing.T) {
|
2020-04-21 16:09:29 -05:00
|
|
|
dir := NewDirWithPlatform(testDir, windowsPlatform)
|
2020-03-11 17:26:18 -05:00
|
|
|
|
|
|
|
got := dir.ProviderVersion(nullProvider, versions.MustParseVersion("2.0.0"))
|
|
|
|
want := &CachedProvider{
|
|
|
|
Provider: nullProvider,
|
|
|
|
Version: versions.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-11 17:26:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong result\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("specified version is not cached", func(t *testing.T) {
|
2020-04-21 16:09:29 -05:00
|
|
|
dir := NewDirWithPlatform(testDir, windowsPlatform)
|
2020-03-11 17:26:18 -05:00
|
|
|
|
|
|
|
// there is no v5.0.0 package in our fixtures dir
|
|
|
|
got := dir.ProviderVersion(nullProvider, versions.MustParseVersion("5.0.0"))
|
|
|
|
var want *CachedProvider
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong result\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("no package for current platform", func(t *testing.T) {
|
2020-04-21 16:09:29 -05:00
|
|
|
dir := NewDirWithPlatform(testDir, windowsPlatform)
|
2020-03-11 17:26:18 -05:00
|
|
|
|
|
|
|
// random provider 1.2.0 is only cached for linux_amd64 in our fixtures dir
|
|
|
|
got := dir.ProviderVersion(randomProvider, versions.MustParseVersion("1.2.0"))
|
|
|
|
var want *CachedProvider
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong result\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("no versions available at all", func(t *testing.T) {
|
2020-04-21 16:09:29 -05:00
|
|
|
dir := NewDirWithPlatform(testDir, windowsPlatform)
|
2020-03-11 17:26:18 -05:00
|
|
|
|
|
|
|
// nonexist provider is not present in our fixtures dir at all
|
|
|
|
got := dir.ProviderVersion(nonExistProvider, versions.MustParseVersion("1.0.0"))
|
|
|
|
var want *CachedProvider
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong result\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AllAvailablePackages", func(t *testing.T) {
|
2020-04-21 16:09:29 -05:00
|
|
|
dir := NewDirWithPlatform(testDir, linuxPlatform)
|
2020-03-11 17:26:18 -05:00
|
|
|
|
|
|
|
got := dir.AllAvailablePackages()
|
|
|
|
want := map[addrs.Provider][]CachedProvider{
|
|
|
|
legacyProvider: {
|
|
|
|
{
|
2020-07-07 13:36:04 -05:00
|
|
|
Provider: legacyProvider,
|
|
|
|
Version: versions.MustParseVersion("1.0.0"),
|
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/-/legacy/1.0.0/linux_amd64",
|
2020-03-11 17:26:18 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
nullProvider: {
|
|
|
|
{
|
2020-07-07 13:36:04 -05:00
|
|
|
Provider: nullProvider,
|
|
|
|
Version: versions.MustParseVersion("2.0.0"),
|
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/null/2.0.0/linux_amd64",
|
2020-03-11 17:26:18 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
randomProvider: {
|
|
|
|
{
|
2020-07-07 13:36:04 -05:00
|
|
|
Provider: randomProvider,
|
|
|
|
Version: versions.MustParseVersion("1.2.0"),
|
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/random/1.2.0/linux_amd64",
|
2020-03-11 17:26:18 -05:00
|
|
|
},
|
|
|
|
},
|
2020-04-16 14:54:33 -05:00
|
|
|
randomBetaProvider: {
|
|
|
|
{
|
2020-07-07 13:36:04 -05:00
|
|
|
Provider: randomBetaProvider,
|
|
|
|
Version: versions.MustParseVersion("1.2.0"),
|
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/hashicorp/random-beta/1.2.0/linux_amd64",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
missingExecutableProvider: {
|
|
|
|
{
|
|
|
|
Provider: missingExecutableProvider,
|
|
|
|
Version: versions.MustParseVersion("2.0.0"),
|
|
|
|
PackageDir: "testdata/cachedir/registry.terraform.io/missing/executable/2.0.0/linux_amd64",
|
2020-04-16 14:54:33 -05:00
|
|
|
},
|
|
|
|
},
|
2020-03-11 17:26:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong result\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|