Skip to content

Commit

Permalink
Support namespaces in test helpers
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cipherboy committed Apr 11, 2023
1 parent 05c1fb0 commit 4af9ca0
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions helper/testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func GenerateRootWithError(t testing.T, cluster *vault.TestCluster, kind Generat
keys = cluster.BarrierKeys
}
client := cluster.Cores[0].Client
oldNS := client.Namespace()
defer client.SetNamespace(oldNS)
client.ClearNamespace()

var err error
var status *api.GenerateRootStatusResponse
Expand Down Expand Up @@ -177,6 +180,10 @@ func AttemptUnsealCore(c *vault.TestCluster, core *vault.TestClusterCore) error
}

client := core.Client
oldNS := client.Namespace()
defer client.SetNamespace(oldNS)
client.ClearNamespace()

client.Sys().ResetUnsealProcess()
for j := 0; j < len(c.BarrierKeys); j++ {
statusResp, err := client.Sys().Unseal(base64.StdEncoding.EncodeToString(c.BarrierKeys[j]))
Expand Down Expand Up @@ -245,7 +252,10 @@ func DeriveActiveCore(t testing.T, cluster *vault.TestCluster) *vault.TestCluste
t.Helper()
for i := 0; i < 60; i++ {
for _, core := range cluster.Cores {
oldNS := core.Client.Namespace()
core.Client.ClearNamespace()
leaderResp, err := core.Client.Sys().Leader()
core.Client.SetNamespace(oldNS)
if err != nil {
t.Fatal(err)
}
Expand All @@ -263,7 +273,10 @@ func DeriveStandbyCores(t testing.T, cluster *vault.TestCluster) []*vault.TestCl
t.Helper()
cores := make([]*vault.TestClusterCore, 0, 2)
for _, core := range cluster.Cores {
oldNS := core.Client.Namespace()
core.Client.ClearNamespace()
leaderResp, err := core.Client.Sys().Leader()
core.Client.SetNamespace(oldNS)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 4af9ca0

Please sign in to comment.