plugin/discovery: create target plugin directory if it does not exist (#20575)

If the user supplies a plugin cache dir, the provider installer was not
checking if target directory exists before linking the cached pluging.
This commit is contained in:
Kristin Laemmert 2019-03-05 12:39:24 -08:00 committed by GitHub
parent 81c94f7ea7
commit 9402b86217
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 0 deletions

View File

@ -270,6 +270,14 @@ func (i *ProviderInstaller) install(provider string, version Version, url string
// normal resolution machinery can find it.
filename := filepath.Base(cached)
targetPath := filepath.Join(i.Dir, filename)
// check if the target dir exists, and create it if not
var err error
if _, StatErr := os.Stat(i.Dir); os.IsNotExist(StatErr) {
err = os.Mkdir(i.Dir, 0700)
}
if err != nil {
return err
}
log.Printf("[DEBUG] installing %s %s to %s from local cache %s", provider, version, targetPath, cached)

View File

@ -474,6 +474,63 @@ func TestProviderInstallerGet(t *testing.T) {
}
// test that the provider installer can install plugins from a plugin cache dir
// into a target directory that does not exist.
// https://github.com/hashicorp/terraform/issues/20532
func TestProviderInstallerGet_cache(t *testing.T) {
server := testReleaseServer()
server.Start()
defer server.Close()
tmpDir, err := ioutil.TempDir("", "tf-plugin")
if err != nil {
t.Fatal(err)
}
cache := NewLocalPluginCache(filepath.Join(tmpDir, "cache"))
targetDir := filepath.Join(tmpDir, "non-existant-dir")
defer os.RemoveAll(tmpDir)
i := &ProviderInstaller{
Dir: targetDir,
Cache: cache,
PluginProtocolVersion: 4,
SkipVerify: true,
Ui: cli.NewMockUi(),
registry: registry.NewClient(Disco(server), nil),
OS: "mockos",
Arch: "mockarch",
}
gotMeta, err := i.Get("test", AllVersions)
if err != nil {
t.Fatal(err)
}
// we should have version 1.2.4
dest := filepath.Join(targetDir, "terraform-provider-test_v1.2.4")
wantMeta := PluginMeta{
Name: "test",
Version: VersionStr("1.2.4"),
Path: dest,
}
if !reflect.DeepEqual(gotMeta, wantMeta) {
t.Errorf("wrong result meta\ngot: %#v\nwant: %#v", gotMeta, wantMeta)
}
f, err := ioutil.ReadFile(dest)
if err != nil {
t.Fatal(err)
}
// provider should have been unzipped
if string(f) != testProviderFile {
t.Fatalf("test provider contains: %q", f)
}
}
func TestProviderInstallerPurgeUnused(t *testing.T) {
server := testReleaseServer()
defer server.Close()