From 5622adc5fa87ceda7cfc2c18cf6585fe5424d4c9 Mon Sep 17 00:00:00 2001 From: Joel Rebello Date: Mon, 27 Nov 2023 11:17:04 +0100 Subject: [PATCH] redfish/GetBiosconfiguration: tests and fixtures moved under redfishwrapper package --- internal/redfishwrapper/bios_test.go | 94 +++++++++++++++++++ .../redfishwrapper/fixtures}/dell/bios.json | 0 .../fixtures/dell/serviceroot.json | 80 ++++++++++++++++ .../fixtures}/dell/system.embedded.1.json | 0 .../redfishwrapper/fixtures/dell/systems.json | 13 +++ providers/redfish/bios_test.go | 57 ----------- 6 files changed, 187 insertions(+), 57 deletions(-) create mode 100644 internal/redfishwrapper/bios_test.go rename {providers/redfish/fixtures/v1 => internal/redfishwrapper/fixtures}/dell/bios.json (100%) create mode 100644 internal/redfishwrapper/fixtures/dell/serviceroot.json rename {providers/redfish/fixtures/v1 => internal/redfishwrapper/fixtures}/dell/system.embedded.1.json (100%) create mode 100644 internal/redfishwrapper/fixtures/dell/systems.json delete mode 100644 providers/redfish/bios_test.go diff --git a/internal/redfishwrapper/bios_test.go b/internal/redfishwrapper/bios_test.go new file mode 100644 index 00000000..643ebda2 --- /dev/null +++ b/internal/redfishwrapper/bios_test.go @@ -0,0 +1,94 @@ +package redfishwrapper + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "net/url" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func biosConfigFromFixture(t *testing.T) map[string]string { + t.Helper() + + fixturePath := fixturesDir + "/dell/bios.json" + fh, err := os.Open(fixturePath) + if err != nil { + t.Fatalf("%s, failed to open fixture: %s", err.Error(), fixturePath) + } + + defer fh.Close() + + b, err := io.ReadAll(fh) + if err != nil { + t.Fatalf("%s, failed to read fixture: %s", err.Error(), fixturePath) + } + + var bios map[string]any + err = json.Unmarshal([]byte(b), &bios) + if err != nil { + t.Fatalf("%s, failed to unmarshal fixture: %s", err.Error(), fixturePath) + } + + expectedBiosConfig := make(map[string]string) + for k, v := range bios["Attributes"].(map[string]any) { + expectedBiosConfig[k] = fmt.Sprintf("%v", v) + } + + return expectedBiosConfig +} + +func TestGetBiosConfiguration(t *testing.T) { + tests := []struct { + testName string + hfunc map[string]func(http.ResponseWriter, *http.Request) + expectedBiosConfig map[string]string + }{ + { + "GetBiosConfiguration", + map[string]func(http.ResponseWriter, *http.Request){ + "/redfish/v1/": endpointFunc(t, "/dell/serviceroot.json"), + "/redfish/v1/Systems": endpointFunc(t, "/dell/systems.json"), + "/redfish/v1/Systems/System.Embedded.1": endpointFunc(t, "/dell/system.embedded.1.json"), + "/redfish/v1/Systems/System.Embedded.1/Bios": endpointFunc(t, "/dell/bios.json"), + }, + biosConfigFromFixture(t), + }, + } + + for _, tc := range tests { + t.Run(tc.testName, func(t *testing.T) { + mux := http.NewServeMux() + handleFunc := tc.hfunc + for endpoint, handler := range handleFunc { + mux.HandleFunc(endpoint, handler) + } + + server := httptest.NewTLSServer(mux) + defer server.Close() + + parsedURL, err := url.Parse(server.URL) + if err != nil { + t.Fatal(err) + } + + ctx := context.Background() + client := NewClient(parsedURL.Hostname(), parsedURL.Port(), "", "", WithBasicAuthEnabled(true)) + + err = client.Open(ctx) + if err != nil { + t.Fatal(err) + } + + biosConfig, err := client.GetBiosConfiguration(ctx) + assert.Nil(t, err) + assert.Equal(t, tc.expectedBiosConfig, biosConfig) + }) + } +} diff --git a/providers/redfish/fixtures/v1/dell/bios.json b/internal/redfishwrapper/fixtures/dell/bios.json similarity index 100% rename from providers/redfish/fixtures/v1/dell/bios.json rename to internal/redfishwrapper/fixtures/dell/bios.json diff --git a/internal/redfishwrapper/fixtures/dell/serviceroot.json b/internal/redfishwrapper/fixtures/dell/serviceroot.json new file mode 100644 index 00000000..4bd38c6f --- /dev/null +++ b/internal/redfishwrapper/fixtures/dell/serviceroot.json @@ -0,0 +1,80 @@ +{ + "@odata.context": "/redfish/v1/$metadata#ServiceRoot.ServiceRoot", + "@odata.id": "/redfish/v1", + "@odata.type": "#ServiceRoot.v1_6_0.ServiceRoot", + "AccountService": { + "@odata.id": "/redfish/v1/AccountService" + }, + "CertificateService": { + "@odata.id": "/redfish/v1/CertificateService" + }, + "Chassis": { + "@odata.id": "/redfish/v1/Chassis" + }, + "Description": "Root Service", + "EventService": { + "@odata.id": "/redfish/v1/EventService" + }, + "Fabrics": { + "@odata.id": "/redfish/v1/Fabrics" + }, + "Id": "RootService", + "JobService": { + "@odata.id": "/redfish/v1/JobService" + }, + "JsonSchemas": { + "@odata.id": "/redfish/v1/JsonSchemas" + }, + "Links": { + "Sessions": { + "@odata.id": "/redfish/v1/SessionService/Sessions" + } + }, + "Managers": { + "@odata.id": "/redfish/v1/Managers" + }, + "Name": "Root Service", + "Oem": { + "Dell": { + "@odata.context": "/redfish/v1/$metadata#DellServiceRoot.DellServiceRoot", + "@odata.type": "#DellServiceRoot.v1_0_0.DellServiceRoot", + "IsBranded": 0, + "ManagerMACAddress": "d0:8e:79:bb:3e:ea", + "ServiceTag": "FOOBAR" + } + }, + "Product": "Integrated Dell Remote Access Controller", + "ProtocolFeaturesSupported": { + "ExcerptQuery": false, + "ExpandQuery": { + "ExpandAll": true, + "Levels": true, + "Links": true, + "MaxLevels": 1, + "NoLinks": true + }, + "FilterQuery": true, + "OnlyMemberQuery": true, + "SelectQuery": true + }, + "RedfishVersion": "1.9.0", + "Registries": { + "@odata.id": "/redfish/v1/Registries" + }, + "SessionService": { + "@odata.id": "/redfish/v1/SessionService" + }, + "Systems": { + "@odata.id": "/redfish/v1/Systems" + }, + "Tasks": { + "@odata.id": "/redfish/v1/TaskService" + }, + "TelemetryService": { + "@odata.id": "/redfish/v1/TelemetryService" + }, + "UpdateService": { + "@odata.id": "/redfish/v1/UpdateService" + }, + "Vendor": "Dell" +} \ No newline at end of file diff --git a/providers/redfish/fixtures/v1/dell/system.embedded.1.json b/internal/redfishwrapper/fixtures/dell/system.embedded.1.json similarity index 100% rename from providers/redfish/fixtures/v1/dell/system.embedded.1.json rename to internal/redfishwrapper/fixtures/dell/system.embedded.1.json diff --git a/internal/redfishwrapper/fixtures/dell/systems.json b/internal/redfishwrapper/fixtures/dell/systems.json new file mode 100644 index 00000000..1611fec8 --- /dev/null +++ b/internal/redfishwrapper/fixtures/dell/systems.json @@ -0,0 +1,13 @@ +{ + "@odata.context": "/redfish/v1/$metadata#ComputerSystemCollection.ComputerSystemCollection", + "@odata.id": "/redfish/v1/Systems", + "@odata.type": "#ComputerSystemCollection.ComputerSystemCollection", + "Description": "Collection of Computer Systems", + "Members": [ + { + "@odata.id": "/redfish/v1/Systems/System.Embedded.1" + } + ], + "Members@odata.count": 1, + "Name": "Computer System Collection" +} \ No newline at end of file diff --git a/providers/redfish/bios_test.go b/providers/redfish/bios_test.go deleted file mode 100644 index a4cd7a91..00000000 --- a/providers/redfish/bios_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package redfish - -import ( - "context" - "encoding/json" - "fmt" - "io" - "log" - "os" - "testing" - - "github.com/stretchr/testify/assert" -) - -func Test_GetBiosConfiguration(t *testing.T) { - fixturePath := fixturesDir + "/v1/dell/bios.json" - fh, err := os.Open(fixturePath) - if err != nil { - log.Fatalf("%s, failed to open fixture: %s", err.Error(), fixturePath) - } - - defer fh.Close() - - b, err := io.ReadAll(fh) - if err != nil { - log.Fatalf("%s, failed to read fixture: %s", err.Error(), fixturePath) - } - - var bios map[string]any - err = json.Unmarshal([]byte(b), &bios) - if err != nil { - log.Fatalf("%s, failed to unmarshal fixture: %s", err.Error(), fixturePath) - } - - expectedBiosConfig := make(map[string]string) - for k, v := range bios["Attributes"].(map[string]any) { - expectedBiosConfig[k] = fmt.Sprintf("%v", v) - } - - tests := []struct { - testName string - expectedBiosConfig map[string]string - }{ - { - "GetBiosConfiguration", - expectedBiosConfig, - }, - } - - for _, tc := range tests { - t.Run(tc.testName, func(t *testing.T) { - biosConfig, err := mockClient.GetBiosConfiguration(context.TODO()) - assert.Nil(t, err) - assert.Equal(t, tc.expectedBiosConfig, biosConfig) - }) - } -}