-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(agent): add unit test for ipmi package
Signed-off-by: Zespre Schmidt <[email protected]>
- Loading branch information
Showing
5 changed files
with
389 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
package ipmi | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
goipmi "github.com/vmware/goipmi" | ||
"go.uber.org/mock/gomock" | ||
|
||
"kubevirt.io/kubevirtbmc/pkg/resourcemanager" | ||
) | ||
|
||
func TestChassisControlHandler(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockRM := resourcemanager.NewMockResourceManager(ctrl) | ||
handler := NewHandler(mockRM) | ||
|
||
testCases := []struct { | ||
name string | ||
chassisControl goipmi.ChassisControl | ||
expectedCall func() | ||
expectedCode goipmi.CompletionCode | ||
}{ | ||
{ | ||
name: "PowerOn success", | ||
chassisControl: goipmi.ControlPowerUp, | ||
expectedCall: func() { | ||
mockRM.EXPECT().PowerOn().Return(nil) | ||
}, | ||
expectedCode: goipmi.CommandCompleted, | ||
}, | ||
{ | ||
name: "PowerOn failure", | ||
chassisControl: goipmi.ControlPowerUp, | ||
expectedCall: func() { | ||
mockRM.EXPECT().PowerOn().Return(fmt.Errorf("error")) | ||
}, | ||
expectedCode: goipmi.ErrInvalidState, | ||
}, | ||
{ | ||
name: "PowerOff success", | ||
chassisControl: goipmi.ControlPowerDown, | ||
expectedCall: func() { | ||
mockRM.EXPECT().PowerOff().Return(nil) | ||
}, | ||
expectedCode: goipmi.CommandCompleted, | ||
}, | ||
{ | ||
name: "PowerOff failure", | ||
chassisControl: goipmi.ControlPowerDown, | ||
expectedCall: func() { | ||
mockRM.EXPECT().PowerOff().Return(fmt.Errorf("error")) | ||
}, | ||
expectedCode: goipmi.ErrInvalidState, | ||
}, | ||
{ | ||
name: "PowerCycle success", | ||
chassisControl: goipmi.ControlPowerCycle, | ||
expectedCall: func() { | ||
mockRM.EXPECT().PowerCycle().Return(nil) | ||
}, | ||
expectedCode: goipmi.CommandCompleted, | ||
}, | ||
{ | ||
name: "PowerCycle failure", | ||
chassisControl: goipmi.ControlPowerCycle, | ||
expectedCall: func() { | ||
mockRM.EXPECT().PowerCycle().Return(fmt.Errorf("error")) | ||
}, | ||
expectedCode: goipmi.ErrInvalidState, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tc.expectedCall() | ||
|
||
message := &goipmi.Message{ | ||
Data: []byte{byte(tc.chassisControl)}, | ||
} | ||
|
||
response := handler.chassisControlHandler(message) | ||
|
||
assert.IsType(t, &goipmi.ChassisControlResponse{}, response) | ||
res, _ := response.(*goipmi.ChassisControlResponse) | ||
assert.Equal(t, tc.expectedCode, res.CompletionCode) | ||
}) | ||
} | ||
} | ||
|
||
func TestChassisStatusHandler(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockRM := resourcemanager.NewMockResourceManager(ctrl) | ||
handler := NewHandler(mockRM) | ||
|
||
testCases := []struct { | ||
name string | ||
expectedStatus uint8 | ||
expectedCall func() | ||
expectedCode goipmi.CompletionCode | ||
}{ | ||
{ | ||
name: "PowerStatus on", | ||
expectedStatus: goipmi.SystemPower, | ||
expectedCall: func() { | ||
mockRM.EXPECT().GetPowerStatus().Return(true, nil) | ||
}, | ||
expectedCode: goipmi.CommandCompleted, | ||
}, | ||
{ | ||
name: "PowerStatus off", | ||
expectedStatus: goipmi.PowerOverload, | ||
expectedCall: func() { | ||
mockRM.EXPECT().GetPowerStatus().Return(false, nil) | ||
}, | ||
expectedCode: goipmi.CommandCompleted, | ||
}, | ||
{ | ||
name: "PowerStatus error", | ||
expectedCall: func() { | ||
mockRM.EXPECT().GetPowerStatus().Return(false, fmt.Errorf("error")) | ||
}, | ||
expectedCode: goipmi.ErrInvalidState, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tc.expectedCall() | ||
|
||
message := &goipmi.Message{} | ||
|
||
response := handler.chassisStatusHandler(message) | ||
|
||
assert.IsType(t, &goipmi.ChassisStatusResponse{}, response) | ||
res, _ := response.(*goipmi.ChassisStatusResponse) | ||
assert.Equal(t, tc.expectedCode, res.CompletionCode) | ||
if tc.expectedStatus != 0 { | ||
assert.Equal(t, tc.expectedStatus, res.PowerState) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSetSystemBootOptionsHandler(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockRM := resourcemanager.NewMockResourceManager(ctrl) | ||
handler := NewHandler(mockRM) | ||
|
||
testCases := []struct { | ||
name string | ||
bootDevice goipmi.BootDevice | ||
expectedCall func() | ||
expectedCode goipmi.CompletionCode | ||
}{ | ||
{ | ||
name: "SetSystemBootOptions with PXE success", | ||
bootDevice: goipmi.BootDevicePxe, | ||
expectedCall: func() { | ||
mockRM.EXPECT().SetBootDevice(gomock.Any()).Return(nil) | ||
}, | ||
expectedCode: goipmi.CommandCompleted, | ||
}, | ||
{ | ||
name: "SetSystemBootOptions with PXE failed", | ||
bootDevice: goipmi.BootDevicePxe, | ||
expectedCall: func() { | ||
mockRM.EXPECT().SetBootDevice(gomock.Any()).Return(fmt.Errorf("error")) | ||
}, | ||
expectedCode: goipmi.ErrUnspecified, | ||
}, | ||
{ | ||
name: "SetSystemBootOptions with disk success", | ||
bootDevice: goipmi.BootDeviceDisk, | ||
expectedCall: func() { | ||
mockRM.EXPECT().SetBootDevice(gomock.Any()).Return(nil) | ||
}, | ||
expectedCode: goipmi.CommandCompleted, | ||
}, | ||
{ | ||
name: "SetSystemBootOptions with disk failed", | ||
bootDevice: goipmi.BootDeviceDisk, | ||
expectedCall: func() { | ||
mockRM.EXPECT().SetBootDevice(gomock.Any()).Return(fmt.Errorf("error")) | ||
}, | ||
expectedCode: goipmi.ErrUnspecified, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tc.expectedCall() | ||
|
||
message := &goipmi.Message{ | ||
Data: []byte{5, uint8(tc.bootDevice), 0, 0, 0, 0}, | ||
} | ||
|
||
response := handler.setSystemBootOptionsHandler(message) | ||
|
||
assert.IsType(t, &goipmi.SetSystemBootOptionsResponse{}, response) | ||
res, _ := response.(*goipmi.SetSystemBootOptionsResponse) | ||
assert.Equal(t, tc.expectedCode, res.CompletionCode) | ||
}) | ||
} | ||
} |
Oops, something went wrong.