github.com/hugorut/terraform@v1.1.3/src/backend/remote-state/etcdv3/client_test.go (about)

     1  package etcd
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hugorut/terraform/src/backend"
    10  	"github.com/hugorut/terraform/src/states/remote"
    11  	"github.com/hugorut/terraform/src/states/statemgr"
    12  )
    13  
    14  func TestRemoteClient_impl(t *testing.T) {
    15  	var _ remote.Client = new(RemoteClient)
    16  }
    17  
    18  func TestRemoteClient(t *testing.T) {
    19  	prepareEtcdv3(t)
    20  	defer cleanupEtcdv3(t)
    21  
    22  	prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339))
    23  
    24  	// Get the backend
    25  	b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
    26  		"endpoints": stringsToInterfaces(etcdv3Endpoints),
    27  		"prefix":    prefix,
    28  	}))
    29  
    30  	// Grab the client
    31  	state, err := b.StateMgr(backend.DefaultStateName)
    32  	if err != nil {
    33  		t.Fatalf("Error: %s.", err)
    34  	}
    35  
    36  	// Test
    37  	remote.TestClient(t, state.(*remote.State).Client)
    38  }
    39  
    40  func TestEtcdv3_stateLock(t *testing.T) {
    41  	prepareEtcdv3(t)
    42  	defer cleanupEtcdv3(t)
    43  
    44  	prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339))
    45  
    46  	// Get the backend
    47  	s1, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
    48  		"endpoints": stringsToInterfaces(etcdv3Endpoints),
    49  		"prefix":    prefix,
    50  	})).StateMgr(backend.DefaultStateName)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	s2, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
    56  		"endpoints": stringsToInterfaces(etcdv3Endpoints),
    57  		"prefix":    prefix,
    58  	})).StateMgr(backend.DefaultStateName)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	remote.TestRemoteLocks(t, s1.(*remote.State).Client, s2.(*remote.State).Client)
    64  }
    65  
    66  func TestEtcdv3_destroyLock(t *testing.T) {
    67  	prepareEtcdv3(t)
    68  	defer cleanupEtcdv3(t)
    69  
    70  	prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339))
    71  
    72  	// Get the backend
    73  	b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
    74  		"endpoints": stringsToInterfaces(etcdv3Endpoints),
    75  		"prefix":    prefix,
    76  	}))
    77  
    78  	// Grab the client
    79  	s, err := b.StateMgr(backend.DefaultStateName)
    80  	if err != nil {
    81  		t.Fatalf("err: %s", err)
    82  	}
    83  
    84  	c := s.(*remote.State).Client.(*RemoteClient)
    85  
    86  	info := statemgr.NewLockInfo()
    87  	id, err := c.Lock(info)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  
    92  	if err := c.Unlock(id); err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	res, err := c.Client.KV.Get(context.TODO(), c.info.Path)
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	if res.Count != 0 {
   101  		t.Fatalf("lock key not cleaned up at: %s", string(res.Kvs[0].Key))
   102  	}
   103  }