github.com/ctrox/terraform@v0.11.12-beta1/backend/remote-state/etcdv3/backend_test.go (about) 1 package etcd 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "strings" 8 "testing" 9 "time" 10 11 etcdv3 "github.com/coreos/etcd/clientv3" 12 "github.com/hashicorp/terraform/backend" 13 ) 14 15 var ( 16 etcdv3Endpoints = strings.Split(os.Getenv("TF_ETCDV3_ENDPOINTS"), ",") 17 ) 18 19 const ( 20 keyPrefix = "tf-unit" 21 ) 22 23 func TestBackend_impl(t *testing.T) { 24 var _ backend.Backend = new(Backend) 25 } 26 27 func cleanupEtcdv3(t *testing.T) { 28 client, err := etcdv3.New(etcdv3.Config{ 29 Endpoints: etcdv3Endpoints, 30 }) 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 res, err := client.KV.Delete(context.TODO(), keyPrefix, etcdv3.WithPrefix()) 36 if err != nil { 37 t.Fatal(err) 38 } 39 t.Logf("Cleaned up %d keys.", res.Deleted) 40 } 41 42 func prepareEtcdv3(t *testing.T) { 43 skip := os.Getenv("TF_ACC") == "" && os.Getenv("TF_ETCDV3_TEST") == "" 44 if skip { 45 t.Log("etcd server tests require setting TF_ACC or TF_ETCDV3_TEST") 46 t.Skip() 47 } 48 cleanupEtcdv3(t) 49 } 50 51 func TestBackend(t *testing.T) { 52 prepareEtcdv3(t) 53 defer cleanupEtcdv3(t) 54 55 prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339)) 56 57 // Get the backend. We need two to test locking. 58 b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{ 59 "endpoints": etcdv3Endpoints, 60 "prefix": prefix, 61 }) 62 63 b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{ 64 "endpoints": etcdv3Endpoints, 65 "prefix": prefix, 66 }) 67 68 // Test 69 backend.TestBackendStates(t, b1) 70 backend.TestBackendStateLocks(t, b1, b2) 71 backend.TestBackendStateForceUnlock(t, b1, b2) 72 } 73 74 func TestBackend_lockDisabled(t *testing.T) { 75 prepareEtcdv3(t) 76 defer cleanupEtcdv3(t) 77 78 prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339)) 79 80 // Get the backend. We need two to test locking. 81 b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{ 82 "endpoints": etcdv3Endpoints, 83 "prefix": prefix, 84 "lock": false, 85 }) 86 87 b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{ 88 "endpoints": etcdv3Endpoints, 89 "prefix": prefix + "/" + "different", // Diff so locking test would fail if it was locking 90 "lock": false, 91 }) 92 93 // Test 94 backend.TestBackendStateLocks(t, b1, b2) 95 }