pbacsko commented on a change in pull request #336: URL: https://github.com/apache/incubator-yunikorn-core/pull/336#discussion_r748274190
########## File path: pkg/webservice/handlers_test.go ########## @@ -1279,3 +1282,159 @@ func TestGetNodesUtilization(t *testing.T) { assert.NilError(t, err) assert.Equal(t, len(nodesDao), 0) } + +func TestGetFullStateDump(t *testing.T) { + schedulerContext = prepareSchedulerContext(t) + + imHistory = history.NewInternalMetricsHistory(5) + req, err := http.NewRequest("GET", "/ws/v1/getfullstatedump", strings.NewReader("")) + req = mux.SetURLVars(req, make(map[string]string)) + assert.NilError(t, err) + resp := &MockResponseWriter{} + + getFullStateDump(resp, req) + receivedBytes := resp.outputBytes + statusCode := resp.statusCode + assert.Assert(t, len(receivedBytes) > 0, "json response is empty") + assert.Check(t, statusCode != http.StatusInternalServerError, "response status code") + var aggregated AggregatedStateInfo + if err := json.Unmarshal(receivedBytes, &aggregated); err != nil { + t.Fatal(err) + } + verifyStateDumpJSON(t, &aggregated) +} + +func TestEnableDisablePeriodicStateDump(t *testing.T) { + schedulerContext = prepareSchedulerContext(t) + defer deleteStateDumpFile() + defer terminateGoroutine() + + imHistory = history.NewInternalMetricsHistory(5) + req, err := http.NewRequest("GET", "/ws/v1/enableperiodicstatedump", strings.NewReader("")) + vars := map[string]string{ + "period": "3", + } + req = mux.SetURLVars(req, vars) + + assert.NilError(t, err) + resp := &MockResponseWriter{} + + // enable state dump, check file contents + enablePeriodicStateDump(resp, req) + statusCode := resp.statusCode + assert.Check(t, statusCode != http.StatusInternalServerError, "response status code") + + waitForStateDumpFile(t) + fileContents, err2 := ioutil.ReadFile(stateDumpFilePath) + if err2 != nil { + t.Fatal(err2) + } + var aggregated AggregatedStateInfo + if err3 := json.Unmarshal(fileContents, &aggregated); err3 != nil { + t.Fatal(err3) + } + verifyStateDumpJSON(t, &aggregated) + + // disable + req, err = http.NewRequest("GET", "/ws/v1/disableperiodicstatedump", strings.NewReader("")) + req = mux.SetURLVars(req, make(map[string]string)) + assert.NilError(t, err) + resp = &MockResponseWriter{} + disablePeriodicStateDump(resp, req) + statusCode = resp.statusCode + assert.Assert(t, statusCode != http.StatusInternalServerError, "response status code") +} + +func TestTryEnableStateDumpTwice(t *testing.T) { + defer deleteStateDumpFile() + defer terminateGoroutine() + + req, err := http.NewRequest("GET", "/ws/v1/enableperiodicstatedump", strings.NewReader("")) + req = mux.SetURLVars(req, make(map[string]string)) + assert.NilError(t, err) + resp := &MockResponseWriter{} + + enablePeriodicStateDump(resp, req) + enablePeriodicStateDump(resp, req) + + statusCode := resp.statusCode + assert.Assert(t, statusCode == http.StatusInternalServerError, "response status code") +} + +func TestTryDisableNotRunningStateDump(t *testing.T) { + req, err := http.NewRequest("GET", "/ws/v1/disableperiodicstatedump", strings.NewReader("")) + req = mux.SetURLVars(req, make(map[string]string)) + assert.NilError(t, err) + resp := &MockResponseWriter{} + + disablePeriodicStateDump(resp, req) + + statusCode := resp.statusCode + assert.Assert(t, statusCode == http.StatusInternalServerError, "response status code") +} + +func prepareSchedulerContext(t *testing.T) *scheduler.ClusterContext { + configs.MockSchedulerConfigByData([]byte(configDefault)) + var err error + schedulerContext, err = scheduler.NewClusterContext(rmID, policyGroup) + assert.NilError(t, err, "Error when load clusterInfo from config") + assert.Equal(t, 1, len(schedulerContext.GetPartitionMapClone())) + + return schedulerContext +} + +func waitForStateDumpFile(t *testing.T) { + for { + var attempts int + + info, err := os.Stat(stateDumpFilePath) + + // tolerate only "file not found" errors + if err != nil && !os.IsNotExist(err) { Review comment: Other error mean different things (eg. permission denied) and it does not make sense to continue (I don't expect it to happen though). "File not found" is normal, we have to wait until it is created by the background goroutine. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@yunikorn.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org