diff --git a/bmc/floppy.go b/bmc/floppy.go index 0965de37..2cbb4e69 100644 --- a/bmc/floppy.go +++ b/bmc/floppy.go @@ -5,6 +5,7 @@ import ( "fmt" "io" + bmclibErrs "github.com/bmc-toolbox/bmclib/v2/errors" "github.com/hashicorp/go-multierror" "github.com/pkg/errors" ) @@ -66,7 +67,14 @@ func UploadFloppyImageFromInterfaces(ctx context.Context, image io.Reader, p []i } if len(providers) == 0 { - return metadata, multierror.Append(err, errors.New("no FloppyImageUploader implementations found")) + return metadata, multierror.Append( + err, + errors.Wrap( + bmclibErrs.ErrProviderImplementation, + "no FloppyImageUploader implementations found", + ), + ) + } return uploadFloppyImage(ctx, image, providers) @@ -129,7 +137,13 @@ func UnmountFloppyImageFromInterfaces(ctx context.Context, p []interface{}) (met } if len(providers) == 0 { - return metadata, multierror.Append(err, errors.New("no FloppyImageUnmounter implementations found")) + return metadata, multierror.Append( + err, + errors.Wrap( + bmclibErrs.ErrProviderImplementation, + "no FloppyImageUnmounter implementations found", + ), + ) } return unmountFloppyImage(ctx, providers) diff --git a/bmc/floppy_test.go b/bmc/floppy_test.go new file mode 100644 index 00000000..bbb413e2 --- /dev/null +++ b/bmc/floppy_test.go @@ -0,0 +1,114 @@ +package bmc + +import ( + "context" + "io" + "testing" + "time" + + bmclibErrs "github.com/bmc-toolbox/bmclib/v2/errors" + "github.com/stretchr/testify/assert" +) + +type uploadFloppyImageTester struct { + returnError error +} + +func (p *uploadFloppyImageTester) UploadFloppyImage(ctx context.Context, reader io.Reader) (err error) { + return p.returnError +} + +func (p *uploadFloppyImageTester) Name() string { + return "foo" +} + +func TestUploadFloppyFromInterfaces(t *testing.T) { + testCases := []struct { + testName string + image io.Reader + returnError error + ctxTimeout time.Duration + providerName string + providersAttempted int + badImplementation bool + }{ + {"success with metadata", nil, nil, 5 * time.Second, "foo", 1, false}, + {"failure with bad implementation", nil, bmclibErrs.ErrProviderImplementation, 1 * time.Nanosecond, "foo", 1, true}, + } + + for _, tc := range testCases { + t.Run(tc.testName, func(t *testing.T) { + var generic []interface{} + if tc.badImplementation { + badImplementation := struct{}{} + generic = []interface{}{&badImplementation} + } else { + testImplementation := &uploadFloppyImageTester{returnError: tc.returnError} + generic = []interface{}{testImplementation} + } + metadata, err := UploadFloppyImageFromInterfaces(context.Background(), tc.image, generic) + if tc.returnError != nil { + assert.ErrorContains(t, err, tc.returnError.Error()) + return + } + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, tc.returnError, err) + assert.Equal(t, tc.providerName, metadata.SuccessfulProvider) + }) + } +} + +type unmountFloppyImageTester struct { + returnError error +} + +func (p *unmountFloppyImageTester) UnmountFloppyImage(ctx context.Context) (err error) { + return p.returnError +} + +func (p *unmountFloppyImageTester) Name() string { + return "foo" +} + +func TestUnmountFloppyFromInterfaces(t *testing.T) { + testCases := []struct { + testName string + returnError error + ctxTimeout time.Duration + providerName string + providersAttempted int + badImplementation bool + }{ + {"success with metadata", nil, 5 * time.Second, "foo", 1, false}, + {"failure with bad implementation", bmclibErrs.ErrProviderImplementation, 1 * time.Nanosecond, "foo", 1, true}, + } + + for _, tc := range testCases { + t.Run(tc.testName, func(t *testing.T) { + var generic []interface{} + if tc.badImplementation { + badImplementation := struct{}{} + generic = []interface{}{&badImplementation} + } else { + testImplementation := &unmountFloppyImageTester{returnError: tc.returnError} + generic = []interface{}{testImplementation} + } + metadata, err := UnmountFloppyImageFromInterfaces(context.Background(), generic) + if tc.returnError != nil { + assert.ErrorContains(t, err, tc.returnError.Error()) + return + } + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, tc.returnError, err) + assert.Equal(t, tc.providerName, metadata.SuccessfulProvider) + }) + } +}