Skip to content

Commit

Permalink
redfishwrapper: to return constants.TaskState
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Nov 20, 2023
1 parent bae0b9a commit d47fb27
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 17 deletions.
36 changes: 27 additions & 9 deletions internal/redfishwrapper/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
gofishrf "github.com/stmcginnis/gofish/redfish"
)

var (
errUnexpectedTaskState = errors.New("unexpected task state")
)

func (c *Client) Task(ctx context.Context, taskID string) (*gofishrf.Task, error) {
tasks, err := c.Tasks(ctx)
if err != nil {
Expand All @@ -28,29 +32,43 @@ func (c *Client) Task(ctx context.Context, taskID string) (*gofishrf.Task, error
return nil, bmclibErrs.ErrTaskNotFound
}

func (c *Client) TaskStatus(ctx context.Context, taskID string) (state, status string, err error) {
func (c *Client) TaskStatus(ctx context.Context, taskID string) (constants.TaskState, string, error) {
task, err := c.Task(ctx, taskID)
if err != nil {
return "", "", errors.Wrap(err, "error querying redfish for taskID: "+taskID)
}
taskInfo := fmt.Sprintf("id: %s, state: %s, status: %s", task.ID, task.TaskState, task.TaskStatus)

state = strings.ToLower(string(task.TaskState))
state := strings.ToLower(string(task.TaskState))
return c.ConvertTaskState(state), taskInfo, nil
}

func (c *Client) ConvertTaskState(state string) constants.TaskState {
switch state {
case "starting", "downloading", "downloaded":
return constants.FirmwareInstallInitializing, taskInfo, nil
return constants.Initializing
case "running", "stopping", "cancelling", "scheduling":
return constants.FirmwareInstallRunning, taskInfo, nil
return constants.Running
case "pending", "new":
return constants.FirmwareInstallQueued, taskInfo, nil
return constants.Queued
case "scheduled":
return constants.FirmwareInstallPowerCycleHost, taskInfo, nil
return constants.PowerCycleHost
case "interrupted", "killed", "exception", "cancelled", "suspended", "failed":
return constants.FirmwareInstallFailed, taskInfo, nil
return constants.Failed
case "completed":
return constants.FirmwareInstallComplete, taskInfo, nil
return constants.Complete
default:
return constants.Unknown
}
}

func (c *Client) TaskStateActive(state constants.TaskState) (bool, error) {
switch state {
case constants.Initializing, constants.Running, constants.Queued:
return true, nil
case constants.Complete, constants.Failed:
return false, nil
default:
return constants.FirmwareInstallUnknown, taskInfo, nil
return false, errors.Wrap(errUnexpectedTaskState, string(state))
}
}
86 changes: 78 additions & 8 deletions internal/redfishwrapper/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,76 @@ import (
"github.com/stretchr/testify/assert"
)

func TestConvertTaskState(t *testing.T) {
testCases := []struct {
testName string
state string
expected constants.TaskState
}{
{"starting state", "starting", constants.Initializing},
{"downloading state", "downloading", constants.Initializing},
{"downloaded state", "downloaded", constants.Initializing},
{"running state", "running", constants.Running},
{"stopping state", "stopping", constants.Running},
{"cancelling state", "cancelling", constants.Running},
{"scheduling state", "scheduling", constants.Running},
{"pending state", "pending", constants.Queued},
{"new state", "new", constants.Queued},
{"scheduled state", "scheduled", constants.PowerCycleHost},
{"interrupted state", "interrupted", constants.Failed},
{"killed state", "killed", constants.Failed},
{"exception state", "exception", constants.Failed},
{"cancelled state", "cancelled", constants.Failed},
{"suspended state", "suspended", constants.Failed},
{"failed state", "failed", constants.Failed},
{"completed state", "completed", constants.Complete},
{"unknown state", "unknown_state", constants.Unknown},
}

client := Client{}
for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
result := client.ConvertTaskState(tc.state)
assert.Equal(t, tc.expected, result)
})
}
}

func TestTaskStateActive(t *testing.T) {
testCases := []struct {
testName string
taskState constants.TaskState
expected bool
err error
}{
{"active initializing", constants.Initializing, true, nil},
{"active running", constants.Running, true, nil},
{"active queued", constants.Queued, true, nil},
{"inactive complete", constants.Complete, false, nil},
{"inactive failed", constants.Failed, false, nil},
{"unknown state", "foobar", false, errUnexpectedTaskState},
}

client := &Client{}

for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
active, err := client.TaskStateActive(tc.taskState)

if tc.err != nil {
assert.ErrorIs(t, err, tc.err)
return
}

if err != nil {
t.Fatal(err)
}

assert.Equal(t, tc.expected, active)
})
}
}

func TestTaskStatus(t *testing.T) {
type hmap map[string]func(http.ResponseWriter, *http.Request)
withHandler := func(s string, f func(http.ResponseWriter, *http.Request)) hmap {
Expand All @@ -29,7 +99,7 @@ func TestTaskStatus(t *testing.T) {

tests := map[string]struct {
hmap hmap
expectedState string
expectedState constants.TaskState
expectedStatus string
expectedErr error
}{
Expand All @@ -38,7 +108,7 @@ func TestTaskStatus(t *testing.T) {
"/redfish/v1/TaskService/Tasks/1",
endpointFunc(t, "tasks/tasks_1_starting.json"),
),
expectedState: constants.FirmwareInstallInitializing,
expectedState: constants.Initializing,
expectedStatus: "id: 1, state: Starting, status: OK",
expectedErr: nil,
},
Expand All @@ -47,7 +117,7 @@ func TestTaskStatus(t *testing.T) {
"/redfish/v1/TaskService/Tasks/1",
endpointFunc(t, "tasks/tasks_1_running.json"),
),
expectedState: constants.FirmwareInstallRunning,
expectedState: constants.Running,
expectedStatus: "id: 1, state: Running, status: OK",
expectedErr: nil,
},
Expand All @@ -56,7 +126,7 @@ func TestTaskStatus(t *testing.T) {
"/redfish/v1/TaskService/Tasks/1",
endpointFunc(t, "tasks/tasks_1_pending.json"),
),
expectedState: constants.FirmwareInstallQueued,
expectedState: constants.Queued,
expectedStatus: "id: 1, state: Pending, status: OK",
expectedErr: nil,
},
Expand All @@ -65,7 +135,7 @@ func TestTaskStatus(t *testing.T) {
"/redfish/v1/TaskService/Tasks/1",
endpointFunc(t, "tasks/tasks_1_scheduled.json"),
),
expectedState: constants.FirmwareInstallPowerCycleHost,
expectedState: constants.PowerCycleHost,
expectedStatus: "id: 1, state: Scheduled, status: OK",
expectedErr: nil,
},
Expand All @@ -74,7 +144,7 @@ func TestTaskStatus(t *testing.T) {
"/redfish/v1/TaskService/Tasks/1",
endpointFunc(t, "tasks/tasks_1_failed.json"),
),
expectedState: constants.FirmwareInstallFailed,
expectedState: constants.Failed,
expectedStatus: "id: 1, state: Failed, status: OK",
expectedErr: nil,
},
Expand All @@ -83,7 +153,7 @@ func TestTaskStatus(t *testing.T) {
"/redfish/v1/TaskService/Tasks/1",
endpointFunc(t, "tasks/tasks_1_completed.json"),
),
expectedState: constants.FirmwareInstallComplete,
expectedState: constants.Complete,
expectedStatus: "id: 1, state: Completed, status: OK",
expectedErr: nil,
},
Expand All @@ -92,7 +162,7 @@ func TestTaskStatus(t *testing.T) {
"/redfish/v1/TaskService/Tasks/1",
endpointFunc(t, "tasks/tasks_1_unknown.json"),
),
expectedState: constants.FirmwareInstallUnknown,
expectedState: constants.Unknown,
expectedStatus: "id: 1, state: foobared, status: OK",
expectedErr: nil,
},
Expand Down

0 comments on commit d47fb27

Please sign in to comment.