Skip to content

Commit

Permalink
[FAB-7728] Check for error in fabsdk option
Browse files Browse the repository at this point in the history
Change-Id: Id628c689a9b4e11e7ef8c435745265dc8c3b7c4e
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Jan 14, 2018
1 parent ab16930 commit 6662c7f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/fabsdk/fabsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ func New(options ...SDKOption) (*FabricSDK, error) {
}

for _, option := range options {
option(&sdk)
_, err := option(&sdk)
if err != nil {
return nil, errors.WithMessage(err, "Error in option passed to New")
}
}

// Initialize logging provider with default logging provider (if needed)
Expand Down
26 changes: 26 additions & 0 deletions pkg/fabsdk/fabsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/def/factory/defclient"
"github.com/hyperledger/fabric-sdk-go/def/factory/defcore"
"github.com/hyperledger/fabric-sdk-go/def/factory/defsvc"
"github.com/hyperledger/fabric-sdk-go/pkg/errors"
apisdk "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/api"
"github.com/hyperledger/fabric-sdk-go/pkg/logging/deflogger"
)
Expand All @@ -28,6 +29,31 @@ func defPkgSuite() SDKOption {
return PkgSuiteAsOpt(pkgSuite)
}

func TestNewGoodOpt(t *testing.T) {
_, err := New(ConfigFile("../../test/fixtures/config/config_test.yaml"), goodOpt(), defPkgSuite())
if err != nil {
t.Fatalf("Expected no error from New, but got %v", err)
}
}

func goodOpt() SDKOption {
return func(sdk *FabricSDK) (*FabricSDK, error) {
return sdk, nil
}
}

func TestNewBadOpt(t *testing.T) {
_, err := New(ConfigFile("../../test/fixtures/config/config_test.yaml"), badOpt(), defPkgSuite())
if err == nil {
t.Fatalf("Expected error from New")
}
}

func badOpt() SDKOption {
return func(sdk *FabricSDK) (*FabricSDK, error) {
return sdk, errors.New("Bad Opt")
}
}
func TestNewDefaultSDK(t *testing.T) {
// Test new SDK with invalid config file
_, err := New(ConfigFile("../../test/fixtures/config/invalid.yaml"), StateStorePath("/tmp/state"), defPkgSuite())
Expand Down

0 comments on commit 6662c7f

Please sign in to comment.