From f070884b6057829e34583062609e5464225e2301 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Mon, 18 Sep 2023 15:38:37 +0800 Subject: [PATCH] add test with failpoint Signed-off-by: lhy1024 --- pkg/utils/apiutil/serverapi/middleware.go | 7 ++++++- tests/integrations/mcs/scheduling/api_test.go | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/utils/apiutil/serverapi/middleware.go b/pkg/utils/apiutil/serverapi/middleware.go index 1b97ce4d6aa..063ad042dbb 100644 --- a/pkg/utils/apiutil/serverapi/middleware.go +++ b/pkg/utils/apiutil/serverapi/middleware.go @@ -19,6 +19,7 @@ import ( "net/url" "strings" + "github.com/pingcap/failpoint" "github.com/pingcap/log" "github.com/tikv/pd/pkg/errs" "github.com/tikv/pd/pkg/slice" @@ -171,7 +172,11 @@ func (h *redirector) ServeHTTP(w http.ResponseWriter, r *http.Request, next http return } clientUrls = append(clientUrls, targetAddr) - w.Header().Set(apiutil.ForwardToMicroServiceHeader, "true") + failpoint.Inject("checkHeader", func() { + // add a header to the response, this is not a failure injection + // it is used for testing, to check whether the request is forwarded to the micro service + w.Header().Set(apiutil.ForwardToMicroServiceHeader, "true") + }) } else { leader := h.s.GetMember().GetLeader() if leader == nil { diff --git a/tests/integrations/mcs/scheduling/api_test.go b/tests/integrations/mcs/scheduling/api_test.go index ea9cd2df9c5..04671d84798 100644 --- a/tests/integrations/mcs/scheduling/api_test.go +++ b/tests/integrations/mcs/scheduling/api_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "github.com/pingcap/failpoint" "github.com/stretchr/testify/suite" _ "github.com/tikv/pd/pkg/mcs/scheduling/server/apis/v1" "github.com/tikv/pd/pkg/utils/apiutil" @@ -116,6 +117,11 @@ func (suite *apiTestSuite) TestAPIForward() { defer tc.Destroy() tc.WaitForPrimaryServing(re) + failpoint.Enable("github.com/tikv/pd/pkg/utils/apiutil/serverapi/checkHeader", "return(true)") + defer func() { + failpoint.Disable("github.com/tikv/pd/pkg/utils/apiutil/serverapi/checkHeader") + }() + urlPrefix := fmt.Sprintf("%s/pd/api/v1", suite.backendEndpoints) var slice []string var resp map[string]interface{}