optimize NewTestCluster (#24300)

This commit is contained in:
Raymond Ho
2023-12-01 11:30:58 -08:00
committed by GitHub
parent 4a7bee5a02
commit 73f46fca3e
3 changed files with 43 additions and 38 deletions

View File

@@ -9,6 +9,7 @@ import (
"testing"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/helper/testhelpers"
vaulthttp "github.com/hashicorp/vault/http"
@@ -143,7 +144,7 @@ func testSysRekey_Verification(t *testing.T, recovery bool, legacyShamir bool) {
// Sealing should clear state, so after this we should be able to perform
// the above again
cluster.EnsureCoresSealed(t)
if err := cluster.UnsealCoresWithError(recovery); err != nil {
if err := cluster.UnsealCoresWithError(t, recovery); err != nil {
t.Fatal(err)
}
doRekeyInitialSteps()
@@ -259,7 +260,7 @@ func testSysRekey_Verification(t *testing.T, recovery bool, legacyShamir bool) {
cluster.Start()
defer cluster.Cleanup()
if err := cluster.UnsealCoresWithError(false); err == nil {
if err := cluster.UnsealCoresWithError(t, false); err == nil {
t.Fatal("expected error")
}
@@ -273,7 +274,7 @@ func testSysRekey_Verification(t *testing.T, recovery bool, legacyShamir bool) {
newKeyBytes = append(newKeyBytes, val)
}
cluster.BarrierKeys = newKeyBytes
if err := cluster.UnsealCoresWithError(false); err != nil {
if err := cluster.UnsealCoresWithError(t, false); err != nil {
t.Fatal(err)
}
} else {

View File

@@ -13,6 +13,7 @@ import (
"github.com/go-test/deep"
"github.com/hashicorp/go-hclog"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/helper/namespace"
"github.com/hashicorp/vault/helper/testhelpers"
@@ -801,7 +802,7 @@ func runAutoseal(t *testing.T, logger hclog.Logger, storage teststorage.Reusable
t.Fatal(err)
}
} else {
if err := cluster.UnsealCoresWithError(true); err != nil {
if err := cluster.UnsealCoresWithError(t, true); err != nil {
t.Fatal(err)
}
}

View File

@@ -37,6 +37,11 @@ import (
"github.com/hashicorp/go-secure-stdlib/reloadutil"
raftlib "github.com/hashicorp/raft"
kv "github.com/hashicorp/vault-plugin-secrets-kv"
"github.com/mitchellh/copystructure"
"github.com/mitchellh/go-testing-interface"
"golang.org/x/crypto/ed25519"
"golang.org/x/net/http2"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/audit"
auditFile "github.com/hashicorp/vault/builtin/audit/file"
@@ -59,10 +64,6 @@ import (
backendplugin "github.com/hashicorp/vault/sdk/plugin"
"github.com/hashicorp/vault/vault/cluster"
"github.com/hashicorp/vault/vault/seal"
"github.com/mitchellh/copystructure"
"github.com/mitchellh/go-testing-interface"
"golang.org/x/crypto/ed25519"
"golang.org/x/net/http2"
)
// This file contains a number of methods that are useful for unit
@@ -873,7 +874,7 @@ func (c *TestCluster) start(t testing.T) {
activeCore := -1
WAITACTIVE:
for i := 0; i < 60; i++ {
for i := 0; i < 600; i++ {
for i, core := range c.Cores {
if standby, _ := core.Core.Standby(); !standby {
activeCore = i
@@ -881,7 +882,7 @@ WAITACTIVE:
}
}
time.Sleep(time.Second)
time.Sleep(100 * time.Millisecond)
}
if activeCore == -1 {
t.Fatalf("no core became active")
@@ -917,12 +918,12 @@ WAITACTIVE:
// UnsealCores uses the cluster barrier keys to unseal the test cluster cores
func (c *TestCluster) UnsealCores(t testing.T) {
t.Helper()
if err := c.UnsealCoresWithError(false); err != nil {
if err := c.UnsealCoresWithError(t, false); err != nil {
t.Fatal(err)
}
}
func (c *TestCluster) UnsealCoresWithError(useStoredKeys bool) error {
func (c *TestCluster) UnsealCoresWithError(t testing.T, useStoredKeys bool) error {
unseal := func(core *Core) error {
for _, key := range c.BarrierKeys {
if _, err := core.Unseal(TestKeyCopy(key)); err != nil {
@@ -959,19 +960,21 @@ func (c *TestCluster) UnsealCoresWithError(useStoredKeys bool) error {
}
// Let them come fully up to standby
time.Sleep(2 * time.Second)
corehelpers.RetryUntil(t, 2*time.Second, func() error {
// Ensure cluster connection info is populated.
// Other cores should not come up as leaders.
for i := 1; i < len(c.Cores); i++ {
isLeader, _, _, err := c.Cores[i].Leader()
if err != nil {
return err
}
if isLeader {
return fmt.Errorf("core[%d] should not be leader", i)
}
}
// Ensure cluster connection info is populated.
// Other cores should not come up as leaders.
for i := 1; i < len(c.Cores); i++ {
isLeader, _, _, err := c.Cores[i].Leader()
if err != nil {
return err
}
if isLeader {
return fmt.Errorf("core[%d] should not be leader", i)
}
}
return nil
})
return nil
}
@@ -1109,8 +1112,6 @@ func (c *TestCluster) Cleanup() {
os.RemoveAll(c.TempDir)
}
// Give time to actually shut down/clean up before the next test
time.Sleep(time.Second)
if c.CleanupFunc != nil {
c.CleanupFunc()
}
@@ -2177,19 +2178,21 @@ func (tc *TestCluster) initCores(t testing.T, opts *TestClusterOptions, addAudit
}
// Let them come fully up to standby
time.Sleep(2 * time.Second)
corehelpers.RetryUntil(t, 2*time.Second, func() error {
// Ensure cluster connection info is populated.
// Other cores should not come up as leaders.
for i := 1; i < numCores; i++ {
isLeader, _, _, err := tc.Cores[i].Core.Leader()
if err != nil {
return err
}
if isLeader {
return fmt.Errorf("core[%d] should not be leader", i)
}
}
// Ensure cluster connection info is populated.
// Other cores should not come up as leaders.
for i := 1; i < numCores; i++ {
isLeader, _, _, err := tc.Cores[i].Core.Leader()
if err != nil {
t.Fatal(err)
}
if isLeader {
t.Fatalf("core[%d] should not be leader", i)
}
}
return nil
})
}
//