Skip to content

Commit

Permalink
Minor improvements to test cluster libraries (#25329)
Browse files Browse the repository at this point in the history
Add WaitForMatchingMerkleRootsClients and Clients to sdk testcluster.  Fix internal TestCluster.SetRootToken, which wasn't updating the builtin clients' token.
  • Loading branch information
ncabatoff authored Feb 9, 2024
1 parent 53f0622 commit 1b8606d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 41 deletions.
3 changes: 3 additions & 0 deletions changelog/25329.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
sdk/helper/testcluster: add some new helpers, improve some error messages.
```
24 changes: 0 additions & 24 deletions helper/testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,30 +433,6 @@ func RekeyCluster(t testing.T, cluster *vault.TestCluster, recovery bool) [][]by
return newKeys
}

func RaftClusterJoinNodes(t testing.T, cluster *vault.TestCluster) {
leader := cluster.Cores[0]

leaderInfos := []*raft.LeaderJoinInfo{
{
LeaderAPIAddr: leader.Client.Address(),
TLSConfig: leader.TLSConfig(),
},
}

// Join followers
for i := 1; i < len(cluster.Cores); i++ {
core := cluster.Cores[i]
_, err := core.JoinRaftCluster(namespace.RootContext(context.Background()), leaderInfos, false)
if err != nil {
t.Fatal(err)
}

cluster.UnsealCore(t, core)
}

WaitForNCoresUnsealed(t, cluster, len(cluster.Cores))
}

// HardcodedServerAddressProvider is a ServerAddressProvider that uses
// a hardcoded map of raft node addresses.
//
Expand Down
27 changes: 25 additions & 2 deletions helper/testhelpers/teststorage/teststorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package teststorage

import (
"context"
"fmt"
"io/ioutil"
"math/rand"
Expand All @@ -18,7 +19,7 @@ import (
auditSyslog "github.com/hashicorp/vault/builtin/audit/syslog"
logicalDb "github.com/hashicorp/vault/builtin/logical/database"
"github.com/hashicorp/vault/builtin/plugin"
"github.com/hashicorp/vault/helper/testhelpers"
"github.com/hashicorp/vault/helper/namespace"
"github.com/hashicorp/vault/helper/testhelpers/corehelpers"
vaulthttp "github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/internalshared/configutil"
Expand Down Expand Up @@ -233,6 +234,28 @@ func FileBackendSetup(conf *vault.CoreConfig, opts *vault.TestClusterOptions) {
opts.PhysicalFactory = SharedPhysicalFactory(MakeFileBackend)
}

func RaftClusterJoinNodes(t testing.T, cluster *vault.TestCluster) {
leader := cluster.Cores[0]

leaderInfos := []*raft.LeaderJoinInfo{
{
LeaderAPIAddr: leader.Client.Address(),
TLSConfig: leader.TLSConfig(),
},
}

// Join followers
for i := 1; i < len(cluster.Cores); i++ {
core := cluster.Cores[i]
_, err := core.JoinRaftCluster(namespace.RootContext(context.Background()), leaderInfos, false)
if err != nil {
t.Fatal(err)
}

cluster.UnsealCore(t, core)
}
}

func RaftBackendSetup(conf *vault.CoreConfig, opts *vault.TestClusterOptions) {
opts.KeepStandbysSealed = true
var bridge *raft.ClusterAddrBridge
Expand All @@ -252,7 +275,7 @@ func RaftBackendSetup(conf *vault.CoreConfig, opts *vault.TestClusterOptions) {
}
opts.SetupFunc = func(t testing.T, c *vault.TestCluster) {
if opts.NumCores != 1 {
testhelpers.RaftClusterJoinNodes(t, c)
RaftClusterJoinNodes(t, c)
time.Sleep(15 * time.Second)
}
}
Expand Down
34 changes: 22 additions & 12 deletions sdk/helper/testcluster/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func EnablePerfPrimary(ctx context.Context, pri VaultCluster) error {
client := pri.Nodes()[0].APIClient()
_, err := client.Logical().WriteWithContext(ctx, "sys/replication/performance/primary/enable", nil)
if err != nil {
return err
return fmt.Errorf("error enabling perf primary: %w", err)
}

err = WaitForPerfReplicationState(ctx, pri, consts.ReplicationPerformancePrimary)
if err != nil {
return err
return fmt.Errorf("error waiting for perf primary to have the correct state: %w", err)
}
return WaitForActiveNodeAndPerfStandbys(ctx, pri)
}
Expand Down Expand Up @@ -108,6 +108,10 @@ func EnablePerformanceSecondary(ctx context.Context, perfToken string, pri, sec
}

func WaitForMatchingMerkleRoots(ctx context.Context, endpoint string, pri, sec VaultCluster) error {
return WaitForMatchingMerkleRootsClients(ctx, endpoint, pri.Nodes()[0].APIClient(), sec.Nodes()[0].APIClient())
}

func WaitForMatchingMerkleRootsClients(ctx context.Context, endpoint string, pri, sec *api.Client) error {
getRoot := func(mode string, cli *api.Client) (string, error) {
status, err := cli.Logical().Read(endpoint + "status")
if err != nil {
Expand All @@ -122,16 +126,19 @@ func WaitForMatchingMerkleRoots(ctx context.Context, endpoint string, pri, sec V
return status.Data["merkle_root"].(string), nil
}

secClient := sec.Nodes()[0].APIClient()
priClient := pri.Nodes()[0].APIClient()
for i := 0; i < 30; i++ {
secRoot, err := getRoot("secondary", secClient)
var priRoot, secRoot string
var err error
genRet := func() error {
return fmt.Errorf("unequal merkle roots, pri=%s sec=%s, err=%w", priRoot, secRoot, err)
}
for ctx.Err() == nil {
secRoot, err = getRoot("secondary", sec)
if err != nil {
return err
return genRet()
}
priRoot, err := getRoot("primary", priClient)
priRoot, err = getRoot("primary", pri)
if err != nil {
return err
return genRet()
}

if reflect.DeepEqual(priRoot, secRoot) {
Expand Down Expand Up @@ -281,15 +288,18 @@ func WaitForPerfReplicationWorking(ctx context.Context, pri, sec VaultCluster) e

func SetupTwoClusterPerfReplication(ctx context.Context, pri, sec VaultCluster) error {
if err := EnablePerfPrimary(ctx, pri); err != nil {
return err
return fmt.Errorf("failed to enable perf primary: %w", err)
}
perfToken, err := GetPerformanceToken(pri, sec.ClusterID(), "")
if err != nil {
return err
return fmt.Errorf("failed to get performance token from perf primary: %w", err)
}

_, err = EnablePerformanceSecondary(ctx, perfToken, pri, sec, false, false)
return err
if err != nil {
return fmt.Errorf("failed to enable perf secondary: %w", err)
}
return nil
}

// PassiveWaitForActiveNodeAndPerfStandbys should be used instead of
Expand Down
14 changes: 11 additions & 3 deletions sdk/helper/testcluster/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func WaitForActiveNodeAndPerfStandbys(ctx context.Context, cluster VaultCluster)
// this call to WaitForActiveNode by reworking the logic in this method.
leaderIdx, err := WaitForActiveNode(ctx, cluster)
if err != nil {
return err
return fmt.Errorf("did not find leader: %w", err)
}

if len(cluster.Nodes()) == 1 {
Expand All @@ -307,7 +307,7 @@ func WaitForActiveNodeAndPerfStandbys(ctx context.Context, cluster VaultCluster)
time.Sleep(1 * time.Second)
}
if err != nil {
return fmt.Errorf("unable to mount KV engine: %v", err)
return fmt.Errorf("unable to mount KV engine: %w", err)
}
path := mountPoint + "/waitforactivenodeandperfstandbys"
var standbys, actives int64
Expand Down Expand Up @@ -381,11 +381,19 @@ func WaitForActiveNodeAndPerfStandbys(ctx context.Context, cluster VaultCluster)
time.Sleep(time.Second)
}
if err != nil {
return fmt.Errorf("unable to unmount KV engine on primary")
return fmt.Errorf("unable to unmount KV engine: %w", err)
}
return nil
}

func Clients(vc VaultCluster) []*api.Client {
var ret []*api.Client
for _, n := range vc.Nodes() {
ret = append(ret, n.APIClient())
}
return ret
}

type GenerateRootKind int

const (
Expand Down
3 changes: 3 additions & 0 deletions vault/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,9 @@ type TestCluster struct {

func (c *TestCluster) SetRootToken(token string) {
c.RootToken = token
for _, c := range c.Cores {
c.Client.SetToken(token)
}
}

func (c *TestCluster) Start() {
Expand Down

0 comments on commit 1b8606d

Please sign in to comment.