github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/plugin/resource_provisioner_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 TestResourceProvisioner_impl(t *testing.T) {
    13  	var _ plugin.Plugin = new(ResourceProvisionerPlugin)
    14  	var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
    15  }
    16  
    17  func TestResourceProvisioner_apply(t *testing.T) {
    18  	// Create a mock provider
    19  	p := new(terraform.MockResourceProvisioner)
    20  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
    21  		ProvisionerFunc: testProvisionerFixed(p),
    22  	}))
    23  	defer client.Close()
    24  
    25  	// Request the provider
    26  	raw, err := client.Dispense(ProvisionerPluginName)
    27  	if err != nil {
    28  		t.Fatalf("err: %s", err)
    29  	}
    30  	provisioner := raw.(terraform.ResourceProvisioner)
    31  
    32  	// Apply
    33  	output := &terraform.MockUIOutput{}
    34  	state := &terraform.InstanceState{}
    35  	conf := &terraform.ResourceConfig{}
    36  	err = provisioner.Apply(output, state, conf)
    37  	if !p.ApplyCalled {
    38  		t.Fatal("apply should be called")
    39  	}
    40  	if !reflect.DeepEqual(p.ApplyConfig, conf) {
    41  		t.Fatalf("bad: %#v", p.ApplyConfig)
    42  	}
    43  	if err != nil {
    44  		t.Fatalf("bad: %#v", err)
    45  	}
    46  }
    47  
    48  func TestResourceProvisioner_validate(t *testing.T) {
    49  	// Create a mock provider
    50  	p := new(terraform.MockResourceProvisioner)
    51  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
    52  		ProvisionerFunc: testProvisionerFixed(p),
    53  	}))
    54  	defer client.Close()
    55  
    56  	// Request the provider
    57  	raw, err := client.Dispense(ProvisionerPluginName)
    58  	if err != nil {
    59  		t.Fatalf("err: %s", err)
    60  	}
    61  	provisioner := raw.(terraform.ResourceProvisioner)
    62  
    63  	// Configure
    64  	config := &terraform.ResourceConfig{
    65  		Raw: map[string]interface{}{"foo": "bar"},
    66  	}
    67  	w, e := provisioner.Validate(config)
    68  	if !p.ValidateCalled {
    69  		t.Fatal("configure should be called")
    70  	}
    71  	if !reflect.DeepEqual(p.ValidateConfig, config) {
    72  		t.Fatalf("bad: %#v", p.ValidateConfig)
    73  	}
    74  	if w != nil {
    75  		t.Fatalf("bad: %#v", w)
    76  	}
    77  	if e != nil {
    78  		t.Fatalf("bad: %#v", e)
    79  	}
    80  }
    81  
    82  func TestResourceProvisioner_validate_errors(t *testing.T) {
    83  	// Create a mock provider
    84  	p := new(terraform.MockResourceProvisioner)
    85  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
    86  		ProvisionerFunc: testProvisionerFixed(p),
    87  	}))
    88  	defer client.Close()
    89  
    90  	// Request the provider
    91  	raw, err := client.Dispense(ProvisionerPluginName)
    92  	if err != nil {
    93  		t.Fatalf("err: %s", err)
    94  	}
    95  	provisioner := raw.(terraform.ResourceProvisioner)
    96  
    97  	p.ValidateReturnErrors = []error{errors.New("foo")}
    98  
    99  	// Configure
   100  	config := &terraform.ResourceConfig{
   101  		Raw: map[string]interface{}{"foo": "bar"},
   102  	}
   103  	w, e := provisioner.Validate(config)
   104  	if !p.ValidateCalled {
   105  		t.Fatal("configure should be called")
   106  	}
   107  	if !reflect.DeepEqual(p.ValidateConfig, config) {
   108  		t.Fatalf("bad: %#v", p.ValidateConfig)
   109  	}
   110  	if w != nil {
   111  		t.Fatalf("bad: %#v", w)
   112  	}
   113  
   114  	if len(e) != 1 {
   115  		t.Fatalf("bad: %#v", e)
   116  	}
   117  	if e[0].Error() != "foo" {
   118  		t.Fatalf("bad: %#v", e)
   119  	}
   120  }
   121  
   122  func TestResourceProvisioner_validate_warns(t *testing.T) {
   123  	// Create a mock provider
   124  	p := new(terraform.MockResourceProvisioner)
   125  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   126  		ProvisionerFunc: testProvisionerFixed(p),
   127  	}))
   128  	defer client.Close()
   129  
   130  	// Request the provider
   131  	raw, err := client.Dispense(ProvisionerPluginName)
   132  	if err != nil {
   133  		t.Fatalf("err: %s", err)
   134  	}
   135  	provisioner := raw.(terraform.ResourceProvisioner)
   136  
   137  	p.ValidateReturnWarns = []string{"foo"}
   138  
   139  	// Configure
   140  	config := &terraform.ResourceConfig{
   141  		Raw: map[string]interface{}{"foo": "bar"},
   142  	}
   143  	w, e := provisioner.Validate(config)
   144  	if !p.ValidateCalled {
   145  		t.Fatal("configure should be called")
   146  	}
   147  	if !reflect.DeepEqual(p.ValidateConfig, config) {
   148  		t.Fatalf("bad: %#v", p.ValidateConfig)
   149  	}
   150  	if e != nil {
   151  		t.Fatalf("bad: %#v", e)
   152  	}
   153  
   154  	expected := []string{"foo"}
   155  	if !reflect.DeepEqual(w, expected) {
   156  		t.Fatalf("bad: %#v", w)
   157  	}
   158  }
   159  
   160  func TestResourceProvisioner_close(t *testing.T) {
   161  	// Create a mock provider
   162  	p := new(terraform.MockResourceProvisioner)
   163  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   164  		ProvisionerFunc: testProvisionerFixed(p),
   165  	}))
   166  	defer client.Close()
   167  
   168  	// Request the provider
   169  	raw, err := client.Dispense(ProvisionerPluginName)
   170  	if err != nil {
   171  		t.Fatalf("err: %s", err)
   172  	}
   173  	provisioner := raw.(terraform.ResourceProvisioner)
   174  
   175  	pCloser, ok := raw.(terraform.ResourceProvisionerCloser)
   176  	if !ok {
   177  		t.Fatal("should be a ResourceProvisionerCloser")
   178  	}
   179  
   180  	if err := pCloser.Close(); err != nil {
   181  		t.Fatalf("failed to close provisioner: %s", err)
   182  	}
   183  
   184  	// The connection should be closed now, so if we to make a
   185  	// new call we should get an error.
   186  	o := &terraform.MockUIOutput{}
   187  	s := &terraform.InstanceState{}
   188  	c := &terraform.ResourceConfig{}
   189  	err = provisioner.Apply(o, s, c)
   190  	if err == nil {
   191  		t.Fatal("should have error")
   192  	}
   193  }