github.com/ctrox/terraform@v0.11.12-beta1/state/remote/remote_test.go (about)

     1  package remote
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/state"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  // testClient is a generic function to test any client.
    12  func testClient(t *testing.T, c Client) {
    13  	var buf bytes.Buffer
    14  	s := state.TestStateInitial()
    15  	if err := terraform.WriteState(s, &buf); err != nil {
    16  		t.Fatalf("err: %s", err)
    17  	}
    18  	data := buf.Bytes()
    19  
    20  	if err := c.Put(data); err != nil {
    21  		t.Fatalf("put: %s", err)
    22  	}
    23  
    24  	p, err := c.Get()
    25  	if err != nil {
    26  		t.Fatalf("get: %s", err)
    27  	}
    28  	if !bytes.Equal(p.Data, data) {
    29  		t.Fatalf("bad: %#v", p)
    30  	}
    31  
    32  	if err := c.Delete(); err != nil {
    33  		t.Fatalf("delete: %s", err)
    34  	}
    35  
    36  	p, err = c.Get()
    37  	if err != nil {
    38  		t.Fatalf("get: %s", err)
    39  	}
    40  	if p != nil {
    41  		t.Fatalf("bad: %#v", p)
    42  	}
    43  }
    44  
    45  func TestRemoteClient_noPayload(t *testing.T) {
    46  	s := &State{
    47  		Client: nilClient{},
    48  	}
    49  	if err := s.RefreshState(); err != nil {
    50  		t.Fatal("error refreshing empty remote state")
    51  	}
    52  }
    53  
    54  // nilClient returns nil for everything
    55  type nilClient struct{}
    56  
    57  func (nilClient) Get() (*Payload, error) { return nil, nil }
    58  
    59  func (c nilClient) Put([]byte) error { return nil }
    60  
    61  func (c nilClient) Delete() error { return nil }