Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add ut to test the input when creating evict-leader scheduler #8768

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion server/api/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member Author

@okJiang okJiang Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_, ok := input["store_id"] represents the input("store_id") doesn't exist.

storeID, ok := input["store_id"].(float64) represents the input("store_id") can not be convert to float.

So I split them to report different error messages.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can put it here. It won't be bad at all.

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
Expand Down
20 changes: 17 additions & 3 deletions tests/server/api/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down