github.com/vic3lord/terraform@v0.8.0-rc1.0.20170626102919-16c6dd2cb372/command/plugins_test.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "reflect" 9 "testing" 10 11 "github.com/hashicorp/terraform/plugin/discovery" 12 ) 13 14 func TestPluginPath(t *testing.T) { 15 td, err := ioutil.TempDir("", "tf") 16 defer os.RemoveAll(td) 17 defer testChdir(t, td)() 18 19 pluginPath := []string{"a", "b", "c"} 20 21 m := Meta{} 22 if err := m.storePluginPath(pluginPath); err != nil { 23 t.Fatal(err) 24 } 25 26 restoredPath, err := m.loadPluginPath() 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 if !reflect.DeepEqual(pluginPath, restoredPath) { 32 t.Fatalf("expected plugin path %#v, got %#v", pluginPath, restoredPath) 33 } 34 } 35 36 // mockProviderInstaller is a discovery.PluginInstaller implementation that 37 // is a mock for discovery.ProviderInstaller. 38 type mockProviderInstaller struct { 39 // A map of provider names to available versions. 40 // The tests expect the versions to be in order from newest to oldest. 41 Providers map[string][]string 42 43 Dir string 44 PurgeUnusedCalled bool 45 } 46 47 func (i *mockProviderInstaller) FileName(provider, version string) string { 48 return fmt.Sprintf("terraform-provider-%s_v%s_x4", provider, version) 49 } 50 51 func (i *mockProviderInstaller) Get(provider string, req discovery.Constraints) (discovery.PluginMeta, error) { 52 noMeta := discovery.PluginMeta{} 53 versions := i.Providers[provider] 54 if len(versions) == 0 { 55 return noMeta, fmt.Errorf("provider %q not found", provider) 56 } 57 58 err := os.MkdirAll(i.Dir, 0755) 59 if err != nil { 60 return noMeta, fmt.Errorf("error creating plugins directory: %s", err) 61 } 62 63 for _, v := range versions { 64 version, err := discovery.VersionStr(v).Parse() 65 if err != nil { 66 panic(err) 67 } 68 69 if req.Allows(version) { 70 // provider filename 71 name := i.FileName(provider, v) 72 path := filepath.Join(i.Dir, name) 73 f, err := os.Create(path) 74 if err != nil { 75 return noMeta, fmt.Errorf("error fetching provider: %s", err) 76 } 77 f.Close() 78 return discovery.PluginMeta{ 79 Name: provider, 80 Version: discovery.VersionStr(v), 81 Path: path, 82 }, nil 83 } 84 } 85 86 return noMeta, fmt.Errorf("no suitable version for provider %q found with constraints %s", provider, req) 87 } 88 89 func (i *mockProviderInstaller) PurgeUnused(map[string]discovery.PluginMeta) (discovery.PluginMetaSet, error) { 90 i.PurgeUnusedCalled = true 91 ret := make(discovery.PluginMetaSet) 92 ret.Add(discovery.PluginMeta{ 93 Name: "test", 94 Version: "0.0.0", 95 Path: "mock-test", 96 }) 97 return ret, nil 98 } 99 100 type callbackPluginInstaller func(provider string, req discovery.Constraints) (discovery.PluginMeta, error) 101 102 func (cb callbackPluginInstaller) Get(provider string, req discovery.Constraints) (discovery.PluginMeta, error) { 103 return cb(provider, req) 104 } 105 106 func (cb callbackPluginInstaller) PurgeUnused(map[string]discovery.PluginMeta) (discovery.PluginMetaSet, error) { 107 // does nothing 108 return make(discovery.PluginMetaSet), nil 109 }