add init getPlugin test

add a mock plugin getter, and test that we can fetch requested version
of the plugins.
This commit is contained in:
James Bardin 2017-05-04 14:03:57 -04:00 committed by Martin Atkins
parent 4c32cd432a
commit 2994c6ac5d
3 changed files with 151 additions and 0 deletions

View File

@ -534,6 +534,90 @@ func TestInit_inputFalse(t *testing.T) {
} }
} }
func TestInit_getProvider(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
copy.CopyDir(testFixturePath("init-get-providers"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
getter := &mockGetProvider{
Providers: map[string][]string{
// looking for an exact version
"exact": []string{"1.2.3"},
// config requires >= 2.3.3
"greater_than": []string{"2.3.4", "2.3.3", "2.3.0"},
// config specifies
"between": []string{"3.4.5", "2.3.4", "1.2.3"},
},
}
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
},
getProvider: getter.GetProvider,
}
args := []string{}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
// check that we got the providers for our config
exactPath := filepath.Join(c.pluginDir(), getter.FileName("exact", "1.2.3"))
if _, err := os.Stat(exactPath); os.IsNotExist(err) {
t.Fatal("provider 'exact' not downloaded")
}
greaterThanPath := filepath.Join(c.pluginDir(), getter.FileName("greater_than", "2.3.4"))
if _, err := os.Stat(greaterThanPath); os.IsNotExist(err) {
t.Fatal("provider 'greater_than' not downloaded")
}
betweenPath := filepath.Join(c.pluginDir(), getter.FileName("between", "2.3.4"))
if _, err := os.Stat(betweenPath); os.IsNotExist(err) {
t.Fatal("provider 'between' not downloaded")
}
}
func TestInit_getProviderMissing(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
copy.CopyDir(testFixturePath("init-get-providers"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
getter := &mockGetProvider{
Providers: map[string][]string{
// looking for exact version 1.2.3
"exact": []string{"1.2.4"},
// config requires >= 2.3.3
"greater_than": []string{"2.3.4", "2.3.3", "2.3.0"},
// config specifies
"between": []string{"3.4.5", "2.3.4", "1.2.3"},
},
}
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
},
getProvider: getter.GetProvider,
}
args := []string{}
if code := c.Run(args); code == 0 {
t.Fatalf("expceted error, got output: \n%s", ui.OutputWriter.String())
}
if !strings.Contains(ui.ErrorWriter.String(), "no suitable version for provider") {
t.Fatalf("unexpected error output: %s", ui.ErrorWriter)
}
}
/* /*
func TestInit_remoteState(t *testing.T) { func TestInit_remoteState(t *testing.T) {
tmp, cwd := testCwd(t) tmp, cwd := testCwd(t)

56
command/plugins_test.go Normal file
View File

@ -0,0 +1,56 @@
package command
import (
"fmt"
"os"
"path/filepath"
"github.com/hashicorp/terraform/plugin/discovery"
)
// mockGetProvider providers a GetProvider method for testing automatic
// provider downloads
type mockGetProvider struct {
// A map of provider names to available versions.
// The tests expect the versions to be in order from newest to oldest.
Providers map[string][]string
}
func (m mockGetProvider) FileName(provider, version string) string {
return fmt.Sprintf("terraform-provider-%s-V%s-X4", provider, version)
}
// GetProvider will check the Providers map to see if it can find a suitable
// version, and put an empty file in the dst directory.
func (m mockGetProvider) GetProvider(dst, provider string, req discovery.Constraints) error {
versions := m.Providers[provider]
if len(versions) == 0 {
return fmt.Errorf("provider %q not found", provider)
}
err := os.MkdirAll(dst, 0755)
if err != nil {
return fmt.Errorf("error creating plugins directory: %s", err)
}
for _, v := range versions {
version, err := discovery.VersionStr(v).Parse()
if err != nil {
panic(err)
}
if req.Has(version) {
// provider filename
name := m.FileName(provider, v)
path := filepath.Join(dst, name)
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("error fetching provider: %s", err)
}
f.Close()
return nil
}
}
return fmt.Errorf("no suitable version for provider %q found with constraints %s", provider, req)
}

View File

@ -0,0 +1,11 @@
provider "exact" {
version = "1.2.3"
}
provider "greater_than" {
version = ">= 2.3.3"
}
provider "between" {
version = "> 1.0.0 , < 3.0.0"
}