Skip to content

Commit

Permalink
bmc/floppy: fixes returned error and adds test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Oct 20, 2023
1 parent a1ef9ca commit 1bbb62f
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
18 changes: 16 additions & 2 deletions bmc/floppy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"

bmclibErrs "github.com/bmc-toolbox/bmclib/v2/errors"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
114 changes: 114 additions & 0 deletions bmc/floppy_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit 1bbb62f

Please sign in to comment.