github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/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_stop(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  	provider := raw.(terraform.ResourceProvisioner)
    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 TestResourceProvisioner_stopErrors(t *testing.T) {
    43  	p := new(terraform.MockResourceProvisioner)
    44  	p.StopReturnError = errors.New("foo")
    45  
    46  	// Create a mock provider
    47  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
    48  		ProvisionerFunc: testProvisionerFixed(p),
    49  	}))
    50  	defer client.Close()
    51  
    52  	// Request the provider
    53  	raw, err := client.Dispense(ProvisionerPluginName)
    54  	if err != nil {
    55  		t.Fatalf("err: %s", err)
    56  	}
    57  	provider := raw.(terraform.ResourceProvisioner)
    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 TestResourceProvisioner_apply(t *testing.T) {
    73  	// Create a mock provider
    74  	p := new(terraform.MockResourceProvisioner)
    75  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
    76  		ProvisionerFunc: testProvisionerFixed(p),
    77  	}))
    78  	defer client.Close()
    79  
    80  	// Request the provider
    81  	raw, err := client.Dispense(ProvisionerPluginName)
    82  	if err != nil {
    83  		t.Fatalf("err: %s", err)
    84  	}
    85  	provisioner := raw.(terraform.ResourceProvisioner)
    86  
    87  	// Apply
    88  	output := &terraform.MockUIOutput{}
    89  	state := &terraform.InstanceState{}
    90  	conf := &terraform.ResourceConfig{}
    91  	err = provisioner.Apply(output, state, conf)
    92  	if !p.ApplyCalled {
    93  		t.Fatal("apply should be called")
    94  	}
    95  	if !reflect.DeepEqual(p.ApplyConfig, conf) {
    96  		t.Fatalf("bad: %#v", p.ApplyConfig)
    97  	}
    98  	if err != nil {
    99  		t.Fatalf("bad: %#v", err)
   100  	}
   101  }
   102  
   103  func TestResourceProvisioner_validate(t *testing.T) {
   104  	// Create a mock provider
   105  	p := new(terraform.MockResourceProvisioner)
   106  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   107  		ProvisionerFunc: testProvisionerFixed(p),
   108  	}))
   109  	defer client.Close()
   110  
   111  	// Request the provider
   112  	raw, err := client.Dispense(ProvisionerPluginName)
   113  	if err != nil {
   114  		t.Fatalf("err: %s", err)
   115  	}
   116  	provisioner := raw.(terraform.ResourceProvisioner)
   117  
   118  	// Configure
   119  	config := &terraform.ResourceConfig{
   120  		Raw: map[string]interface{}{"foo": "bar"},
   121  	}
   122  	w, e := provisioner.Validate(config)
   123  	if !p.ValidateCalled {
   124  		t.Fatal("configure should be called")
   125  	}
   126  	if !reflect.DeepEqual(p.ValidateConfig, config) {
   127  		t.Fatalf("bad: %#v", p.ValidateConfig)
   128  	}
   129  	if w != nil {
   130  		t.Fatalf("bad: %#v", w)
   131  	}
   132  	if e != nil {
   133  		t.Fatalf("bad: %#v", e)
   134  	}
   135  }
   136  
   137  func TestResourceProvisioner_validate_errors(t *testing.T) {
   138  	// Create a mock provider
   139  	p := new(terraform.MockResourceProvisioner)
   140  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   141  		ProvisionerFunc: testProvisionerFixed(p),
   142  	}))
   143  	defer client.Close()
   144  
   145  	// Request the provider
   146  	raw, err := client.Dispense(ProvisionerPluginName)
   147  	if err != nil {
   148  		t.Fatalf("err: %s", err)
   149  	}
   150  	provisioner := raw.(terraform.ResourceProvisioner)
   151  
   152  	p.ValidateReturnErrors = []error{errors.New("foo")}
   153  
   154  	// Configure
   155  	config := &terraform.ResourceConfig{
   156  		Raw: map[string]interface{}{"foo": "bar"},
   157  	}
   158  	w, e := provisioner.Validate(config)
   159  	if !p.ValidateCalled {
   160  		t.Fatal("configure should be called")
   161  	}
   162  	if !reflect.DeepEqual(p.ValidateConfig, config) {
   163  		t.Fatalf("bad: %#v", p.ValidateConfig)
   164  	}
   165  	if w != nil {
   166  		t.Fatalf("bad: %#v", w)
   167  	}
   168  
   169  	if len(e) != 1 {
   170  		t.Fatalf("bad: %#v", e)
   171  	}
   172  	if e[0].Error() != "foo" {
   173  		t.Fatalf("bad: %#v", e)
   174  	}
   175  }
   176  
   177  func TestResourceProvisioner_validate_warns(t *testing.T) {
   178  	// Create a mock provider
   179  	p := new(terraform.MockResourceProvisioner)
   180  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   181  		ProvisionerFunc: testProvisionerFixed(p),
   182  	}))
   183  	defer client.Close()
   184  
   185  	// Request the provider
   186  	raw, err := client.Dispense(ProvisionerPluginName)
   187  	if err != nil {
   188  		t.Fatalf("err: %s", err)
   189  	}
   190  	provisioner := raw.(terraform.ResourceProvisioner)
   191  
   192  	p.ValidateReturnWarns = []string{"foo"}
   193  
   194  	// Configure
   195  	config := &terraform.ResourceConfig{
   196  		Raw: map[string]interface{}{"foo": "bar"},
   197  	}
   198  	w, e := provisioner.Validate(config)
   199  	if !p.ValidateCalled {
   200  		t.Fatal("configure should be called")
   201  	}
   202  	if !reflect.DeepEqual(p.ValidateConfig, config) {
   203  		t.Fatalf("bad: %#v", p.ValidateConfig)
   204  	}
   205  	if e != nil {
   206  		t.Fatalf("bad: %#v", e)
   207  	}
   208  
   209  	expected := []string{"foo"}
   210  	if !reflect.DeepEqual(w, expected) {
   211  		t.Fatalf("bad: %#v", w)
   212  	}
   213  }
   214  
   215  func TestResourceProvisioner_close(t *testing.T) {
   216  	// Create a mock provider
   217  	p := new(terraform.MockResourceProvisioner)
   218  	client, _ := plugin.TestPluginRPCConn(t, pluginMap(&ServeOpts{
   219  		ProvisionerFunc: testProvisionerFixed(p),
   220  	}))
   221  	defer client.Close()
   222  
   223  	// Request the provider
   224  	raw, err := client.Dispense(ProvisionerPluginName)
   225  	if err != nil {
   226  		t.Fatalf("err: %s", err)
   227  	}
   228  	provisioner := raw.(terraform.ResourceProvisioner)
   229  
   230  	pCloser, ok := raw.(terraform.ResourceProvisionerCloser)
   231  	if !ok {
   232  		t.Fatal("should be a ResourceProvisionerCloser")
   233  	}
   234  
   235  	if err := pCloser.Close(); err != nil {
   236  		t.Fatalf("failed to close provisioner: %s", err)
   237  	}
   238  
   239  	// The connection should be closed now, so if we to make a
   240  	// new call we should get an error.
   241  	o := &terraform.MockUIOutput{}
   242  	s := &terraform.InstanceState{}
   243  	c := &terraform.ResourceConfig{}
   244  	err = provisioner.Apply(o, s, c)
   245  	if err == nil {
   246  		t.Fatal("should have error")
   247  	}
   248  }