opentofu/backend/remote-state/etcdv3/backend_test.go
Kristin Laemmert 68dfc3046d backend/remote-state: etcdv3, oss, and manta acc tests should fail
faster

The acceptance tests for etcdv3, oss and manta were not validating
required env variablea, chosing to assume that if one was running
acceptance tests they had already configured the credentials.

It was not always clear if this was a bug in the tests or the provider,
so I opted to make the tests fail faster when required attributes were
unset (or "").
2019-09-27 08:45:12 -04:00

100 lines
2.3 KiB
Go

package etcd
import (
"context"
"fmt"
"os"
"reflect"
"strings"
"testing"
"time"
etcdv3 "github.com/coreos/etcd/clientv3"
"github.com/hashicorp/terraform/backend"
)
var (
etcdv3Endpoints = strings.Split(os.Getenv("TF_ETCDV3_ENDPOINTS"), ",")
)
const (
keyPrefix = "tf-unit"
)
func TestBackend_impl(t *testing.T) {
var _ backend.Backend = new(Backend)
}
func cleanupEtcdv3(t *testing.T) {
client, err := etcdv3.New(etcdv3.Config{
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)
}
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()
}
if reflect.DeepEqual(etcdv3Endpoints, []string{""}) {
t.Fatal("etcd server tests require setting TF_ETCDV3_ENDPOINTS")
}
cleanupEtcdv3(t)
}
func TestBackend(t *testing.T) {
prepareEtcdv3(t)
defer cleanupEtcdv3(t)
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(), backend.TestWrapConfig(map[string]interface{}{
"endpoints": etcdv3Endpoints,
"prefix": prefix,
}))
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
"endpoints": etcdv3Endpoints,
"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)
defer cleanupEtcdv3(t)
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(), backend.TestWrapConfig(map[string]interface{}{
"endpoints": etcdv3Endpoints,
"prefix": prefix,
"lock": false,
}))
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
"endpoints": etcdv3Endpoints,
"prefix": prefix + "/" + "different", // Diff so locking test would fail if it was locking
"lock": false,
}))
// Test
backend.TestBackendStateLocks(t, b1, b2)
}