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

bmc: add WaitForSystemPowerState function #905

Merged
merged 1 commit into from
Feb 3, 2025
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
21 changes: 21 additions & 0 deletions pkg/bmc/bmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,27 @@ func (bmc *BMC) SystemPowerState() (string, error) {
return string(system.PowerState), nil
}

// WaitForSystemPowerState waits up to timeout until the BMC returns the provided system power state.
func (bmc *BMC) WaitForSystemPowerState(powerState redfish.PowerState, timeout time.Duration) error {
if valid, err := bmc.validateRedfish(); !valid {
return err
}

glog.V(100).Infof("Waiting up to %s until BMC returns power state %s", timeout, powerState)

return wait.PollUntilContextTimeout(
context.TODO(), 10*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
systemPowerState, err := bmc.SystemPowerState()
if err != nil {
glog.V(100).Infof("Failed to get system power state from BMC: %v", err)

return false, nil
}

return systemPowerState == string(powerState), nil
})
}

// PowerUsage returns the current power usage of the chassis in watts using the Redfish API. This method uses the first
// chassis with a power link and the power control index for the BMC client.
func (bmc *BMC) PowerUsage() (float32, error) {
Expand Down
17 changes: 17 additions & 0 deletions pkg/bmc/bmc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bmc

import (
"context"
_ "embed"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -594,6 +595,22 @@ func TestBMCSystemPowerState(t *testing.T) {
assert.Equal(t, expectedPowerState, powerState)
}

func TestBMCWaitForSystemPowerState(t *testing.T) {
// Create fake redfish endpoint.
redfishServer := createFakeRedfishLocalServer(false, redfishAPIResponseCallbacks{})
defer redfishServer.Close()

host := strings.Split(redfishServer.URL, "//")[1]
bmc := New(host).WithRedfishUser(defaultUsername, defaultPassword)

// The fake endpoint should be On, so will succeed when waiting till On and time out waiting for Off.
err := bmc.WaitForSystemPowerState(redfish.OnPowerState, time.Second)
assert.NoError(t, err)

err = bmc.WaitForSystemPowerState(redfish.OffPowerState, time.Second)
assert.Equal(t, context.DeadlineExceeded, err)
}

func TestBMCPowerUsage(t *testing.T) {
// Create a fake redfish api endpoint with secureBoot "disabled"
redfishServer := createFakeRedfishLocalServer(false, redfishAPIResponseCallbacks{})
Expand Down
Loading