Skip to content

Commit

Permalink
[FAB-8845] Update to usage of WithTargetURLs
Browse files Browse the repository at this point in the history
This change updates integration tests to use WithTargetURLs.
QueryChannels and QueryInstalledChaincodes are updated to use
functional options to specify target.

Change-Id: Ia04318fd6639fe795e17837af556209751bfc91e
Signed-off-by: Troy Ronda <[email protected]>
  • Loading branch information
troyronda committed Mar 13, 2018
1 parent 92a48d0 commit cc12848
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 91 deletions.
2 changes: 1 addition & 1 deletion pkg/client/ledger/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func WithTargetURLs(urls ...string) RequestOption {

peer, err := ctx.InfraProvider().CreatePeerFromConfig(peerCfg)
if err != nil {
return errors.WithMessage(err, "creating orderer from config failed")
return errors.WithMessage(err, "creating peer from config failed")
}

targets = append(targets, peer)
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/resmgmt/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func WithTargetURLs(urls ...string) RequestOption {

peer, err := ctx.InfraProvider().CreatePeerFromConfig(peerCfg)
if err != nil {
return errors.WithMessage(err, "creating orderer from config failed")
return errors.WithMessage(err, "creating peer from config failed")
}

targets = append(targets, peer)
Expand Down
26 changes: 22 additions & 4 deletions pkg/client/resmgmt/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,17 @@ func (rc *Client) UpgradeCC(channelID string, req UpgradeCCRequest, options ...R

// QueryInstalledChaincodes queries the installed chaincodes on a peer.
// Returns the details of all chaincodes installed on a peer.
func (rc *Client) QueryInstalledChaincodes(proposalProcessor fab.ProposalProcessor) (*pb.ChaincodeQueryResponse, error) {
return resource.QueryInstalledChaincodes(rc.ctx, proposalProcessor)
func (rc *Client) QueryInstalledChaincodes(options ...RequestOption) (*pb.ChaincodeQueryResponse, error) {
opts, err := rc.prepareRequestOpts(options...)
if err != nil {
return nil, err
}

if len(opts.Targets) != 1 {
return nil, errors.New("only one target is supported")
}

return resource.QueryInstalledChaincodes(rc.ctx, opts.Targets[0])
}

// QueryInstantiatedChaincodes queries the instantiated chaincodes on a peer for specific channel.
Expand Down Expand Up @@ -429,8 +438,17 @@ func (rc *Client) QueryInstantiatedChaincodes(channelID string, options ...Reque

// QueryChannels queries the names of all the channels that a peer has joined.
// Returns the details of all channels that peer has joined.
func (rc *Client) QueryChannels(proposalProcessor fab.ProposalProcessor) (*pb.ChannelQueryResponse, error) {
return resource.QueryChannels(rc.ctx, proposalProcessor)
func (rc *Client) QueryChannels(options ...RequestOption) (*pb.ChannelQueryResponse, error) {
opts, err := rc.prepareRequestOpts(options...)
if err != nil {
return nil, err
}

if len(opts.Targets) != 1 {
return nil, errors.New("only one target is supported")
}

return resource.QueryChannels(rc.ctx, opts.Targets[0])
}

// sendCCProposal sends proposal for type Instantiate, Upgrade
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/resmgmt/resmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,15 @@ func TestQueryInstalledChaincodes(t *testing.T) {
rc := setupDefaultResMgmtClient(t)

// Test error
_, err := rc.QueryInstalledChaincodes(nil)
_, err := rc.QueryInstalledChaincodes()
if err == nil {
t.Fatalf("QueryInstalledChaincodes: peer cannot be nil")
}

peer := &fcmocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com", MockRoles: []string{}, MockCert: nil, MockMSP: "Org1MSP", Status: http.StatusOK}

// Test success (valid peer)
_, err = rc.QueryInstalledChaincodes(peer)
_, err = rc.QueryInstalledChaincodes(WithTargets(peer))
if err != nil {
t.Fatal(err)
}
Expand All @@ -434,7 +434,7 @@ func TestQueryChannels(t *testing.T) {
}

// Test error
_, err = rc.QueryChannels(nil)
_, err = rc.QueryChannels()
if err == nil {
t.Fatalf("QueryChannels: peer cannot be nil")
}
Expand All @@ -443,7 +443,7 @@ func TestQueryChannels(t *testing.T) {

// Test success (valid peer)
found := false
response, err = rc.QueryChannels(peer)
response, err = rc.QueryChannels(WithTargets(peer))
if err != nil {
t.Fatalf("failed to query channel for peer: %s", err)
}
Expand Down
14 changes: 4 additions & 10 deletions test/integration/base_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import (

"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
packager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/peer"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"
"github.com/pkg/errors"
Expand All @@ -25,7 +23,7 @@ import (
// BaseSetupImpl implementation of BaseTestSetup
type BaseSetupImpl struct {
Identity msp.Identity
Targets []fab.ProposalProcessor
Targets []string
ConfigFile string
OrgID string
ChannelID string
Expand Down Expand Up @@ -91,19 +89,15 @@ func (setup *BaseSetupImpl) Initialize(sdk *fabsdk.FabricSDK) error {
return nil
}

func getOrgTargets(config core.Config, org string) ([]fab.ProposalProcessor, error) {
targets := []fab.ProposalProcessor{}
func getOrgTargets(config core.Config, org string) ([]string, error) {
var targets []string

peerConfig, err := config.PeersConfig(org)
if err != nil {
return nil, errors.WithMessage(err, "reading peer config failed")
}
for _, p := range peerConfig {
target, err := peer.New(config, peer.FromPeerConfig(&core.NetworkPeer{PeerConfig: p}))
if err != nil {
return nil, errors.WithMessage(err, "NewPeer failed")
}
targets = append(targets, target)
targets = append(targets, p.URL)
}
return targets, nil
}
Expand Down
36 changes: 18 additions & 18 deletions test/integration/fab/channel_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
orgName = org1Name
)

func initializeLedgerTests(t *testing.T) (*fabsdk.FabricSDK, []fab.Peer) {
func initializeLedgerTests(t *testing.T) (*fabsdk.FabricSDK, []string) {
// Using shared SDK instance to increase test speed.
sdk := mainSDK

Expand All @@ -45,14 +45,14 @@ func initializeLedgerTests(t *testing.T) (*fabsdk.FabricSDK, []fab.Peer) {
t.Fatalf("failed to load signing identity: %s", err)
}

targets, err := integration.CreateProposalProcessors(sdk.Config(), []string{orgName})
targets, err := integration.OrgTargetPeers(sdk.Config(), []string{orgName})
if err != nil {
t.Fatalf("creating peers failed: %v", err)
}

channelConfig := path.Join("../../../", metadata.ChannelConfigPath, channelConfigFile)
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: channelConfig, SigningIdentities: []msp.Identity{adminIdentity}}
err = integration.InitializeChannel(sdk, orgName, req, integration.ProposalProcessors(targets))
err = integration.InitializeChannel(sdk, orgName, req, targets)
if err != nil {
t.Fatalf("failed to ensure channel has been initialized: %s", err)
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestLedgerQueries(t *testing.T) {

// Test Query Info - retrieve values before transaction
testTargets := targets[0:1]
bciBeforeTx, err := ledgerClient.QueryInfo(ledger.WithTargets(testTargets...))
bciBeforeTx, err := ledgerClient.QueryInfo(ledger.WithTargetURLs(testTargets...))
if err != nil {
t.Fatalf("QueryInfo return error: %v", err)
}
Expand All @@ -98,7 +98,7 @@ func TestLedgerQueries(t *testing.T) {
}

// Test Query Info - retrieve values after transaction
bciAfterTx, err := ledgerClient.QueryInfo(ledger.WithTargets(testTargets...))
bciAfterTx, err := ledgerClient.QueryInfo(ledger.WithTargetURLs(testTargets...))
if err != nil {
t.Fatalf("QueryInfo return error: %v", err)
}
Expand Down Expand Up @@ -158,10 +158,10 @@ func changeBlockState(t *testing.T, client *channel.Client, chaincodeID string)
return txID, nil
}

func testQueryTransaction(t *testing.T, ledgerClient *ledger.Client, txID fab.TransactionID, targets []fab.Peer) {
func testQueryTransaction(t *testing.T, ledgerClient *ledger.Client, txID fab.TransactionID, targets []string) {

// Test Query Transaction -- verify that valid transaction has been processed
processedTransaction, err := ledgerClient.QueryTransaction(txID, ledger.WithTargets(targets...))
processedTransaction, err := ledgerClient.QueryTransaction(txID, ledger.WithTargetURLs(targets...))
if err != nil {
t.Fatalf("QueryTransaction return error: %v", err)
}
Expand All @@ -171,22 +171,22 @@ func testQueryTransaction(t *testing.T, ledgerClient *ledger.Client, txID fab.Tr
}

// Test Query Transaction -- Retrieve non existing transaction
_, err = ledgerClient.QueryTransaction("123ABC", ledger.WithTargets(targets...))
_, err = ledgerClient.QueryTransaction("123ABC", ledger.WithTargetURLs(targets...))
if err == nil {
t.Fatalf("QueryTransaction non-existing didn't return an error")
}
}

func testQueryBlock(t *testing.T, ledgerClient *ledger.Client, targets []fab.Peer) {
func testQueryBlock(t *testing.T, ledgerClient *ledger.Client, targets []string) {

// Retrieve current blockchain info
bci, err := ledgerClient.QueryInfo(ledger.WithTargets(targets...))
bci, err := ledgerClient.QueryInfo(ledger.WithTargetURLs(targets...))
if err != nil {
t.Fatalf("QueryInfo return error: %v", err)
}

// Test Query Block by Hash - retrieve current block by hash
block, err := ledgerClient.QueryBlockByHash(bci.BCI.CurrentBlockHash, ledger.WithTargets(targets...))
block, err := ledgerClient.QueryBlockByHash(bci.BCI.CurrentBlockHash, ledger.WithTargetURLs(targets...))
if err != nil {
t.Fatalf("QueryBlockByHash return error: %v", err)
}
Expand All @@ -196,13 +196,13 @@ func testQueryBlock(t *testing.T, ledgerClient *ledger.Client, targets []fab.Pee
}

// Test Query Block by Hash - retrieve block by non-existent hash
_, err = ledgerClient.QueryBlockByHash([]byte("non-existent"), ledger.WithTargets(targets...))
_, err = ledgerClient.QueryBlockByHash([]byte("non-existent"), ledger.WithTargetURLs(targets...))
if err == nil {
t.Fatalf("QueryBlockByHash non-existent didn't return an error")
}

// Test Query Block - retrieve block by number
block, err = ledgerClient.QueryBlock(1, ledger.WithTargets(targets...))
block, err = ledgerClient.QueryBlock(1, ledger.WithTargetURLs(targets...))
if err != nil {
t.Fatalf("QueryBlock return error: %v", err)
}
Expand All @@ -211,18 +211,18 @@ func testQueryBlock(t *testing.T, ledgerClient *ledger.Client, targets []fab.Pee
}

// Test Query Block - retrieve block by non-existent number
_, err = ledgerClient.QueryBlock(2147483647, ledger.WithTargets(targets...))
_, err = ledgerClient.QueryBlock(2147483647, ledger.WithTargetURLs(targets...))
if err == nil {
t.Fatalf("QueryBlock non-existent didn't return an error")
}
}

func testInstantiatedChaincodes(t *testing.T, ccID string, channelID string, resmgmtClient *resmgmt.Client, targets []fab.Peer) {
func testInstantiatedChaincodes(t *testing.T, ccID string, channelID string, resmgmtClient *resmgmt.Client, targets []string) {

found := false

// Test Query Instantiated chaincodes
chaincodeQueryResponse, err := resmgmtClient.QueryInstantiatedChaincodes(channelID, resmgmt.WithTargets(targets...))
chaincodeQueryResponse, err := resmgmtClient.QueryInstantiatedChaincodes(channelID, resmgmt.WithTargetURLs(targets...))
if err != nil {
t.Fatalf("QueryInstantiatedChaincodes return error: %v", err)
}
Expand Down Expand Up @@ -259,10 +259,10 @@ func moveFundsAndGetTxID(t *testing.T, client *channel.Client, chaincodeID strin
return resp.TransactionID, nil
}

func testQueryConfigBlock(t *testing.T, ledgerClient *ledger.Client, targets []fab.Peer) {
func testQueryConfigBlock(t *testing.T, ledgerClient *ledger.Client, targets []string) {

// Retrieve current channel configuration
cfgEnvelope, err := ledgerClient.QueryConfig(ledger.WithTargets(targets...))
cfgEnvelope, err := ledgerClient.QueryConfig(ledger.WithTargetURLs(targets...))
if err != nil {
t.Fatalf("QueryConfig return error: %v", err)
}
Expand Down
6 changes: 5 additions & 1 deletion test/integration/fab/eventclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/factory/defcore"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/provider/fabpvdr"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

"github.com/hyperledger/fabric-sdk-go/test/integration"
)
Expand Down Expand Up @@ -82,12 +83,15 @@ func testEventService(t *testing.T, testSetup *integration.BaseSetupImpl, sdk *f
t.Fatalf("Failed to get channel transactor: %s", err)
}

peers, err := getProposalProcessors(sdk, "Admin", testSetup.OrgID, testSetup.Targets)
assert.Nil(t, err, "creating peers failed")

tpResponses, prop, err := createAndSendTransactionProposal(
transactor,
chainCodeID,
"invoke",
[][]byte{[]byte("move"), []byte("a"), []byte("b"), []byte("10")},
testSetup.Targets,
peers,
nil,
)
if err != nil {
Expand Down
17 changes: 12 additions & 5 deletions test/integration/fab/install_chaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric-sdk-go/test/integration"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

const (
Expand Down Expand Up @@ -71,11 +72,14 @@ func testChaincodeInstallUsingChaincodePath(t *testing.T, sdk *fabsdk.FabricSDK,
t.Fatalf("Failed to get resource: %s", err)
}

if err := installCC(client, chainCodeName, chainCodePath, chainCodeVersion, ccPkg, testSetup.Targets); err != nil {
peers, err := getProposalProcessors(sdk, "Admin", testSetup.OrgID, testSetup.Targets)
assert.Nil(t, err, "creating peers failed")

if err := installCC(client, chainCodeName, chainCodePath, chainCodeVersion, ccPkg, peers); err != nil {
t.Fatalf("installCC return error: %v", err)
}

chaincodeQueryResponse, err := resource.QueryInstalledChaincodes(client, testSetup.Targets[0])
chaincodeQueryResponse, err := resource.QueryInstalledChaincodes(client, peers[0])
if err != nil {
t.Fatalf("QueryInstalledChaincodes return error: %v", err)
}
Expand All @@ -91,7 +95,7 @@ func testChaincodeInstallUsingChaincodePath(t *testing.T, sdk *fabsdk.FabricSDK,
t.Fatalf("Failed to retrieve installed chaincode.")
}
//Install same chaincode again, should fail
err = installCC(client, chainCodeName, chainCodePath, chainCodeVersion, ccPkg, testSetup.Targets)
err = installCC(client, chainCodeName, chainCodePath, chainCodeVersion, ccPkg, peers)
if err == nil {
t.Fatalf("install same chaincode didn't return error")
}
Expand All @@ -116,13 +120,16 @@ func testChaincodeInstallUsingChaincodePackage(t *testing.T, sdk *fabsdk.FabricS
t.Fatalf("Failed to get resource: %s", err)
}

err = installCC(client, "install", "github.com/example_cc_pkg", chainCodeVersion, ccPkg, testSetup.Targets)
peers, err := getProposalProcessors(sdk, "Admin", testSetup.OrgID, testSetup.Targets)
assert.Nil(t, err, "creating peers failed")

err = installCC(client, "install", "github.com/example_cc_pkg", chainCodeVersion, ccPkg, peers)
if err != nil {
t.Fatalf("installCC return error: %v", err)
}

//Install same chaincode again, should fail
err = installCC(client, "install", chainCodePath, chainCodeVersion, ccPkg, testSetup.Targets)
err = installCC(client, "install", chainCodePath, chainCodeVersion, ccPkg, peers)
if err == nil {
t.Fatalf("install same chaincode didn't return error")
}
Expand Down
8 changes: 6 additions & 2 deletions test/integration/fab/resource_queries_test.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/context"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
"github.com/stretchr/testify/assert"
)

func TestChannelQueries(t *testing.T) {
Expand All @@ -28,9 +29,12 @@ func TestChannelQueries(t *testing.T) {
t.Fatalf("Failed to get resource: %s", err)
}

testQueryChannels(t, client, testSetup.Targets[0])
peers, err := getProposalProcessors(sdk, "Admin", testSetup.OrgID, testSetup.Targets[:1])
assert.Nil(t, err, "creating peers failed")

testInstalledChaincodes(t, chaincodeID, client, testSetup.Targets[0])
testQueryChannels(t, client, peers[0])

testInstalledChaincodes(t, chaincodeID, client, peers[0])

}

Expand Down
Loading

0 comments on commit cc12848

Please sign in to comment.