2017-03-02 07:58:51 +01:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2020-08-13 16:29:43 +02:00
|
|
|
"flag"
|
2017-03-02 07:58:51 +01:00
|
|
|
"fmt"
|
2017-03-14 15:49:38 +01:00
|
|
|
"io/ioutil"
|
2017-03-14 19:41:16 +01:00
|
|
|
"os"
|
2017-03-02 07:58:51 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-03-13 23:25:58 +01:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2021-05-17 17:42:17 +02:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2017-10-29 01:45:10 +02:00
|
|
|
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)
|
2017-03-14 19:41:16 +01:00
|
|
|
}
|
|
|
|
|
2017-10-29 01:45:10 +02:00
|
|
|
rc := m.Run()
|
|
|
|
srv.Stop()
|
|
|
|
os.Exit(rc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConsulTestServer() (*testutil.TestServer, error) {
|
|
|
|
srv, err := testutil.NewTestServerConfig(func(c *testutil.TestServerConfig) {
|
2017-03-14 15:49:38 +01:00
|
|
|
c.LogLevel = "warn"
|
|
|
|
|
2020-08-13 16:29:43 +02:00
|
|
|
if !flag.Parsed() {
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2017-03-14 15:49:38 +01:00
|
|
|
if !testing.Verbose() {
|
|
|
|
c.Stdout = ioutil.Discard
|
|
|
|
c.Stderr = ioutil.Discard
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-29 01:45:10 +02:00
|
|
|
return srv, err
|
2017-03-14 15:49:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-02 07:58:51 +01:00
|
|
|
func TestBackend(t *testing.T) {
|
2017-03-15 01:28:42 +01:00
|
|
|
path := fmt.Sprintf("tf-unit/%s", time.Now().String())
|
|
|
|
|
|
|
|
// Get the backend. We need two to test locking.
|
2018-03-21 02:43:02 +01:00
|
|
|
b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-13 23:25:58 +01:00
|
|
|
"address": srv.HTTPAddr,
|
2017-03-15 01:28:42 +01:00
|
|
|
"path": path,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-03-15 01:28:42 +01:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-15 01:28:42 +01:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-03-02 07:58:51 +01:00
|
|
|
|
|
|
|
// Test
|
2018-02-21 03:05:58 +01:00
|
|
|
backend.TestBackendStates(t, b1)
|
|
|
|
backend.TestBackendStateLocks(t, b1, b2)
|
2017-03-15 01:28:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_lockDisabled(t *testing.T) {
|
|
|
|
path := fmt.Sprintf("tf-unit/%s", time.Now().String())
|
|
|
|
|
|
|
|
// Get the backend. We need two to test locking.
|
2018-03-21 02:43:02 +01:00
|
|
|
b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-15 01:28:42 +01:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
|
|
|
"lock": false,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-03-15 01:28:42 +01:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-15 01:28:42 +01:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path + "different", // Diff so locking test would fail if it was locking
|
|
|
|
"lock": false,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-03-15 01:28:42 +01:00
|
|
|
|
|
|
|
// Test
|
2018-02-21 03:05:58 +01:00
|
|
|
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
|
2018-03-21 02:43:02 +01:00
|
|
|
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,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-03-14 20:24:50 +01:00
|
|
|
|
|
|
|
// Test
|
2018-02-21 03:05:58 +01:00
|
|
|
backend.TestBackendStates(t, b)
|
2017-03-14 20:24:50 +01:00
|
|
|
}
|