Skip to content

Commit

Permalink
worker: initialize device queryor earlier, invoke FirmwareInstallSteps
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Nov 20, 2023
1 parent dd39923 commit d8ec55d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
37 changes: 26 additions & 11 deletions internal/worker/task_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@ var (
// taskHandler implements the taskTransitionHandler methods
type taskHandler struct{}

func (h *taskHandler) Init(_ sw.StateSwitch, _ sw.TransitionArgs) error {
func (h *taskHandler) Init(_ sw.StateSwitch, args sw.TransitionArgs) error {
tctx, ok := args.(*sm.HandlerContext)
if !ok {
return sm.ErrInvalidtaskHandlerContext
}

if tctx.DeviceQueryor == nil {
// TODO(joel): DeviceQueryor is to be instantiated based on the method(s) for the firmwares to be installed
// if its a mix of inband, out of band firmware to be installed, then both are to be queried and
// so this DeviceQueryor would have to be extended
//
// For this to work with both inband and out of band, the firmware set data should include the install method.
tctx.DeviceQueryor = outofband.NewDeviceQueryor(tctx.Ctx, tctx.Asset, tctx.Logger)
}

return nil
}

Expand Down Expand Up @@ -231,15 +245,6 @@ func (h *taskHandler) planFromFirmwareSet(tctx *sm.HandlerContext, task *model.T

// query device components inventory from the device itself.
func (h *taskHandler) queryFromDevice(tctx *sm.HandlerContext) (model.Components, error) {
if tctx.DeviceQueryor == nil {
// TODO(joel): DeviceQueryor is to be instantiated based on the method(s) for the firmwares to be installed
// if its a mix of inband, out of band firmware to be installed, then both are to be queried and
// so this DeviceQueryor would have to be extended
//
// For this to work with both inband and out of band, the firmware set data should include the install method.
tctx.DeviceQueryor = outofband.NewDeviceQueryor(tctx.Ctx, tctx.Asset, tctx.Logger)
}

tctx.Task.Status.Append("connecting to device BMC")
tctx.Publisher.Publish(tctx)

Expand Down Expand Up @@ -305,11 +310,21 @@ func (h *taskHandler) planInstall(hCtx *sm.HandlerContext, task *model.Task, fir
// generate an action ID
actionID := sm.ActionID(task.ID.String(), firmware.Component, idx)

steps, err := hCtx.DeviceQueryor.FirmwareInstallSteps(hCtx.Ctx, firmware.Component)
if err != nil {
return nil, nil, err
}

errFirmwareInstallSteps := errors.New("no firmware install steps identified for component")
if len(steps) == 0 {
return nil, nil, errors.Wrap(errFirmwareInstallSteps, firmware.Component)
}

// TODO: The firmware is to define the preferred install method
// based on that the action plan is setup.
//
// For now this is hardcoded to outofband.
m, err := outofband.NewActionStateMachine(actionID)
m, err := outofband.NewActionStateMachine(actionID, steps)
if err != nil {
return nil, nil, err
}
Expand Down
18 changes: 17 additions & 1 deletion internal/worker/task_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package worker
import (
"testing"

"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/metal-toolbox/flasher/internal/fixtures"
"github.com/metal-toolbox/flasher/internal/model"
sm "github.com/metal-toolbox/flasher/internal/statemachine"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.hollow.sh/toolbox/events/registry"

bconsts "github.com/bmc-toolbox/bmclib/v2/constants"
rctypes "github.com/metal-toolbox/rivets/condition"
)

Expand Down Expand Up @@ -160,6 +163,11 @@ func TestPlanInstall(t *testing.T) {
Component: "bios",
},
}

ctrl := gomock.NewController(t)
defer ctrl.Finish()
q := fixtures.NewMockDeviceQueryor(ctrl)

serverID := uuid.MustParse("fa125199-e9dd-47d4-8667-ce1d26f58c4a")
taskID := uuid.MustParse("05c3296d-be5d-473a-b90c-4ce66cfdec65")
ctx := &sm.HandlerContext{
Expand All @@ -180,7 +188,8 @@ func TestPlanInstall(t *testing.T) {
Task: &model.Task{
ID: taskID,
},
WorkerID: registry.GetID("test-app"),
WorkerID: registry.GetID("test-app"),
DeviceQueryor: q,
}

h := &taskHandler{}
Expand All @@ -192,6 +201,13 @@ func TestPlanInstall(t *testing.T) {
},
}

q.EXPECT().FirmwareInstallSteps(gomock.Any(), gomock.Any()).
Times(1).
Return([]bconsts.FirmwareInstallStep{
bconsts.FirmwareInstallStepUploadInitiateInstall,
bconsts.FirmwareInstallStepInstallStatus,
}, nil)

sms, actions, err := h.planInstall(ctx, taskParam, fwSet)
require.NoError(t, err)
require.Equal(t, 1, len(sms))
Expand Down

0 comments on commit d8ec55d

Please sign in to comment.