Skip to content

Commit

Permalink
[FAB-7821] Update tests to use generic client opts
Browse files Browse the repository at this point in the history
This patch updates the integration tests to use the updated
SDK client APIs. This patch also adds helper client functions
to reduce boiler plate.

Change-Id: I9bb77b29f56011b07e16edfcac3eed2c8b86948b
Signed-off-by: Troy Ronda <[email protected]>
  • Loading branch information
troyronda committed Jan 19, 2018
1 parent 864ac4b commit 5843985
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 47 deletions.
16 changes: 13 additions & 3 deletions def/fabapi/deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,30 @@ func TestNewSDK(t *testing.T) {
t.Fatalf("Error initializing SDK: %s", err)
}

c1, err := sdk.NewClient(fabsdk.WithUser("User1"))
if err != nil {
t.Fatalf("Failed to create client: %s", err)
}

// Default channel client (uses organisation from client configuration)
_, err = sdk.NewChannelClient("mychannel", "User1")
_, err = c1.Channel("mychannel")
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}

c2, err := sdk.NewClient(fabsdk.WithUser("User1"), fabsdk.WithOrg("Org2"))
if err != nil {
t.Fatalf("Failed to create client: %s", err)
}

// Test configuration failure for channel client (mychannel does't have event source configured for Org2)
_, err = sdk.NewChannelClientWithOpts("mychannel", "User1", &fabsdk.ChannelClientOpts{OrgName: "Org2"})
_, err = c2.Channel("mychannel")
if err == nil {
t.Fatalf("Should have failed to create channel client since event source not configured for Org2")
}

// Test new channel client with options
_, err = sdk.NewChannelClientWithOpts("orgchannel", "User1", &fabsdk.ChannelClientOpts{OrgName: "Org2"})
_, err = c2.Channel("orgchannel")
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/fabsdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,29 @@ func (c *Client) Channel(id string) (apitxn.ChannelClient, error) {
return client, nil
}

// NewChannelMgmtClient returns a new client for managing channels
func (sdk *FabricSDK) NewChannelMgmtClient(userName string, opts ...ClientOption) (chmgmt.ChannelMgmtClient, error) {
c, err := sdk.NewClient(WithUser(userName), opts...)
// NewClientChannelMgmt returns a new client for managing channels
func (sdk *FabricSDK) NewClientChannelMgmt(identity IdentityOption, opts ...ClientOption) (chmgmt.ChannelMgmtClient, error) {
c, err := sdk.NewClient(identity, opts...)
if err != nil {
return nil, errors.WithMessage(err, "error creating client from SDK")
}

return c.ChannelMgmt()
}

// NewResourceMgmtClient returns a new client for managing system resources
func (sdk *FabricSDK) NewResourceMgmtClient(userName string, opts ...ClientOption) (resmgmt.ResourceMgmtClient, error) {
c, err := sdk.NewClient(WithUser(userName), opts...)
// NewClientResourceMgmt returns a new client for managing system resources
func (sdk *FabricSDK) NewClientResourceMgmt(identity IdentityOption, opts ...ClientOption) (resmgmt.ResourceMgmtClient, error) {
c, err := sdk.NewClient(identity, opts...)
if err != nil {
return nil, errors.WithMessage(err, "error creating client from SDK")
}

return c.ResourceMgmt()
}

// NewChannelClient returns a new client for a channel
func (sdk *FabricSDK) NewChannelClient(channelID string, userName string, opts ...ClientOption) (apitxn.ChannelClient, error) {
c, err := sdk.NewClient(WithUser(userName), opts...)
// NewClientChannel returns a new client for a channel
func (sdk *FabricSDK) NewClientChannel(identity IdentityOption, channelID string, opts ...ClientOption) (apitxn.ChannelClient, error) {
c, err := sdk.NewClient(identity, opts...)
if err != nil {
return nil, errors.WithMessage(err, "error creating client from SDK")
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/fabsdk/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

const (
clientConfigFile = "testdata/test.yaml"
clientValidAdmin = "Admin"
clientValidUser = "User1"
clientValidExtraOrg = "OrgX"
clientValidExtraUser = "OrgXUser"
Expand Down Expand Up @@ -168,13 +169,13 @@ func TestNewChannelMgmtClient(t *testing.T) {
}

// Test configuration failure for channel management client (invalid user/default organisation)
_, err = sdk.NewChannelMgmtClient("Invalid")
_, err = sdk.NewClientChannelMgmt(WithUser("Invalid"))
if err == nil {
t.Fatalf("Should have failed to create channel client due to invalid user")
}

// Test valid configuration for channel management client
_, err = sdk.NewChannelMgmtClient(txValidClientAdmin)
_, err = sdk.NewClientChannelMgmt(WithUser(clientValidAdmin))
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}
Expand All @@ -192,13 +193,13 @@ func TestNewResourceMgmtClient(t *testing.T) {
}

// Test configuration failure for resource management client (invalid user/default organisation)
_, err = sdk.NewResourceMgmtClient("Invalid")
_, err = sdk.NewClientResourceMgmt(WithUser("Invalid"))
if err == nil {
t.Fatalf("Should have failed to create resource management client due to invalid user")
}

// Test valid configuration for resource management client
_, err = sdk.NewResourceMgmtClient(txValidClientAdmin)
_, err = sdk.NewClientResourceMgmt(WithUser(clientValidAdmin))
if err != nil {
t.Fatalf("Failed to create new resource management client: %s", err)
}
Expand Down
49 changes: 41 additions & 8 deletions pkg/fabsdk/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ type ResourceMgmtClientOpts struct {
ConfigProvider apiconfig.Config
}

// NewSystemClient returns a new client for the system (operations not on a channel)
// TODO: Reduced immutable interface
// TODO: Parameter for setting up the peers
// This function is deprecated.
func (sdk *FabricSDK) NewSystemClient(s apisdk.Session) (apifabclient.FabricClient, error) {
return sdk.FabricProvider().NewClient(s.Identity())
}

// NewChannelMgmtClientWithOpts returns a new client for managing channels with options
// This function is deprecated.
func (sdk *FabricSDK) NewChannelMgmtClientWithOpts(userName string, opt *ChannelMgmtClientOpts) (chmgmt.ChannelMgmtClient, error) {
Expand Down Expand Up @@ -106,6 +98,47 @@ func (sdk *FabricSDK) NewChannelClientWithOpts(channelID string, userName string
return c.Channel(channelID)
}

// NewChannelMgmtClient returns a new client for managing channels
// This function is deprecated.
func (sdk *FabricSDK) NewChannelMgmtClient(userName string, opts ...ClientOption) (chmgmt.ChannelMgmtClient, error) {
c, err := sdk.NewClient(WithUser(userName), opts...)
if err != nil {
return nil, errors.WithMessage(err, "error creating client from SDK")
}

return c.ChannelMgmt()
}

// NewResourceMgmtClient returns a new client for managing system resources
// This function is deprecated.
func (sdk *FabricSDK) NewResourceMgmtClient(userName string, opts ...ClientOption) (resmgmt.ResourceMgmtClient, error) {
c, err := sdk.NewClient(WithUser(userName), opts...)
if err != nil {
return nil, errors.WithMessage(err, "error creating client from SDK")
}

return c.ResourceMgmt()
}

// NewChannelClient returns a new client for a channel
// This function is deprecated.
func (sdk *FabricSDK) NewChannelClient(channelID string, userName string, opts ...ClientOption) (apitxn.ChannelClient, error) {
c, err := sdk.NewClient(WithUser(userName), opts...)
if err != nil {
return nil, errors.WithMessage(err, "error creating client from SDK")
}

return c.Channel(channelID)
}

// NewSystemClient returns a new client for the system (operations not on a channel)
// TODO: Reduced immutable interface
// TODO: Parameter for setting up the peers
// This function is deprecated.
func (sdk *FabricSDK) NewSystemClient(s apisdk.Session) (apifabclient.FabricClient, error) {
return sdk.FabricProvider().NewClient(s.Identity())
}

// NewPreEnrolledUserSession returns a new pre-enrolled user session
// This function is deprecated.
func (sdk *FabricSDK) NewPreEnrolledUserSession(orgID string, id string) (*Session, error) {
Expand Down
24 changes: 24 additions & 0 deletions pkg/fabsdk/deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func TestNewChannelMgmtWithOptsClient(t *testing.T) {
t.Fatalf("Error initializing SDK: %s", err)
}

// Test configuration failure for channel management client (invalid user/default organisation)
_, err = sdk.NewChannelMgmtClient("Invalid")
if err == nil {
t.Fatalf("Should have failed to create channel client due to invalid user")
}

// Test valid configuration for channel management client
_, err = sdk.NewChannelMgmtClient(clientValidAdmin)
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}

// Test configuration failure for new channel management client with options (invalid org)
_, err = sdk.NewChannelMgmtClientWithOpts(txValidClientAdmin, &ChannelMgmtClientOpts{OrgName: "Invalid"})
if err == nil {
Expand All @@ -55,6 +67,18 @@ func TestNewResourceMgmtWithOptsClient(t *testing.T) {
t.Fatalf("Error initializing SDK: %s", err)
}

// Test configuration failure for resource management client (invalid user/default organisation)
_, err = sdk.NewResourceMgmtClient("Invalid")
if err == nil {
t.Fatalf("Should have failed to create resource management client due to invalid user")
}

// Test valid configuration for resource management client
_, err = sdk.NewResourceMgmtClient(clientValidAdmin)
if err != nil {
t.Fatalf("Failed to create new resource management client: %s", err)
}

// Test configuration failure for new resource management client with options (invalid org)
_, err = sdk.NewResourceMgmtClientWithOpts(txValidClientAdmin, &ResourceMgmtClientOpts{OrgName: "Invalid"})
if err == nil {
Expand Down
41 changes: 33 additions & 8 deletions pkg/fabsdk/fabsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,30 @@ func TestNewDefaultSDK(t *testing.T) {
t.Fatalf("Error initializing SDK: %s", err)
}

clt1, err := sdk.NewClient(WithUser(sdkValidClientUser))
if err != nil {
t.Fatalf("Failed to create client: %s", err)
}

// Default channel client (uses organisation from client configuration)
_, err = sdk.NewChannelClient("mychannel", sdkValidClientUser)
_, err = clt1.Channel("mychannel")
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}

clt2, err := sdk.NewClient(WithUser(sdkValidClientUser), WithOrg(sdkValidClientOrg2))
if err != nil {
t.Fatalf("Failed to create client: %s", err)
}

// Test configuration failure for channel client (mychannel does't have event source configured for Org2)
_, err = sdk.NewChannelClientWithOpts("mychannel", sdkValidClientUser, &ChannelClientOpts{OrgName: sdkValidClientOrg2})
_, err = clt2.Channel("mychannel")
if err == nil {
t.Fatalf("Should have failed to create channel client since event source not configured for Org2")
}

// Test new channel client with options
_, err = sdk.NewChannelClientWithOpts("orgchannel", sdkValidClientUser, &ChannelClientOpts{OrgName: sdkValidClientOrg2})
_, err = clt2.Channel("orgchannel")
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}
Expand Down Expand Up @@ -238,8 +248,13 @@ func TestWithSessionPkg(t *testing.T) {
}
factory.EXPECT().NewChannelMgmtClient(sdk, gomock.Any(), c).Return(cm, nil)

clt, err := sdk.NewClient(WithUser(sdkValidClientUser))
if err != nil {
t.Fatalf("Unexpected error getting client: %s", err)
}

// Use a method that invokes credential manager (e.g., new user)
_, err = sdk.NewChannelMgmtClient(sdkValidClientUser)
_, err = clt.ChannelMgmt()
if err != nil {
t.Fatalf("Unexpected error getting channel management client: %s", err)
}
Expand Down Expand Up @@ -334,25 +349,35 @@ func TestNewDefaultTwoValidSDK(t *testing.T) {
t.Fatalf("Unexpected org in config: %s", client1.Organization)
}

clt1, err := sdk1.NewClient(WithUser(sdkValidClientUser))
if err != nil {
t.Fatalf("Failed to create client: %s", err)
}

// Test SDK1 channel clients ('mychannel', 'orgchannel')
_, err = sdk1.NewChannelClient("mychannel", sdkValidClientUser)
_, err = clt1.Channel("mychannel")
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}

_, err = sdk1.NewChannelClient("orgchannel", sdkValidClientUser)
_, err = clt1.Channel("orgchannel")
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}

clt2, err := sdk2.NewClient(WithUser(sdkValidClientUser))
if err != nil {
t.Fatalf("Failed to create client: %s", err)
}

// SDK 2 doesn't have 'mychannel' configured
_, err = sdk2.NewChannelClient("mychannel", sdkValidClientUser)
_, err = clt2.Channel("mychannel")
if err == nil {
t.Fatalf("Should have failed to create channel that is not configured")
}

// SDK 2 has 'orgchannel' configured
_, err = sdk2.NewChannelClient("orgchannel", sdkValidClientUser)
_, err = clt2.Channel("orgchannel")
if err != nil {
t.Fatalf("Failed to create new 'orgchannel' channel client: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/base_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ func (setup *BaseSetupImpl) Initialize(t *testing.T) error {
setup.Channel = channel

// Channel management client is responsible for managing channels (create/update)
chMgmtClient, err := sdk.NewChannelMgmtClientWithOpts("Admin", &fabsdk.ChannelMgmtClientOpts{OrgName: "ordererorg"})
chMgmtClient, err := sdk.NewClientChannelMgmt(fabsdk.WithUser("Admin"), fabsdk.WithOrg("ordererorg"))
if err != nil {
t.Fatalf("Failed to create new channel management client: %s", err)
}

// Resource management client is responsible for managing resources (joining channels, install/instantiate/upgrade chaincodes)
resMgmtClient, err = sdk.NewResourceMgmtClient("Admin")
resMgmtClient, err = sdk.NewClientResourceMgmt(fabsdk.WithUser("Admin"))
if err != nil {
t.Fatalf("Failed to create new resource management client: %s", err)
}
Expand Down
14 changes: 10 additions & 4 deletions test/integration/e2e/end_to_end.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Run(t *testing.T, config apiconfig.Config, sdkOpts ...fabsdk.Option) {

// Channel management client is responsible for managing channels (create/update channel)
// Supply user that has privileges to create channel (in this case orderer admin)
chMgmtClient, err := sdk.NewChannelMgmtClientWithOpts("Admin", &fabsdk.ChannelMgmtClientOpts{OrgName: "ordererorg"})
chMgmtClient, err := sdk.NewClientChannelMgmt(fabsdk.WithUser("Admin"), fabsdk.WithOrg("ordererorg"))
if err != nil {
t.Fatalf("Failed to create channel management client: %s", err)
}
Expand All @@ -73,8 +73,8 @@ func Run(t *testing.T, config apiconfig.Config, sdkOpts ...fabsdk.Option) {
// Allow orderer to process channel creation
time.Sleep(time.Second * 3)

// Org resource management client (Org1 is default org)
orgResMgmt, err := sdk.NewResourceMgmtClient(orgAdmin)
// Org resource management client
orgResMgmt, err := sdk.NewClientResourceMgmt(fabsdk.WithUser(orgAdmin))
if err != nil {
t.Fatalf("Failed to create new resource management client: %s", err)
}
Expand Down Expand Up @@ -108,8 +108,14 @@ func Run(t *testing.T, config apiconfig.Config, sdkOpts ...fabsdk.Option) {

// ************ Test setup complete ************** //

// Client provides access to APIs for transacting with Fabric (Org1 is default org)
client, err := sdk.NewClient(fabsdk.WithUser("User1"))
if err != nil {
t.Fatalf("Failed to create client: %s", err)
}

// Channel client is used to query and execute transactions
chClient, err := sdk.NewChannelClient(channelID, "User1")
chClient, err := client.Channel(channelID)
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}
Expand Down
Loading

0 comments on commit 5843985

Please sign in to comment.