Skip to content

Commit

Permalink
add test with failpoint
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Sep 18, 2023
1 parent 2193aff commit f070884
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/utils/apiutil/serverapi/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions tests/integrations/mcs/scheduling/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit f070884

Please sign in to comment.