Skip to content

Commit

Permalink
providers/supermicro: fix TestOpen()
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Nov 28, 2023
1 parent bbc734d commit d22930a
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
62 changes: 62 additions & 0 deletions providers/supermicro/fixtures/serviceroot.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
12 changes: 11 additions & 1 deletion providers/supermicro/supermicro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
48 changes: 47 additions & 1 deletion providers/supermicro/supermicro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d22930a

Please sign in to comment.