mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
When a system-wide shared plugin cache is configured, we'll want to make use of entries already in the shared cache when populating a local (configuration-specific) cache. This new method LinkFromOtherCache encapsulates the work of placing a link from one cache to another. If possible it will create a symlink, therefore retaining a key advantage of configuring a shared plugin cache, but otherwise we'll do a deep copy of the package directory from one cache to the other. Our old provider installer would always skip trying to create symlinks on Windows because Go standard library support for os.Symlink on Windows was inconsistent in older versions. However, os.Symlink can now create symlinks using a new API introduced in a Windows 10 update and cleanly fail if symlink creation is impossible, so it's safe for us to just try to create the symlink and react if that produces an error, just as we used to do on non-Windows systems when possibly creating symlinks on filesystems that cannot support them.
99 lines
3.3 KiB
Go
99 lines
3.3 KiB
Go
package providercache
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/apparentlymart/go-versions/versions"
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/internal/getproviders"
|
|
)
|
|
|
|
func LinkFromOtherCache(t *testing.T) {
|
|
srcDirPath := "testdata/cachedir"
|
|
tmpDirPath, err := ioutil.TempDir("", "terraform-test-providercache")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(tmpDirPath)
|
|
|
|
windowsPlatform := getproviders.Platform{
|
|
OS: "windows",
|
|
Arch: "amd64",
|
|
}
|
|
nullProvider := addrs.NewProvider(
|
|
addrs.DefaultRegistryHost, "hashicorp", "null",
|
|
)
|
|
|
|
srcDir := newDirWithPlatform(srcDirPath, windowsPlatform)
|
|
tmpDir := newDirWithPlatform(tmpDirPath, windowsPlatform)
|
|
|
|
// First we'll check our preconditions: srcDir should have only the
|
|
// null provider version 2.0.0 in it, because we're faking that we're on
|
|
// Windows, and tmpDir should have no providers in it at all.
|
|
|
|
gotSrcInitial := srcDir.AllAvailablePackages()
|
|
wantSrcInitial := map[addrs.Provider][]CachedProvider{
|
|
nullProvider: {
|
|
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"),
|
|
|
|
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.exe",
|
|
},
|
|
},
|
|
}
|
|
if diff := cmp.Diff(wantSrcInitial, gotSrcInitial); diff != "" {
|
|
t.Fatalf("incorrect initial source directory contents\n%s", diff)
|
|
}
|
|
|
|
gotTmpInitial := tmpDir.AllAvailablePackages()
|
|
wantTmpInitial := map[addrs.Provider][]CachedProvider{}
|
|
if diff := cmp.Diff(wantTmpInitial, gotTmpInitial); diff != "" {
|
|
t.Fatalf("incorrect initial temp directory contents\n%s", diff)
|
|
}
|
|
|
|
cacheEntry := srcDir.ProviderLatestVersion(nullProvider)
|
|
if cacheEntry == nil {
|
|
// This is weird because we just checked for the presence of this above
|
|
t.Fatalf("null provider has no latest version in source directory")
|
|
}
|
|
|
|
err = tmpDir.LinkFromOtherCache(cacheEntry)
|
|
if err != nil {
|
|
t.Fatalf("LinkFromOtherCache failed: %s", err)
|
|
}
|
|
|
|
// Now we should see the same version reflected in the temporary directory.
|
|
got := tmpDir.AllAvailablePackages()
|
|
want := map[addrs.Provider][]CachedProvider{
|
|
nullProvider: {
|
|
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"),
|
|
|
|
// These are still pointed into the testdata directory because
|
|
// we created a symlink in our tmpDir. (This part of the test
|
|
// is expected to fail if the temporary directory is on a
|
|
// filesystem that cannot support symlinks, in which case
|
|
// we should see the temporary directory paths here instead.)
|
|
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.exe",
|
|
},
|
|
},
|
|
}
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Errorf("wrong cache contents after link\n%s", diff)
|
|
}
|
|
}
|