Create a new test object for each redfish unittest

This removes the SampleSystem testing variable and replaces it by a
function which generates the object. This guarantees that the test
system has the same initial configuration for each test regardless of
the order in which tests are run.

Change-Id: I9875723ac4194425674c2806cf099509f94ad771
This commit is contained in:
Ian Howell 2019-11-20 13:58:36 -06:00
parent 013d814a93
commit 8dd721830b

View File

@ -11,71 +11,25 @@ import (
redfishClient "github.com/Nordix/go-redfish/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
. "opendev.org/airship/airshipctl/pkg/remote/redfish"
)
const (
computerSystemID = "server-100"
defaultURL = "https://localhost:1234"
)
var (
ctx = context.Background()
defaultURL = "https://localhost:1234"
SampleSystem = redfishClient.ComputerSystem{
Id: computerSystemID,
Name: "server-100",
UUID: "58893887-8974-2487-2389-841168418919",
Status: redfishClient.Status{
State: "Enabled",
Health: "OK",
},
Links: redfishClient.SystemLinks{
ManagedBy: []redfishClient.IdRef{
{OdataId: "/redfish/v1/Managers/manager-1"},
},
},
Boot: redfishClient.Boot{
BootSourceOverrideTarget: redfishClient.BOOTSOURCE_CD,
BootSourceOverrideEnabled: redfishClient.BOOTSOURCEOVERRIDEENABLED_CONTINUOUS,
BootSourceOverrideTargetRedfishAllowableValues: []redfishClient.BootSource{
redfishClient.BOOTSOURCE_CD,
redfishClient.BOOTSOURCE_FLOPPY,
redfishClient.BOOTSOURCE_HDD,
redfishClient.BOOTSOURCE_PXE,
},
},
}
)
func getDefaultRedfishRemoteDirectObj(t *testing.T, api redfishAPI.RedfishAPI) RedfishRemoteDirect {
t.Helper()
rDCfg, err := NewRedfishRemoteDirectClient(
ctx,
defaultURL,
computerSystemID,
"/tmp/test.iso",
)
assert.NoError(t, err)
rDCfg.Api = api
return rDCfg
}
func TestRedfishRemoteDirectNormal(t *testing.T) {
m := &redfishMocks.RedfishAPI{}
defer m.AssertExpectations(t)
systemID := computerSystemID
m.On("GetSystem", ctx, systemID).
Return(SampleSystem, nil, nil)
m.On("InsertVirtualMedia", ctx, "manager-1", "Cd", mock.Anything).
m.On("GetSystem", context.Background(), systemID).
Return(getTestSystem(), nil, nil)
m.On("InsertVirtualMedia", context.Background(), "manager-1", "Cd", mock.Anything).
Return(redfishClient.RedfishError{}, nil, nil)
systemReq := redfishClient.ComputerSystem{
@ -83,18 +37,18 @@ func TestRedfishRemoteDirectNormal(t *testing.T) {
BootSourceOverrideTarget: redfishClient.BOOTSOURCE_CD,
},
}
m.On("SetSystem", ctx, systemID, systemReq).
m.On("SetSystem", context.Background(), systemID, systemReq).
Times(1).
Return(redfishClient.ComputerSystem{}, nil, nil)
resetReq := redfishClient.ResetRequestBody{}
resetReq.ResetType = redfishClient.RESETTYPE_FORCE_OFF
m.On("ResetSystem", ctx, systemID, resetReq).
m.On("ResetSystem", context.Background(), systemID, resetReq).
Times(1).
Return(redfishClient.RedfishError{}, nil, nil)
resetReq.ResetType = redfishClient.RESETTYPE_ON
m.On("ResetSystem", ctx, systemID, resetReq).
m.On("ResetSystem", context.Background(), systemID, resetReq).
Times(1).
Return(redfishClient.RedfishError{}, nil, nil)
@ -116,7 +70,7 @@ func TestRedfishRemoteDirectInvalidSystemId(t *testing.T) {
localRDCfg.EphemeralNodeId = systemID
realErr := fmt.Errorf("%s system do not exist", systemID)
m.On("GetSystem", ctx, systemID).
m.On("GetSystem", context.Background(), systemID).
Times(1).
Return(redfishClient.ComputerSystem{}, nil, realErr)
@ -136,7 +90,7 @@ func TestRedfishRemoteDirectGetSystemNetworkError(t *testing.T) {
httpResp := &http.Response{
StatusCode: 408,
}
m.On("GetSystem", ctx, systemID).
m.On("GetSystem", context.Background(), systemID).
Times(1).
Return(redfishClient.ComputerSystem{}, httpResp, realErr)
@ -163,11 +117,11 @@ func TestRedfishRemoteDirectInvalidIsoPath(t *testing.T) {
httpResp := &http.Response{
StatusCode: 500,
}
m.On("GetSystem", ctx, systemID).
m.On("GetSystem", context.Background(), systemID).
Times(1).
Return(SampleSystem, nil, nil)
Return(getTestSystem(), nil, nil)
m.On("InsertVirtualMedia", ctx, "manager-1", "Cd", mock.Anything).
m.On("InsertVirtualMedia", context.Background(), "manager-1", "Cd", mock.Anything).
Return(redfishClient.RedfishError{}, httpResp, realErr)
err := localRDCfg.DoRemoteDirect()
@ -182,16 +136,16 @@ func TestRedfishRemoteDirectCdDvdNotAvailableInBootSources(t *testing.T) {
defer m.AssertExpectations(t)
systemID := computerSystemID
invalidSystem := SampleSystem
invalidSystem := getTestSystem()
invalidSystem.Boot.BootSourceOverrideTargetRedfishAllowableValues = []redfishClient.BootSource{
redfishClient.BOOTSOURCE_HDD,
redfishClient.BOOTSOURCE_PXE,
}
m.On("GetSystem", ctx, systemID).
m.On("GetSystem", context.Background(), systemID).
Return(invalidSystem, nil, nil)
m.On("InsertVirtualMedia", ctx, "manager-1", "Cd", mock.Anything).
m.On("InsertVirtualMedia", context.Background(), "manager-1", "Cd", mock.Anything).
Return(redfishClient.RedfishError{}, nil, nil)
rDCfg := getDefaultRedfishRemoteDirectObj(t, m)
@ -208,17 +162,17 @@ func TestRedfishRemoteDirectSetSystemBootSourceFailed(t *testing.T) {
defer m.AssertExpectations(t)
systemID := computerSystemID
m.On("GetSystem", ctx, systemID).
Return(SampleSystem, nil, nil)
m.On("GetSystem", context.Background(), systemID).
Return(getTestSystem(), nil, nil)
m.On("InsertVirtualMedia", ctx, "manager-1", "Cd", mock.Anything).
m.On("InsertVirtualMedia", context.Background(), "manager-1", "Cd", mock.Anything).
Return(redfishClient.RedfishError{}, nil, nil)
realErr := fmt.Errorf("Unauthorized")
httpResp := &http.Response{
StatusCode: 401,
}
m.On("SetSystem", ctx, systemID, mock.Anything).
m.On("SetSystem", context.Background(), systemID, mock.Anything).
Times(1).
Return(redfishClient.ComputerSystem{}, httpResp, realErr)
@ -237,13 +191,13 @@ func TestRedfishRemoteDirectSystemRebootFailed(t *testing.T) {
systemID := computerSystemID
m.On("GetSystem", ctx, systemID).
Return(SampleSystem, nil, nil)
m.On("GetSystem", context.Background(), systemID).
Return(getTestSystem(), nil, nil)
m.On("InsertVirtualMedia", ctx, mock.Anything, mock.Anything, mock.Anything).
m.On("InsertVirtualMedia", context.Background(), mock.Anything, mock.Anything, mock.Anything).
Return(redfishClient.RedfishError{}, nil, nil)
m.On("SetSystem", ctx, systemID, mock.Anything).
m.On("SetSystem", context.Background(), systemID, mock.Anything).
Times(1).
Return(redfishClient.ComputerSystem{}, nil, nil)
@ -253,7 +207,7 @@ func TestRedfishRemoteDirectSystemRebootFailed(t *testing.T) {
}
resetReq := redfishClient.ResetRequestBody{}
resetReq.ResetType = redfishClient.RESETTYPE_FORCE_OFF
m.On("ResetSystem", ctx, systemID, resetReq).
m.On("ResetSystem", context.Background(), systemID, resetReq).
Times(1).
Return(redfishClient.RedfishError{}, httpResp, realErr)
@ -264,3 +218,47 @@ func TestRedfishRemoteDirectSystemRebootFailed(t *testing.T) {
_, ok := err.(*RedfishClientError)
assert.True(t, ok)
}
func getTestSystem() redfishClient.ComputerSystem {
return redfishClient.ComputerSystem{
Id: computerSystemID,
Name: "server-100",
UUID: "58893887-8974-2487-2389-841168418919",
Status: redfishClient.Status{
State: "Enabled",
Health: "OK",
},
Links: redfishClient.SystemLinks{
ManagedBy: []redfishClient.IdRef{
{OdataId: "/redfish/v1/Managers/manager-1"},
},
},
Boot: redfishClient.Boot{
BootSourceOverrideTarget: redfishClient.BOOTSOURCE_CD,
BootSourceOverrideEnabled: redfishClient.BOOTSOURCEOVERRIDEENABLED_CONTINUOUS,
BootSourceOverrideTargetRedfishAllowableValues: []redfishClient.BootSource{
redfishClient.BOOTSOURCE_CD,
redfishClient.BOOTSOURCE_FLOPPY,
redfishClient.BOOTSOURCE_HDD,
redfishClient.BOOTSOURCE_PXE,
},
},
}
}
func getDefaultRedfishRemoteDirectObj(t *testing.T, api redfishAPI.RedfishAPI) RedfishRemoteDirect {
t.Helper()
rDCfg, err := NewRedfishRemoteDirectClient(
context.Background(),
defaultURL,
computerSystemID,
"/tmp/test.iso",
)
require.NoError(t, err)
rDCfg.Api = api
return rDCfg
}