terraform/internal/backend/remote-state/consul/backend_test.go

107 lines
2.3 KiB
Go
Raw Normal View History

2017-03-02 07:58:51 +01:00
package consul
import (
"flag"
2017-03-02 07:58:51 +01:00
"fmt"
"io/ioutil"
"os"
2017-03-02 07:58:51 +01:00
"testing"
"time"
"github.com/hashicorp/consul/testutil"
"github.com/hashicorp/terraform/internal/backend"
2017-03-02 07:58:51 +01:00
)
func TestBackend_impl(t *testing.T) {
var _ backend.Backend = new(Backend)
}
var srv *testutil.TestServer
func TestMain(m *testing.M) {
if os.Getenv("TF_ACC") == "" && os.Getenv("TF_CONSUL_TEST") == "" {
fmt.Println("consul server tests require setting TF_ACC or TF_CONSUL_TEST")
return
}
var err error
srv, err = newConsulTestServer()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
rc := m.Run()
srv.Stop()
os.Exit(rc)
}
func newConsulTestServer() (*testutil.TestServer, error) {
srv, err := testutil.NewTestServerConfig(func(c *testutil.TestServerConfig) {
c.LogLevel = "warn"
if !flag.Parsed() {
flag.Parse()
}
if !testing.Verbose() {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
}
})
return srv, err
}
2017-03-02 07:58:51 +01:00
func TestBackend(t *testing.T) {
path := fmt.Sprintf("tf-unit/%s", time.Now().String())
// Get the backend. We need two to test locking.
b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
"address": srv.HTTPAddr,
"path": path,
}))
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
"address": srv.HTTPAddr,
"path": path,
}))
2017-03-02 07:58:51 +01:00
// Test
backend.TestBackendStates(t, b1)
backend.TestBackendStateLocks(t, b1, b2)
}
func TestBackend_lockDisabled(t *testing.T) {
path := fmt.Sprintf("tf-unit/%s", time.Now().String())
// Get the backend. We need two to test locking.
b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
"address": srv.HTTPAddr,
"path": path,
"lock": false,
}))
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
"address": srv.HTTPAddr,
"path": path + "different", // Diff so locking test would fail if it was locking
"lock": false,
}))
// Test
backend.TestBackendStates(t, b1)
backend.TestBackendStateLocks(t, b1, b2)
2017-03-02 07:58:51 +01:00
}
2017-03-14 20:24:50 +01:00
func TestBackend_gzip(t *testing.T) {
// Get the backend
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
2017-03-14 20:24:50 +01:00
"address": srv.HTTPAddr,
"path": fmt.Sprintf("tf-unit/%s", time.Now().String()),
"gzip": true,
}))
2017-03-14 20:24:50 +01:00
// Test
backend.TestBackendStates(t, b)
2017-03-14 20:24:50 +01:00
}