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