airshipctl/pkg/remote/redfish/utils_test.go
James Gu 1b93c84e54 Uplift go-redfish version
Newer version has the redfish client support required for ilo5

Change-Id: Ia601f008c192f39abe14cf4100420937b2c32691
Signed-off-by: James Gu <james.gu@microsoft.com>
2021-10-05 16:52:43 -04:00

257 lines
7.5 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package redfish_test
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
redfishMocks "opendev.org/airship/go-redfish/api/mocks"
redfishClient "opendev.org/airship/go-redfish/client"
"opendev.org/airship/airshipctl/pkg/remote/redfish"
testutil "opendev.org/airship/airshipctl/testutil/redfishutils/helpers"
)
const (
redfishHTTPErrDMTF = `
{
"error": {
"message": "A general error has occurred. See Resolution for information on how to resolve the error.",
"@Message.ExtendedInfo": [
{
"Message": "Extended error message.",
"Resolution": "Resolution message."
},
{
"Message": "Extended message 2.",
"Resolution": "Resolution message 2."
}
]
}
}`
redfishHTTPErrOther = `
{
"error": {
"message": "A general error has occurred. See Resolution for information on how to resolve the error.",
"@Message.ExtendedInfo": {
"Message": "Extended error message.",
"Resolution": "Resolution message."
}
}
}`
redfishHTTPErrMalformatted = `
{
"error": {
"message": "A general error has occurred. See Resolution for information on how to resolve the error.",
"@Message.ExtendedInfo": {}
}
}`
redfishHTTPErrEmptyList = `
{
"error": {
"message": "A general error has occurred. See Resolution for information on how to resolve the error.",
"@Message.ExtendedInfo": []
}
}`
)
func TestDecodeRawErrorEmptyInput(t *testing.T) {
_, err := redfish.DecodeRawError([]byte("{}"))
assert.Error(t, err)
}
func TestDecodeRawErrorEmptyList(t *testing.T) {
_, err := redfish.DecodeRawError([]byte(redfishHTTPErrEmptyList))
assert.Error(t, err)
}
func TestDecodeRawErrorEmptyMalformatted(t *testing.T) {
_, err := redfish.DecodeRawError([]byte(redfishHTTPErrMalformatted))
assert.Error(t, err)
}
func TestDecodeRawErrorDMTF(t *testing.T) {
message, err := redfish.DecodeRawError([]byte(redfishHTTPErrDMTF))
assert.NoError(t, err)
assert.Equal(t, "Extended message 2. Resolution message 2.\nExtended error message. Resolution message.\n",
message)
}
func TestDecodeRawErrorOther(t *testing.T) {
message, err := redfish.DecodeRawError([]byte(redfishHTTPErrOther))
assert.NoError(t, err)
assert.Equal(t, "Extended error message. Resolution message.",
message)
}
func TestRedfishErrorNoError(t *testing.T) {
err := redfish.ScreenRedfishError(&http.Response{StatusCode: 200}, nil)
assert.NoError(t, err)
}
func TestRedfishErrorNonNilErrorWithoutHttpResp(t *testing.T) {
err := redfish.ScreenRedfishError(nil, redfishClient.GenericOpenAPIError{})
_, ok := err.(redfish.ErrRedfishClient)
assert.True(t, ok)
}
func TestRedfishErrorNonNilErrorWithHttpRespError(t *testing.T) {
respErr := redfishClient.GenericOpenAPIError{}
err := redfish.ScreenRedfishError(&http.Response{StatusCode: 408}, respErr)
_, ok := err.(redfish.ErrRedfishClient)
assert.True(t, ok)
err = redfish.ScreenRedfishError(&http.Response{StatusCode: 500}, respErr)
_, ok = err.(redfish.ErrRedfishClient)
assert.True(t, ok)
err = redfish.ScreenRedfishError(&http.Response{StatusCode: 199}, respErr)
_, ok = err.(redfish.ErrRedfishClient)
assert.True(t, ok)
}
func TestRedfishErrorNonNilErrorWithHttpRespOK(t *testing.T) {
respErr := redfishClient.GenericOpenAPIError{}
// NOTE: Redfish client only uses HTTP 200 & HTTP 204 for success.
err := redfish.ScreenRedfishError(&http.Response{StatusCode: 204}, respErr)
assert.NoError(t, err)
err = redfish.ScreenRedfishError(&http.Response{StatusCode: 200}, respErr)
assert.NoError(t, err)
}
func TestRedfishUtilGetResIDFromURL(t *testing.T) {
// simple case
url := "api/user/123"
id := redfish.GetResourceIDFromURL(url)
assert.Equal(t, id, "123")
// FQDN
url = "http://www.abc.com/api/user/123"
id = redfish.GetResourceIDFromURL(url)
assert.Equal(t, id, "123")
//Trailing slash
url = "api/user/123/"
id = redfish.GetResourceIDFromURL(url)
assert.Equal(t, id, "123")
}
func TestRedfishUtilIsIdInList(t *testing.T) {
path1 := "/path/to/id/1"
path2 := "/path/to/id/2"
path3 := "/path/to/id/3"
path4 := "/path/to/id/4"
idList := []redfishClient.IdRef{
{OdataId: &path1},
{OdataId: &path2},
{OdataId: &path3},
{OdataId: &path4},
}
var emptyList []redfishClient.IdRef
res := redfish.IsIDInList(idList, "1")
assert.True(t, res)
res = redfish.IsIDInList(idList, "100")
assert.False(t, res)
res = redfish.IsIDInList(emptyList, "1")
assert.False(t, res)
}
func TestGetVirtualMediaID(t *testing.T) {
m := &redfishMocks.RedfishAPI{}
defer m.AssertExpectations(t)
ctx := context.Background()
httpResp := &http.Response{StatusCode: 200}
testutil.MockOnGetSystem(ctx, m, mock.Anything, testutil.GetTestSystem(), httpResp, nil, -1)
testutil.MockOnListManagerVirtualMedia(ctx, m, testutil.ManagerID,
testutil.GetMediaCollection([]string{"Floppy", "Cd"}), httpResp, nil, 1)
testutil.MockOnGetManagerVirtualMedia(ctx, m, testutil.ManagerID, "Floppy",
testutil.GetVirtualMedia([]string{"Floppy", "USBStick"}), httpResp, nil)
testutil.MockOnGetManagerVirtualMedia(ctx, m, testutil.ManagerID, "Cd",
testutil.GetVirtualMedia([]string{"CD"}), httpResp, nil)
mediaID, mediaType, err := redfish.GetVirtualMediaID(ctx, m, testutil.ManagerID)
assert.Equal(t, mediaID, "Cd")
assert.Equal(t, mediaType, "CD")
assert.NoError(t, err)
}
func TestGetVirtualMediaIDNoMedia(t *testing.T) {
m := &redfishMocks.RedfishAPI{}
defer m.AssertExpectations(t)
ctx := context.Background()
httpResp := &http.Response{StatusCode: 200}
// Remove available media types from test system
system := testutil.GetTestSystem()
boot := system.GetBoot()
boot.SetBootSourceOverrideTargetRedfishAllowableValues([]redfishClient.BootSource{})
testutil.MockOnGetSystem(ctx, m, mock.Anything, system,
&http.Response{StatusCode: 200}, nil, -1)
testutil.MockOnListManagerVirtualMedia(ctx, m, testutil.ManagerID,
redfishClient.Collection{}, httpResp, nil, 1)
mediaID, mediaType, err := redfish.GetVirtualMediaID(ctx, m, testutil.ManagerID)
assert.Empty(t, mediaID)
assert.Empty(t, mediaType)
assert.Error(t, err)
}
func TestGetVirtualMediaIDUnacceptableMediaTypes(t *testing.T) {
m := &redfishMocks.RedfishAPI{}
defer m.AssertExpectations(t)
ctx := context.Background()
httpResp := &http.Response{StatusCode: 200}
system := testutil.GetTestSystem()
boot := system.GetBoot()
boot.SetBootSourceOverrideTargetRedfishAllowableValues([]redfishClient.BootSource{
redfishClient.BOOTSOURCE_PXE,
})
testutil.MockOnGetSystem(ctx, m, mock.Anything, system,
&http.Response{StatusCode: 200}, nil, -1)
testutil.MockOnListManagerVirtualMedia(ctx, m, testutil.ManagerID,
testutil.GetMediaCollection([]string{"Floppy"}), httpResp, nil, 1)
testutil.MockOnGetManagerVirtualMedia(ctx, m, testutil.ManagerID, "Floppy",
testutil.GetVirtualMedia([]string{"Floppy", "USBStick"}), httpResp, nil)
mediaID, mediaType, err := redfish.GetVirtualMediaID(ctx, m, testutil.ManagerID)
assert.Empty(t, mediaID)
assert.Empty(t, mediaType)
assert.Error(t, err)
}