Files
vault/physical/raft/testing.go
Paul Banks 3a2a922b26 Raft config refactor for mount entry size limit (#25992)
* CE parts for mount-namespace entry limit

* Remove redundant code from refactor

* Add doc comment note about ent-only use of interface

* Add CHANGELOG
2024-03-19 17:28:23 +00:00

104 lines
2.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package raft
import (
"context"
"fmt"
"io"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-uuid"
)
func GetRaft(t testing.TB, bootstrap bool, noStoreState bool) (*RaftBackend, string) {
return getRaftInternal(t, bootstrap, defaultRaftConfig(t, bootstrap, noStoreState), nil, nil)
}
func GetRaftWithConfig(t testing.TB, bootstrap bool, noStoreState bool, conf map[string]string) (*RaftBackend, string) {
defaultConf := defaultRaftConfig(t, bootstrap, noStoreState)
conf["path"] = defaultConf["path"]
conf["doNotStoreLatestState"] = defaultConf["doNotStoreLatestState"]
return getRaftInternal(t, bootstrap, conf, nil, nil)
}
func GetRaftWithConfigAndInitFn(t testing.TB, bootstrap bool, noStoreState bool, conf map[string]string, initFn func(b *RaftBackend)) (*RaftBackend, string) {
defaultConf := defaultRaftConfig(t, bootstrap, noStoreState)
conf["path"] = defaultConf["path"]
conf["doNotStoreLatestState"] = defaultConf["doNotStoreLatestState"]
return getRaftInternal(t, bootstrap, conf, nil, initFn)
}
func GetRaftWithLogOutput(t testing.TB, bootstrap bool, noStoreState bool, logOutput io.Writer) (*RaftBackend, string) {
return getRaftInternal(t, bootstrap, defaultRaftConfig(t, bootstrap, noStoreState), logOutput, nil)
}
func defaultRaftConfig(t testing.TB, bootstrap bool, noStoreState bool) map[string]string {
raftDir := t.TempDir()
t.Logf("raft dir: %s", raftDir)
conf := map[string]string{
"path": raftDir,
"trailing_logs": "100",
}
if noStoreState {
conf["doNotStoreLatestState"] = ""
}
return conf
}
func getRaftInternal(t testing.TB, bootstrap bool, conf map[string]string, logOutput io.Writer, initFn func(b *RaftBackend)) (*RaftBackend, string) {
id, err := uuid.GenerateUUID()
if err != nil {
t.Fatal(err)
}
logger := hclog.New(&hclog.LoggerOptions{
Name: fmt.Sprintf("raft-%s", id),
Level: hclog.Trace,
Output: logOutput,
})
conf["node_id"] = id
backendRaw, err := NewRaftBackend(conf, logger)
if err != nil {
t.Fatal(err)
}
backend := backendRaw.(*RaftBackend)
if initFn != nil {
initFn(backend)
}
if bootstrap {
err = backend.Bootstrap([]Peer{
{
ID: backend.NodeID(),
Address: backend.NodeID(),
},
})
if err != nil {
t.Fatal(err)
}
err = backend.SetupCluster(context.Background(), SetupOpts{})
if err != nil {
t.Fatal(err)
}
for {
if backend.raft.AppliedIndex() >= 2 {
break
}
}
}
backend.DisableAutopilot()
return backend, conf["path"]
}