github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/rpc/client_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  func TestClient_ResourceProvider(t *testing.T) {
    11  	clientConn, serverConn := testConn(t)
    12  
    13  	p := new(terraform.MockResourceProvider)
    14  	server := &Server{ProviderFunc: testProviderFixed(p)}
    15  	go server.ServeConn(serverConn)
    16  
    17  	client, err := NewClient(clientConn)
    18  	if err != nil {
    19  		t.Fatalf("err: %s", err)
    20  	}
    21  	defer client.Close()
    22  
    23  	provider, err := client.ResourceProvider()
    24  	if err != nil {
    25  		t.Fatalf("err: %s", err)
    26  	}
    27  
    28  	// Configure
    29  	config := &terraform.ResourceConfig{
    30  		Raw: map[string]interface{}{"foo": "bar"},
    31  	}
    32  	e := provider.Configure(config)
    33  	if !p.ConfigureCalled {
    34  		t.Fatal("configure should be called")
    35  	}
    36  	if !reflect.DeepEqual(p.ConfigureConfig, config) {
    37  		t.Fatalf("bad: %#v", p.ConfigureConfig)
    38  	}
    39  	if e != nil {
    40  		t.Fatalf("bad: %#v", e)
    41  	}
    42  }
    43  
    44  func TestClient_ResourceProvisioner(t *testing.T) {
    45  	clientConn, serverConn := testConn(t)
    46  
    47  	p := new(terraform.MockResourceProvisioner)
    48  	server := &Server{ProvisionerFunc: testProvisionerFixed(p)}
    49  	go server.ServeConn(serverConn)
    50  
    51  	client, err := NewClient(clientConn)
    52  	if err != nil {
    53  		t.Fatalf("err: %s", err)
    54  	}
    55  	defer client.Close()
    56  
    57  	provisioner, err := client.ResourceProvisioner()
    58  	if err != nil {
    59  		t.Fatalf("err: %s", err)
    60  	}
    61  
    62  	// Apply
    63  	output := &terraform.MockUIOutput{}
    64  	state := &terraform.InstanceState{}
    65  	conf := &terraform.ResourceConfig{}
    66  	err = provisioner.Apply(output, state, conf)
    67  	if !p.ApplyCalled {
    68  		t.Fatal("apply should be called")
    69  	}
    70  	if !reflect.DeepEqual(p.ApplyConfig, conf) {
    71  		t.Fatalf("bad: %#v", p.ApplyConfig)
    72  	}
    73  	if err != nil {
    74  		t.Fatalf("bad: %#v", err)
    75  	}
    76  }