github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/backend/remote-state/consul/backend_test.go (about)

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