Skip to content

Commit

Permalink
add test with debug log
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 4e18485 commit 12eab46
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/utils/apiutil/serverapi/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (h *redirector) matchMicroServiceRedirectRules(r *http.Request) (bool, stri
}
for _, rule := range h.microserviceRedirectRules {
if strings.HasPrefix(r.URL.Path, rule.matchPath) && slice.Contains(rule.matchMethods, r.Method) {
beforePath := 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",
Expand All @@ -130,6 +131,7 @@ func (h *redirector) matchMicroServiceRedirectRules(r *http.Request) (bool, stri
} else {
r.URL.Path = rule.targetPath
}
log.Debug("redirect to microservice", zap.String("before-path", beforePath), zap.String("after-path", r.URL.Path), zap.String("target", addr))
return true, addr
}
}
Expand Down
47 changes: 45 additions & 2 deletions tests/integrations/mcs/scheduling/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package scheduling_test

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"testing"
"time"

Expand Down Expand Up @@ -114,6 +116,11 @@ func (suite *apiTestSuite) TestAPIForward() {
defer tc.Destroy()
tc.WaitForPrimaryServing(re)

fname := testutil.InitTempFileLogger("debug")
defer os.RemoveAll(fname)
schedulers := suite.cluster.GetServer(suite.cluster.GetLeader()).GetRaftCluster().GetSchedulers()
re.Empty(schedulers)

urlPrefix := fmt.Sprintf("%s/pd/api/v1", suite.backendEndpoints)
var slice []string
var resp map[string]interface{}
Expand All @@ -127,13 +134,49 @@ func (suite *apiTestSuite) TestAPIForward() {
re.NoError(err)
re.Nil(resp)

// Test checker
// Test checker: only read-only requests are forwarded
err = testutil.ReadGetJSON(re, testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "checker/merge"), &resp)
re.NoError(err)
suite.False(resp["paused"].(bool))

// Test scheduler
// Test scheduler: only read-only requests are forwarded
err = testutil.ReadGetJSON(re, testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "schedulers"), &slice)
re.NoError(err)
re.Contains(slice, "balance-leader-scheduler")

b, _ := os.ReadFile(fname)
suite.Contains(string(b), "redirect to microservice")
}

func (suite *apiTestSuite) TestAPIForward2() {
// POST requests are not forwarded about checkers and schedulers.
re := suite.Require()
tc, err := tests.NewTestSchedulingCluster(suite.ctx, 2, suite.backendEndpoints)
re.NoError(err)
defer tc.Destroy()
tc.WaitForPrimaryServing(re)

fname := testutil.InitTempFileLogger("debug")
defer os.RemoveAll(fname)
schedulers := suite.cluster.GetServer(suite.cluster.GetLeader()).GetRaftCluster().GetSchedulers()
re.Empty(schedulers)

urlPrefix := fmt.Sprintf("%s/pd/api/v1", suite.backendEndpoints)
input := make(map[string]interface{})
input["delay"] = 10
pauseArgs, err := json.Marshal(input)
suite.NoError(err)
err = testutil.CheckPostJSON(testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "checker/merge"), pauseArgs,
testutil.StatusOK(re))
suite.NoError(err)

input["delay"] = 30
pauseArgs, err = json.Marshal(input)
suite.NoError(err)
err = testutil.CheckPostJSON(testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "schedulers/all"), pauseArgs,
testutil.StatusOK(re))
suite.NoError(err)

b, _ := os.ReadFile(fname)
suite.NotContains(string(b), "redirect to microservice")
}

0 comments on commit 12eab46

Please sign in to comment.