Skip to content

Commit

Permalink
addressed feedback: renamed all the things to be clearer, fixed example
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcburns committed Sep 19, 2023
1 parent cf178b2 commit 3bfa968
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 56 deletions.
36 changes: 18 additions & 18 deletions bmc/sel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (
"github.com/pkg/errors"
)

// SELServices for various SEL related services
type SELService interface {
ClearSEL(ctx context.Context) (err error)
// System Event Log Services for related services
type SystemEventLog interface {
ClearSystemEventLog(ctx context.Context) (err error)
}

type selProviders struct {
name string
selProvider SELService
type systemEventLogProviders struct {
name string
systemEventLogProvider SystemEventLog
}

func clearSEL(ctx context.Context, timeout time.Duration, s []selProviders) (metadata Metadata, err error) {
func clearSystemEventLog(ctx context.Context, timeout time.Duration, s []systemEventLogProviders) (metadata Metadata, err error) {
var metadataLocal Metadata

for _, elem := range s {
if elem.selProvider == nil {
if elem.systemEventLogProvider == nil {
continue
}
select {
Expand All @@ -35,7 +35,7 @@ func clearSEL(ctx context.Context, timeout time.Duration, s []selProviders) (met
metadataLocal.ProvidersAttempted = append(metadataLocal.ProvidersAttempted, elem.name)
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
selErr := elem.selProvider.ClearSEL(ctx)
selErr := elem.systemEventLogProvider.ClearSystemEventLog(ctx)
if selErr != nil {
err = multierror.Append(err, errors.WithMessagef(selErr, "provider: %v", elem.name))
continue
Expand All @@ -46,24 +46,24 @@ func clearSEL(ctx context.Context, timeout time.Duration, s []selProviders) (met

}

return metadataLocal, multierror.Append(err, errors.New("failed to reset SEL"))
return metadataLocal, multierror.Append(err, errors.New("failed to reset System Event Log"))
}

func ClearSELFromInterfaces(ctx context.Context, timeout time.Duration, generic []interface{}) (metadata Metadata, err error) {
selServices := make([]selProviders, 0)
func ClearSystemEventLogFromInterfaces(ctx context.Context, timeout time.Duration, generic []interface{}) (metadata Metadata, err error) {
selServices := make([]systemEventLogProviders, 0)
for _, elem := range generic {
temp := selProviders{name: getProviderName(elem)}
temp := systemEventLogProviders{name: getProviderName(elem)}
switch p := elem.(type) {
case SELService:
temp.selProvider = p
case SystemEventLog:
temp.systemEventLogProvider = p
selServices = append(selServices, temp)
default:
e := fmt.Sprintf("not a SELService implementation: %T", p)
e := fmt.Sprintf("not a SystemEventLog service implementation: %T", p)
err = multierror.Append(err, errors.New(e))
}
}
if len(selServices) == 0 {
return metadata, multierror.Append(err, errors.New("no SelService implementations found"))
return metadata, multierror.Append(err, errors.New("no SystemEventLog implementations found"))
}
return clearSEL(ctx, timeout, selServices)
return clearSystemEventLog(ctx, timeout, selServices)
}
34 changes: 17 additions & 17 deletions bmc/sel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,53 @@ import (
"github.com/stretchr/testify/assert"
)

type mockSELService struct {
type mockSystemEventLogService struct {
name string
err error
}

func (m *mockSELService) ClearSEL(ctx context.Context) error {
func (m *mockSystemEventLogService) ClearSystemEventLog(ctx context.Context) error {
return m.err
}

func (m *mockSELService) Name() string {
func (m *mockSystemEventLogService) Name() string {
return m.name
}

func TestClearSEL(t *testing.T) {
func TestClearSystemEventLog(t *testing.T) {
ctx := context.Background()
timeout := 1 * time.Second

// Test with a mock SELService that returns nil
mockService := &mockSELService{name: "mock1", err: nil}
metadata, err := clearSEL(ctx, timeout, []selProviders{{name: mockService.name, selProvider: mockService}})
// Test with a mock SystemEventLogService that returns nil
mockService := &mockSystemEventLogService{name: "mock1", err: nil}
metadata, err := clearSystemEventLog(ctx, timeout, []systemEventLogProviders{{name: mockService.name, systemEventLogProvider: mockService}})
assert.Nil(t, err)
assert.Equal(t, mockService.name, metadata.SuccessfulProvider)

// Test with a mock SELService that returns an error
mockService = &mockSELService{name: "mock2", err: errors.New("mock error")}
metadata, err = clearSEL(ctx, timeout, []selProviders{{name: mockService.name, selProvider: mockService}})
// Test with a mock SystemEventLogService that returns an error
mockService = &mockSystemEventLogService{name: "mock2", err: errors.New("mock error")}
metadata, err = clearSystemEventLog(ctx, timeout, []systemEventLogProviders{{name: mockService.name, systemEventLogProvider: mockService}})
assert.NotNil(t, err)
assert.NotEqual(t, mockService.name, metadata.SuccessfulProvider)
}

func TestClearSELFromInterfaces(t *testing.T) {
func TestClearSystemEventLogFromInterfaces(t *testing.T) {
ctx := context.Background()
timeout := 1 * time.Second

// Test with an empty slice
metadata, err := ClearSELFromInterfaces(ctx, timeout, []interface{}{})
metadata, err := ClearSystemEventLogFromInterfaces(ctx, timeout, []interface{}{})
assert.NotNil(t, err)
assert.Empty(t, metadata.SuccessfulProvider)

// Test with a slice containing a non-SELService object
metadata, err = ClearSELFromInterfaces(ctx, timeout, []interface{}{"not a SELService"})
// Test with a slice containing a non-SystemEventLog object
metadata, err = ClearSystemEventLogFromInterfaces(ctx, timeout, []interface{}{"not a SystemEventLog Service"})
assert.NotNil(t, err)
assert.Empty(t, metadata.SuccessfulProvider)

// Test with a slice containing a mock SELService
mockService := &mockSELService{name: "mock1"}
metadata, err = ClearSELFromInterfaces(ctx, timeout, []interface{}{mockService})
// Test with a slice containing a mock SystemEventLogService that returns nil
mockService := &mockSystemEventLogService{name: "mock1"}
metadata, err = ClearSystemEventLogFromInterfaces(ctx, timeout, []interface{}{mockService})
assert.Nil(t, err)
assert.Equal(t, mockService.name, metadata.SuccessfulProvider)
}
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ func (c *Client) Screenshot(ctx context.Context) (image []byte, fileType string,
return image, fileType, err
}

func (c *Client) ClearSEL(ctx context.Context) (err error) {
metadata, err := bmc.ClearSELFromInterfaces(ctx, c.perProviderTimeout(ctx), c.registry().GetDriverInterfaces())
func (c *Client) ClearSystemEventLog(ctx context.Context) (err error) {
metadata, err := bmc.ClearSystemEventLogFromInterfaces(ctx, c.perProviderTimeout(ctx), c.registry().GetDriverInterfaces())
c.setMetadata(metadata)
return err
}
8 changes: 4 additions & 4 deletions examples/sel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
}

cl := bmclib.NewClient(*host, *user, *pass, clientOpts...)
cl.Registry.Drivers = cl.Registry.Supports(providers.FeatureSELClear)
cl.Registry.Drivers = cl.Registry.Supports(providers.FeatureClearSystemEventLog)

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
Expand All @@ -60,9 +60,9 @@ func main() {
}
defer cl.Close(ctx)

err = cl.ClearSEL(ctx)
err = cl.ClearSystemEventLog(ctx)
if err != nil {
l.WithError(err).Error()
l.WithError(err).Fatal(err, "failed to clear System Event Log")
}
l.Info("SEL cleared")
l.Info("System Event Log cleared")
}
4 changes: 2 additions & 2 deletions internal/ipmi/ipmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ func (i *Ipmi) ReadUsers(ctx context.Context) (users []map[string]string, err er
return users, err
}

// Clear SEL clears the SEL
func (i *Ipmi) ClearSEL(ctx context.Context) (err error) {
// ClearSystemEventLog clears the system event log
func (i *Ipmi) ClearSystemEventLog(ctx context.Context) (err error) {
_, err = i.run(ctx, []string{"sel", "clear"})
return err
}
4 changes: 2 additions & 2 deletions internal/redfishwrapper/sel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/pkg/errors"
)

// Clear SEL clears all of the LogServices logs
func (c *Client) ClearSEL(ctx context.Context) (err error) {
// ClearSystemEventLog clears all of the LogServices logs
func (c *Client) ClearSystemEventLog(ctx context.Context) (err error) {
if err := c.SessionActive(); err != nil {
return errors.Wrap(bmclibErrs.ErrNotAuthenticated, err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions providers/ipmitool/ipmitool.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
providers.FeatureUserRead,
providers.FeatureBmcReset,
providers.FeatureBootDeviceSet,
providers.FeatureSELClear,
providers.FeatureClearSystemEventLog,
}
)

Expand Down Expand Up @@ -182,6 +182,6 @@ func (c *Conn) PowerSet(ctx context.Context, state string) (ok bool, err error)
return ok, err
}

func (c *Conn) ClearSEL(ctx context.Context) (err error) {
return c.ipmitool.ClearSEL(ctx)
func (c *Conn) ClearSystemEventLog(ctx context.Context) (err error) {
return c.ipmitool.ClearSystemEventLog(ctx)
}
6 changes: 3 additions & 3 deletions providers/ipmitool/ipmitool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestBMCReset(t *testing.T) {
t.Fatal()
}

func TestSELClear(t *testing.T) {
func TestSystemEventLogClear(t *testing.T) {
t.Skip("need real ipmi server")
host := "127.0.0.1"
port := "623"
Expand All @@ -117,10 +117,10 @@ func TestSELClear(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = i.ClearSEL(context.Background())
err = i.ClearSystemEventLog(context.Background())
if err != nil {
t.Fatal(err)
}
t.Log("SEL cleared")
t.Log("System Event Log cleared")
t.Fatal()
}
4 changes: 2 additions & 2 deletions providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ const (
FeaturePostCodeRead registrar.Feature = "postcoderead"
// FeatureScreenshot means an implementation that returns a screenshot of the video.
FeatureScreenshot registrar.Feature = "screenshot"
// FeatureSELClear means an implementation that clears the BMC SEL
FeatureSELClear registrar.Feature = "selclear"
// FeatureClearSystemEventLog means an implementation that clears the BMC System Event Log (SEL)
FeatureClearSystemEventLog registrar.Feature = "clearsystemeventlog"
)
2 changes: 1 addition & 1 deletion providers/redfish/redfish.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (
providers.FeatureFirmwareInstall,
providers.FeatureFirmwareInstallStatus,
providers.FeatureBmcReset,
providers.FeatureSELClear,
providers.FeatureClearSystemEventLog,
}
)

Expand Down
4 changes: 2 additions & 2 deletions providers/redfish/sel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package redfish

import "context"

func (c *Conn) ClearSEL(ctx context.Context) (err error) {
return c.redfishwrapper.ClearSEL(ctx)
func (c *Conn) ClearSystemEventLog(ctx context.Context) (err error) {
return c.redfishwrapper.ClearSystemEventLog(ctx)
}

0 comments on commit 3bfa968

Please sign in to comment.