This is an automated email from the ASF dual-hosted git repository. mani pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/yunikorn-core.git
The following commit(s) were added to refs/heads/master by this push: new 9e310fd6 [YUNIKORN-2164] Use ParseUint instead of ParseInt in getEvents() (#721) 9e310fd6 is described below commit 9e310fd6b4b68eadda1b1bc1f2db69d161fc450c Author: Peter Bacsko <pbac...@cloudera.com> AuthorDate: Mon Nov 20 21:54:46 2023 +0530 [YUNIKORN-2164] Use ParseUint instead of ParseInt in getEvents() (#721) Closes: #721 Signed-off-by: Manikandan R <maniraj...@gmail.com> --- pkg/webservice/handlers.go | 16 ++++++---------- pkg/webservice/handlers_test.go | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go index 4b7cacdd..382dd230 100644 --- a/pkg/webservice/handlers.go +++ b/pkg/webservice/handlers.go @@ -914,29 +914,25 @@ func getEvents(w http.ResponseWriter, r *http.Request) { var start uint64 if countStr := r.URL.Query().Get("count"); countStr != "" { - c, err := strconv.ParseInt(countStr, 10, 64) + var err error + count, err = strconv.ParseUint(countStr, 10, 64) if err != nil { buildJSONErrorResponse(w, err.Error(), http.StatusBadRequest) return } - if c <= 0 { - buildJSONErrorResponse(w, fmt.Sprintf("Illegal number of events: %d", c), http.StatusBadRequest) + if count == 0 { + buildJSONErrorResponse(w, `0 is not a valid value for "count"`, http.StatusBadRequest) return } - count = uint64(c) } if startStr := r.URL.Query().Get("start"); startStr != "" { - i, err := strconv.ParseInt(startStr, 10, 64) + var err error + start, err = strconv.ParseUint(startStr, 10, 64) if err != nil { buildJSONErrorResponse(w, err.Error(), http.StatusBadRequest) return } - if i < 0 { - buildJSONErrorResponse(w, fmt.Sprintf("Illegal id: %d", i), http.StatusBadRequest) - return - } - start = uint64(i) } records, lowestID, highestID := eventSystem.GetEventsFromID(start, count) diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index 713564d8..d67c63e0 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -1537,10 +1537,11 @@ func TestGetEvents(t *testing.T) { checkSingleEvent(t, queueEvent, "start=2") // illegal requests - checkIllegalBatchRequest(t, "count=xyz", "strconv.ParseInt: parsing \"xyz\": invalid syntax") - checkIllegalBatchRequest(t, "count=-100", "Illegal number of events: -100") - checkIllegalBatchRequest(t, "start=xyz", "strconv.ParseInt: parsing \"xyz\": invalid syntax") - checkIllegalBatchRequest(t, "start=-100", "Illegal id: -100") + checkIllegalBatchRequest(t, "count=xyz", `strconv.ParseUint: parsing "xyz": invalid syntax`) + checkIllegalBatchRequest(t, "count=-100", `strconv.ParseUint: parsing "-100": invalid syntax`) + checkIllegalBatchRequest(t, "count=0", `0 is not a valid value for "count`) + checkIllegalBatchRequest(t, "start=xyz", `strconv.ParseUint: parsing "xyz": invalid syntax`) + checkIllegalBatchRequest(t, "start=-100", `strconv.ParseUint: parsing "-100": invalid syntax`) } func TestGetEventsWhenTrackingDisabled(t *testing.T) { @@ -1564,6 +1565,7 @@ func TestGetEventsWhenTrackingDisabled(t *testing.T) { } func addEvents(t *testing.T) (appEvent, nodeEvent, queueEvent *si.EventRecord) { + t.Helper() events.Init() ev := events.GetEventSystem().(*events.EventSystemImpl) //nolint:errcheck ev.StartServiceWithPublisher(false) @@ -1622,12 +1624,14 @@ func checkSingleEvent(t *testing.T, event *si.EventRecord, query string) { } func checkIllegalBatchRequest(t *testing.T, query, msg string) { + t.Helper() req, err := http.NewRequest("GET", "/ws/v1/events/batch?"+query, strings.NewReader("")) assert.NilError(t, err) readIllegalRequest(t, req, msg) } func readIllegalRequest(t *testing.T, req *http.Request, errMsg string) { + t.Helper() rr := httptest.NewRecorder() handler := http.HandlerFunc(getEvents) handler.ServeHTTP(rr, req) @@ -1638,10 +1642,11 @@ func readIllegalRequest(t *testing.T, req *http.Request, errMsg string) { var errObject dao.YAPIError err = json.Unmarshal(jsonBytes[:n], &errObject) assert.NilError(t, err, "cannot unmarshal events dao") - assert.Assert(t, strings.Contains(errObject.Message, errMsg)) + assert.Assert(t, strings.Contains(errObject.Message, errMsg), "Error message [%s] not found inside the string: [%s]", errMsg, errObject.Message) } func checkAllEvents(t *testing.T, events []*si.EventRecord) { + t.Helper() req, err := http.NewRequest("GET", "/ws/v1/events/batch/", strings.NewReader("")) assert.NilError(t, err) eventDao := getEventRecordDao(t, req) @@ -1652,6 +1657,7 @@ func checkAllEvents(t *testing.T, events []*si.EventRecord) { } func compareEvents(t *testing.T, event, eventFromDao *si.EventRecord) { + t.Helper() assert.Equal(t, event.TimestampNano, eventFromDao.TimestampNano) assert.Equal(t, event.EventChangeType, eventFromDao.EventChangeType) assert.Equal(t, event.EventChangeDetail, eventFromDao.EventChangeDetail) @@ -1664,6 +1670,7 @@ func compareEvents(t *testing.T, event, eventFromDao *si.EventRecord) { } func getEventRecordDao(t *testing.T, req *http.Request) dao.EventRecordDAO { + t.Helper() rr := httptest.NewRecorder() handler := http.HandlerFunc(getEvents) handler.ServeHTTP(rr, req) --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@yunikorn.apache.org For additional commands, e-mail: issues-h...@yunikorn.apache.org