From 3e36a58cf75c73e37b594c1327042d7f2be391c5 Mon Sep 17 00:00:00 2001 From: Alexander Scheel Date: Tue, 11 Apr 2023 10:09:16 -0400 Subject: [PATCH] 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 --- helper/testhelpers/testhelpers.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/helper/testhelpers/testhelpers.go b/helper/testhelpers/testhelpers.go index 270784568..deda63c29 100644 --- a/helper/testhelpers/testhelpers.go +++ b/helper/testhelpers/testhelpers.go @@ -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 @@ -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])) @@ -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) } @@ -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) }