-
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.
Merge pull request #381 from zevweiss/terminate-sol
Add TerminateSOL method
- Loading branch information
Showing
7 changed files
with
215 additions
and
0 deletions.
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,70 @@ | ||
package bmc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// SOLDeactivator for deactivating SOL sessions on a BMC. | ||
type SOLDeactivator interface { | ||
DeactivateSOL(ctx context.Context) (err error) | ||
} | ||
|
||
// deactivatorProvider is an internal struct to correlate an implementation/provider and its name | ||
type deactivatorProvider struct { | ||
name string | ||
solDeactivator SOLDeactivator | ||
} | ||
|
||
// deactivateSOL tries all implementations for a successful SOL deactivation | ||
func deactivateSOL(ctx context.Context, timeout time.Duration, b []deactivatorProvider) (metadata Metadata, err error) { | ||
var metadataLocal Metadata | ||
|
||
for _, elem := range b { | ||
if elem.solDeactivator == nil { | ||
continue | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
err = multierror.Append(err, ctx.Err()) | ||
|
||
return metadata, err | ||
default: | ||
metadataLocal.ProvidersAttempted = append(metadataLocal.ProvidersAttempted, elem.name) | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
newErr := elem.solDeactivator.DeactivateSOL(ctx) | ||
if newErr != nil { | ||
err = multierror.Append(err, errors.WithMessagef(newErr, "provider: %v", elem.name)) | ||
continue | ||
} | ||
metadataLocal.SuccessfulProvider = elem.name | ||
return metadataLocal, nil | ||
} | ||
} | ||
return metadataLocal, multierror.Append(err, errors.New("failed to deactivate SOL session")) | ||
} | ||
|
||
// DeactivateSOLFromInterfaces identifies implementations of the SOLDeactivator interface and passes them to the deactivateSOL() wrapper method. | ||
func DeactivateSOLFromInterfaces(ctx context.Context, timeout time.Duration, generic []interface{}) (metadata Metadata, err error) { | ||
deactivators := make([]deactivatorProvider, 0) | ||
for _, elem := range generic { | ||
temp := deactivatorProvider{name: getProviderName(elem)} | ||
switch p := elem.(type) { | ||
case SOLDeactivator: | ||
temp.solDeactivator = p | ||
deactivators = append(deactivators, temp) | ||
default: | ||
e := fmt.Sprintf("not an SOLDeactivator implementation: %T", p) | ||
err = multierror.Append(err, errors.New(e)) | ||
} | ||
} | ||
if len(deactivators) == 0 { | ||
return metadata, multierror.Append(err, errors.New("no SOLDeactivator implementations found")) | ||
} | ||
return deactivateSOL(ctx, timeout, deactivators) | ||
} |
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,99 @@ | ||
package bmc | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/go-multierror" | ||
) | ||
|
||
type solTermTester struct { | ||
MakeErrorOut bool | ||
} | ||
|
||
func (r *solTermTester) DeactivateSOL(ctx context.Context) (err error) { | ||
if r.MakeErrorOut { | ||
return errors.New("SOL deactivation failed") | ||
} | ||
return nil | ||
} | ||
|
||
func (r *solTermTester) Name() string { | ||
return "test provider" | ||
} | ||
|
||
func TestDeactivateSOL(t *testing.T) { | ||
testCases := map[string]struct { | ||
makeErrorOut bool | ||
err error | ||
ctxTimeout time.Duration | ||
}{ | ||
"success": {makeErrorOut: false}, | ||
"error": {makeErrorOut: true, err: &multierror.Error{Errors: []error{errors.New("provider: test provider: SOL deactivation failed"), errors.New("failed to deactivate SOL session")}}}, | ||
"error context timeout": {makeErrorOut: false, err: &multierror.Error{Errors: []error{errors.New("context deadline exceeded")}}, ctxTimeout: time.Nanosecond * 1}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
testImplementation := solTermTester{MakeErrorOut: tc.makeErrorOut} | ||
if tc.ctxTimeout == 0 { | ||
tc.ctxTimeout = time.Second * 3 | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), tc.ctxTimeout) | ||
defer cancel() | ||
_, err := deactivateSOL(ctx, 0, []deactivatorProvider{{"test provider", &testImplementation}}) | ||
var diff string | ||
if err != nil && tc.err != nil { | ||
diff = cmp.Diff(err.Error(), tc.err.Error()) | ||
} else { | ||
diff = cmp.Diff(err, tc.err) | ||
} | ||
if diff != "" { | ||
t.Fatal(diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDeactivateSOLFromInterfaces(t *testing.T) { | ||
testCases := map[string]struct { | ||
err error | ||
badImplementation bool | ||
withName bool | ||
}{ | ||
"success": {}, | ||
"success with metadata": {withName: true}, | ||
"no implementations found": {badImplementation: true, err: &multierror.Error{Errors: []error{errors.New("not an SOLDeactivator implementation: *struct {}"), errors.New("no SOLDeactivator implementations found")}}}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
var generic []interface{} | ||
if tc.badImplementation { | ||
badImplementation := struct{}{} | ||
generic = []interface{}{&badImplementation} | ||
} else { | ||
testImplementation := solTermTester{} | ||
generic = []interface{}{&testImplementation} | ||
} | ||
metadata, err := DeactivateSOLFromInterfaces(context.Background(), 0, generic) | ||
var diff string | ||
if err != nil && tc.err != nil { | ||
diff = cmp.Diff(err.Error(), tc.err.Error()) | ||
} else { | ||
diff = cmp.Diff(err, tc.err) | ||
} | ||
if diff != "" { | ||
t.Fatal(diff) | ||
} | ||
if tc.withName { | ||
if diff := cmp.Diff(metadata.SuccessfulProvider, "test provider"); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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