From 9e2f6f3b4fb2304ddb0f19c2abd81ed0491dc66f Mon Sep 17 00:00:00 2001 From: Chris Capurso Date: Fri, 18 Feb 2022 13:24:46 -0500 Subject: [PATCH] wrap kv reqs with kvRequestWithRetry in external tests (#14163) --- vault/external_tests/kv/kv_patch_test.go | 55 ++++++++----- vault/external_tests/kv/kv_subkeys_test.go | 92 ++++++++++++++++------ 2 files changed, 106 insertions(+), 41 deletions(-) diff --git a/vault/external_tests/kv/kv_patch_test.go b/vault/external_tests/kv/kv_patch_test.go index 84320ca63..52e60215d 100644 --- a/vault/external_tests/kv/kv_patch_test.go +++ b/vault/external_tests/kv/kv_patch_test.go @@ -52,36 +52,48 @@ func TestKV_Patch_BadContentTypeHeader(t *testing.T) { }, } - resp, err := c.Logical().Write("kv/data/foo", kvData) + secretRaw, err := kvRequestWithRetry(t, func() (interface{}, error) { + return c.Logical().Write("kv/data/foo", kvData) + }) if err != nil { - t.Fatalf("write failed - err :%#v, resp: %#v\n", err, resp) + t.Fatalf("write failed - err :%#v, resp: %#v\n", err, secretRaw) } - resp, err = c.Logical().Read("kv/data/foo") + secretRaw, err = kvRequestWithRetry(t, func() (interface{}, error) { + return c.Logical().Read("kv/data/foo") + }) if err != nil { - t.Fatalf("read failed - err :%#v, resp: %#v\n", err, resp) + t.Fatalf("read failed - err :%#v, resp: %#v\n", err, secretRaw) } - req := c.NewRequest("PATCH", "/v1/kv/data/foo") - req.Headers = http.Header{ - "Content-Type": []string{"application/json"}, + apiRespRaw, err := kvRequestWithRetry(t, func() (interface{}, error) { + req := c.NewRequest("PATCH", "/v1/kv/data/foo") + req.Headers = http.Header{ + "Content-Type": []string{"application/json"}, + } + + if err := req.SetJSONBody(kvData); err != nil { + t.Fatal(err) + } + + return c.RawRequestWithContext(context.Background(), req) + }) + + apiResp, ok := apiRespRaw.(*api.Response) + if !ok { + t.Fatalf("response not an api.Response, actual: %#v", apiRespRaw) } - if err := req.SetJSONBody(kvData); err != nil { - t.Fatal(err) - } - - apiResp, err := c.RawRequestWithContext(context.Background(), req) if err == nil || apiResp.StatusCode != http.StatusUnsupportedMediaType { t.Fatalf("expected PATCH request to fail with %d status code - err :%#v, resp: %#v\n", http.StatusUnsupportedMediaType, err, apiResp) } } -func kvRequestWithRetry(t *testing.T, req func() (*api.Secret, error)) (*api.Secret, error) { +func kvRequestWithRetry(t *testing.T, req func() (interface{}, error)) (interface{}, error) { t.Helper() var err error - var resp *api.Secret + var resp interface{} // Loop until return message does not indicate upgrade, or timeout. timeout := time.After(20 * time.Second) @@ -156,7 +168,7 @@ func TestKV_Patch_Audit(t *testing.T) { }, } - resp, err := kvRequestWithRetry(t, func() (*api.Secret, error) { + resp, err := kvRequestWithRetry(t, func() (interface{}, error) { return c.Logical().Write("kv/data/foo", writeData) }) if err != nil { @@ -169,7 +181,7 @@ func TestKV_Patch_Audit(t *testing.T) { }, } - resp, err = kvRequestWithRetry(t, func() (*api.Secret, error) { + resp, err = kvRequestWithRetry(t, func() (interface{}, error) { return c.Logical().JSONMergePatch(context.Background(), "kv/data/foo", patchData) }) @@ -233,7 +245,7 @@ func TestKV_Patch_RootToken(t *testing.T) { } // Write a kv value and patch it - _, err = kvRequestWithRetry(t, func() (*api.Secret, error) { + _, err = kvRequestWithRetry(t, func() (interface{}, error) { data := map[string]interface{}{ "data": map[string]interface{}{ "bar": "baz", @@ -247,7 +259,7 @@ func TestKV_Patch_RootToken(t *testing.T) { t.Fatal(err) } - _, err = kvRequestWithRetry(t, func() (*api.Secret, error) { + _, err = kvRequestWithRetry(t, func() (interface{}, error) { data := map[string]interface{}{ "data": map[string]interface{}{ "bar": "quux", @@ -260,13 +272,18 @@ func TestKV_Patch_RootToken(t *testing.T) { t.Fatal(err) } - secret, err := kvRequestWithRetry(t, func() (*api.Secret, error) { + secretRaw, err := kvRequestWithRetry(t, func() (interface{}, error) { return client.Logical().Read("kv/data/foo") }) if err != nil { t.Fatal(err) } + secret, ok := secretRaw.(*api.Secret) + if !ok { + t.Fatalf("response not an api.Secret, actual: %#v", secretRaw) + } + bar := secret.Data["data"].(map[string]interface{})["bar"] if bar != "quux" { t.Fatalf("expected bar to be quux but it was %q", bar) diff --git a/vault/external_tests/kv/kv_subkeys_test.go b/vault/external_tests/kv/kv_subkeys_test.go index 866abc528..116d41a7a 100644 --- a/vault/external_tests/kv/kv_subkeys_test.go +++ b/vault/external_tests/kv/kv_subkeys_test.go @@ -43,8 +43,15 @@ func TestKV_Subkeys_NotFound(t *testing.T) { t.Fatal(err) } - req := c.NewRequest("GET", "/v1/kv/subkeys/foo") - apiResp, err := c.RawRequestWithContext(context.Background(), req) + apiRespRaw, err := kvRequestWithRetry(t, func() (interface{}, error) { + req := c.NewRequest("GET", "/v1/kv/subkeys/foo") + return c.RawRequestWithContext(context.Background(), req) + }) + + apiResp, ok := apiRespRaw.(*api.Response) + if !ok { + t.Fatalf("response not an api.Response, actual: %#v", apiRespRaw) + } if err == nil || apiResp == nil { t.Fatalf("expected subkeys request to fail, err :%v, resp: %#v", err, apiResp) @@ -93,19 +100,31 @@ func TestKV_Subkeys_Deleted(t *testing.T) { }, } - resp, err := c.Logical().Write("kv/data/foo", kvData) + resp, err := kvRequestWithRetry(t, func() (interface{}, error) { + return c.Logical().Write("kv/data/foo", kvData) + }) if err != nil { t.Fatalf("write failed, err :%v, resp: %#v", err, resp) } - resp, err = c.Logical().Delete("kv/data/foo") + secretRaw, err := kvRequestWithRetry(t, func() (interface{}, error) { + return c.Logical().Delete("kv/data/foo") + }) if err != nil { - t.Fatalf("delete failed, err :%v, resp: %#v", err, resp) + t.Fatalf("delete failed, err :%v, resp: %#v", err, secretRaw) } - req := c.NewRequest("GET", "/v1/kv/subkeys/foo") - apiResp, err := c.RawRequestWithContext(context.Background(), req) - if resp != nil { + apiRespRaw, err := kvRequestWithRetry(t, func() (interface{}, error) { + req := c.NewRequest("GET", "/v1/kv/subkeys/foo") + return c.RawRequestWithContext(context.Background(), req) + }) + + apiResp, ok := apiRespRaw.(*api.Response) + if !ok { + t.Fatalf("response not a api.Response, actual: %#v", apiRespRaw) + } + + if apiResp != nil { defer apiResp.Body.Close() } @@ -180,22 +199,39 @@ func TestKV_Subkeys_Destroyed(t *testing.T) { }, } - resp, err := c.Logical().Write("kv/data/foo", kvData) + secretRaw, err := kvRequestWithRetry(t, func() (interface{}, error) { + return c.Logical().Write("kv/data/foo", kvData) + }) if err != nil { - t.Fatalf("write failed, err :%v, resp: %#v", err, resp) + t.Fatalf("write failed, err :%v, resp: %#v", err, secretRaw) } destroyVersions := map[string]interface{}{ "versions": []int{1}, } - resp, err = c.Logical().Write("kv/destroy/foo", destroyVersions) + secretRaw, err = kvRequestWithRetry(t, func() (interface{}, error) { + return c.Logical().Write("kv/destroy/foo", destroyVersions) + }) if err != nil { - t.Fatalf("destroy failed, err :%v, resp: %#v", err, resp) + t.Fatalf("destroy failed, err :%v, resp: %#v", err, secretRaw) + } + + secret, ok := secretRaw.(*api.Secret) + if !ok { + t.Fatalf("response not an api.Secret, actual: %#v", secretRaw) + } + + apiRespRaw, err := kvRequestWithRetry(t, func() (interface{}, error) { + req := c.NewRequest("GET", "/v1/kv/subkeys/foo") + return c.RawRequestWithContext(context.Background(), req) + }) + + apiResp, ok := apiRespRaw.(*api.Response) + if !ok { + t.Fatalf("response not a api.Response, actual: %#v", apiRespRaw) } - req := c.NewRequest("GET", "/v1/kv/subkeys/foo") - apiResp, err := c.RawRequestWithContext(context.Background(), req) if apiResp != nil { defer apiResp.Body.Close() } @@ -208,7 +244,7 @@ func TestKV_Subkeys_Destroyed(t *testing.T) { t.Fatalf("expected subkeys request to fail with %d status code, resp: %#v", http.StatusNotFound, apiResp) } - secret, err := api.ParseSecret(apiResp.Body) + secret, err = api.ParseSecret(apiResp.Body) if err != nil { t.Fatalf("failed to parse resp body, err: %v", err) } @@ -276,9 +312,11 @@ func TestKV_Subkeys_CurrentVersion(t *testing.T) { }, } - resp, err := c.Logical().Write("kv/data/foo", kvData) + secretRaw, err := kvRequestWithRetry(t, func() (interface{}, error) { + return c.Logical().Write("kv/data/foo", kvData) + }) if err != nil { - t.Fatalf("write failed, err :%v, resp: %#v", err, resp) + t.Fatalf("write failed, err :%v, resp: %#v", err, secretRaw) } kvData = map[string]interface{}{ @@ -287,14 +325,24 @@ func TestKV_Subkeys_CurrentVersion(t *testing.T) { }, } - resp, err = c.Logical().JSONMergePatch(context.Background(), "kv/data/foo", kvData) + secretRaw, err = kvRequestWithRetry(t, func() (interface{}, error) { + return c.Logical().JSONMergePatch(context.Background(), "kv/data/foo", kvData) + }) if err != nil { - t.Fatalf("patch failed, err :%v, resp: %#v", err, resp) + t.Fatalf("patch failed, err :%v, resp: %#v", err, secretRaw) } - req := c.NewRequest("GET", "/v1/kv/subkeys/foo") - apiResp, err := c.RawRequestWithContext(context.Background(), req) - if resp != nil { + apiRespRaw, err := kvRequestWithRetry(t, func() (interface{}, error) { + req := c.NewRequest("GET", "/v1/kv/subkeys/foo") + return c.RawRequestWithContext(context.Background(), req) + }) + + apiResp, ok := apiRespRaw.(*api.Response) + if !ok { + t.Fatalf("response not a api.Response, actual: %#v", apiRespRaw) + } + + if apiResp != nil { defer apiResp.Body.Close() }