github.com/kevholditch/terraform@v0.9.7-0.20170613192930-9706042ddd51/command/plugins_test.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/hashicorp/terraform/plugin/discovery"
     9  )
    10  
    11  // mockProviderInstaller is a discovery.PluginInstaller implementation that
    12  // is a mock for discovery.ProviderInstaller.
    13  type mockProviderInstaller struct {
    14  	// A map of provider names to available versions.
    15  	// The tests expect the versions to be in order from newest to oldest.
    16  	Providers map[string][]string
    17  
    18  	Dir               string
    19  	PurgeUnusedCalled bool
    20  }
    21  
    22  func (i *mockProviderInstaller) FileName(provider, version string) string {
    23  	return fmt.Sprintf("terraform-provider-%s_v%s_x4", provider, version)
    24  }
    25  
    26  func (i *mockProviderInstaller) Get(provider string, req discovery.Constraints) (discovery.PluginMeta, error) {
    27  	noMeta := discovery.PluginMeta{}
    28  	versions := i.Providers[provider]
    29  	if len(versions) == 0 {
    30  		return noMeta, fmt.Errorf("provider %q not found", provider)
    31  	}
    32  
    33  	err := os.MkdirAll(i.Dir, 0755)
    34  	if err != nil {
    35  		return noMeta, fmt.Errorf("error creating plugins directory: %s", err)
    36  	}
    37  
    38  	for _, v := range versions {
    39  		version, err := discovery.VersionStr(v).Parse()
    40  		if err != nil {
    41  			panic(err)
    42  		}
    43  
    44  		if req.Allows(version) {
    45  			// provider filename
    46  			name := i.FileName(provider, v)
    47  			path := filepath.Join(i.Dir, name)
    48  			f, err := os.Create(path)
    49  			if err != nil {
    50  				return noMeta, fmt.Errorf("error fetching provider: %s", err)
    51  			}
    52  			f.Close()
    53  			return discovery.PluginMeta{
    54  				Name:    provider,
    55  				Version: discovery.VersionStr(v),
    56  				Path:    path,
    57  			}, nil
    58  		}
    59  	}
    60  
    61  	return noMeta, fmt.Errorf("no suitable version for provider %q found with constraints %s", provider, req)
    62  }
    63  
    64  func (i *mockProviderInstaller) PurgeUnused(map[string]discovery.PluginMeta) (discovery.PluginMetaSet, error) {
    65  	i.PurgeUnusedCalled = true
    66  	ret := make(discovery.PluginMetaSet)
    67  	ret.Add(discovery.PluginMeta{
    68  		Name:    "test",
    69  		Version: "0.0.0",
    70  		Path:    "mock-test",
    71  	})
    72  	return ret, nil
    73  }
    74  
    75  type callbackPluginInstaller func(provider string, req discovery.Constraints) (discovery.PluginMeta, error)
    76  
    77  func (cb callbackPluginInstaller) Get(provider string, req discovery.Constraints) (discovery.PluginMeta, error) {
    78  	return cb(provider, req)
    79  }
    80  
    81  func (cb callbackPluginInstaller) PurgeUnused(map[string]discovery.PluginMeta) (discovery.PluginMetaSet, error) {
    82  	// does nothing
    83  	return make(discovery.PluginMetaSet), nil
    84  }