Skip to content

Commit

Permalink
[YUNIKORN-2162] Add unit test for checkHealthStatus (apache#743)
Browse files Browse the repository at this point in the history
Closes: apache#743

Signed-off-by: Peter Bacsko <[email protected]>
  • Loading branch information
brandboat authored and pbacsko committed Nov 30, 2023
1 parent b516ca1 commit 2291cea
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions pkg/webservice/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
64 changes: 64 additions & 0 deletions pkg/webservice/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 2291cea

Please sign in to comment.