Support namespaces in test helpers (#20048)

Sometimes the tests will modify the client to set a namespace; this
results in testhelpers sometimes trying to access sys/ endpoints with a
namespace, which usually don't work well.

Detect an unset namespaces, if present, before executing, and restore
afterwards so as not to affect the tests.

Signed-off-by: Alexander Scheel <alex.scheel@hashicorp.com>
This commit is contained in:
Alexander Scheel 2023-04-11 10:09:16 -04:00 committed by GitHub
parent 8a4e50fa64
commit 3e36a58cf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions

View File

@ -56,6 +56,9 @@ func GenerateRootWithError(t testing.T, cluster *vault.TestCluster, kind Generat
keys = cluster.BarrierKeys keys = cluster.BarrierKeys
} }
client := cluster.Cores[0].Client client := cluster.Cores[0].Client
oldNS := client.Namespace()
defer client.SetNamespace(oldNS)
client.ClearNamespace()
var err error var err error
var status *api.GenerateRootStatusResponse var status *api.GenerateRootStatusResponse
@ -177,6 +180,10 @@ func AttemptUnsealCore(c *vault.TestCluster, core *vault.TestClusterCore) error
} }
client := core.Client client := core.Client
oldNS := client.Namespace()
defer client.SetNamespace(oldNS)
client.ClearNamespace()
client.Sys().ResetUnsealProcess() client.Sys().ResetUnsealProcess()
for j := 0; j < len(c.BarrierKeys); j++ { for j := 0; j < len(c.BarrierKeys); j++ {
statusResp, err := client.Sys().Unseal(base64.StdEncoding.EncodeToString(c.BarrierKeys[j])) statusResp, err := client.Sys().Unseal(base64.StdEncoding.EncodeToString(c.BarrierKeys[j]))
@ -245,7 +252,10 @@ func DeriveActiveCore(t testing.T, cluster *vault.TestCluster) *vault.TestCluste
t.Helper() t.Helper()
for i := 0; i < 60; i++ { for i := 0; i < 60; i++ {
for _, core := range cluster.Cores { for _, core := range cluster.Cores {
oldNS := core.Client.Namespace()
core.Client.ClearNamespace()
leaderResp, err := core.Client.Sys().Leader() leaderResp, err := core.Client.Sys().Leader()
core.Client.SetNamespace(oldNS)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -263,7 +273,10 @@ func DeriveStandbyCores(t testing.T, cluster *vault.TestCluster) []*vault.TestCl
t.Helper() t.Helper()
cores := make([]*vault.TestClusterCore, 0, 2) cores := make([]*vault.TestClusterCore, 0, 2)
for _, core := range cluster.Cores { for _, core := range cluster.Cores {
oldNS := core.Client.Namespace()
core.Client.ClearNamespace()
leaderResp, err := core.Client.Sys().Leader() leaderResp, err := core.Client.Sys().Leader()
core.Client.SetNamespace(oldNS)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }