From d22930ad1be3ee3309224dbe05a07a40f61c565d Mon Sep 17 00:00:00 2001 From: Joel Rebello Date: Tue, 28 Nov 2023 10:14:11 +0100 Subject: [PATCH] providers/supermicro: fix TestOpen() --- .../supermicro/fixtures/serviceroot.json | 62 +++++++++++++++++++ providers/supermicro/supermicro.go | 12 +++- providers/supermicro/supermicro_test.go | 48 +++++++++++++- 3 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 providers/supermicro/fixtures/serviceroot.json diff --git a/providers/supermicro/fixtures/serviceroot.json b/providers/supermicro/fixtures/serviceroot.json new file mode 100644 index 00000000..11078082 --- /dev/null +++ b/providers/supermicro/fixtures/serviceroot.json @@ -0,0 +1,62 @@ +{ + "@odata.type": "#ServiceRoot.v1_5_2.ServiceRoot", + "@odata.id": "/redfish/v1", + "Id": "ServiceRoot", + "Name": "Root Service", + "RedfishVersion": "1.9.0", + "UUID": "00000000-0000-0000-0000-3CECEFCEFEDA", + "Systems": { + "@odata.id": "/redfish/v1/Systems" + }, + "Chassis": { + "@odata.id": "/redfish/v1/Chassis" + }, + "Managers": { + "@odata.id": "/redfish/v1/Managers" + }, + "Tasks": { + "@odata.id": "/redfish/v1/TaskService" + }, + "SessionService": { + "@odata.id": "/redfish/v1/SessionService" + }, + "AccountService": { + "@odata.id": "/redfish/v1/AccountService" + }, + "EventService": { + "@odata.id": "/redfish/v1/EventService" + }, + "UpdateService": { + "@odata.id": "/redfish/v1/UpdateService" + }, + "CertificateService": { + "@odata.id": "/redfish/v1/CertificateService" + }, + "Registries": { + "@odata.id": "/redfish/v1/Registries" + }, + "JsonSchemas": { + "@odata.id": "/redfish/v1/JsonSchemas" + }, + "TelemetryService": { + "@odata.id": "/redfish/v1/TelemetryService" + }, + "Links": { + "Sessions": { + "@odata.id": "/redfish/v1/SessionService/Sessions" + } + }, + "ProtocolFeaturesSupported": { + "FilterQuery": true, + "SelectQuery": true, + "ExcerptQuery": false, + "OnlyMemberQuery": false, + "ExpandQuery": { + "Links": true, + "NoLinks": true, + "ExpandAll": true, + "Levels": true, + "MaxLevels": 2 + } + } +} \ No newline at end of file diff --git a/providers/supermicro/supermicro.go b/providers/supermicro/supermicro.go index 7add3b80..b201e374 100644 --- a/providers/supermicro/supermicro.go +++ b/providers/supermicro/supermicro.go @@ -185,6 +185,10 @@ func (c *Client) Open(ctx context.Context) (err error) { return errors.Wrap(bmclibErrs.ErrLoginFailed, err.Error()) } + if err := c.serviceClient.redfishSession(ctx); err != nil { + return errors.Wrap(bmclibErrs.ErrLoginFailed, err.Error()) + } + return nil } @@ -392,7 +396,13 @@ func (c *serviceClient) redfishSession(ctx context.Context) (err error) { return nil } - c.redfish = redfishwrapper.NewClient(c.host, "", c.user, c.pass, redfishwrapper.WithHTTPClient(c.client)) + c.redfish = redfishwrapper.NewClient( + c.host, + c.port, + c.user, + c.pass, + redfishwrapper.WithHTTPClient(c.client), + ) if err := c.redfish.Open(ctx); err != nil { return err } diff --git a/providers/supermicro/supermicro_test.go b/providers/supermicro/supermicro_test.go index ca0313c8..f503813a 100644 --- a/providers/supermicro/supermicro_test.go +++ b/providers/supermicro/supermicro_test.go @@ -7,12 +7,18 @@ import ( "net/http" "net/http/httptest" "net/url" + "os" "testing" + "github.com/bmc-toolbox/bmclib/v2/internal/redfishwrapper" "github.com/go-logr/logr" "github.com/stretchr/testify/assert" ) +const ( + fixturesDir = "./fixtures" +) + func TestParseToken(t *testing.T) { testcases := []struct { name string @@ -66,6 +72,37 @@ func TestParseToken(t *testing.T) { } } +func mustReadFile(t *testing.T, filename string) []byte { + t.Helper() + + fixture := fixturesDir + "/" + filename + fh, err := os.Open(fixture) + if err != nil { + log.Fatal(err) + } + + defer fh.Close() + + b, err := io.ReadAll(fh) + if err != nil { + log.Fatal(err) + } + + return b +} + +var endpointFunc = func(t *testing.T, file string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // expect either GET or Delete methods + if r.Method != http.MethodGet && r.Method != http.MethodPost && r.Method != http.MethodDelete { + w.WriteHeader(http.StatusNotFound) + return + } + + _, _ = w.Write(mustReadFile(t, file)) + } +} + func TestOpen(t *testing.T) { type handlerFuncMap map[string]func(http.ResponseWriter, *http.Request) testcases := []struct { @@ -84,6 +121,7 @@ func TestOpen(t *testing.T) { "/": func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }, + "/redfish/v1/": endpointFunc(t, "serviceroot.json"), // first request to login "/cgi/login.cgi": func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, http.MethodPost) @@ -182,13 +220,21 @@ func TestOpen(t *testing.T) { server := httptest.NewTLSServer(mux) defer server.Close() - server.Config.ErrorLog = log.Default() + server.Config.ErrorLog = log.New(os.Stdout, "foo", 3) parsedURL, err := url.Parse(server.URL) if err != nil { t.Fatal(err) } client := NewClient(parsedURL.Hostname(), tc.user, tc.pass, logr.Discard(), WithPort(parsedURL.Port())) + client.serviceClient.redfish = redfishwrapper.NewClient( + parsedURL.Hostname(), + parsedURL.Port(), + tc.user, + tc.pass, + redfishwrapper.WithHTTPClient(client.serviceClient.client), + ) + err = client.Open(context.Background()) if tc.errorContains != "" { assert.ErrorContains(t, err, tc.errorContains)