From 4e1848591935dfd458b3c66b97a9e92ee8713fb8 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Mon, 18 Sep 2023 14:00:11 +0800 Subject: [PATCH] Revert "add test" This reverts commit 2193aff21727842949a328d9400bdb1ed1412844. --- pkg/utils/apiutil/apiutil.go | 2 - pkg/utils/apiutil/serverapi/middleware.go | 1 - pkg/utils/testutil/api_check.go | 73 ++++++++----------- server/api/hot_status_test.go | 5 +- server/api/region_test.go | 2 +- server/api/rule_test.go | 2 +- tests/integrations/mcs/scheduling/api_test.go | 33 ++------- 7 files changed, 39 insertions(+), 79 deletions(-) diff --git a/pkg/utils/apiutil/apiutil.go b/pkg/utils/apiutil/apiutil.go index 0b72b9af10fe..bcab7c8e9e71 100644 --- a/pkg/utils/apiutil/apiutil.go +++ b/pkg/utils/apiutil/apiutil.go @@ -57,8 +57,6 @@ const ( XForwardedPortHeader = "X-Forwarded-Port" // XRealIPHeader is used to mark the real client IP. XRealIPHeader = "X-Real-Ip" - // ForwardToMicroServiceHeader is used to mark the request is forwarded to micro service. - ForwardToMicroServiceHeader = "Forward-To-Micro-Service" // ErrRedirectFailed is the error message for redirect failed. ErrRedirectFailed = "redirect failed" diff --git a/pkg/utils/apiutil/serverapi/middleware.go b/pkg/utils/apiutil/serverapi/middleware.go index 1b97ce4d6aae..6682f4aabbc6 100644 --- a/pkg/utils/apiutil/serverapi/middleware.go +++ b/pkg/utils/apiutil/serverapi/middleware.go @@ -171,7 +171,6 @@ func (h *redirector) ServeHTTP(w http.ResponseWriter, r *http.Request, next http return } clientUrls = append(clientUrls, targetAddr) - w.Header().Set(apiutil.ForwardToMicroServiceHeader, "true") } else { leader := h.s.GetMember().GetLeader() if leader == nil { diff --git a/pkg/utils/testutil/api_check.go b/pkg/utils/testutil/api_check.go index d11d575967d1..fcc445b7e7a7 100644 --- a/pkg/utils/testutil/api_check.go +++ b/pkg/utils/testutil/api_check.go @@ -23,71 +23,56 @@ import ( "github.com/tikv/pd/pkg/utils/apiutil" ) -// Status is used to check whether http response code is equal given code. -func Status(re *require.Assertions, code int) func([]byte, int, http.Header) { - return func(resp []byte, i int, _ http.Header) { +// Status is used to check whether http response code is equal given code +func Status(re *require.Assertions, code int) func([]byte, int) { + return func(resp []byte, i int) { re.Equal(code, i, "resp: "+string(resp)) } } -// StatusOK is used to check whether http response code is equal http.StatusOK. -func StatusOK(re *require.Assertions) func([]byte, int, http.Header) { +// StatusOK is used to check whether http response code is equal http.StatusOK +func StatusOK(re *require.Assertions) func([]byte, int) { return Status(re, http.StatusOK) } -// StatusNotOK is used to check whether http response code is not equal http.StatusOK. -func StatusNotOK(re *require.Assertions) func([]byte, int, http.Header) { - return func(_ []byte, i int, _ http.Header) { +// StatusNotOK is used to check whether http response code is not equal http.StatusOK +func StatusNotOK(re *require.Assertions) func([]byte, int) { + return func(_ []byte, i int) { re.NotEqual(http.StatusOK, i) } } -// ExtractJSON is used to check whether given data can be extracted successfully. -func ExtractJSON(re *require.Assertions, data interface{}) func([]byte, int, http.Header) { - return func(res []byte, _ int, _ http.Header) { +// ExtractJSON is used to check whether given data can be extracted successfully +func ExtractJSON(re *require.Assertions, data interface{}) func([]byte, int) { + return func(res []byte, _ int) { re.NoError(json.Unmarshal(res, data)) } } -// StringContain is used to check whether response context contains given string. -func StringContain(re *require.Assertions, sub string) func([]byte, int, http.Header) { - return func(res []byte, _ int, _ http.Header) { +// StringContain is used to check whether response context contains given string +func StringContain(re *require.Assertions, sub string) func([]byte, int) { + return func(res []byte, _ int) { re.Contains(string(res), sub) } } -// StringEqual is used to check whether response context equal given string. -func StringEqual(re *require.Assertions, str string) func([]byte, int, http.Header) { - return func(res []byte, _ int, _ http.Header) { +// StringEqual is used to check whether response context equal given string +func StringEqual(re *require.Assertions, str string) func([]byte, int) { + return func(res []byte, _ int) { re.Contains(string(res), str) } } -// WithHeader is used to check whether response header contains given key and value. -func WithHeader(re *require.Assertions, key, value string) func([]byte, int, http.Header) { - return func(_ []byte, _ int, header http.Header) { - re.Equal(value, header.Get(key)) - } -} - -// WithoutHeader is used to check whether response header does not contain given key. -func WithoutHeader(re *require.Assertions, key string) func([]byte, int, http.Header) { - return func(_ []byte, _ int, header http.Header) { - re.Empty(header.Get(key)) - } -} - -// ReadGetJSON is used to do get request and check whether given data can be extracted successfully. -func ReadGetJSON(re *require.Assertions, client *http.Client, url string, data interface{}, checkOpts ...func([]byte, int, http.Header)) error { +// ReadGetJSON is used to do get request and check whether given data can be extracted successfully +func ReadGetJSON(re *require.Assertions, client *http.Client, url string, data interface{}) error { resp, err := apiutil.GetJSON(client, url, nil) if err != nil { return err } - checkOpts = append(checkOpts, StatusOK(re), ExtractJSON(re, data)) - return checkResp(resp, checkOpts...) + return checkResp(resp, StatusOK(re), ExtractJSON(re, data)) } -// ReadGetJSONWithBody is used to do get request with input and check whether given data can be extracted successfully. +// ReadGetJSONWithBody is used to do get request with input and check whether given data can be extracted successfully func ReadGetJSONWithBody(re *require.Assertions, client *http.Client, url string, input []byte, data interface{}) error { resp, err := apiutil.GetJSON(client, url, input) if err != nil { @@ -96,8 +81,8 @@ func ReadGetJSONWithBody(re *require.Assertions, client *http.Client, url string return checkResp(resp, StatusOK(re), ExtractJSON(re, data)) } -// CheckPostJSON is used to do post request and do check options. -func CheckPostJSON(client *http.Client, url string, data []byte, checkOpts ...func([]byte, int, http.Header)) error { +// CheckPostJSON is used to do post request and do check options +func CheckPostJSON(client *http.Client, url string, data []byte, checkOpts ...func([]byte, int)) error { resp, err := apiutil.PostJSON(client, url, data) if err != nil { return err @@ -105,8 +90,8 @@ func CheckPostJSON(client *http.Client, url string, data []byte, checkOpts ...fu return checkResp(resp, checkOpts...) } -// CheckGetJSON is used to do get request and do check options. -func CheckGetJSON(client *http.Client, url string, data []byte, checkOpts ...func([]byte, int, http.Header)) error { +// CheckGetJSON is used to do get request and do check options +func CheckGetJSON(client *http.Client, url string, data []byte, checkOpts ...func([]byte, int)) error { resp, err := apiutil.GetJSON(client, url, data) if err != nil { return err @@ -114,8 +99,8 @@ func CheckGetJSON(client *http.Client, url string, data []byte, checkOpts ...fun return checkResp(resp, checkOpts...) } -// CheckPatchJSON is used to do patch request and do check options. -func CheckPatchJSON(client *http.Client, url string, data []byte, checkOpts ...func([]byte, int, http.Header)) error { +// CheckPatchJSON is used to do patch request and do check options +func CheckPatchJSON(client *http.Client, url string, data []byte, checkOpts ...func([]byte, int)) error { resp, err := apiutil.PatchJSON(client, url, data) if err != nil { return err @@ -123,14 +108,14 @@ func CheckPatchJSON(client *http.Client, url string, data []byte, checkOpts ...f return checkResp(resp, checkOpts...) } -func checkResp(resp *http.Response, checkOpts ...func([]byte, int, http.Header)) error { +func checkResp(resp *http.Response, checkOpts ...func([]byte, int)) error { res, err := io.ReadAll(resp.Body) resp.Body.Close() if err != nil { return err } for _, opt := range checkOpts { - opt(res, resp.StatusCode, resp.Header) + opt(res, resp.StatusCode) } return nil } diff --git a/server/api/hot_status_test.go b/server/api/hot_status_test.go index d3d495f86fab..a1d1bbc2617c 100644 --- a/server/api/hot_status_test.go +++ b/server/api/hot_status_test.go @@ -17,7 +17,6 @@ package api import ( "encoding/json" "fmt" - "net/http" "testing" "time" @@ -93,7 +92,7 @@ func (suite *hotStatusTestSuite) TestGetHistoryHotRegionsTimeRange() { StartTime: now.UnixNano() / int64(time.Millisecond), EndTime: now.Add(10*time.Second).UnixNano() / int64(time.Millisecond), } - check := func(res []byte, statusCode int, _ http.Header) { + check := func(res []byte, statusCode int) { suite.Equal(200, statusCode) historyHotRegions := &storage.HistoryHotRegions{} json.Unmarshal(res, historyHotRegions) @@ -178,7 +177,7 @@ func (suite *hotStatusTestSuite) TestGetHistoryHotRegionsIDAndTypes() { IsLearners: []bool{false}, EndTime: now.Add(10*time.Minute).UnixNano() / int64(time.Millisecond), } - check := func(res []byte, statusCode int, _ http.Header) { + check := func(res []byte, statusCode int) { suite.Equal(200, statusCode) historyHotRegions := &storage.HistoryHotRegions{} json.Unmarshal(res, historyHotRegions) diff --git a/server/api/region_test.go b/server/api/region_test.go index acd305884d48..63da19ab0823 100644 --- a/server/api/region_test.go +++ b/server/api/region_test.go @@ -408,7 +408,7 @@ func (suite *regionTestSuite) TestSplitRegions() { hex.EncodeToString([]byte("bbb")), hex.EncodeToString([]byte("ccc")), hex.EncodeToString([]byte("ddd"))) - checkOpt := func(res []byte, code int, _ http.Header) { + checkOpt := func(res []byte, code int) { s := &struct { ProcessedPercentage int `json:"processed-percentage"` NewRegionsID []uint64 `json:"regions-id"` diff --git a/server/api/rule_test.go b/server/api/rule_test.go index 4cea15234015..d2000eb95620 100644 --- a/server/api/rule_test.go +++ b/server/api/rule_test.go @@ -829,7 +829,7 @@ func (suite *ruleTestSuite) TestBundleBadRequest() { } for _, testCase := range testCases { err := tu.CheckPostJSON(testDialClient, suite.urlPrefix+testCase.uri, []byte(testCase.data), - func(_ []byte, code int, _ http.Header) { + func(_ []byte, code int) { suite.Equal(testCase.ok, code == http.StatusOK) }) suite.NoError(err) diff --git a/tests/integrations/mcs/scheduling/api_test.go b/tests/integrations/mcs/scheduling/api_test.go index ea9cd2df9c5f..03db3433ec94 100644 --- a/tests/integrations/mcs/scheduling/api_test.go +++ b/tests/integrations/mcs/scheduling/api_test.go @@ -2,7 +2,6 @@ package scheduling_test import ( "context" - "encoding/json" "fmt" "net/http" "testing" @@ -10,7 +9,6 @@ import ( "github.com/stretchr/testify/suite" _ "github.com/tikv/pd/pkg/mcs/scheduling/server/apis/v1" - "github.com/tikv/pd/pkg/utils/apiutil" "github.com/tikv/pd/pkg/utils/tempurl" "github.com/tikv/pd/pkg/utils/testutil" "github.com/tikv/pd/tests" @@ -121,40 +119,21 @@ func (suite *apiTestSuite) TestAPIForward() { var resp map[string]interface{} // Test opeartor - err = testutil.ReadGetJSON(re, testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "operators"), &slice, - testutil.WithHeader(re, apiutil.ForwardToMicroServiceHeader, "true")) + err = testutil.ReadGetJSON(re, testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "operators"), &slice) re.NoError(err) re.Len(slice, 0) - err = testutil.ReadGetJSON(re, testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "operators/2"), &resp, - testutil.WithHeader(re, apiutil.ForwardToMicroServiceHeader, "true")) + err = testutil.ReadGetJSON(re, testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "operators/2"), &resp) re.NoError(err) re.Nil(resp) - // Test checker: only read-only requests are forwarded - err = testutil.ReadGetJSON(re, testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "checker/merge"), &resp, - testutil.WithHeader(re, apiutil.ForwardToMicroServiceHeader, "true")) + // Test checker + err = testutil.ReadGetJSON(re, testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "checker/merge"), &resp) re.NoError(err) suite.False(resp["paused"].(bool)) - 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), testutil.WithoutHeader(re, apiutil.PDRedirectorHeader)) - suite.NoError(err) - - // Test scheduler: only read-only requests are forwarded - err = testutil.ReadGetJSON(re, testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "schedulers"), &slice, - testutil.WithHeader(re, apiutil.ForwardToMicroServiceHeader, "true")) + // Test scheduler + err = testutil.ReadGetJSON(re, testDialClient, fmt.Sprintf("%s/%s", urlPrefix, "schedulers"), &slice) re.NoError(err) re.Contains(slice, "balance-leader-scheduler") - - 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), testutil.WithoutHeader(re, apiutil.ForwardToMicroServiceHeader)) - suite.NoError(err) }