github.com/Kevinklinger/open_terraform@v0.11.12-beta1/backend/remote-state/consul/backend_test.go (about)

     1  package consul
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/hashicorp/consul/testutil"
    11  	"github.com/hashicorp/terraform/backend"
    12  )
    13  
    14  func TestBackend_impl(t *testing.T) {
    15  	var _ backend.Backend = new(Backend)
    16  }
    17  
    18  var srv *testutil.TestServer
    19  
    20  func TestMain(m *testing.M) {
    21  	if os.Getenv("TF_ACC") == "" && os.Getenv("TF_CONSUL_TEST") == "" {
    22  		fmt.Println("consul server tests require setting TF_ACC or TF_CONSUL_TEST")
    23  		return
    24  	}
    25  
    26  	var err error
    27  	srv, err = newConsulTestServer()
    28  	if err != nil {
    29  		fmt.Println(err)
    30  		os.Exit(1)
    31  	}
    32  
    33  	rc := m.Run()
    34  	srv.Stop()
    35  	os.Exit(rc)
    36  }
    37  
    38  func newConsulTestServer() (*testutil.TestServer, error) {
    39  	srv, err := testutil.NewTestServerConfig(func(c *testutil.TestServerConfig) {
    40  		c.LogLevel = "warn"
    41  
    42  		if !testing.Verbose() {
    43  			c.Stdout = ioutil.Discard
    44  			c.Stderr = ioutil.Discard
    45  		}
    46  	})
    47  
    48  	return srv, err
    49  }
    50  
    51  func TestBackend(t *testing.T) {
    52  	path := fmt.Sprintf("tf-unit/%s", time.Now().String())
    53  
    54  	// Get the backend. We need two to test locking.
    55  	b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{
    56  		"address": srv.HTTPAddr,
    57  		"path":    path,
    58  	})
    59  
    60  	b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{
    61  		"address": srv.HTTPAddr,
    62  		"path":    path,
    63  	})
    64  
    65  	// Test
    66  	backend.TestBackendStates(t, b1)
    67  	backend.TestBackendStateLocks(t, b1, b2)
    68  }
    69  
    70  func TestBackend_lockDisabled(t *testing.T) {
    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(), map[string]interface{}{
    75  		"address": srv.HTTPAddr,
    76  		"path":    path,
    77  		"lock":    false,
    78  	})
    79  
    80  	b2 := backend.TestBackendConfig(t, New(), 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  	// Get the backend
    93  	b := backend.TestBackendConfig(t, New(), map[string]interface{}{
    94  		"address": srv.HTTPAddr,
    95  		"path":    fmt.Sprintf("tf-unit/%s", time.Now().String()),
    96  		"gzip":    true,
    97  	})
    98  
    99  	// Test
   100  	backend.TestBackendStates(t, b)
   101  }