From aeba847ffc6f17e1e2d67b711689c381ac2da828 Mon Sep 17 00:00:00 2001 From: Drew Walters Date: Mon, 4 May 2020 20:43:15 +0000 Subject: [PATCH] Add Redfish SystemPowerOn/Off tests The Redfish client functions SystemPowerOn and SystemPowerOff do not have test cases. This change adds tests to verify their functionality and bring their test coverage to 100%. Change-Id: I84a9c3dfe9ed022c9e1086c22cb88497a5367863 Signed-off-by: Drew Walters --- pkg/remote/redfish/client_test.go | 116 ++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/pkg/remote/redfish/client_test.go b/pkg/remote/redfish/client_test.go index b24b88937..eae3c6f55 100644 --- a/pkg/remote/redfish/client_test.go +++ b/pkg/remote/redfish/client_test.go @@ -515,6 +515,122 @@ func TestSetVirtualMediaInsertVirtualMediaError(t *testing.T) { assert.True(t, ok) } +func TestSystemPowerOff(t *testing.T) { + m := &redfishMocks.RedfishAPI{} + defer m.AssertExpectations(t) + + _, client, err := NewClient(redfishURL, false, false, "", "") + require.NoError(t, err) + + client.nodeID = nodeID + + ctx := context.WithValue(context.Background(), ctxKeyNumRetries, 1) + + m.On("ResetSystem", ctx, client.nodeID, mock.Anything).Return( + redfishClient.RedfishError{}, + &http.Response{StatusCode: 200}, nil) + + m.On("GetSystem", ctx, client.nodeID).Return( + redfishClient.ComputerSystem{PowerState: redfishClient.POWERSTATE_ON}, + &http.Response{StatusCode: 200}, nil).Times(1) + + m.On("GetSystem", ctx, client.nodeID).Return( + redfishClient.ComputerSystem{PowerState: redfishClient.POWERSTATE_OFF}, + &http.Response{StatusCode: 200}, nil).Times(1) + + // Replace normal API client with mocked API client + client.RedfishAPI = m + + // Mock out the Sleep function so we don't have to wait on it + client.Sleep = func(_ time.Duration) {} + + err = client.SystemPowerOff(ctx) + assert.NoError(t, err) +} + +func TestSystemPowerOffResetSystemError(t *testing.T) { + m := &redfishMocks.RedfishAPI{} + defer m.AssertExpectations(t) + + _, client, err := NewClient(redfishURL, false, false, "", "") + require.NoError(t, err) + + client.nodeID = nodeID + + ctx := context.WithValue(context.Background(), ctxKeyNumRetries, 1) + + m.On("ResetSystem", ctx, client.nodeID, mock.Anything).Return( + redfishClient.RedfishError{}, + &http.Response{StatusCode: 500}, nil) + + // Replace normal API client with mocked API client + client.RedfishAPI = m + + // Mock out the Sleep function so we don't have to wait on it + client.Sleep = func(_ time.Duration) {} + + err = client.SystemPowerOff(ctx) + assert.Error(t, err) +} + +func TestSystemPowerOn(t *testing.T) { + m := &redfishMocks.RedfishAPI{} + defer m.AssertExpectations(t) + + _, client, err := NewClient(redfishURL, false, false, "", "") + require.NoError(t, err) + + client.nodeID = nodeID + + ctx := context.WithValue(context.Background(), ctxKeyNumRetries, 1) + + m.On("ResetSystem", ctx, client.nodeID, mock.Anything).Return( + redfishClient.RedfishError{}, + &http.Response{StatusCode: 200}, nil) + + m.On("GetSystem", ctx, client.nodeID).Return( + redfishClient.ComputerSystem{PowerState: redfishClient.POWERSTATE_OFF}, + &http.Response{StatusCode: 200}, nil).Times(1) + + m.On("GetSystem", ctx, client.nodeID).Return( + redfishClient.ComputerSystem{PowerState: redfishClient.POWERSTATE_ON}, + &http.Response{StatusCode: 200}, nil).Times(1) + + // Replace normal API client with mocked API client + client.RedfishAPI = m + + // Mock out the Sleep function so we don't have to wait on it + client.Sleep = func(_ time.Duration) {} + + err = client.SystemPowerOn(ctx) + assert.NoError(t, err) +} + +func TestSystemPowerOnResetSystemError(t *testing.T) { + m := &redfishMocks.RedfishAPI{} + defer m.AssertExpectations(t) + + _, client, err := NewClient(redfishURL, false, false, "", "") + require.NoError(t, err) + + client.nodeID = nodeID + + ctx := context.WithValue(context.Background(), ctxKeyNumRetries, 1) + + m.On("ResetSystem", ctx, client.nodeID, mock.Anything).Return( + redfishClient.RedfishError{}, + &http.Response{StatusCode: 500}, nil) + + // Replace normal API client with mocked API client + client.RedfishAPI = m + + // Mock out the Sleep function so we don't have to wait on it + client.Sleep = func(_ time.Duration) {} + + err = client.SystemPowerOn(ctx) + assert.Error(t, err) +} + func TestSystemPowerStatusUnknown(t *testing.T) { m := &redfishMocks.RedfishAPI{} defer m.AssertExpectations(t)