Skip to content

Commit

Permalink
Add function for getting openbmc status (testable)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofaurax committed Aug 10, 2023
1 parent cea07b8 commit 82e20fa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
30 changes: 30 additions & 0 deletions providers/redfish/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package redfish

import (
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
Expand Down Expand Up @@ -171,3 +172,32 @@ func (c *Conn) dellJobs(state string) ([]*gofishrf.Task, error) {

return tasks, nil
}

func (c *Conn) openbmcGetStatus(jsonstr []byte) (state string, err error) {
type TaskMsg struct {
Message string
}

type TaskStatus struct {
TaskState string
TaskStatus string
Messages []TaskMsg
}

var status TaskStatus

err = json.Unmarshal(jsonstr, &status)
if err != nil {
fmt.Println(err)
} else {
state = strings.ToLower(status.TaskState)
if state != "running" {
// Display all messages when not running (failed or completed)
fmt.Println(status.TaskState, status.TaskStatus)
for _, m := range status.Messages {
fmt.Println(m.Message)

Check warning on line 198 in providers/redfish/tasks.go

View check run for this annotation

Codecov / codecov/patch

providers/redfish/tasks.go#L198

Added line #L198 was not covered by tests
}
}
}
return state, err
}
28 changes: 28 additions & 0 deletions providers/redfish/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,31 @@ func Test_dellPurgeScheduledFirmwareInstallJob(t *testing.T) {
t.Fatal(err)
}
}

func Test_openbmcGetStatus(t *testing.T) {
var err error
var state string

// empty (invalid json)
_, err = mockClient.openbmcGetStatus([]byte(""))
if err == nil {
t.Fatal("no error with empty invalid json")
}

// empty valid json
_, err = mockClient.openbmcGetStatus([]byte("{}"))
if err != nil {
t.Fatal(err)
}

// empty valid json
state, err = mockClient.openbmcGetStatus([]byte(
"{\"Id\":\"15\", \"TaskState\": \"TestState\", \"TaskStatus\": \"TestStatus\"}",
))
if err != nil {
t.Fatal(err)
}
if state != "teststate" {
t.Fatal("Wrong test state:", state)
}
}

0 comments on commit 82e20fa

Please sign in to comment.