Skip to content

Commit

Permalink
[FABG-748] fix for MVCC errors in channel tests
Browse files Browse the repository at this point in the history
- to avoid occasional MVCC_READ_CONFLICT errors in
channel client tests, a set of unique keys will be used in
channel client tests for example cc 'move' invoke.
- this stategy can be reused in other tests if we find such
conflict issues

Change-Id: I1c7db0f7a350eccb1ab3856f57728b0a300b122a
Signed-off-by: Sudesh Shetty <[email protected]>
  • Loading branch information
sudeshrshetty committed Aug 29, 2018
1 parent 02c1c6f commit b2585b7
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 56 deletions.
53 changes: 46 additions & 7 deletions test/integration/base_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"
"testing"

"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
mspclient "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
Expand Down Expand Up @@ -49,32 +50,48 @@ const (
ExampleCCUpgradeB = "400"
AdminUser = "Admin"
OrdererOrgName = "OrdererOrg"
keyExp = "key-%s-%s"
)

// ExampleCC query and transaction arguments
var queryArgs = [][]byte{[]byte("query"), []byte("b")}
var txArgs = [][]byte{[]byte("move"), []byte("a"), []byte("b"), []byte("1")}
var defaultQueryArgs = [][]byte{[]byte("query"), []byte("b")}
var defaultTxArgs = [][]byte{[]byte("move"), []byte("a"), []byte("b"), []byte("1")}

// ExampleCC init and upgrade args
var initArgs = [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte(ExampleCCInitB)}
var upgradeArgs = [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte(ExampleCCUpgradeB)}
var resetArgs = [][]byte{[]byte("a"), []byte("100"), []byte("b"), []byte(ExampleCCInitB)}

// ExampleCCDefaultQueryArgs returns example cc query args
func ExampleCCDefaultQueryArgs() [][]byte {
return defaultQueryArgs
}

// ExampleCCQueryArgs returns example cc query args
func ExampleCCQueryArgs() [][]byte {
return queryArgs
func ExampleCCQueryArgs(key string) [][]byte {
return [][]byte{[]byte("query"), []byte(key)}
}

// ExampleCCTxArgs returns example cc query args
func ExampleCCTxArgs(from, to, val string) [][]byte {
return [][]byte{[]byte("move"), []byte(from), []byte(to), []byte(val)}
}

// ExampleCCTxArgs returns example cc move funds args
func ExampleCCTxArgs() [][]byte {
return txArgs
// ExampleCCDefaultTxArgs returns example cc move funds args
func ExampleCCDefaultTxArgs() [][]byte {
return defaultTxArgs
}

// ExampleCCTxRandomSetArgs returns example cc set args with random key-value pairs
func ExampleCCTxRandomSetArgs() [][]byte {
return [][]byte{[]byte("set"), []byte(GenerateRandomID()), []byte(GenerateRandomID())}
}

//ExampleCCTxSetArgs sets the given key value in examplecc
func ExampleCCTxSetArgs(key, value string) [][]byte {
return [][]byte{[]byte("set"), []byte(key), []byte(value)}
}

//ExampleCCInitArgs returns example cc initialization args
func ExampleCCInitArgs() [][]byte {
return initArgs
Expand Down Expand Up @@ -418,3 +435,25 @@ func isCCInstalled(resMgmt *resmgmt.Client, ccName, ccVersion string, peers []fa
}
return installedOnAllPeers, nil
}

//GetKeyName creates random key name based on test name
func GetKeyName(t *testing.T) string {
return fmt.Sprintf(keyExp, t.Name(), GenerateRandomID())
}

//ResetKeys resets given set of keys in example cc to given value
func ResetKeys(t *testing.T, chClient *channel.Client, chaincodeID, value string, keys ...string) {
for _, key := range keys {
// Synchronous transaction
_, err := chClient.Execute(
channel.Request{
ChaincodeID: chaincodeID,
Fcn: "invoke",
Args: ExampleCCTxSetArgs(key, value),
},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to reset keys: %s", err)
}
}
}
4 changes: 2 additions & 2 deletions test/integration/e2e/end_to_end.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ func verifyFundsIsMoved(t *testing.T, client *channel.Client, value []byte, ccEv
}

func executeCC(t *testing.T, client *channel.Client) {
_, err := client.Execute(channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCTxArgs()},
_, err := client.Execute(channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}
}

func queryCC(t *testing.T, client *channel.Client, targetEndpoints ...string) []byte {
response, err := client.Query(channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()},
response, err := client.Query(channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCDefaultQueryArgs()},
channel.WithRetry(retry.DefaultChannelOpts),
channel.WithTargetEndpoints(targetEndpoints...),
)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/e2e/no_orderer_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func queryCCUsingTargetFilter(t *testing.T, client *channel.Client) []byte {
discoveryFilter := &mockDiscoveryFilter{called: false}

response, err := client.Query(
channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()},
channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCDefaultQueryArgs()},
channel.WithTargetFilter(discoveryFilter),
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/e2e/orgs/multi_orgs_singleorgconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestMultiOrgWithSingleOrgConfig(t *testing.T, examplecc string) {
req := channel.Request{
ChaincodeID: examplecc,
Fcn: "invoke",
Args: integration.ExampleCCQueryArgs(),
Args: integration.ExampleCCDefaultQueryArgs(),
}
resp, err := chClientOrg1User.Query(req, channel.WithRetry(retry.DefaultChannelOpts))

Expand Down
14 changes: 7 additions & 7 deletions test/integration/e2e/orgs/multiple_orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,13 @@ func createChannel(org1AdminUser msp.SigningIdentity, org2AdminUser msp.SigningI
}

func testCCPolicy(chClientOrg2User *channel.Client, t *testing.T, ccName string) {
_, err := chClientOrg2User.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCTxArgs()}, channel.WithTargets(orgTestPeer1),
_, err := chClientOrg2User.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()}, channel.WithTargets(orgTestPeer1),
channel.WithRetry(retry.DefaultChannelOpts))
if err == nil {
t.Fatal("Should have failed to move funds due to cc policy")
}
// Org2 user moves funds (cc policy ok since we have provided peers for both Orgs)
_, err = chClientOrg2User.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCTxArgs()}, channel.WithRetry(retry.DefaultChannelOpts))
_, err = chClientOrg2User.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()}, channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}
Expand Down Expand Up @@ -378,7 +378,7 @@ func upgradeCC(t *testing.T, mc *multiorgContext, ccPkg *resource.CCPackage, ccN
}

func moveFunds(chClientOrgUser *channel.Client, t *testing.T, ccName string) fab.TransactionID {
response, err := chClientOrgUser.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCTxArgs()}, channel.WithRetry(retry.DefaultChannelOpts))
response, err := chClientOrgUser.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()}, channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}
Expand Down Expand Up @@ -419,7 +419,7 @@ func getBlockchainInfo(ledgerClient *ledger.Client, t *testing.T) *fab.Blockchai
}

func queryCC(chClientOrg1User *channel.Client, t *testing.T, ccName string) []byte {
response, err := chClientOrg1User.Query(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()},
response, err := chClientOrg1User.Query(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCDefaultQueryArgs()},
channel.WithRetry(retry.DefaultChannelOpts))

require.NoError(t, err, "Failed to query funds")
Expand All @@ -432,7 +432,7 @@ func queryCC(chClientOrg1User *channel.Client, t *testing.T, ccName string) []by
}

func verifyErrorFromCC(chClientOrg1User *channel.Client, t *testing.T, ccName string) {
r, err := chClientOrg1User.Query(channel.Request{ChaincodeID: ccName, Fcn: "DUMMY_FUNCTION", Args: integration.ExampleCCQueryArgs()},
r, err := chClientOrg1User.Query(channel.Request{ChaincodeID: ccName, Fcn: "DUMMY_FUNCTION", Args: integration.ExampleCCDefaultQueryArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
t.Logf("verifyErrorFromCC err: %s ***** responses: %v", err, r)

Expand Down Expand Up @@ -590,7 +590,7 @@ func testWithOrg2(t *testing.T, expectedValue int, ccName string) int {
}

// Org2 user moves funds (dynamic selection will inspect chaincode policy to determine endorsers)
_, err = chClientOrg2User.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCTxArgs()},
_, err = chClientOrg2User.Execute(channel.Request{ChaincodeID: ccName, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
Expand All @@ -617,7 +617,7 @@ func verifyValue(t *testing.T, chClient *channel.Client, expectedValue int, ccNa
req := channel.Request{
ChaincodeID: ccName,
Fcn: "invoke",
Args: integration.ExampleCCQueryArgs(),
Args: integration.ExampleCCDefaultQueryArgs(),
}

_, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
Expand Down
2 changes: 1 addition & 1 deletion test/integration/negative/revoked/revoked_peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func queryCC(org1ChannelClientContext contextAPI.ChannelProvider, t *testing.T)
// Error in container is :
// .... identity 0 does not satisfy principal:
// Could not validate identity against certification chain, err The certificate has been revoked
_, err = chClientOrg1User.Query(channel.Request{ChaincodeID: "exampleCC", Fcn: "invoke", Args: integration.ExampleCCQueryArgs()},
_, err = chClientOrg1User.Query(channel.Request{ChaincodeID: "exampleCC", Fcn: "invoke", Args: integration.ExampleCCDefaultQueryArgs()},
channel.WithRetry(retry.DefaultChannelOpts))
if err == nil {
t.Fatal("Expected error: '....Description: could not find chaincode with name 'exampleCC',,, ")
Expand Down
Loading

0 comments on commit b2585b7

Please sign in to comment.