github.com/adamar/terraform@v0.2.2-0.20141016210445-2e703afdad0e/rpc/resource_provisioner_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestResourceProvisioner_impl(t *testing.T) {
    12  	var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
    13  }
    14  
    15  func TestResourceProvisioner_apply(t *testing.T) {
    16  	client, server := testNewClientServer(t)
    17  	defer client.Close()
    18  
    19  	p := server.ProvisionerFunc().(*terraform.MockResourceProvisioner)
    20  
    21  	provisioner, err := client.ResourceProvisioner()
    22  	if err != nil {
    23  		t.Fatalf("err: %s", err)
    24  	}
    25  
    26  	// Apply
    27  	output := &terraform.MockUIOutput{}
    28  	state := &terraform.InstanceState{}
    29  	conf := &terraform.ResourceConfig{}
    30  	err = provisioner.Apply(output, state, conf)
    31  	if !p.ApplyCalled {
    32  		t.Fatal("apply should be called")
    33  	}
    34  	if !reflect.DeepEqual(p.ApplyConfig, conf) {
    35  		t.Fatalf("bad: %#v", p.ApplyConfig)
    36  	}
    37  	if err != nil {
    38  		t.Fatalf("bad: %#v", err)
    39  	}
    40  }
    41  
    42  func TestResourceProvisioner_validate(t *testing.T) {
    43  	p := new(terraform.MockResourceProvisioner)
    44  	client, server := testClientServer(t)
    45  	name, err := Register(server, p)
    46  	if err != nil {
    47  		t.Fatalf("err: %s", err)
    48  	}
    49  	provisioner := &ResourceProvisioner{Client: client, Name: name}
    50  
    51  	// Configure
    52  	config := &terraform.ResourceConfig{
    53  		Raw: map[string]interface{}{"foo": "bar"},
    54  	}
    55  	w, e := provisioner.Validate(config)
    56  	if !p.ValidateCalled {
    57  		t.Fatal("configure should be called")
    58  	}
    59  	if !reflect.DeepEqual(p.ValidateConfig, config) {
    60  		t.Fatalf("bad: %#v", p.ValidateConfig)
    61  	}
    62  	if w != nil {
    63  		t.Fatalf("bad: %#v", w)
    64  	}
    65  	if e != nil {
    66  		t.Fatalf("bad: %#v", e)
    67  	}
    68  }
    69  
    70  func TestResourceProvisioner_validate_errors(t *testing.T) {
    71  	p := new(terraform.MockResourceProvisioner)
    72  	p.ValidateReturnErrors = []error{errors.New("foo")}
    73  
    74  	client, server := testClientServer(t)
    75  	name, err := Register(server, p)
    76  	if err != nil {
    77  		t.Fatalf("err: %s", err)
    78  	}
    79  	provisioner := &ResourceProvisioner{Client: client, Name: name}
    80  
    81  	// Configure
    82  	config := &terraform.ResourceConfig{
    83  		Raw: map[string]interface{}{"foo": "bar"},
    84  	}
    85  	w, e := provisioner.Validate(config)
    86  	if !p.ValidateCalled {
    87  		t.Fatal("configure should be called")
    88  	}
    89  	if !reflect.DeepEqual(p.ValidateConfig, config) {
    90  		t.Fatalf("bad: %#v", p.ValidateConfig)
    91  	}
    92  	if w != nil {
    93  		t.Fatalf("bad: %#v", w)
    94  	}
    95  
    96  	if len(e) != 1 {
    97  		t.Fatalf("bad: %#v", e)
    98  	}
    99  	if e[0].Error() != "foo" {
   100  		t.Fatalf("bad: %#v", e)
   101  	}
   102  }
   103  
   104  func TestResourceProvisioner_validate_warns(t *testing.T) {
   105  	p := new(terraform.MockResourceProvisioner)
   106  	p.ValidateReturnWarns = []string{"foo"}
   107  
   108  	client, server := testClientServer(t)
   109  	name, err := Register(server, p)
   110  	if err != nil {
   111  		t.Fatalf("err: %s", err)
   112  	}
   113  	provisioner := &ResourceProvisioner{Client: client, Name: name}
   114  
   115  	// Configure
   116  	config := &terraform.ResourceConfig{
   117  		Raw: map[string]interface{}{"foo": "bar"},
   118  	}
   119  	w, e := provisioner.Validate(config)
   120  	if !p.ValidateCalled {
   121  		t.Fatal("configure should be called")
   122  	}
   123  	if !reflect.DeepEqual(p.ValidateConfig, config) {
   124  		t.Fatalf("bad: %#v", p.ValidateConfig)
   125  	}
   126  	if e != nil {
   127  		t.Fatalf("bad: %#v", e)
   128  	}
   129  
   130  	expected := []string{"foo"}
   131  	if !reflect.DeepEqual(w, expected) {
   132  		t.Fatalf("bad: %#v", w)
   133  	}
   134  }