From 077f2843b989abe44d9e04ba98b1b8ea3ed2e29d Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Tue, 7 Nov 2023 21:10:10 +0800 Subject: [PATCH] fix test Signed-off-by: lhy1024 --- pkg/mcs/scheduling/server/apis/v1/api.go | 6 ++---- pkg/utils/apiutil/serverapi/middleware.go | 5 +++-- server/api/server.go | 4 ++-- tests/pdctl/config/config_test.go | 10 +++++++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pkg/mcs/scheduling/server/apis/v1/api.go b/pkg/mcs/scheduling/server/apis/v1/api.go index abeec5af6b4..172515d8620 100644 --- a/pkg/mcs/scheduling/server/apis/v1/api.go +++ b/pkg/mcs/scheduling/server/apis/v1/api.go @@ -16,7 +16,6 @@ package apis import ( "encoding/hex" - "fmt" "net/http" "strconv" "sync" @@ -118,7 +117,6 @@ func NewService(srv *scheserver.Service) *Service { s.RegisterSchedulersRouter() s.RegisterCheckersRouter() s.RegisterHotspotRouter() - s.RegisterConfigRouter() return s } @@ -188,7 +186,7 @@ func (s *Service) RegisterConfigRouter() { groups := router.Group("rule_groups") groups.GET("", getAllGroupConfigs) - groups.GET("/:id", getGroupConfig) + groups.GET("/:id", getRuleGroupConfig) placementRule := router.Group("placement-rule") placementRule.GET("", getPlacementRules) @@ -892,7 +890,7 @@ func getAllGroupConfigs(c *gin.Context) { // @Failure 412 {string} string "Placement rules feature is disabled." // @Failure 500 {string} string "PD server failed to proceed the request." // @Router /config/rule_groups/{id} [get] -func getGroupConfig(c *gin.Context) { +func getRuleGroupConfig(c *gin.Context) { handler := c.MustGet(handlerKey).(*handler.Handler) manager, err := handler.GetRuleManager() if err == errs.ErrPlacementDisabled { diff --git a/pkg/utils/apiutil/serverapi/middleware.go b/pkg/utils/apiutil/serverapi/middleware.go index 19438ad0f91..2bb742ccbba 100644 --- a/pkg/utils/apiutil/serverapi/middleware.go +++ b/pkg/utils/apiutil/serverapi/middleware.go @@ -117,6 +117,7 @@ func (h *redirector) matchMicroServiceRedirectRules(r *http.Request) (bool, stri r.URL.Path = strings.TrimRight(r.URL.Path, "/") for _, rule := range h.microserviceRedirectRules { if strings.HasPrefix(r.URL.Path, rule.matchPath) && slice.Contains(rule.matchMethods, r.Method) { + origin := r.URL.Path addr, ok := h.s.GetServicePrimaryAddr(r.Context(), rule.targetServiceName) if !ok || addr == "" { log.Warn("failed to get the service primary addr when trying to match redirect rules", @@ -134,8 +135,8 @@ func (h *redirector) matchMicroServiceRedirectRules(r *http.Request) (bool, stri } else { r.URL.Path = rule.targetPath } - log.Debug("redirect to micro service", zap.String("path", r.URL.Path), zap.String("target", addr), - zap.String("method", r.Method)) + log.Debug("redirect to micro service", zap.String("path", r.URL.Path), zap.String("origin-path", origin), + zap.String("target", addr), zap.String("method", r.Method)) return true, addr } } diff --git a/server/api/server.go b/server/api/server.go index c95e3c60607..77a51eb04e5 100644 --- a/server/api/server.go +++ b/server/api/server.go @@ -90,12 +90,12 @@ func NewHandler(_ context.Context, svr *server.Server) (http.Handler, apiutil.AP mcs.SchedulingServiceName, []string{http.MethodGet}), serverapi.MicroserviceRedirectRule( - prefix+"/config/rule", + prefix+"/config/rule/", scheapi.APIPathPrefix+"/config/rule", mcs.SchedulingServiceName, []string{http.MethodGet}), serverapi.MicroserviceRedirectRule( - prefix+"/config/rule_group", + prefix+"/config/rule_group/", scheapi.APIPathPrefix+"/config/rule_groups", // Note: this is a typo in the original code mcs.SchedulingServiceName, []string{http.MethodGet}), diff --git a/tests/pdctl/config/config_test.go b/tests/pdctl/config/config_test.go index 26d70bb955f..2cc8427911a 100644 --- a/tests/pdctl/config/config_test.go +++ b/tests/pdctl/config/config_test.go @@ -19,6 +19,7 @@ import ( "encoding/json" "os" "reflect" + "strings" "testing" "time" @@ -409,9 +410,12 @@ func (suite *configTestSuite) checkPlacementRuleGroups(cluster *tests.TestCluste // test show var group placement.RuleGroup - output, err = pdctl.ExecuteCommand(cmd, "-u", pdAddr, "config", "placement-rules", "rule-group", "show", "pd") - re.NoError(err) - re.NoError(json.Unmarshal(output, &group)) + testutil.Eventually(re, func() bool { // wait for the config to be synced to the scheduling server + output, err = pdctl.ExecuteCommand(cmd, "-u", pdAddr, "config", "placement-rules", "rule-group", "show", "pd") + re.NoError(err) + return !strings.Contains(string(output), "404") + }) + re.NoError(json.Unmarshal(output, &group), string(output)) re.Equal(placement.RuleGroup{ID: "pd"}, group) // test set