github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/plugin/resource_provider_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/go-plugin"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestResourceProvider_impl(t *testing.T) {
    13  	var _ plugin.Plugin = new(ResourceProviderPlugin)
    14  	var _ terraform.ResourceProvider = new(ResourceProvider)
    15  }
    16  
    17  func TestResourceProvider_stop(t *testing.T) {
    18  	// Create a mock provider
    19  	p := new(terraform.MockResourceProvider)
    20  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
    21  		ProviderFunc: testProviderFixed(p),
    22  	}))
    23  	defer client.Close()
    24  
    25  	// Request the provider
    26  	raw, err := client.Dispense(ProviderPluginName)
    27  	if err != nil {
    28  		t.Fatalf("err: %s", err)
    29  	}
    30  	provider := raw.(terraform.ResourceProvider)
    31  
    32  	// Stop
    33  	e := provider.Stop()
    34  	if !p.StopCalled {
    35  		t.Fatal("stop should be called")
    36  	}
    37  	if e != nil {
    38  		t.Fatalf("bad: %#v", e)
    39  	}
    40  }
    41  
    42  func TestResourceProvider_stopErrors(t *testing.T) {
    43  	p := new(terraform.MockResourceProvider)
    44  	p.StopReturnError = errors.New("foo")
    45  
    46  	// Create a mock provider
    47  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
    48  		ProviderFunc: testProviderFixed(p),
    49  	}))
    50  	defer client.Close()
    51  
    52  	// Request the provider
    53  	raw, err := client.Dispense(ProviderPluginName)
    54  	if err != nil {
    55  		t.Fatalf("err: %s", err)
    56  	}
    57  	provider := raw.(terraform.ResourceProvider)
    58  
    59  	// Stop
    60  	e := provider.Stop()
    61  	if !p.StopCalled {
    62  		t.Fatal("stop should be called")
    63  	}
    64  	if e == nil {
    65  		t.Fatal("should have error")
    66  	}
    67  	if e.Error() != "foo" {
    68  		t.Fatalf("bad: %s", e)
    69  	}
    70  }
    71  
    72  func TestResourceProvider_input(t *testing.T) {
    73  	// Create a mock provider
    74  	p := new(terraform.MockResourceProvider)
    75  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
    76  		ProviderFunc: testProviderFixed(p),
    77  	}))
    78  	defer client.Close()
    79  
    80  	// Request the provider
    81  	raw, err := client.Dispense(ProviderPluginName)
    82  	if err != nil {
    83  		t.Fatalf("err: %s", err)
    84  	}
    85  	provider := raw.(terraform.ResourceProvider)
    86  
    87  	input := new(terraform.MockUIInput)
    88  
    89  	expected := &terraform.ResourceConfig{
    90  		Raw: map[string]interface{}{"bar": "baz"},
    91  	}
    92  	p.InputReturnConfig = expected
    93  
    94  	// Input
    95  	config := &terraform.ResourceConfig{
    96  		Raw: map[string]interface{}{"foo": "bar"},
    97  	}
    98  	actual, err := provider.Input(input, config)
    99  	if !p.InputCalled {
   100  		t.Fatal("input should be called")
   101  	}
   102  	if !reflect.DeepEqual(p.InputConfig, config) {
   103  		t.Fatalf("bad: %#v", p.InputConfig)
   104  	}
   105  	if err != nil {
   106  		t.Fatalf("bad: %#v", err)
   107  	}
   108  
   109  	if !reflect.DeepEqual(actual, expected) {
   110  		t.Fatalf("bad: %#v", actual)
   111  	}
   112  }
   113  
   114  func TestResourceProvider_configure(t *testing.T) {
   115  	// Create a mock provider
   116  	p := new(terraform.MockResourceProvider)
   117  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   118  		ProviderFunc: testProviderFixed(p),
   119  	}))
   120  	defer client.Close()
   121  
   122  	// Request the provider
   123  	raw, err := client.Dispense(ProviderPluginName)
   124  	if err != nil {
   125  		t.Fatalf("err: %s", err)
   126  	}
   127  	provider := raw.(terraform.ResourceProvider)
   128  
   129  	// Configure
   130  	config := &terraform.ResourceConfig{
   131  		Raw: map[string]interface{}{"foo": "bar"},
   132  	}
   133  	e := provider.Configure(config)
   134  	if !p.ConfigureCalled {
   135  		t.Fatal("configure should be called")
   136  	}
   137  	if !reflect.DeepEqual(p.ConfigureConfig, config) {
   138  		t.Fatalf("bad: %#v", p.ConfigureConfig)
   139  	}
   140  	if e != nil {
   141  		t.Fatalf("bad: %#v", e)
   142  	}
   143  }
   144  
   145  func TestResourceProvider_configure_errors(t *testing.T) {
   146  	p := new(terraform.MockResourceProvider)
   147  	p.ConfigureReturnError = errors.New("foo")
   148  
   149  	// Create a mock provider
   150  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   151  		ProviderFunc: testProviderFixed(p),
   152  	}))
   153  	defer client.Close()
   154  
   155  	// Request the provider
   156  	raw, err := client.Dispense(ProviderPluginName)
   157  	if err != nil {
   158  		t.Fatalf("err: %s", err)
   159  	}
   160  	provider := raw.(terraform.ResourceProvider)
   161  
   162  	// Configure
   163  	config := &terraform.ResourceConfig{
   164  		Raw: map[string]interface{}{"foo": "bar"},
   165  	}
   166  	e := provider.Configure(config)
   167  	if !p.ConfigureCalled {
   168  		t.Fatal("configure should be called")
   169  	}
   170  	if !reflect.DeepEqual(p.ConfigureConfig, config) {
   171  		t.Fatalf("bad: %#v", p.ConfigureConfig)
   172  	}
   173  	if e == nil {
   174  		t.Fatal("should have error")
   175  	}
   176  	if e.Error() != "foo" {
   177  		t.Fatalf("bad: %s", e)
   178  	}
   179  }
   180  
   181  func TestResourceProvider_configure_warnings(t *testing.T) {
   182  	p := new(terraform.MockResourceProvider)
   183  
   184  	// Create a mock provider
   185  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   186  		ProviderFunc: testProviderFixed(p),
   187  	}))
   188  	defer client.Close()
   189  
   190  	// Request the provider
   191  	raw, err := client.Dispense(ProviderPluginName)
   192  	if err != nil {
   193  		t.Fatalf("err: %s", err)
   194  	}
   195  	provider := raw.(terraform.ResourceProvider)
   196  
   197  	// Configure
   198  	config := &terraform.ResourceConfig{
   199  		Raw: map[string]interface{}{"foo": "bar"},
   200  	}
   201  	e := provider.Configure(config)
   202  	if !p.ConfigureCalled {
   203  		t.Fatal("configure should be called")
   204  	}
   205  	if !reflect.DeepEqual(p.ConfigureConfig, config) {
   206  		t.Fatalf("bad: %#v", p.ConfigureConfig)
   207  	}
   208  	if e != nil {
   209  		t.Fatalf("bad: %#v", e)
   210  	}
   211  }
   212  
   213  func TestResourceProvider_apply(t *testing.T) {
   214  	p := new(terraform.MockResourceProvider)
   215  
   216  	// Create a mock provider
   217  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   218  		ProviderFunc: testProviderFixed(p),
   219  	}))
   220  	defer client.Close()
   221  
   222  	// Request the provider
   223  	raw, err := client.Dispense(ProviderPluginName)
   224  	if err != nil {
   225  		t.Fatalf("err: %s", err)
   226  	}
   227  	provider := raw.(terraform.ResourceProvider)
   228  
   229  	p.ApplyReturn = &terraform.InstanceState{
   230  		ID: "bob",
   231  	}
   232  
   233  	// Apply
   234  	info := &terraform.InstanceInfo{}
   235  	state := &terraform.InstanceState{}
   236  	diff := &terraform.InstanceDiff{}
   237  	newState, err := provider.Apply(info, state, diff)
   238  	if !p.ApplyCalled {
   239  		t.Fatal("apply should be called")
   240  	}
   241  	if !reflect.DeepEqual(p.ApplyDiff, diff) {
   242  		t.Fatalf("bad: %#v", p.ApplyDiff)
   243  	}
   244  	if err != nil {
   245  		t.Fatalf("bad: %#v", err)
   246  	}
   247  	if !reflect.DeepEqual(p.ApplyReturn, newState) {
   248  		t.Fatalf("bad: %#v", newState)
   249  	}
   250  }
   251  
   252  func TestResourceProvider_diff(t *testing.T) {
   253  	p := new(terraform.MockResourceProvider)
   254  
   255  	// Create a mock provider
   256  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   257  		ProviderFunc: testProviderFixed(p),
   258  	}))
   259  	defer client.Close()
   260  
   261  	// Request the provider
   262  	raw, err := client.Dispense(ProviderPluginName)
   263  	if err != nil {
   264  		t.Fatalf("err: %s", err)
   265  	}
   266  	provider := raw.(terraform.ResourceProvider)
   267  
   268  	p.DiffReturn = &terraform.InstanceDiff{
   269  		Attributes: map[string]*terraform.ResourceAttrDiff{
   270  			"foo": &terraform.ResourceAttrDiff{
   271  				Old: "",
   272  				New: "bar",
   273  			},
   274  		},
   275  	}
   276  
   277  	// Diff
   278  	info := &terraform.InstanceInfo{}
   279  	state := &terraform.InstanceState{}
   280  	config := &terraform.ResourceConfig{
   281  		Raw: map[string]interface{}{"foo": "bar"},
   282  	}
   283  	diff, err := provider.Diff(info, state, config)
   284  	if !p.DiffCalled {
   285  		t.Fatal("diff should be called")
   286  	}
   287  	if !reflect.DeepEqual(p.DiffDesired, config) {
   288  		t.Fatalf("bad: %#v", p.DiffDesired)
   289  	}
   290  	if err != nil {
   291  		t.Fatalf("bad: %#v", err)
   292  	}
   293  	if !reflect.DeepEqual(p.DiffReturn, diff) {
   294  		t.Fatalf("bad: %#v", diff)
   295  	}
   296  }
   297  
   298  func TestResourceProvider_diff_error(t *testing.T) {
   299  	p := new(terraform.MockResourceProvider)
   300  
   301  	// Create a mock provider
   302  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   303  		ProviderFunc: testProviderFixed(p),
   304  	}))
   305  	defer client.Close()
   306  
   307  	// Request the provider
   308  	raw, err := client.Dispense(ProviderPluginName)
   309  	if err != nil {
   310  		t.Fatalf("err: %s", err)
   311  	}
   312  	provider := raw.(terraform.ResourceProvider)
   313  
   314  	p.DiffReturnError = errors.New("foo")
   315  
   316  	// Diff
   317  	info := &terraform.InstanceInfo{}
   318  	state := &terraform.InstanceState{}
   319  	config := &terraform.ResourceConfig{
   320  		Raw: map[string]interface{}{"foo": "bar"},
   321  	}
   322  	diff, err := provider.Diff(info, state, config)
   323  	if !p.DiffCalled {
   324  		t.Fatal("diff should be called")
   325  	}
   326  	if !reflect.DeepEqual(p.DiffDesired, config) {
   327  		t.Fatalf("bad: %#v", p.DiffDesired)
   328  	}
   329  	if err == nil {
   330  		t.Fatal("should have error")
   331  	}
   332  	if diff != nil {
   333  		t.Fatal("should not have diff")
   334  	}
   335  }
   336  
   337  func TestResourceProvider_refresh(t *testing.T) {
   338  	p := new(terraform.MockResourceProvider)
   339  
   340  	// Create a mock provider
   341  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   342  		ProviderFunc: testProviderFixed(p),
   343  	}))
   344  	defer client.Close()
   345  
   346  	// Request the provider
   347  	raw, err := client.Dispense(ProviderPluginName)
   348  	if err != nil {
   349  		t.Fatalf("err: %s", err)
   350  	}
   351  	provider := raw.(terraform.ResourceProvider)
   352  
   353  	p.RefreshReturn = &terraform.InstanceState{
   354  		ID: "bob",
   355  	}
   356  
   357  	// Refresh
   358  	info := &terraform.InstanceInfo{}
   359  	state := &terraform.InstanceState{}
   360  	newState, err := provider.Refresh(info, state)
   361  	if !p.RefreshCalled {
   362  		t.Fatal("refresh should be called")
   363  	}
   364  	if !reflect.DeepEqual(p.RefreshState, state) {
   365  		t.Fatalf("bad: %#v", p.RefreshState)
   366  	}
   367  	if err != nil {
   368  		t.Fatalf("bad: %#v", err)
   369  	}
   370  	if !reflect.DeepEqual(p.RefreshReturn, newState) {
   371  		t.Fatalf("bad: %#v", newState)
   372  	}
   373  }
   374  
   375  func TestResourceProvider_importState(t *testing.T) {
   376  	p := new(terraform.MockResourceProvider)
   377  
   378  	// Create a mock provider
   379  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   380  		ProviderFunc: testProviderFixed(p),
   381  	}))
   382  	defer client.Close()
   383  
   384  	// Request the provider
   385  	raw, err := client.Dispense(ProviderPluginName)
   386  	if err != nil {
   387  		t.Fatalf("err: %s", err)
   388  	}
   389  	provider := raw.(terraform.ResourceProvider)
   390  
   391  	p.ImportStateReturn = []*terraform.InstanceState{
   392  		&terraform.InstanceState{
   393  			ID: "bob",
   394  		},
   395  	}
   396  
   397  	// ImportState
   398  	info := &terraform.InstanceInfo{}
   399  	states, err := provider.ImportState(info, "foo")
   400  	if !p.ImportStateCalled {
   401  		t.Fatal("ImportState should be called")
   402  	}
   403  	if !reflect.DeepEqual(p.ImportStateInfo, info) {
   404  		t.Fatalf("bad: %#v", p.ImportStateInfo)
   405  	}
   406  	if err != nil {
   407  		t.Fatalf("bad: %#v", err)
   408  	}
   409  	if !reflect.DeepEqual(p.ImportStateReturn, states) {
   410  		t.Fatalf("bad: %#v", states)
   411  	}
   412  }
   413  
   414  func TestResourceProvider_resources(t *testing.T) {
   415  	p := new(terraform.MockResourceProvider)
   416  
   417  	// Create a mock provider
   418  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   419  		ProviderFunc: testProviderFixed(p),
   420  	}))
   421  	defer client.Close()
   422  
   423  	// Request the provider
   424  	raw, err := client.Dispense(ProviderPluginName)
   425  	if err != nil {
   426  		t.Fatalf("err: %s", err)
   427  	}
   428  	provider := raw.(terraform.ResourceProvider)
   429  
   430  	expected := []terraform.ResourceType{
   431  		terraform.ResourceType{Name: "foo"},
   432  		terraform.ResourceType{Name: "bar", Importable: true},
   433  	}
   434  
   435  	p.ResourcesReturn = expected
   436  
   437  	// Resources
   438  	result := provider.Resources()
   439  	if !p.ResourcesCalled {
   440  		t.Fatal("resources should be called")
   441  	}
   442  	if !reflect.DeepEqual(result, expected) {
   443  		t.Fatalf("bad: %#v", result)
   444  	}
   445  }
   446  
   447  func TestResourceProvider_readdataapply(t *testing.T) {
   448  	p := new(terraform.MockResourceProvider)
   449  
   450  	// Create a mock provider
   451  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   452  		ProviderFunc: testProviderFixed(p),
   453  	}))
   454  	defer client.Close()
   455  
   456  	// Request the provider
   457  	raw, err := client.Dispense(ProviderPluginName)
   458  	if err != nil {
   459  		t.Fatalf("err: %s", err)
   460  	}
   461  	provider := raw.(terraform.ResourceProvider)
   462  
   463  	p.ReadDataApplyReturn = &terraform.InstanceState{
   464  		ID: "bob",
   465  	}
   466  
   467  	// ReadDataApply
   468  	info := &terraform.InstanceInfo{}
   469  	diff := &terraform.InstanceDiff{}
   470  	newState, err := provider.ReadDataApply(info, diff)
   471  	if !p.ReadDataApplyCalled {
   472  		t.Fatal("ReadDataApply should be called")
   473  	}
   474  	if !reflect.DeepEqual(p.ReadDataApplyDiff, diff) {
   475  		t.Fatalf("bad: %#v", p.ReadDataApplyDiff)
   476  	}
   477  	if err != nil {
   478  		t.Fatalf("bad: %#v", err)
   479  	}
   480  	if !reflect.DeepEqual(p.ReadDataApplyReturn, newState) {
   481  		t.Fatalf("bad: %#v", newState)
   482  	}
   483  }
   484  
   485  func TestResourceProvider_datasources(t *testing.T) {
   486  	p := new(terraform.MockResourceProvider)
   487  
   488  	// Create a mock provider
   489  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   490  		ProviderFunc: testProviderFixed(p),
   491  	}))
   492  	defer client.Close()
   493  
   494  	// Request the provider
   495  	raw, err := client.Dispense(ProviderPluginName)
   496  	if err != nil {
   497  		t.Fatalf("err: %s", err)
   498  	}
   499  	provider := raw.(terraform.ResourceProvider)
   500  
   501  	expected := []terraform.DataSource{
   502  		{Name: "foo"},
   503  		{Name: "bar"},
   504  	}
   505  
   506  	p.DataSourcesReturn = expected
   507  
   508  	// DataSources
   509  	result := provider.DataSources()
   510  	if !p.DataSourcesCalled {
   511  		t.Fatal("DataSources should be called")
   512  	}
   513  	if !reflect.DeepEqual(result, expected) {
   514  		t.Fatalf("bad: %#v", result)
   515  	}
   516  }
   517  
   518  func TestResourceProvider_validate(t *testing.T) {
   519  	p := new(terraform.MockResourceProvider)
   520  
   521  	// Create a mock provider
   522  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   523  		ProviderFunc: testProviderFixed(p),
   524  	}))
   525  	defer client.Close()
   526  
   527  	// Request the provider
   528  	raw, err := client.Dispense(ProviderPluginName)
   529  	if err != nil {
   530  		t.Fatalf("err: %s", err)
   531  	}
   532  	provider := raw.(terraform.ResourceProvider)
   533  
   534  	// Configure
   535  	config := &terraform.ResourceConfig{
   536  		Raw: map[string]interface{}{"foo": "bar"},
   537  	}
   538  	w, e := provider.Validate(config)
   539  	if !p.ValidateCalled {
   540  		t.Fatal("configure should be called")
   541  	}
   542  	if !reflect.DeepEqual(p.ValidateConfig, config) {
   543  		t.Fatalf("bad: %#v", p.ValidateConfig)
   544  	}
   545  	if w != nil {
   546  		t.Fatalf("bad: %#v", w)
   547  	}
   548  	if e != nil {
   549  		t.Fatalf("bad: %#v", e)
   550  	}
   551  }
   552  
   553  func TestResourceProvider_validate_errors(t *testing.T) {
   554  	p := new(terraform.MockResourceProvider)
   555  
   556  	// Create a mock provider
   557  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   558  		ProviderFunc: testProviderFixed(p),
   559  	}))
   560  	defer client.Close()
   561  
   562  	// Request the provider
   563  	raw, err := client.Dispense(ProviderPluginName)
   564  	if err != nil {
   565  		t.Fatalf("err: %s", err)
   566  	}
   567  	provider := raw.(terraform.ResourceProvider)
   568  
   569  	p.ValidateReturnErrors = []error{errors.New("foo")}
   570  
   571  	// Configure
   572  	config := &terraform.ResourceConfig{
   573  		Raw: map[string]interface{}{"foo": "bar"},
   574  	}
   575  	w, e := provider.Validate(config)
   576  	if !p.ValidateCalled {
   577  		t.Fatal("configure should be called")
   578  	}
   579  	if !reflect.DeepEqual(p.ValidateConfig, config) {
   580  		t.Fatalf("bad: %#v", p.ValidateConfig)
   581  	}
   582  	if w != nil {
   583  		t.Fatalf("bad: %#v", w)
   584  	}
   585  
   586  	if len(e) != 1 {
   587  		t.Fatalf("bad: %#v", e)
   588  	}
   589  	if e[0].Error() != "foo" {
   590  		t.Fatalf("bad: %#v", e)
   591  	}
   592  }
   593  
   594  func TestResourceProvider_validate_warns(t *testing.T) {
   595  	p := new(terraform.MockResourceProvider)
   596  
   597  	// Create a mock provider
   598  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   599  		ProviderFunc: testProviderFixed(p),
   600  	}))
   601  	defer client.Close()
   602  
   603  	// Request the provider
   604  	raw, err := client.Dispense(ProviderPluginName)
   605  	if err != nil {
   606  		t.Fatalf("err: %s", err)
   607  	}
   608  	provider := raw.(terraform.ResourceProvider)
   609  
   610  	p.ValidateReturnWarns = []string{"foo"}
   611  
   612  	// Configure
   613  	config := &terraform.ResourceConfig{
   614  		Raw: map[string]interface{}{"foo": "bar"},
   615  	}
   616  	w, e := provider.Validate(config)
   617  	if !p.ValidateCalled {
   618  		t.Fatal("configure should be called")
   619  	}
   620  	if !reflect.DeepEqual(p.ValidateConfig, config) {
   621  		t.Fatalf("bad: %#v", p.ValidateConfig)
   622  	}
   623  	if e != nil {
   624  		t.Fatalf("bad: %#v", e)
   625  	}
   626  
   627  	expected := []string{"foo"}
   628  	if !reflect.DeepEqual(w, expected) {
   629  		t.Fatalf("bad: %#v", w)
   630  	}
   631  }
   632  
   633  func TestResourceProvider_validateResource(t *testing.T) {
   634  	p := new(terraform.MockResourceProvider)
   635  
   636  	// Create a mock provider
   637  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   638  		ProviderFunc: testProviderFixed(p),
   639  	}))
   640  	defer client.Close()
   641  
   642  	// Request the provider
   643  	raw, err := client.Dispense(ProviderPluginName)
   644  	if err != nil {
   645  		t.Fatalf("err: %s", err)
   646  	}
   647  	provider := raw.(terraform.ResourceProvider)
   648  
   649  	// Configure
   650  	config := &terraform.ResourceConfig{
   651  		Raw: map[string]interface{}{"foo": "bar"},
   652  	}
   653  	w, e := provider.ValidateResource("foo", config)
   654  	if !p.ValidateResourceCalled {
   655  		t.Fatal("configure should be called")
   656  	}
   657  	if p.ValidateResourceType != "foo" {
   658  		t.Fatalf("bad: %#v", p.ValidateResourceType)
   659  	}
   660  	if !reflect.DeepEqual(p.ValidateResourceConfig, config) {
   661  		t.Fatalf("bad: %#v", p.ValidateResourceConfig)
   662  	}
   663  	if w != nil {
   664  		t.Fatalf("bad: %#v", w)
   665  	}
   666  	if e != nil {
   667  		t.Fatalf("bad: %#v", e)
   668  	}
   669  }
   670  
   671  func TestResourceProvider_validateResource_errors(t *testing.T) {
   672  	p := new(terraform.MockResourceProvider)
   673  
   674  	// Create a mock provider
   675  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   676  		ProviderFunc: testProviderFixed(p),
   677  	}))
   678  	defer client.Close()
   679  
   680  	// Request the provider
   681  	raw, err := client.Dispense(ProviderPluginName)
   682  	if err != nil {
   683  		t.Fatalf("err: %s", err)
   684  	}
   685  	provider := raw.(terraform.ResourceProvider)
   686  
   687  	p.ValidateResourceReturnErrors = []error{errors.New("foo")}
   688  
   689  	// Configure
   690  	config := &terraform.ResourceConfig{
   691  		Raw: map[string]interface{}{"foo": "bar"},
   692  	}
   693  	w, e := provider.ValidateResource("foo", config)
   694  	if !p.ValidateResourceCalled {
   695  		t.Fatal("configure should be called")
   696  	}
   697  	if p.ValidateResourceType != "foo" {
   698  		t.Fatalf("bad: %#v", p.ValidateResourceType)
   699  	}
   700  	if !reflect.DeepEqual(p.ValidateResourceConfig, config) {
   701  		t.Fatalf("bad: %#v", p.ValidateResourceConfig)
   702  	}
   703  	if w != nil {
   704  		t.Fatalf("bad: %#v", w)
   705  	}
   706  
   707  	if len(e) != 1 {
   708  		t.Fatalf("bad: %#v", e)
   709  	}
   710  	if e[0].Error() != "foo" {
   711  		t.Fatalf("bad: %#v", e)
   712  	}
   713  }
   714  
   715  func TestResourceProvider_validateResource_warns(t *testing.T) {
   716  	p := new(terraform.MockResourceProvider)
   717  
   718  	// Create a mock provider
   719  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   720  		ProviderFunc: testProviderFixed(p),
   721  	}))
   722  	defer client.Close()
   723  
   724  	// Request the provider
   725  	raw, err := client.Dispense(ProviderPluginName)
   726  	if err != nil {
   727  		t.Fatalf("err: %s", err)
   728  	}
   729  	provider := raw.(terraform.ResourceProvider)
   730  
   731  	p.ValidateResourceReturnWarns = []string{"foo"}
   732  
   733  	// Configure
   734  	config := &terraform.ResourceConfig{
   735  		Raw: map[string]interface{}{"foo": "bar"},
   736  	}
   737  	w, e := provider.ValidateResource("foo", config)
   738  	if !p.ValidateResourceCalled {
   739  		t.Fatal("configure should be called")
   740  	}
   741  	if p.ValidateResourceType != "foo" {
   742  		t.Fatalf("bad: %#v", p.ValidateResourceType)
   743  	}
   744  	if !reflect.DeepEqual(p.ValidateResourceConfig, config) {
   745  		t.Fatalf("bad: %#v", p.ValidateResourceConfig)
   746  	}
   747  	if e != nil {
   748  		t.Fatalf("bad: %#v", e)
   749  	}
   750  
   751  	expected := []string{"foo"}
   752  	if !reflect.DeepEqual(w, expected) {
   753  		t.Fatalf("bad: %#v", w)
   754  	}
   755  }
   756  
   757  func TestResourceProvider_validateDataSource(t *testing.T) {
   758  	p := new(terraform.MockResourceProvider)
   759  
   760  	// Create a mock provider
   761  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   762  		ProviderFunc: testProviderFixed(p),
   763  	}))
   764  	defer client.Close()
   765  
   766  	// Request the provider
   767  	raw, err := client.Dispense(ProviderPluginName)
   768  	if err != nil {
   769  		t.Fatalf("err: %s", err)
   770  	}
   771  	provider := raw.(terraform.ResourceProvider)
   772  
   773  	// Configure
   774  	config := &terraform.ResourceConfig{
   775  		Raw: map[string]interface{}{"foo": "bar"},
   776  	}
   777  	w, e := provider.ValidateDataSource("foo", config)
   778  	if !p.ValidateDataSourceCalled {
   779  		t.Fatal("configure should be called")
   780  	}
   781  	if p.ValidateDataSourceType != "foo" {
   782  		t.Fatalf("bad: %#v", p.ValidateDataSourceType)
   783  	}
   784  	if !reflect.DeepEqual(p.ValidateDataSourceConfig, config) {
   785  		t.Fatalf("bad: %#v", p.ValidateDataSourceConfig)
   786  	}
   787  	if w != nil {
   788  		t.Fatalf("bad: %#v", w)
   789  	}
   790  	if e != nil {
   791  		t.Fatalf("bad: %#v", e)
   792  	}
   793  }
   794  
   795  func TestResourceProvider_close(t *testing.T) {
   796  	p := new(terraform.MockResourceProvider)
   797  
   798  	// Create a mock provider
   799  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   800  		ProviderFunc: testProviderFixed(p),
   801  	}))
   802  	defer client.Close()
   803  
   804  	// Request the provider
   805  	raw, err := client.Dispense(ProviderPluginName)
   806  	if err != nil {
   807  		t.Fatalf("err: %s", err)
   808  	}
   809  	provider := raw.(terraform.ResourceProvider)
   810  
   811  	var iface interface{} = provider
   812  	pCloser, ok := iface.(terraform.ResourceProviderCloser)
   813  	if !ok {
   814  		t.Fatal("should be a ResourceProviderCloser")
   815  	}
   816  
   817  	if err := pCloser.Close(); err != nil {
   818  		t.Fatalf("failed to close provider: %s", err)
   819  	}
   820  
   821  	// The connection should be closed now, so if we to make a
   822  	// new call we should get an error.
   823  	err = provider.Configure(&terraform.ResourceConfig{})
   824  	if err == nil {
   825  		t.Fatal("should have error")
   826  	}
   827  }