From 133576899c38b9d5252c1719b0ff52e687963d7b Mon Sep 17 00:00:00 2001 From: Chun-Hung Tseng Date: Mon, 22 Apr 2024 18:02:54 +0200 Subject: [PATCH] gofail: show error messages returned by gofail HTTP endpoints Upon HTTP request error from fetchFailpointsBody, PUT and DELETE, all of them will return an error message, which would be nice for debugging if displayed. Simplify `Enabled()` logic (linter suggestion) Signed-off-by: Chun-Hung Tseng --- tests/framework/e2e/etcd_process.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/tests/framework/e2e/etcd_process.go b/tests/framework/e2e/etcd_process.go index af5c437c7..f9d2089a3 100644 --- a/tests/framework/e2e/etcd_process.go +++ b/tests/framework/e2e/etcd_process.go @@ -375,7 +375,11 @@ func (f *BinaryFailpoints) SetupHTTP(ctx context.Context, failpoint, payload str } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { - return fmt.Errorf("bad status code: %d", resp.StatusCode) + errMsg, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("bad status code: %d, err: %w", resp.StatusCode, err) + } + return fmt.Errorf("bad status code: %d, err: %s", resp.StatusCode, errMsg) } return nil } @@ -404,17 +408,18 @@ func (f *BinaryFailpoints) DeactivateHTTP(ctx context.Context, failpoint string) } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { - return fmt.Errorf("bad status code: %d", resp.StatusCode) + errMsg, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("bad status code: %d, err: %w", resp.StatusCode, err) + } + return fmt.Errorf("bad status code: %d, err: %s", resp.StatusCode, errMsg) } return nil } func (f *BinaryFailpoints) Enabled() bool { _, err := failpoints(f.member) - if err != nil { - return false - } - return true + return err == nil } func (f *BinaryFailpoints) Available(failpoint string) bool { @@ -449,8 +454,12 @@ func fetchFailpointsBody(member EtcdProcess) (io.ReadCloser, error) { return nil, err } if resp.StatusCode != http.StatusOK { - resp.Body.Close() - return nil, fmt.Errorf("invalid status code, %d", resp.StatusCode) + defer resp.Body.Close() + errMsg, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("invalid status code: %d, err: %w", resp.StatusCode, err) + } + return nil, fmt.Errorf("invalid status code: %d, err:%s", resp.StatusCode, errMsg) } return resp.Body, nil }