opentofu/internal/providercache/dir_modify_test.go
Kristin Laemmert a43f141f9e
tools/terraform-bundle: refactor to use new provider installer and provider directory layouts (#24629)
* tools/terraform-bundle: refactor to use new provider installer and
provider directory layouts

terraform-bundle now supports a "source" attribute for providers,
uses the new provider installer, and the archive it creates preserves
the new (required) directory hierarchy for providers, under a "plugins"
directory.

This is a breaking change in many ways: source is required for any
non-HashiCorp provider, locally-installed providers must be given a
source (can be arbitrary, see docs) and placed in the expected directory
hierarchy, and the unzipped archive is no longer flat; there is a new
"plugins" directory created with providers in the new directory layout.

This PR also extends the existing test to check the contents of the zip
file.

TODO: Re-enable e2e tests (currently suppressed with a t.Skip)
This commit includes an update to our travis configuration, so the terraform-bundle e2e tests run. It also turns off the e2e tests, which will fail until we have a terraform 0.13.* release under releases.hashicorp.com. We decided it was better to merge this now instead of waiting when we started seeing issues opened from users who built terraform-bundle from 0.13 and found it didn't work with 0.12 - better that they get an immediate error message from the binary directing them to build from the appropriate release.
2020-04-21 17:09:29 -04:00

150 lines
4.5 KiB
Go

package providercache
import (
"context"
"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 TestInstallPackage(t *testing.T) {
tmpDirPath, err := ioutil.TempDir("", "terraform-test-providercache")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDirPath)
linuxPlatform := getproviders.Platform{
OS: "linux",
Arch: "amd64",
}
nullProvider := addrs.NewProvider(
addrs.DefaultRegistryHost, "hashicorp", "null",
)
tmpDir := newDirWithPlatform(tmpDirPath, linuxPlatform)
meta := getproviders.PackageMeta{
Provider: nullProvider,
Version: versions.MustParseVersion("2.1.0"),
ProtocolVersions: getproviders.VersionList{versions.MustParseVersion("5.0.0")},
TargetPlatform: linuxPlatform,
Filename: "terraform-provider-null_2.1.0_linux_amd64.zip",
Location: getproviders.PackageLocalArchive("testdata/terraform-provider-null_2.1.0_linux_amd64.zip"),
}
result, err := tmpDir.InstallPackage(context.TODO(), meta)
if err != nil {
t.Fatalf("InstallPackage failed: %s", err)
}
if result != nil {
t.Errorf("unexpected result %#v, wanted nil", result)
}
// Now we should see the same version reflected in the temporary directory.
got := tmpDir.AllAvailablePackages()
want := map[addrs.Provider][]CachedProvider{
nullProvider: {
CachedProvider{
Provider: nullProvider,
Version: versions.MustParseVersion("2.1.0"),
PackageDir: tmpDirPath + "/registry.terraform.io/hashicorp/null/2.1.0/linux_amd64",
ExecutableFile: tmpDirPath + "/registry.terraform.io/hashicorp/null/2.1.0/linux_amd64/terraform-provider-null",
},
},
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong cache contents after install\n%s", diff)
}
}
func TestLinkFromOtherCache(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"),
PackageDir: tmpDirPath + "/registry.terraform.io/hashicorp/null/2.0.0/windows_amd64",
ExecutableFile: tmpDirPath + "/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)
}
}