Skip to content

Commit

Permalink
[FAB-7485] Re-organize integration tests
Browse files Browse the repository at this point in the history
SDK integration tests are moved to the following folders:
1) e2e  - sdk end to end test
2) fab  - basic fabric client tests
3) orgs - multi org test
4) sdk  - all other sdk tests (e.g. channel client, provider tests etc.)

Change-Id: I0c5b86d0d21cd27c408dafe959823176318e5311
Signed-off-by: Sandra Vrtikapa <sandra.vrtikapa@securekey.com>
  • Loading branch information
sandrask committed Dec 22, 2017
1 parent 3d66361 commit e902e9b
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 209 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -22,8 +22,9 @@ You're good to go, happy coding! Check out the examples for usage demonstrations

### Examples

- [E2E Test](test/integration/end_to_end_test.go) and [Base Test](test/integration/base_test_setup.go): Part of the E2E tests included with the Go SDK.
- [Dynamic Endorser Selection](test/integration/sdk_provider_test.go): An example that uses dynamic endorser selection (based on chaincode policy)
- [E2E Test](test/integration/e2e/end_to_end_test.go): Basic example that uses SDK to query and execute transaction
- [Multi Org Test](test/integration/orgs/multiple_orgs_test.go): An example that has multiple organisations involved in transaction
- [Dynamic Endorser Selection](test/integration/sdk/sdk_provider_test.go): An example that uses dynamic endorser selection (based on chaincode policy)
- [CLI](https://github.com/securekey/fabric-examples/tree/master/fabric-cli/): An example CLI for Fabric built with the Go SDK.
- More examples needed!

Expand Down
27 changes: 1 addition & 26 deletions test/integration/base_test_setup.go
Expand Up @@ -212,7 +212,7 @@ func (setup *BaseSetupImpl) InstallCC(name string, path string, version string,
// GetDeployPath ..
func (setup *BaseSetupImpl) GetDeployPath() string {
pwd, _ := os.Getwd()
return path.Join(pwd, "../fixtures/testdata")
return path.Join(pwd, "../../fixtures/testdata")
}

// InstallAndInstantiateExampleCC install and instantiate using resource management client
Expand Down Expand Up @@ -242,31 +242,6 @@ func (setup *BaseSetupImpl) InstallAndInstantiateCC(ccName, ccPath, ccVersion, g
return resMgmtClient.InstantiateCC("mychannel", resmgmt.InstantiateCCRequest{Name: ccName, Path: ccPath, Version: ccVersion, Args: ccArgs, Policy: ccPolicy})
}

// UpgradeExampleCC upgrade example CC
func (setup *BaseSetupImpl) UpgradeExampleCC() error {

chainCodePath := "github.com/example_cc"
chainCodeVersion := "v1"

if setup.ChainCodeID == "" {
setup.ChainCodeID = GenerateRandomID()
}

ccPkg, err := packager.NewCCPackage(chainCodePath, setup.GetDeployPath())
if err != nil {
return err
}

_, err = resMgmtClient.InstallCC(resmgmt.InstallCCRequest{Name: setup.ChainCodeID, Path: chainCodePath, Version: chainCodeVersion, Package: ccPkg})
if err != nil {
return err
}

ccPolicy := cauthdsl.SignedByMspMember(setup.Client.UserContext().MspID())
return resMgmtClient.UpgradeCC("mychannel", resmgmt.UpgradeCCRequest{Name: setup.ChainCodeID, Path: chainCodePath, Version: chainCodeVersion, Args: upgradeArgs, Policy: ccPolicy})

}

// GetChannel initializes and returns a channel based on config
func (setup *BaseSetupImpl) GetChannel(client fab.FabricClient, channelID string, orgs []string) (fab.Channel, error) {

Expand Down
154 changes: 154 additions & 0 deletions test/integration/e2e/end_to_end_test.go
@@ -0,0 +1,154 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package e2e

import (
"path"
"strconv"
"testing"
"time"

"github.com/hyperledger/fabric-sdk-go/api/apitxn"
"github.com/hyperledger/fabric-sdk-go/def/fabapi"
"github.com/hyperledger/fabric-sdk-go/test/integration"
"github.com/hyperledger/fabric-sdk-go/test/metadata"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"

chmgmt "github.com/hyperledger/fabric-sdk-go/api/apitxn/chmgmtclient"
resmgmt "github.com/hyperledger/fabric-sdk-go/api/apitxn/resmgmtclient"

packager "github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/ccpackager/gopackager"
)

const (
channelID = "mychannel"
orgName = "Org1"
orgAdmin = "Admin"
ccID = "e2eExampleCC"
)

func TestE2E(t *testing.T) {

// Create SDK setup for the integration tests
sdkOptions := fabapi.Options{
ConfigFile: "../" + integration.ConfigTestFile,
}

sdk, err := fabapi.NewSDK(sdkOptions)
if err != nil {
t.Fatalf("Failed to create new SDK: %s", err)
}

// 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", &fabapi.ChannelMgmtClientOpts{OrgName: "ordererorg"})
if err != nil {
t.Fatalf("Failed to create channel management client: %s", err)
}

// Org admin user is signing user for creating channel
orgAdminUser, err := sdk.NewPreEnrolledUser(orgName, orgAdmin)
if err != nil {
t.Fatalf("NewPreEnrolledUser failed for %s, %s: %s", orgName, orgAdmin, err)
}

// Create channel
req := chmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"), SigningUser: orgAdminUser}
if err = chMgmtClient.SaveChannel(req); err != nil {
t.Fatal(err)
}

// Allow orderer to process channel creation
time.Sleep(time.Second * 3)

// Org resource management client (Org1 is default org)
orgResMgmt, err := sdk.NewResourceMgmtClient(orgAdmin)
if err != nil {
t.Fatalf("Failed to create new resource management client: %s", err)
}

// Org peers join channel
if err = orgResMgmt.JoinChannel(channelID); err != nil {
t.Fatalf("Org peers failed to JoinChannel: %s", err)
}

// Create chaincode package for example cc
ccPkg, err := packager.NewCCPackage("github.com/example_cc", "../../fixtures/testdata")
if err != nil {
t.Fatal(err)
}

// Install example cc to org peers
installCCReq := resmgmt.InstallCCRequest{Name: ccID, Path: "github.com/example_cc", Version: "0", Package: ccPkg}
_, err = orgResMgmt.InstallCC(installCCReq)
if err != nil {
t.Fatal(err)
}

// Set up chaincode policy
ccPolicy := cauthdsl.SignedByAnyMember([]string{"Org1MSP"})

// Org resource manager will instantiate 'example_cc' on channel
err = orgResMgmt.InstantiateCC(channelID, resmgmt.InstantiateCCRequest{Name: ccID, Path: "github.com/example_cc", Version: "0", Args: integration.ExampleCCInitArgs(), Policy: ccPolicy})
if err != nil {
t.Fatal(err)
}

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

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

value, err := chClient.Query(apitxn.QueryRequest{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()})
if err != nil {
t.Fatalf("Failed to query funds: %s", err)
}

eventID := "test([a-zA-Z]+)"

// Register chaincode event (pass in channel which receives event details when the event is complete)
notifier := make(chan *apitxn.CCEvent)
rce := chClient.RegisterChaincodeEvent(notifier, ccID, eventID)

// Move funds
_, err = chClient.ExecuteTx(apitxn.ExecuteTxRequest{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCTxArgs()})
if err != nil {
t.Fatalf("Failed to move funds: %s", err)
}

select {
case ccEvent := <-notifier:
t.Logf("Received CC event: %s\n", ccEvent)
case <-time.After(time.Second * 20):
t.Fatalf("Did NOT receive CC event for eventId(%s)\n", eventID)
}

// Unregister chain code event using registration handle
err = chClient.UnregisterChaincodeEvent(rce)
if err != nil {
t.Fatalf("Unregister cc event failed: %s", err)
}

// Verify move funds transaction result
valueAfterInvoke, err := chClient.Query(apitxn.QueryRequest{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCQueryArgs()})
if err != nil {
t.Fatalf("Failed to query funds after transaction: %s", err)
}

valueInt, _ := strconv.Atoi(string(value))
valueAfterInvokeInt, _ := strconv.Atoi(string(valueAfterInvoke))
if valueInt+1 != valueAfterInvokeInt {
t.Fatalf("ExecuteTx failed. Before: %s, after: %s", value, valueAfterInvoke)
}

// Release all channel client resources
chClient.Close()

}
111 changes: 0 additions & 111 deletions test/integration/end_to_end_test.go

This file was deleted.

0 comments on commit e902e9b

Please sign in to comment.