Skip to content

Commit

Permalink
fix: Add correct http status codes
Browse files Browse the repository at this point in the history
* We are always returning 200 for all responses

* Set correct response codes based on actual response

* Update tests

Signed-off-by: Mahendra Paipuri <[email protected]>
  • Loading branch information
mahendrapaipuri committed Jan 5, 2024
1 parent 4f190a7 commit f477ba9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cmd/batchjob_stats_server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func TestBatchjobStatsExecutable(t *testing.T) {
}

jobstats := exec.Command(
binary, "--path.data", tmpDir, "--slurm.sacct.path", tmpSacctPath,
binary, "--path.data", tmpDir,
"--slurm.sacct.path", tmpSacctPath,
"--batch.scheduler.slurm",
"--web.listen-address", address,
)
Expand Down
6 changes: 3 additions & 3 deletions pkg/jobstats/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"
)

func queryExporter(address string) error {
resp, err := http.Get(fmt.Sprintf("http://%s/api/jobs", address))
func queryServer(address string) error {
resp, err := http.Get(fmt.Sprintf("http://%s/api/health", address))
if err != nil {
return err
}
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestBatchStatsServerMain(t *testing.T) {

// Query exporter
for i := 0; i < 10; i++ {
if err := queryExporter("localhost:9020"); err == nil {
if err := queryServer("localhost:9020"); err == nil {
break
}
time.Sleep(500 * time.Millisecond)
Expand Down
11 changes: 9 additions & 2 deletions pkg/jobstats/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ func (s *JobstatsServer) setHeaders(w http.ResponseWriter) {
func (s *JobstatsServer) accounts(w http.ResponseWriter, r *http.Request) {
var response base.AccountsResponse
s.setHeaders(w)
w.WriteHeader(http.StatusOK)

// Get current user from header
loggedUser, dashboardUser := s.getUser(r)
// If no user found, return empty response
if loggedUser == "" {
w.WriteHeader(http.StatusUnauthorized)
response = base.AccountsResponse{
Response: base.Response{
Status: "error",
Expand All @@ -211,6 +211,7 @@ func (s *JobstatsServer) accounts(w http.ResponseWriter, r *http.Request) {
accounts, err := s.Accounts(s.dbConfig.JobstatsDBTable, dashboardUser, s.logger)
if err != nil {
level.Error(s.logger).Log("msg", "Failed to fetch accounts", "user", dashboardUser, "err", err)
w.WriteHeader(http.StatusInternalServerError)
response = base.AccountsResponse{
Response: base.Response{
Status: "error",
Expand All @@ -228,6 +229,7 @@ func (s *JobstatsServer) accounts(w http.ResponseWriter, r *http.Request) {
}

// Write response
w.WriteHeader(http.StatusOK)
response = base.AccountsResponse{
Response: base.Response{
Status: "success",
Expand Down Expand Up @@ -264,7 +266,6 @@ func (s *JobstatsServer) jobs(w http.ResponseWriter, r *http.Request) {
var fromTime, toTime time.Time
var response base.JobsResponse
s.setHeaders(w)
w.WriteHeader(http.StatusOK)

// Initialise utility vars
checkQueryWindow := true // Check query window size
Expand All @@ -274,6 +275,7 @@ func (s *JobstatsServer) jobs(w http.ResponseWriter, r *http.Request) {
loggedUser, dashboardUser := s.getUser(r)
// If no user found, return empty response
if loggedUser == "" {
w.WriteHeader(http.StatusUnauthorized)
s.jobsErrorResponse("User Error", "No user identified", w)
return
}
Expand Down Expand Up @@ -328,6 +330,7 @@ func (s *JobstatsServer) jobs(w http.ResponseWriter, r *http.Request) {
// Return error response if from is not a timestamp
if ts, err := strconv.Atoi(f); err != nil {
level.Error(s.logger).Log("msg", "Failed to parse from timestamp", "from", f, "err", err)
w.WriteHeader(http.StatusBadRequest)
s.jobsErrorResponse("Internal server error", "Malformed 'from' timestamp", w)
return
} else {
Expand All @@ -341,6 +344,7 @@ func (s *JobstatsServer) jobs(w http.ResponseWriter, r *http.Request) {
// Return error response if to is not a timestamp
if ts, err := strconv.Atoi(t); err != nil {
level.Error(s.logger).Log("msg", "Failed to parse to timestamp", "to", t, "err", err)
w.WriteHeader(http.StatusBadRequest)
s.jobsErrorResponse("Internal server error", "Malformed 'to' timestamp", w)
return
} else {
Expand All @@ -357,6 +361,7 @@ func (s *JobstatsServer) jobs(w http.ResponseWriter, r *http.Request) {
"from", fromTime.Format(time.DateTime), "to", toTime.Format(time.DateTime),
"queryWindow", toTime.Sub(fromTime).String(),
)
w.WriteHeader(http.StatusBadRequest)
s.jobsErrorResponse("Internal server error", "Maximum query window exceeded", w)
return
}
Expand All @@ -373,11 +378,13 @@ queryJobs:
jobs, err := s.Jobs(q, s.logger)
if err != nil {
level.Error(s.logger).Log("msg", "Failed to fetch jobs", "loggedUser", loggedUser, "err", err)
w.WriteHeader(http.StatusInternalServerError)
s.jobsErrorResponse("Internal server error", "Failed to fetch user jobs", w)
return
}

// Write response
w.WriteHeader(http.StatusOK)
response = base.JobsResponse{
Response: base.Response{
Status: "success",
Expand Down

0 comments on commit f477ba9

Please sign in to comment.