terraform/backend/remote-state/etcdv3/backend_test.go

96 lines
2.1 KiB
Go
Raw Normal View History

package etcd
import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"
etcdv3 "github.com/coreos/etcd/clientv3"
"github.com/hashicorp/terraform/backend"
)
2017-09-08 13:16:00 +02:00
var (
etcdv3Endpoints = strings.Split(os.Getenv("TF_ETCDV3_ENDPOINTS"), ",")
)
const (
keyPrefix = "tf-unit"
)
func TestBackend_impl(t *testing.T) {
var _ backend.Backend = new(Backend)
}
2017-09-08 23:49:23 +02:00
func cleanupEtcdv3(t *testing.T) {
client, err := etcdv3.New(etcdv3.Config{
2017-09-08 13:16:00 +02:00
Endpoints: etcdv3Endpoints,
})
if err != nil {
t.Fatal(err)
}
res, err := client.KV.Delete(context.TODO(), keyPrefix, etcdv3.WithPrefix())
if err != nil {
t.Fatal(err)
}
t.Logf("Cleaned up %d keys.", res.Deleted)
}
2017-09-08 23:49:23 +02:00
func prepareEtcdv3(t *testing.T) {
skip := os.Getenv("TF_ACC") == "" && os.Getenv("TF_ETCDV3_TEST") == ""
if skip {
t.Log("etcd server tests require setting TF_ACC or TF_ETCDV3_TEST")
t.Skip()
}
cleanupEtcdv3(t)
}
func TestBackend(t *testing.T) {
prepareEtcdv3(t)
2017-09-08 23:49:23 +02:00
defer cleanupEtcdv3(t)
2017-09-08 23:34:15 +02:00
prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339))
// Get the backend. We need two to test locking.
b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{
2017-09-08 13:16:00 +02:00
"endpoints": etcdv3Endpoints,
2017-09-08 23:34:15 +02:00
"prefix": prefix,
})
b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{
2017-09-08 13:16:00 +02:00
"endpoints": etcdv3Endpoints,
2017-09-08 23:34:15 +02:00
"prefix": prefix,
})
// Test
backend.TestBackendStates(t, b1)
backend.TestBackendStateLocks(t, b1, b2)
backend.TestBackendStateForceUnlock(t, b1, b2)
}
func TestBackend_lockDisabled(t *testing.T) {
prepareEtcdv3(t)
2017-09-08 23:49:23 +02:00
defer cleanupEtcdv3(t)
2017-09-08 23:34:15 +02:00
prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339))
// Get the backend. We need two to test locking.
b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{
2017-09-08 13:16:00 +02:00
"endpoints": etcdv3Endpoints,
2017-09-08 23:34:15 +02:00
"prefix": prefix,
"lock": false,
})
b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{
2017-09-08 13:16:00 +02:00
"endpoints": etcdv3Endpoints,
2017-09-08 23:34:15 +02:00
"prefix": prefix + "/" + "different", // Diff so locking test would fail if it was locking
"lock": false,
})
// Test
backend.TestBackendStateLocks(t, b1, b2)
}