diff --git a/pkg/webservice/dao/scheduler-health.go b/pkg/webservice/dao/scheduler_health.go similarity index 75% rename from pkg/webservice/dao/scheduler-health.go rename to pkg/webservice/dao/scheduler_health.go index 020b8d638..d27367362 100644 --- a/pkg/webservice/dao/scheduler-health.go +++ b/pkg/webservice/dao/scheduler_health.go @@ -29,17 +29,3 @@ type HealthCheckInfo struct { Description string `json:",omitempty"` DiagnosisMessage string `json:",omitempty"` } - -func (s *SchedulerHealthDAOInfo) SetHealthStatus() { - s.Healthy = len(s.HealthChecks) == 0 -} - -func (s *SchedulerHealthDAOInfo) AddHealthCheckInfo(succeeded bool, name, description, diagnosis string) { - info := HealthCheckInfo{ - Name: name, - Succeeded: succeeded, - Description: description, - DiagnosisMessage: diagnosis, - } - s.HealthChecks = append(s.HealthChecks, info) -} diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go index 3647e8793..72ba51494 100644 --- a/pkg/webservice/handlers.go +++ b/pkg/webservice/handlers.go @@ -503,8 +503,8 @@ func checkHealthStatus(w http.ResponseWriter, r *http.Request) { } } } else { - log.Log(log.SchedHealth).Info("The healthy status of scheduler is not found", zap.Any("health check info", "")) - buildJSONErrorResponse(w, "The healthy status of scheduler is not found", http.StatusNotFound) + log.Log(log.SchedHealth).Info("Health check is not available") + buildJSONErrorResponse(w, "Health check is not available", http.StatusNotFound) } } diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go index e6e70ea40..cc669958e 100644 --- a/pkg/webservice/handlers_test.go +++ b/pkg/webservice/handlers_test.go @@ -1719,3 +1719,67 @@ func verifyStateDumpJSON(t *testing.T, aggregated *AggregatedStateInfo) { assert.Check(t, len(aggregated.Config.SchedulerConfig.Partitions) > 0) assert.Check(t, len(aggregated.Config.Extra) > 0) } + +func TestCheckHealthStatusNotFound(t *testing.T) { + NewWebApp(&scheduler.ClusterContext{}, nil) + req, err := http.NewRequest("GET", "/ws/v1/scheduler/healthcheck", strings.NewReader("")) + assert.NilError(t, err, "Error while creating the healthcheck request") + resp := &MockResponseWriter{} + checkHealthStatus(resp, req) + + var errInfo dao.YAPIError + err = json.Unmarshal(resp.outputBytes, &errInfo) + assert.NilError(t, err, unmarshalError) + assert.Equal(t, http.StatusNotFound, errInfo.StatusCode, statusCodeError) + assert.Equal(t, "Health check is not available", errInfo.Message, jsonMessageError) +} + +func TestCheckHealthStatus(t *testing.T) { + runHealthCheckTest(t, &dao.SchedulerHealthDAOInfo{ + Healthy: true, + HealthChecks: []dao.HealthCheckInfo{ + { + Name: "Scheduling errors", + Succeeded: true, + Description: "Check for scheduling error entries in metrics", + DiagnosisMessage: "There were 0 scheduling errors logged in the metrics", + }, + }, + }) + + runHealthCheckTest(t, &dao.SchedulerHealthDAOInfo{ + Healthy: false, + HealthChecks: []dao.HealthCheckInfo{ + { + Name: "Failed nodes", + Succeeded: false, + Description: "Check for failed nodes entries in metrics", + DiagnosisMessage: "There were 1 failed nodes logged in the metrics", + }, + }, + }) +} + +func runHealthCheckTest(t *testing.T, expected *dao.SchedulerHealthDAOInfo) { + schedulerContext := &scheduler.ClusterContext{} + schedulerContext.SetLastHealthCheckResult(expected) + NewWebApp(schedulerContext, nil) + + req, err := http.NewRequest("GET", "/ws/v1/scheduler/healthcheck", strings.NewReader("")) + assert.NilError(t, err, "Error while creating the healthcheck request") + resp := &MockResponseWriter{} + checkHealthStatus(resp, req) + + var actual dao.SchedulerHealthDAOInfo + err = json.Unmarshal(resp.outputBytes, &actual) + assert.NilError(t, err, unmarshalError) + assert.Equal(t, expected.Healthy, actual.Healthy) + assert.Equal(t, len(expected.HealthChecks), len(actual.HealthChecks)) + for i, expectedHealthCheck := range expected.HealthChecks { + actualHealthCheck := actual.HealthChecks[i] + assert.Equal(t, expectedHealthCheck.Name, actualHealthCheck.Name) + assert.Equal(t, expectedHealthCheck.Succeeded, actualHealthCheck.Succeeded) + assert.Equal(t, expectedHealthCheck.Description, actualHealthCheck.Description) + assert.Equal(t, expectedHealthCheck.DiagnosisMessage, actualHealthCheck.DiagnosisMessage) + } +}