opentofu/internal/backend/remote-state/etcdv3/backend_test.go
Kevin Burke c047958b57 go.mod,backend: update coreos/etcd dependency to release-3.4 branch
etcd rewrote its import path from coreos/etcd to go.etcd.io/etcd.
Changed the imports path in this commit, which also updates the code
version.

This lets us remove the github.com/ugorji/go/codec dependency, which
was pinned to a fairly old version. The net change is a loss of 30,000
lines of code in the vendor directory. (I first noticed this problem
because the outdated go/codec dependency was causing a dependency
failure when I tried to put Terraform and another project in the same
vendor directory.)

Note the version shows up funkily in go.mod, but I verified
visually it's the same commit as the "release-3.4" tag in
github.com/coreos/etcd. The etcd team plans to fix the release version
tagging in v3.5, which should be released soon.
2021-07-20 12:27:22 -04:00

108 lines
2.6 KiB
Go

package etcd
import (
"context"
"fmt"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/hashicorp/terraform/internal/backend"
etcdv3 "go.etcd.io/etcd/clientv3"
)
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": stringsToInterfaces(etcdv3Endpoints),
"prefix": prefix,
}))
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
"endpoints": stringsToInterfaces(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": stringsToInterfaces(etcdv3Endpoints),
"prefix": prefix,
"lock": false,
}))
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
"endpoints": stringsToInterfaces(etcdv3Endpoints),
"prefix": prefix + "/" + "different", // Diff so locking test would fail if it was locking
"lock": false,
}))
// Test
backend.TestBackendStateLocks(t, b1, b2)
}
func stringsToInterfaces(strSlice []string) []interface{} {
var interfaceSlice []interface{}
for _, v := range strSlice {
interfaceSlice = append(interfaceSlice, v)
}
return interfaceSlice
}