Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taskstate constant #368

Merged
merged 5 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bmc/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ type FirmwareTaskVerifier interface {
// return values:
// state - returns one of the FirmwareTask statuses (see devices/constants.go).
// status - returns firmware task progress or other arbitrary task information.
FirmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string) (state string, status string, err error)
FirmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string) (state constants.TaskState, status string, err error)
}

// firmwareTaskVerifierProvider is an internal struct to correlate an implementation/provider and its name
Expand All @@ -416,7 +416,7 @@ type firmwareTaskVerifierProvider struct {
}

// firmwareTaskStatus returns the status of the firmware upload process.
func firmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string, generic []firmwareTaskVerifierProvider) (state, status string, metadata Metadata, err error) {
func firmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string, generic []firmwareTaskVerifierProvider) (state constants.TaskState, status string, metadata Metadata, err error) {
var metadataLocal Metadata

for _, elem := range generic {
Expand Down Expand Up @@ -446,7 +446,7 @@ func firmwareTaskStatus(ctx context.Context, kind bconsts.FirmwareInstallStep, c
}

// FirmwareTaskStatusFromInterfaces identifies implementations of the FirmwareTaskVerifier interface and passes the found implementations to the firmwareTaskStatus() wrapper.
func FirmwareTaskStatusFromInterfaces(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string, generic []interface{}) (state, status string, metadata Metadata, err error) {
func FirmwareTaskStatusFromInterfaces(ctx context.Context, kind bconsts.FirmwareInstallStep, component, taskID, installVersion string, generic []interface{}) (state constants.TaskState, status string, metadata Metadata, err error) {
metadata = newMetadata()

implementations := make([]firmwareTaskVerifierProvider, 0)
Expand Down
25 changes: 12 additions & 13 deletions bmc/firmware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/bmc-toolbox/bmclib/v2/constants"
"github.com/bmc-toolbox/bmclib/v2/errors"
bmclibErrs "github.com/bmc-toolbox/bmclib/v2/errors"
"github.com/bmc-toolbox/common"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -41,7 +40,7 @@ func TestFirmwareInstall(t *testing.T) {
providersAttempted int
}{
{"success with metadata", common.SlugBIOS, string(constants.OnReset), false, nil, "1234", nil, 5 * time.Second, "foo", 1},
{"failure with metadata", common.SlugBIOS, string(constants.OnReset), false, nil, "1234", errors.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with metadata", common.SlugBIOS, string(constants.OnReset), false, nil, "1234", bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with context timeout", common.SlugBIOS, string(constants.OnReset), false, nil, "1234", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
}

Expand Down Expand Up @@ -136,7 +135,7 @@ func TestFirmwareInstallStatus(t *testing.T) {
providersAttempted int
}{
{"success with metadata", common.SlugBIOS, "1.1", "1234", constants.FirmwareInstallComplete, nil, 5 * time.Second, "foo", 1},
{"failure with metadata", common.SlugBIOS, "1.1", "1234", constants.FirmwareInstallFailed, errors.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with metadata", common.SlugBIOS, "1.1", "1234", constants.FirmwareInstallFailed, bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with context timeout", common.SlugBIOS, "1.1", "1234", "", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
}

Expand Down Expand Up @@ -230,7 +229,7 @@ func TestFirmwareInstallUploaded(t *testing.T) {
providersAttempted int
}{
{"success with metadata", common.SlugBIOS, "1234", "5678", nil, 5 * time.Second, "foo", 1},
{"failure with metadata", common.SlugBIOS, "1234", "", errors.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with metadata", common.SlugBIOS, "1234", "", bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with context timeout", common.SlugBIOS, "1234", "", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
}

Expand Down Expand Up @@ -325,7 +324,7 @@ func TestFirmwareUpload(t *testing.T) {
providersAttempted int
}{
{"success with metadata", common.SlugBIOS, nil, "1234", nil, 5 * time.Second, "foo", 1},
{"failure with metadata", common.SlugBIOS, nil, "1234", errors.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with metadata", common.SlugBIOS, nil, "1234", bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with context timeout", common.SlugBIOS, nil, "1234", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
}

Expand Down Expand Up @@ -430,7 +429,7 @@ func TestFirmwareInstallSteps(t *testing.T) {
providersAttempted int
}{
{"success with metadata", common.SlugBIOS, []constants.FirmwareInstallStep{constants.FirmwareInstallStepUpload, constants.FirmwareInstallStepInstallStatus}, nil, 5 * time.Second, "foo", 1},
{"failure with metadata", common.SlugBIOS, nil, errors.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with metadata", common.SlugBIOS, nil, bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with context timeout", common.SlugBIOS, nil, context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
}

Expand Down Expand Up @@ -459,12 +458,12 @@ func TestFirmwareInstallSteps(t *testing.T) {
}

type firmwareTaskStatusTester struct {
returnState string
returnState constants.TaskState
returnStatus string
returnError error
}

func (f *firmwareTaskStatusTester) FirmwareTaskStatus(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string) (state string, status string, err error) {
func (f *firmwareTaskStatusTester) FirmwareTaskStatus(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string) (state constants.TaskState, status string, err error) {
return f.returnState, f.returnStatus, f.returnError
}

Expand All @@ -479,15 +478,15 @@ func TestFirmwareTaskStatus(t *testing.T) {
component string
taskID string
installVersion string
returnState string
returnState constants.TaskState
returnStatus string
returnError error
ctxTimeout time.Duration
providerName string
providersAttempted int
}{
{"success with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.FirmwareInstallComplete, "Upload completed", nil, 5 * time.Second, "foo", 1},
{"failure with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.FirmwareInstallFailed, "Upload failed", errors.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.FirmwareInstallFailed, "Upload failed", bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with context timeout", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", "", "", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
}

Expand Down Expand Up @@ -523,15 +522,15 @@ func TestFirmwareTaskStatusFromInterfaces(t *testing.T) {
component string
taskID string
installVersion string
returnState string
returnState constants.TaskState
returnStatus string
returnError error
ctxTimeout time.Duration
providerName string
providersAttempted int
}{
{"success with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.FirmwareInstallComplete, "uploading", nil, 5 * time.Second, "foo", 1},
{"failure with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.FirmwareInstallFailed, "failed", errors.ErrNon200Response, 5 * time.Second, "foo", 1},
{"success with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.Complete, "uploading", nil, 5 * time.Second, "foo", 1},
{"failure with metadata", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", constants.Failed, "failed", bmclibErrs.ErrNon200Response, 5 * time.Second, "foo", 1},
{"failure with context timeout", constants.FirmwareInstallStepUpload, common.SlugBIOS, "1234", "1.0", "", "", context.DeadlineExceeded, 1 * time.Nanosecond, "foo", 1},
}

Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@
}

// FirmwareTaskStatus pass through library function to check firmware task statuses
func (c *Client) FirmwareTaskStatus(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string) (state, status string, err error) {
func (c *Client) FirmwareTaskStatus(ctx context.Context, kind constants.FirmwareInstallStep, component, taskID, installVersion string) (state constants.TaskState, status string, err error) {

Check warning on line 626 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L626

Added line #L626 was not covered by tests
ctx, span := c.traceprovider.Tracer(pkgName).Start(ctx, "FirmwareTaskStatus")
defer span.End()

Expand Down
29 changes: 18 additions & 11 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ type (

// The FirmwareInstallStep identifies each phase of a firmware install process.
FirmwareInstallStep string

TaskState string
)

const (
// Unknown is the constant that defines unknown things
Unknown = "Unknown"

// EnvEnableDebug is the const for the environment variable to cause bmclib to dump debugging debugging information.
// the valid parameter for this environment variable is 'true'
EnvEnableDebug = "DEBUG_BMCLIB"
Expand Down Expand Up @@ -49,33 +48,41 @@ const (
// FirmwareInstallInitializing indicates the device is performing init actions to install the update
// this covers the redfish states - 'starting', 'downloading'
// no action is required from the callers part in this state
FirmwareInstallInitializing = "initializing"
FirmwareInstallInitializing = "initializing"
Initializing TaskState = "initializing"

// FirmwareInstallQueued indicates the device has queued the update, but has not started the update task yet
// this covers the redfish states - 'pending', 'new'
// no action is required from the callers part in this state
FirmwareInstallQueued = "queued"
FirmwareInstallQueued = "queued"
Queued TaskState = "queued"

// FirmwareInstallRunner indicates the device is installing the update
// this covers the redfish states - 'running', 'stopping', 'cancelling'
// no action is required from the callers part in this state
FirmwareInstallRunning = "running"
FirmwareInstallRunning = "running"
Running TaskState = "running"

// FirmwareInstallComplete indicates the device completed the firmware install
// this covers the redfish state - 'complete'
FirmwareInstallComplete = "complete"
FirmwareInstallComplete = "complete"
Complete TaskState = "complete"

// FirmwareInstallFailed indicates the firmware install failed
// this covers the redfish states - 'interrupted', 'killed', 'exception', 'cancelled', 'suspended'
FirmwareInstallFailed = "failed"
FirmwareInstallFailed = "failed"
Failed TaskState = "failed"

// FirmwareInstallPowerCycleHost indicates the firmware install requires a host power cycle
FirmwareInstallPowerCycleHost = "powercycle-host"
FirmwareInstallPowerCycleHost = "powercycle-host"
PowerCycleHost TaskState = "powercycle-host"

// FirmwareInstallPowerCycleBMC indicates the firmware install requires a BMC power cycle
FirmwareInstallPowerCycleBMC = "powercycle-bmc"
FirmwareInstallPowerCycleBMC = "powercycle-bmc"
PowerCycleBMC TaskState = "powercycle-bmc"

FirmwareInstallUnknown = "unknown"
FirmwareInstallUnknown = "unknown"
Unknown TaskState = "unknown"

// FirmwareInstallStepUploadInitiateInstall identifies the step to upload _and_ initialize the firmware install.
// as part of the same call.
Expand Down
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))
}
}
Loading