github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/backend/remote-state/consul/backend_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package consul 5 6 import ( 7 "flag" 8 "fmt" 9 "io/ioutil" 10 "os" 11 "testing" 12 "time" 13 14 "github.com/hashicorp/consul/sdk/testutil" 15 "github.com/terramate-io/tf/backend" 16 ) 17 18 func TestBackend_impl(t *testing.T) { 19 var _ backend.Backend = new(Backend) 20 } 21 22 func newConsulTestServer(t *testing.T) *testutil.TestServer { 23 if os.Getenv("TF_ACC") == "" && os.Getenv("TF_CONSUL_TEST") == "" { 24 t.Skipf("consul server tests require setting TF_ACC or TF_CONSUL_TEST") 25 } 26 27 srv, err := testutil.NewTestServerConfigT(t, func(c *testutil.TestServerConfig) { 28 c.LogLevel = "warn" 29 30 if !flag.Parsed() { 31 flag.Parse() 32 } 33 34 if !testing.Verbose() { 35 c.Stdout = ioutil.Discard 36 c.Stderr = ioutil.Discard 37 } 38 }) 39 40 if err != nil { 41 t.Fatalf("failed to create consul test server: %s", err) 42 } 43 44 srv.WaitForSerfCheck(t) 45 srv.WaitForLeader(t) 46 47 return srv 48 } 49 50 func TestBackend(t *testing.T) { 51 srv := newConsulTestServer(t) 52 53 path := fmt.Sprintf("tf-unit/%s", time.Now().String()) 54 55 // Get the backend. We need two to test locking. 56 b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 57 "address": srv.HTTPAddr, 58 "path": path, 59 })) 60 61 b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 62 "address": srv.HTTPAddr, 63 "path": path, 64 })) 65 66 // Test 67 backend.TestBackendStates(t, b1) 68 backend.TestBackendStateLocks(t, b1, b2) 69 } 70 71 func TestBackend_lockDisabled(t *testing.T) { 72 srv := newConsulTestServer(t) 73 74 path := fmt.Sprintf("tf-unit/%s", time.Now().String()) 75 76 // Get the backend. We need two to test locking. 77 b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 78 "address": srv.HTTPAddr, 79 "path": path, 80 "lock": false, 81 })) 82 83 b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 84 "address": srv.HTTPAddr, 85 "path": path + "different", // Diff so locking test would fail if it was locking 86 "lock": false, 87 })) 88 89 // Test 90 backend.TestBackendStates(t, b1) 91 backend.TestBackendStateLocks(t, b1, b2) 92 } 93 94 func TestBackend_gzip(t *testing.T) { 95 srv := newConsulTestServer(t) 96 97 // Get the backend 98 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{ 99 "address": srv.HTTPAddr, 100 "path": fmt.Sprintf("tf-unit/%s", time.Now().String()), 101 "gzip": true, 102 })) 103 104 // Test 105 backend.TestBackendStates(t, b) 106 }