Skip to content

Commit

Permalink
[FAB-8482] Context and ChannelContext integration tests
Browse files Browse the repository at this point in the history
- Updated all integration-tests to use sdk.Context
  and sdk.ChannelContext
- Exposed errors while building contexts
 (added context.ClientProvider and context.ChannelProvider)
- Removed dependencies on fabsdk.NewClient
- Marked fabsdk.NewClient deprecated
- Deleted ClientContext.Session()
- Deleted ChananelService.Ledger()
- Added DiscoveryFilter to channel client

In next commit will remove sdk.NewClient usage in
ccpolicyprovider.


Change-Id: I41de27036205000e2b0a633a72b413c09639a068
Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Mar 7, 2018
1 parent d9320d1 commit eaa71c4
Show file tree
Hide file tree
Showing 29 changed files with 471 additions and 234 deletions.
55 changes: 41 additions & 14 deletions pkg/client/channel/chclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ const (
// An application that requires interaction with multiple channels should create a separate
// instance of the channel client for each channel. Channel client supports non-admin functions only.
type Client struct {
context context.Channel
membership fab.ChannelMembership
transactor fab.Transactor
eventHub fab.EventHub
greylist *greylist.Filter
context context.Channel
membership fab.ChannelMembership
transactor fab.Transactor
eventHub fab.EventHub
greylist *greylist.Filter
discoveryFilter fab.TargetFilter
}

type customChannelContext struct {
Expand All @@ -52,40 +53,66 @@ func (ccc *customChannelContext) DiscoveryService() fab.DiscoveryService {
return ccc.discoveryService
}

// ClientOption describes a functional parameter for the New constructor
type ClientOption func(*Client) error

// WithTargetFilter option to configure new
func WithTargetFilter(filter fab.TargetFilter) ClientOption {
return func(client *Client) error {
client.discoveryFilter = filter
return nil
}
}

// New returns a Client instance.
func New(c context.Channel) (*Client, error) {
func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error) {

channelContext, err := channelProvider()
if err != nil {
return nil, errors.WithMessage(err, "failed to create channel context")
}

greylistProvider := greylist.New(c.Config().TimeoutOrDefault(core.DiscoveryGreylistExpiry))
greylistProvider := greylist.New(channelContext.Config().TimeoutOrDefault(core.DiscoveryGreylistExpiry))

if c.ChannelService() == nil {
if channelContext.ChannelService() == nil {
return nil, errors.New("channel service not initialized")
}

eventHub, err := c.ChannelService().EventHub()
eventHub, err := channelContext.ChannelService().EventHub()
if err != nil {
return nil, errors.WithMessage(err, "event hub creation failed")
}

transactor, err := c.ChannelService().Transactor()
transactor, err := channelContext.ChannelService().Transactor()
if err != nil {
return nil, errors.WithMessage(err, "transactor creation failed")
}

membership, err := c.ChannelService().Membership()
membership, err := channelContext.ChannelService().Membership()
if err != nil {
return nil, errors.WithMessage(err, "membership creation failed")
}

customDiscoveryService := discovery.NewDiscoveryFilterService(c.DiscoveryService(), greylistProvider)

channelClient := Client{
context: &customChannelContext{Channel: c, discoveryService: customDiscoveryService},
membership: membership,
transactor: transactor,
eventHub: eventHub,
greylist: greylistProvider,
}

for _, param := range opts {
param(&channelClient)
}

//target filter
discoveryService := discovery.NewDiscoveryFilterService(channelContext.DiscoveryService(), channelClient.discoveryFilter)

//greylist filter
customDiscoveryService := discovery.NewDiscoveryFilterService(discoveryService, greylistProvider)

//update context
channelClient.context = &customChannelContext{Channel: channelContext, discoveryService: customDiscoveryService}

return &channelClient, nil
}

Expand Down
27 changes: 21 additions & 6 deletions pkg/client/channel/chclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func TestDiscoveryGreylist(t *testing.T) {
assert.Nil(t, err, "Got error %s", err)

fabCtx := setupCustomTestContext(t, selectionService, discoveryService, nil)
ctx := contextImpl.NewChannel(fabCtx, channelID)
ctx := createChannelContext(fabCtx, channelID)

chClient, err := New(ctx)
assert.Nil(t, err, "Got error %s", err)
Expand All @@ -483,7 +483,7 @@ func TestDiscoveryGreylist(t *testing.T) {
assert.EqualValues(t, status.NoPeersFound.ToInt32(), s.Code, "expected No Peers Found status on greylist")
assert.Equal(t, 1, testPeer1.ProcessProposalCalls, "expected peer 1 to be greylisted")
// Wait for greylist expiry
time.Sleep(ctx.Providers().Config().TimeoutOrDefault(core.DiscoveryGreylistExpiry))
time.Sleep(chClient.context.Config().TimeoutOrDefault(core.DiscoveryGreylistExpiry))
testPeer1.ProcessProposalCalls = 0
testPeer1.Error = status.New(status.EndorserServerStatus, int32(common.Status_SERVICE_UNAVAILABLE), "test", nil)
// Try again
Expand Down Expand Up @@ -524,7 +524,7 @@ func setupTestContext() context.Client {
return ctx
}

func setupCustomTestContext(t *testing.T, selectionService fab.SelectionService, discoveryService fab.DiscoveryService, orderers []fab.Orderer) context.Client {
func setupCustomTestContext(t *testing.T, selectionService fab.SelectionService, discoveryService fab.DiscoveryService, orderers []fab.Orderer) context.ClientProvider {
user := fcmocks.NewMockUser("test")
ctx := fcmocks.NewMockContext(user)

Expand All @@ -546,7 +546,7 @@ func setupCustomTestContext(t *testing.T, selectionService fab.SelectionService,
discoveryProvider := ctx.MockProviderContext.DiscoveryProvider()
discoveryProvider.(*fcmocks.MockStaticDiscoveryProvider).SetCustomDiscoveryService(discoveryService)

return ctx
return createClientContext(ctx)
}

func setupTestDiscovery(discErr error, peers []fab.Peer) (fab.DiscoveryService, error) {
Expand Down Expand Up @@ -588,7 +588,7 @@ func setupChannelClientWithError(discErr error, selectionErr error, peers []fab.

fabCtx := setupCustomTestContext(t, selectionService, discoveryService, nil)

ctx := contextImpl.NewChannel(fabCtx, channelID)
ctx := createChannelContext(fabCtx, channelID)

ch, err := New(ctx)
if err != nil {
Expand All @@ -609,7 +609,7 @@ func setupChannelClientWithNodes(peers []fab.Peer,

fabCtx := setupCustomTestContext(t, selectionService, discoveryService, orderers)

ctx := contextImpl.NewChannel(fabCtx, channelID)
ctx := createChannelContext(fabCtx, channelID)

ch, err := New(ctx)
assert.Nil(t, err, "Failed to create new channel client")
Expand Down Expand Up @@ -638,3 +638,18 @@ func createAndSendTestTransactionProposal(sender fab.ProposalSender, chrequest *
tpr, err := sender.SendTransactionProposal(tpreq, targets)
return tpr, tpreq.TxnID, err
}

func createChannelContext(clientContext context.ClientProvider, channelID string) context.ChannelProvider {

channelProvider := func() (context.Channel, error) {
return contextImpl.NewChannel(clientContext, channelID)
}

return channelProvider
}

func createClientContext(client context.Client) context.ClientProvider {
return func() (context.Client, error) {
return client, nil
}
}
19 changes: 12 additions & 7 deletions pkg/client/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,28 @@ func (f *MSPFilter) Accept(peer fab.Peer) bool {
}

// New returns a Client instance.
func New(c context.Client, chName string, opts ...ClientOption) (*Client, error) {
func New(clientProvider context.ClientProvider, channelID string, opts ...ClientOption) (*Client, error) {

l, err := channel.NewLedger(c, chName)
clientContext, err := clientProvider()
if err != nil {
return nil, err
}

discoveryService, err := c.DiscoveryProvider().NewDiscoveryService(chName)
l, err := channel.NewLedger(clientContext, channelID)
if err != nil {
return nil, err
}

discoveryService, err := clientContext.DiscoveryProvider().NewDiscoveryService(channelID)
if err != nil {
return nil, err
}

ledgerClient := Client{
context: c,
context: clientContext,
discovery: discoveryService,
ledger: l,
chName: chName,
chName: channelID,
}

for _, opt := range opts {
Expand All @@ -82,10 +87,10 @@ func New(c context.Client, chName string, opts ...ClientOption) (*Client, error)
// check if target filter was set - if not set the default
if ledgerClient.filter == nil {
// Default target filter is based on user msp
if c.MspID() == "" {
if clientContext.MspID() == "" {
return nil, errors.New("mspID not available in user context")
}
filter := &MSPFilter{mspID: c.MspID()}
filter := &MSPFilter{mspID: clientContext.MspID()}
ledgerClient.filter = filter
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/client/resmgmt/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ func WithDefaultTargetFilter(filter TargetFilter) ClientOption {
}

// New returns a ResourceMgmtClient instance
func New(ctx context.Client, opts ...ClientOption) (*Client, error) {
func New(clientProvider context.ClientProvider, opts ...ClientOption) (*Client, error) {

ctx, err := clientProvider()
if err != nil {
return nil, errors.WithMessage(err, "failed to create resmgmt client")
}

resource := resource.New(ctx)

Expand Down
25 changes: 15 additions & 10 deletions pkg/client/resmgmt/resmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ func TestNoSigningUserFailure(t *testing.T) {
config := getNetworkConfig(t)
fabCtx.SetConfig(config)

ctx := contextImpl.Client{
clientCtx := createClientContext(contextImpl.Client{
Providers: fabCtx,
Identity: fabCtx,
}
_, err := New(ctx)
})

_, err := New(clientCtx)
if err == nil {
t.Fatal("Should have failed due to missing msp")
}
Expand Down Expand Up @@ -1139,10 +1140,7 @@ func setupResMgmtClient(fabCtx context.Client, discErr error, t *testing.T, opts
}
chProvider.SetTransactor(&transactor)

ctx := contextImpl.Client{
Providers: fabCtx,
Identity: fabCtx,
}
ctx := createClientContext(fabCtx)

resClient, err := New(ctx, opts...)
if err != nil {
Expand Down Expand Up @@ -1248,11 +1246,12 @@ func TestSaveChannelFailure(t *testing.T) {
resource := fcmocks.NewMockInvalidResource()
fabCtx := setupTestContext("user", "Org1Msp1")

ctx := contextImpl.Client{
clientCtx := createClientContext(contextImpl.Client{
Providers: fabCtx,
Identity: fabCtx,
}
cc, err := New(ctx)
})

cc, err := New(clientCtx)
if err != nil {
t.Fatalf("Failed to create new channel management client: %s", err)
}
Expand Down Expand Up @@ -1314,3 +1313,9 @@ func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {
t.Fatalf("Failed to save channel with multiple signing identities: %s", err)
}
}

func createClientContext(fabCtx context.Client) context.ClientProvider {
return func() (context.Client, error) {
return fabCtx, nil
}
}
11 changes: 6 additions & 5 deletions pkg/common/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
)

// Session primarily represents the session and identity context
type Session interface {
Identity
}

// Identity supplies the serialized identity and key reference.
type Identity interface {
MspID() string
Expand All @@ -42,3 +37,9 @@ type Channel interface {
SelectionService() fab.SelectionService
ChannelService() fab.ChannelService
}

//ClientProvider returns client context
type ClientProvider func() (Client, error)

//ChannelProvider returns channel client context
type ChannelProvider func() (Channel, error)
1 change: 0 additions & 1 deletion pkg/context/api/fab/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type IdentityContext interface {
// ChannelService supplies services related to a channel.
type ChannelService interface {
Config() (ChannelConfig, error)
Ledger() (ChannelLedger, error)
Transactor() (Transactor, error)
EventHub() (EventHub, error) // TODO support new event delivery
Membership() (ChannelMembership, error)
Expand Down
18 changes: 13 additions & 5 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/context"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
"github.com/pkg/errors"
)

// Client supplies the configuration and signing identity to client objects.
Expand Down Expand Up @@ -174,6 +175,7 @@ func WithChannelProvider(channelProvider fab.ChannelProvider) SDKContextParams {
}

//NewProvider creates new context client provider
// Not be used by end developers, fabsdk package use only
func NewProvider(params ...SDKContextParams) *Provider {
ctxProvider := Provider{}
for _, param := range params {
Expand All @@ -183,27 +185,33 @@ func NewProvider(params ...SDKContextParams) *Provider {
}

//NewChannel creates new channel context client
func NewChannel(client context.Client, channelID string) *Channel {
// Not be used by end developers, fabsdk package use only
func NewChannel(clientProvider context.ClientProvider, channelID string) (*Channel, error) {

client, err := clientProvider()
if err != nil {
return nil, errors.WithMessage(err, "failed to get client context to create channel client")
}

channelService, err := client.ChannelProvider().ChannelService(client, channelID)
if err != nil {
return &Channel{Client: client}
return nil, errors.WithMessage(err, "failed to get channel service to create channel client")
}

discoveryService, err := client.DiscoveryProvider().NewDiscoveryService(channelID)
if err != nil {
return &Channel{Client: client}
return nil, errors.WithMessage(err, "failed to get discovery service to create channel client")
}

selectionService, err := client.SelectionProvider().NewSelectionService(channelID)
if err != nil {
return &Channel{Client: client}
return nil, errors.WithMessage(err, "failed to get selection service to create channel client")
}

return &Channel{
Client: client,
selection: selectionService,
discovery: discoveryService,
channelService: channelService,
}
}, nil
}
5 changes: 0 additions & 5 deletions pkg/fab/mocks/mockchprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,3 @@ func (cs *MockChannelService) Config() (fab.ChannelConfig, error) {
func (cs *MockChannelService) Membership() (fab.ChannelMembership, error) {
return NewMockMembership(), nil
}

// Ledger ...
func (cs *MockChannelService) Ledger() (fab.ChannelLedger, error) {
return nil, nil
}
4 changes: 3 additions & 1 deletion pkg/fab/mocks/mocktransactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func (t *MockTransactor) CreateTransactionHeader() (fab.TransactionHeader, error
// SendTransactionProposal sends a TransactionProposal to the target peers.
func (t *MockTransactor) SendTransactionProposal(proposal *fab.TransactionProposal, targets []fab.ProposalProcessor) ([]*fab.TransactionProposalResponse, error) {
response := make([]*fab.TransactionProposalResponse, 1, 1)
response[0] = &fab.TransactionProposalResponse{Endorser: "example.com", Status: 99, ProposalResponse: nil}
response[0] = &fab.TransactionProposalResponse{Endorser: "example.com", Status: 99,
ProposalResponse: &pb.ProposalResponse{Response: &pb.Response{Payload: []byte("abc")}},
}
return response, nil
}

Expand Down
Loading

0 comments on commit eaa71c4

Please sign in to comment.