From 39575d21458040330b23790395db8c255813345a Mon Sep 17 00:00:00 2001 From: okJiang <819421878@qq.com> Date: Thu, 7 Nov 2024 16:24:25 +0800 Subject: [PATCH] test: add ut to test the input when creating evict-leader scheduler (#8768) ref tikv/pd#4399 Signed-off-by: okJiang <819421878@qq.com> Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com> --- server/api/scheduler.go | 7 ++++++- tests/server/api/scheduler_test.go | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/server/api/scheduler.go b/server/api/scheduler.go index 3d69674db48..8922a833a07 100644 --- a/server/api/scheduler.go +++ b/server/api/scheduler.go @@ -113,11 +113,16 @@ func (h *schedulerHandler) CreateScheduler(w http.ResponseWriter, r *http.Reques return } case types.GrantLeaderScheduler, types.EvictLeaderScheduler: - storeID, ok := input["store_id"].(float64) + _, ok := input["store_id"] if !ok { h.r.JSON(w, http.StatusBadRequest, "missing store id") return } + storeID, ok := input["store_id"].(float64) + if !ok { + h.r.JSON(w, http.StatusBadRequest, "please input a right store id") + return + } var ( exist bool err error diff --git a/tests/server/api/scheduler_test.go b/tests/server/api/scheduler_test.go index b906a72f09e..281f7c1dc7e 100644 --- a/tests/server/api/scheduler_test.go +++ b/tests/server/api/scheduler_test.go @@ -92,10 +92,24 @@ func (suite *scheduleTestSuite) checkOriginAPI(cluster *tests.TestCluster) { input := make(map[string]any) input["name"] = "evict-leader-scheduler" - input["store_id"] = 1 body, err := json.Marshal(input) - re.NoError(err) - re.NoError(tu.CheckPostJSON(tests.TestDialClient, urlPrefix, body, tu.StatusOK(re))) + suite.NoError(err) + suite.NoError(tu.CheckPostJSON(tests.TestDialClient, urlPrefix, body, + tu.Status(re, http.StatusBadRequest), + tu.StringEqual(re, "missing store id")), + ) + input["store_id"] = "abc" // bad case + body, err = json.Marshal(input) + suite.NoError(err) + suite.NoError(tu.CheckPostJSON(tests.TestDialClient, urlPrefix, body, + tu.Status(re, http.StatusBadRequest), + tu.StringEqual(re, "please input a right store id")), + ) + + input["store_id"] = 1 + body, err = json.Marshal(input) + suite.NoError(err) + suite.NoError(tu.CheckPostJSON(tests.TestDialClient, urlPrefix, body, tu.StatusOK(re))) suite.assertSchedulerExists(urlPrefix, "evict-leader-scheduler") resp := make(map[string]any)