Skip to content

Commit

Permalink
[FABG-748] example cc unique keys in int-tests
Browse files Browse the repository at this point in the history
Change-Id: Ic9e1c517cd0437e9f0839cef321c189ea6cd9528
Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Aug 30, 2018
1 parent de7bf5f commit dcb2645
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
6 changes: 5 additions & 1 deletion test/integration/base_test_setup.go
Expand Up @@ -442,7 +442,11 @@ func GetKeyName(t *testing.T) string {
}

//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) {
func ResetKeys(t *testing.T, ctx contextAPI.ChannelProvider, chaincodeID, value string, keys ...string) {
chClient, err := channel.New(ctx)
if err != nil {
t.Fatalf("Failed to create new channel client for reseting keys: %s", err)
}
for _, key := range keys {
// Synchronous transaction
_, err := chClient.Execute(
Expand Down
5 changes: 3 additions & 2 deletions test/integration/pkg/client/channel/channel_client_test.go
Expand Up @@ -43,14 +43,15 @@ func TestChannelClient(t *testing.T) {
//prepare context
org1ChannelClientContext := sdk.ChannelContext(testSetup.ChannelID, fabsdk.WithUser(org1User), fabsdk.WithOrg(org1Name))

//Reset example cc keys
integration.ResetKeys(t, org1ChannelClientContext, chaincodeID, "200", aKey, bKey)

//get channel client
chClient, err := channel.New(org1ChannelClientContext)
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}

integration.ResetKeys(t, chClient, chaincodeID, "200", aKey, bKey)

// Synchronous query
testQuery(t, chClient, "200", chaincodeID, bKey)

Expand Down
31 changes: 19 additions & 12 deletions test/integration/pkg/client/ledger/channel_ledger_test.go
Expand Up @@ -68,6 +68,11 @@ func initializeLedgerTests(t *testing.T) (*fabsdk.FabricSDK, []string) {
func TestLedgerQueries(t *testing.T) {
testSetup := mainTestSetup

aKey := integration.GetKeyName(t)
bKey := integration.GetKeyName(t)
moveTxArg := integration.ExampleCCTxArgs(aKey, bKey, "1")
queryArg := integration.ExampleCCQueryArgs(bKey)

// Setup tests with a random chaincode ID.
sdk, targets := initializeLedgerTests(t)

Expand All @@ -79,9 +84,11 @@ func TestLedgerQueries(t *testing.T) {
require.Nil(t, err, "InstallAndInstantiateExampleCC return error")

//prepare required contexts

channelClientCtx := sdk.ChannelContext(channelID, fabsdk.WithUser("Admin"), fabsdk.WithOrg(orgName))

//Reset example cc keys
integration.ResetKeys(t, channelClientCtx, chaincodeID, "200", aKey, bKey)

// Get a ledger client.
ledgerClient, err := ledger.New(channelClientCtx)
require.Nil(t, err, "ledger new return error")
Expand All @@ -99,12 +106,12 @@ func TestLedgerQueries(t *testing.T) {
t.Fatalf("creating channel failed: %s", err)
}

txID, expectedQueryValue, err := changeBlockState(t, channelClient, chaincodeID)
txID, expectedQueryValue, err := changeBlockState(t, channelClient, queryArg, moveTxArg, chaincodeID)
if err != nil {
t.Fatalf("Failed to change block state (invoke transaction). Return error: %s", err)
}

verifyTargetsChangedBlockState(t, channelClient, chaincodeID, targets, expectedQueryValue)
verifyTargetsChangedBlockState(t, channelClient, chaincodeID, targets, queryArg, expectedQueryValue)

// Test Query Info - retrieve values after transaction
bciAfterTx, err := ledgerClient.QueryInfo(ledger.WithTargetEndpoints(testTargets...))
Expand Down Expand Up @@ -135,12 +142,12 @@ func TestLedgerQueries(t *testing.T) {
testQueryConfigBlock(t, ledgerClient, targets)
}

func changeBlockState(t *testing.T, client *channel.Client, chaincodeID string) (fab.TransactionID, int, error) {
func changeBlockState(t *testing.T, client *channel.Client, queryArg [][]byte, moveArg [][]byte, chaincodeID string) (fab.TransactionID, int, error) {

req := channel.Request{
ChaincodeID: chaincodeID,
Fcn: "invoke",
Args: integration.ExampleCCDefaultQueryArgs(),
Args: queryArg,
}
resp, err := client.Query(req, channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
Expand All @@ -149,7 +156,7 @@ func changeBlockState(t *testing.T, client *channel.Client, chaincodeID string)
value := resp.Payload

// Start transaction that will change block state
txID, err := moveFundsAndGetTxID(t, client, chaincodeID)
txID, err := moveFundsAndGetTxID(t, client, moveArg, chaincodeID)
if err != nil {
return "", 0, errors.WithMessage(err, "move funds failed")
}
Expand All @@ -160,17 +167,17 @@ func changeBlockState(t *testing.T, client *channel.Client, chaincodeID string)
return txID, valueInt, nil
}

func verifyTargetsChangedBlockState(t *testing.T, client *channel.Client, chaincodeID string, targets []string, expectedValue int) {
func verifyTargetsChangedBlockState(t *testing.T, client *channel.Client, chaincodeID string, targets []string, queryArg [][]byte, expectedValue int) {
for _, target := range targets {
verifyTargetChangedBlockState(t, client, chaincodeID, target, expectedValue)
verifyTargetChangedBlockState(t, client, chaincodeID, target, queryArg, expectedValue)
}
}

func verifyTargetChangedBlockState(t *testing.T, client *channel.Client, chaincodeID string, target string, expectedValue int) {
func verifyTargetChangedBlockState(t *testing.T, client *channel.Client, chaincodeID string, target string, queryArg [][]byte, expectedValue int) {
req := channel.Request{
ChaincodeID: chaincodeID,
Fcn: "invoke",
Args: integration.ExampleCCDefaultQueryArgs(),
Args: queryArg,
}

_, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
Expand Down Expand Up @@ -290,15 +297,15 @@ func testInstantiatedChaincodes(t *testing.T, ccID string, channelID string, res
}

// MoveFundsAndGetTxID ...
func moveFundsAndGetTxID(t *testing.T, client *channel.Client, chaincodeID string) (fab.TransactionID, error) {
func moveFundsAndGetTxID(t *testing.T, client *channel.Client, moveArg [][]byte, chaincodeID string) (fab.TransactionID, error) {

transientDataMap := make(map[string][]byte)
transientDataMap["result"] = []byte("Transient data in move funds...")

req := channel.Request{
ChaincodeID: chaincodeID,
Fcn: "invoke",
Args: integration.ExampleCCDefaultTxArgs(),
Args: moveArg,
TransientMap: transientDataMap,
}
resp, err := client.Execute(req, channel.WithRetry(retry.DefaultChannelOpts))
Expand Down
17 changes: 12 additions & 5 deletions test/integration/pkg/fabsdk/provider/sdk_provider_test.go
Expand Up @@ -30,6 +30,10 @@ func TestDynamicSelection(t *testing.T) {

// Using shared SDK instance to increase test speed.
testSetup := mainTestSetup
aKey := integration.GetKeyName(t)
bKey := integration.GetKeyName(t)
moveTxArg := integration.ExampleCCTxArgs(aKey, bKey, "1")
queryArg := integration.ExampleCCQueryArgs(bKey)

// Create SDK setup for channel client with dynamic selection
sdk, err := fabsdk.New(integration.ConfigBackend,
Expand All @@ -51,34 +55,37 @@ func TestDynamicSelection(t *testing.T) {
//prepare contexts
org1ChannelClientContext := sdk.ChannelContext(testSetup.ChannelID, fabsdk.WithUser(org1User), fabsdk.WithOrg(org1Name))

//Reset example cc keys
integration.ResetKeys(t, org1ChannelClientContext, chaincodeID, "200", aKey, bKey)

chClient, err := channel.New(org1ChannelClientContext)
if err != nil {
t.Fatalf("Failed to create new channel client: %s", err)
}

response, err := chClient.Query(channel.Request{ChaincodeID: chaincodeID, Fcn: "invoke", Args: integration.ExampleCCDefaultQueryArgs()},
response, err := chClient.Query(channel.Request{ChaincodeID: chaincodeID, Fcn: "invoke", Args: queryArg},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to query funds: %s", err)
}
value := response.Payload

// Move funds
response, err = chClient.Execute(channel.Request{ChaincodeID: chaincodeID, Fcn: "invoke", Args: integration.ExampleCCDefaultTxArgs()},
response, err = chClient.Execute(channel.Request{ChaincodeID: chaincodeID, Fcn: "invoke", Args: moveTxArg},
channel.WithRetry(retry.DefaultChannelOpts))
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}

valueInt, _ := strconv.Atoi(string(value))
verifyValue(t, chClient, valueInt+1, chaincodeID)
verifyValue(t, chClient, queryArg, valueInt+1, chaincodeID)
}

func verifyValue(t *testing.T, chClient *channel.Client, expectedValue int, ccID string) {
func verifyValue(t *testing.T, chClient *channel.Client, queryArg [][]byte, expectedValue int, ccID string) {
req := channel.Request{
ChaincodeID: ccID,
Fcn: "invoke",
Args: integration.ExampleCCDefaultQueryArgs(),
Args: queryArg,
}

_, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
Expand Down

0 comments on commit dcb2645

Please sign in to comment.