2017-08-03 19:10:50 +02:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-09-26 17:30:52 +02:00
|
|
|
"reflect"
|
2017-08-03 19:10:50 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
2021-01-27 23:04:52 +01:00
|
|
|
etcdv3 "go.etcd.io/etcd/clientv3"
|
2017-08-03 19:10:50 +02:00
|
|
|
)
|
|
|
|
|
2017-09-08 13:16:00 +02:00
|
|
|
var (
|
|
|
|
etcdv3Endpoints = strings.Split(os.Getenv("TF_ETCDV3_ENDPOINTS"), ",")
|
|
|
|
)
|
|
|
|
|
2017-08-03 19:10:50 +02:00
|
|
|
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) {
|
2017-08-03 19:10:50 +02:00
|
|
|
client, err := etcdv3.New(etcdv3.Config{
|
2017-09-08 13:16:00 +02:00
|
|
|
Endpoints: etcdv3Endpoints,
|
2017-08-03 19:10:50 +02:00
|
|
|
})
|
|
|
|
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()
|
|
|
|
}
|
2019-09-26 17:30:52 +02:00
|
|
|
if reflect.DeepEqual(etcdv3Endpoints, []string{""}) {
|
|
|
|
t.Fatal("etcd server tests require setting TF_ETCDV3_ENDPOINTS")
|
|
|
|
}
|
2017-09-08 23:49:23 +02:00
|
|
|
cleanupEtcdv3(t)
|
|
|
|
}
|
|
|
|
|
2017-08-03 19:10:50 +02:00
|
|
|
func TestBackend(t *testing.T) {
|
|
|
|
prepareEtcdv3(t)
|
2017-09-08 23:49:23 +02:00
|
|
|
defer cleanupEtcdv3(t)
|
2017-08-03 19:10:50 +02:00
|
|
|
|
2017-09-08 23:34:15 +02:00
|
|
|
prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339))
|
2017-08-03 19:10:50 +02:00
|
|
|
|
|
|
|
// 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{}{
|
2021-03-15 22:09:44 +01:00
|
|
|
"endpoints": stringsToInterfaces(etcdv3Endpoints),
|
2017-09-08 23:34:15 +02:00
|
|
|
"prefix": prefix,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-08-03 19:10:50 +02:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2021-03-15 22:09:44 +01:00
|
|
|
"endpoints": stringsToInterfaces(etcdv3Endpoints),
|
2017-09-08 23:34:15 +02:00
|
|
|
"prefix": prefix,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-08-03 19:10:50 +02:00
|
|
|
|
|
|
|
// Test
|
2018-02-21 03:05:58 +01:00
|
|
|
backend.TestBackendStates(t, b1)
|
|
|
|
backend.TestBackendStateLocks(t, b1, b2)
|
|
|
|
backend.TestBackendStateForceUnlock(t, b1, b2)
|
2017-08-03 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_lockDisabled(t *testing.T) {
|
|
|
|
prepareEtcdv3(t)
|
2017-09-08 23:49:23 +02:00
|
|
|
defer cleanupEtcdv3(t)
|
2017-08-03 19:10:50 +02:00
|
|
|
|
2017-09-08 23:34:15 +02:00
|
|
|
prefix := fmt.Sprintf("%s/%s/", keyPrefix, time.Now().Format(time.RFC3339))
|
2017-08-03 19:10:50 +02:00
|
|
|
|
|
|
|
// 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{}{
|
2021-03-15 22:09:44 +01:00
|
|
|
"endpoints": stringsToInterfaces(etcdv3Endpoints),
|
2017-09-08 23:34:15 +02:00
|
|
|
"prefix": prefix,
|
2017-08-03 19:10:50 +02:00
|
|
|
"lock": false,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-08-03 19:10:50 +02:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2021-03-15 22:09:44 +01:00
|
|
|
"endpoints": stringsToInterfaces(etcdv3Endpoints),
|
2017-09-08 23:34:15 +02:00
|
|
|
"prefix": prefix + "/" + "different", // Diff so locking test would fail if it was locking
|
2017-08-03 19:10:50 +02:00
|
|
|
"lock": false,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-08-03 19:10:50 +02:00
|
|
|
|
|
|
|
// Test
|
2018-02-21 03:05:58 +01:00
|
|
|
backend.TestBackendStateLocks(t, b1, b2)
|
2017-08-03 19:10:50 +02:00
|
|
|
}
|
2021-03-15 22:09:44 +01:00
|
|
|
|
|
|
|
func stringsToInterfaces(strSlice []string) []interface{} {
|
|
|
|
var interfaceSlice []interface{}
|
|
|
|
for _, v := range strSlice {
|
|
|
|
interfaceSlice = append(interfaceSlice, v)
|
|
|
|
}
|
|
|
|
return interfaceSlice
|
|
|
|
}
|