-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package bmc | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
) | ||
|
||
type NMISender interface { | ||
SendNMI(ctx context.Context) error | ||
} | ||
|
||
func sendNMI(ctx context.Context, timeout time.Duration, sender NMISender, metadata *Metadata) error { | ||
senderName := getProviderName(sender) | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
metadata.ProvidersAttempted = append(metadata.ProvidersAttempted, senderName) | ||
|
||
err := sender.SendNMI(ctx) | ||
if err != nil { | ||
metadata.FailedProviderDetail[senderName] = err.Error() | ||
return err | ||
} | ||
|
||
metadata.SuccessfulProvider = senderName | ||
|
||
return nil | ||
} | ||
|
||
// SendNMIFromInterface will look for providers that implement NMISender | ||
// and attempt to call SendNMI until a provider is successful, | ||
// or all providers have been exhausted. | ||
func SendNMIFromInterface( | ||
ctx context.Context, | ||
timeout time.Duration, | ||
providers []interface{}, | ||
) (metadata Metadata, err error) { | ||
metadata = newMetadata() | ||
|
||
for _, provider := range providers { | ||
sender, ok := provider.(NMISender) | ||
if !ok { | ||
err = multierror.Append(err, fmt.Errorf("not an NMISender implementation: %T", provider)) | ||
continue | ||
} | ||
|
||
sendNMIErr := sendNMI(ctx, timeout, sender, &metadata) | ||
if sendNMIErr != nil { | ||
err = multierror.Append(err, sendNMIErr) | ||
continue | ||
} | ||
return metadata, nil | ||
} | ||
|
||
if len(metadata.ProvidersAttempted) == 0 { | ||
err = multierror.Append(err, errors.New("no NMISender implementations found")) | ||
} else { | ||
err = multierror.Append(err, errors.New("failed to send NMI")) | ||
} | ||
|
||
return metadata, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package bmc | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockNMISender struct { | ||
err error | ||
} | ||
|
||
func (m *mockNMISender) SendNMI(ctx context.Context) error { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
default: | ||
return m.err | ||
} | ||
} | ||
|
||
func (m *mockNMISender) Name() string { | ||
return "mock" | ||
} | ||
|
||
func TestSendNMIFromInterface(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
mockSenders []interface{} | ||
errMsg string | ||
isTimedout bool | ||
expectedMetadata Metadata | ||
}{ | ||
{ | ||
name: "success", | ||
mockSenders: []interface{}{&mockNMISender{}}, | ||
expectedMetadata: Metadata{ | ||
SuccessfulProvider: "mock", | ||
ProvidersAttempted: []string{"mock"}, | ||
FailedProviderDetail: make(map[string]string), | ||
}, | ||
}, | ||
{ | ||
name: "success with multiple senders", | ||
mockSenders: []interface{}{ | ||
nil, | ||
"foo", | ||
&mockNMISender{err: errors.New("err from sender")}, | ||
&mockNMISender{}, | ||
}, | ||
expectedMetadata: Metadata{ | ||
SuccessfulProvider: "mock", | ||
ProvidersAttempted: []string{"mock", "mock"}, | ||
FailedProviderDetail: map[string]string{"mock": "err from sender"}, | ||
}, | ||
}, | ||
{ | ||
name: "not an nmisender", | ||
mockSenders: []interface{}{nil}, | ||
errMsg: "not an NMISender", | ||
expectedMetadata: Metadata{ | ||
FailedProviderDetail: make(map[string]string), | ||
}, | ||
}, | ||
{ | ||
name: "no nmisenders", | ||
mockSenders: []interface{}{}, | ||
errMsg: "no NMISender implementations found", | ||
expectedMetadata: Metadata{ | ||
FailedProviderDetail: make(map[string]string), | ||
}, | ||
}, | ||
{ | ||
name: "timed out", | ||
mockSenders: []interface{}{&mockNMISender{}}, | ||
isTimedout: true, | ||
errMsg: "context deadline exceeded", | ||
expectedMetadata: Metadata{ | ||
ProvidersAttempted: []string{"mock"}, | ||
FailedProviderDetail: map[string]string{"mock": "context deadline exceeded"}, | ||
}, | ||
}, | ||
{ | ||
name: "error from nmisender", | ||
mockSenders: []interface{}{&mockNMISender{err: errors.New("foobar")}}, | ||
errMsg: "foobar", | ||
expectedMetadata: Metadata{ | ||
ProvidersAttempted: []string{"mock"}, | ||
FailedProviderDetail: map[string]string{"mock": "foobar"}, | ||
}, | ||
}, | ||
{ | ||
name: "error when fail to send", | ||
mockSenders: []interface{}{&mockNMISender{err: errors.New("err from sender")}}, | ||
errMsg: "failed to send NMI", | ||
expectedMetadata: Metadata{ | ||
ProvidersAttempted: []string{"mock"}, | ||
FailedProviderDetail: map[string]string{"mock": "err from sender"}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
timeout := time.Second * 60 | ||
if tt.isTimedout { | ||
timeout = 0 | ||
} | ||
|
||
metadata, err := SendNMIFromInterface(context.Background(), timeout, tt.mockSenders) | ||
|
||
if tt.errMsg == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.ErrorContains(t, err, tt.errMsg) | ||
} | ||
|
||
assert.Equal(t, tt.expectedMetadata, metadata) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters