github.com/gavinw2006/hashicorp-terraform@v0.11.12-beta1/state/remote/testing.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("expected full state %q\n\ngot: %q", string(p.Data), string(data))
    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("expected empty state, got: %q", string(p.Data))
    42  	}
    43  }
    44  
    45  // Test the lock implementation for a remote.Client.
    46  // This test requires 2 client instances, in oder to have multiple remote
    47  // clients since some implementations may tie the client to the lock, or may
    48  // have reentrant locks.
    49  func TestRemoteLocks(t *testing.T, a, b Client) {
    50  	lockerA, ok := a.(state.Locker)
    51  	if !ok {
    52  		t.Fatal("client A not a state.Locker")
    53  	}
    54  
    55  	lockerB, ok := b.(state.Locker)
    56  	if !ok {
    57  		t.Fatal("client B not a state.Locker")
    58  	}
    59  
    60  	infoA := state.NewLockInfo()
    61  	infoA.Operation = "test"
    62  	infoA.Who = "clientA"
    63  
    64  	infoB := state.NewLockInfo()
    65  	infoB.Operation = "test"
    66  	infoB.Who = "clientB"
    67  
    68  	lockIDA, err := lockerA.Lock(infoA)
    69  	if err != nil {
    70  		t.Fatal("unable to get initial lock:", err)
    71  	}
    72  
    73  	_, err = lockerB.Lock(infoB)
    74  	if err == nil {
    75  		lockerA.Unlock(lockIDA)
    76  		t.Fatal("client B obtained lock while held by client A")
    77  	}
    78  
    79  	if err := lockerA.Unlock(lockIDA); err != nil {
    80  		t.Fatal("error unlocking client A", err)
    81  	}
    82  
    83  	lockIDB, err := lockerB.Lock(infoB)
    84  	if err != nil {
    85  		t.Fatal("unable to obtain lock from client B")
    86  	}
    87  
    88  	if lockIDB == lockIDA {
    89  		t.Fatalf("duplicate lock IDs: %q", lockIDB)
    90  	}
    91  
    92  	if err = lockerB.Unlock(lockIDB); err != nil {
    93  		t.Fatal("error unlocking client B:", err)
    94  	}
    95  
    96  	// TODO: Should we enforce that Unlock requires the correct ID?
    97  }