opentofu/internal/getproviders/filesystem_search_test.go
Martin Atkins ce67a818db internal/getproviders: Allow basedir for local search to be symlink
The SearchLocalDirectory function was intentionally written to only
support symlinks at the leaves so that it wouldn't risk getting into an
infinite loop traversing intermediate symlinks, but that rule was also
applying to the base directory itself.

It's pretty reasonable to put your local plugins in some location
Terraform wouldn't normally search (e.g. because you want to get them from
a shared filesystem mounted somewhere) and creating a symlink from one
of the locations Terraform _does_ search is a convenient way to help
Terraform find those without going all in on the explicit provider
installation methods configuration that is intended for more complicated
situations.

To allow for that, here we make a special exception for the base
directory, resolving that first before we do any directory walking.

In order to help with debugging a situation where there are for some
reason symlinks at intermediate levels inside the search tree, we also now
emit a WARN log line in that case to be explicit that symlinks are not
supported there and to hint to put the symlink at the top-level if you
want to use symlinks at all.

(The support for symlinks at the deepest level of search is not mentioned
in this message because we allow it primarily for our own cache linking
behavior.)
2020-08-18 14:28:50 -07:00

53 lines
1.4 KiB
Go

package getproviders
import (
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/addrs"
)
func TestSearchLocalDirectory(t *testing.T) {
tests := []struct {
Fixture string
Subdir string
Want map[addrs.Provider]PackageMetaList
}{
{
"symlinks",
"symlink",
map[addrs.Provider]PackageMetaList{
addrs.MustParseProviderSourceString("example.com/foo/bar"): {
{
Provider: addrs.MustParseProviderSourceString("example.com/foo/bar"),
Version: MustParseVersion("1.0.0"),
TargetPlatform: Platform{OS: "linux", Arch: "amd64"},
Filename: "terraform-provider-bar_1.0.0_linux_amd64.zip",
Location: PackageLocalDir("testdata/search-local-directory/symlinks/real/example.com/foo/bar/1.0.0/linux_amd64"),
},
},
// This search doesn't find example.net/foo/bar because only
// the top-level search directory is supported as being a
// symlink, and so we ignore the example.net symlink to
// example.com that is one level deeper.
},
},
}
for _, test := range tests {
t.Run(test.Fixture, func(t *testing.T) {
fullDir := filepath.Join("testdata/search-local-directory", test.Fixture, test.Subdir)
got, err := SearchLocalDirectory(fullDir)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
want := test.Want
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong result\n%s", diff)
}
})
}
}