Skip to content

Commit

Permalink
[FAB-4637] Add multi-org integration test
Browse files Browse the repository at this point in the history
Change-Id: I03fd1fd0dc95c7ebb1222e812ff2cf59fe978615
Signed-off-by: Divyank Katira <Divyank.Katira@securekey.com>
  • Loading branch information
d1vyank committed Jun 29, 2017
1 parent 1bb8099 commit f0f2ef5
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/config.go
Expand Up @@ -21,6 +21,7 @@ type Config interface {
CAClientCertFile(org string) (string, error)
MspID(org string) (string, error)
FabricClientViper() *viper.Viper
OrderersConfig() ([]OrdererConfig, error)
RandomOrdererConfig() (*OrdererConfig, error)
OrdererConfig(name string) (*OrdererConfig, error)
PeersConfig(org string) ([]PeerConfig, error)
Expand Down
13 changes: 13 additions & 0 deletions api/mocks/mockconfig.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions fabric-txn/transaction.go
Expand Up @@ -76,8 +76,8 @@ func InvokeChaincode(client api.FabricClient, channel api.Channel, targets []api

select {
case <-done:
case <-fail:
return fmt.Errorf("invoke Error received from eventhub for txid(%s), error(%v)", txID, fail)
case err := <-fail:
return fmt.Errorf("invoke Error received from eventhub for txid(%s), error(%v)", txID, err)
case <-time.After(time.Second * 30):
return fmt.Errorf("invoke Didn't receive block event for txid(%s)", txID)
}
Expand Down
23 changes: 22 additions & 1 deletion pkg/config/config.go
Expand Up @@ -138,8 +138,12 @@ func (c *config) MspID(org string) (string, error) {
if err != nil {
return "", err
}
mspID := config.Organizations[org].MspID
if mspID == "" {
return "", fmt.Errorf("MSP ID is empty for org: %s", org)
}

return config.Organizations[org].MspID, nil
return mspID, nil
}

// FabricClientViper returns the internal viper instance used by the
Expand All @@ -158,6 +162,23 @@ func (c *config) cacheNetworkConfiguration() error {
return err
}

// GetOrderersConfig returns a list of defined orderers
func (c *config) OrderersConfig() ([]api.OrdererConfig, error) {
orderers := []api.OrdererConfig{}
config, err := c.NetworkConfig()
if err != nil {
return nil, err
}

for _, orderer := range config.Orderers {
orderer.TLS.Certificate = strings.Replace(orderer.TLS.Certificate, "$GOPATH",
os.Getenv("GOPATH"), -1)
orderers = append(orderers, orderer)
}

return orderers, nil
}

// RandomOrdererConfig returns a pseudo-random orderer from the network config
func (c *config) RandomOrdererConfig() (*api.OrdererConfig, error) {
config, err := c.NetworkConfig()
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/config_test.go
Expand Up @@ -9,6 +9,7 @@ package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -289,6 +290,17 @@ func TestNetworkConfig(t *testing.T) {
}
}

func TestOrdererConfig(t *testing.T) {
orderers, err := configImpl.OrderersConfig()
if err != nil {
t.Fatal(err)
}

if !filepath.IsAbs(orderers[0].TLS.Certificate) {
t.Fatal("Expected GOPATH relative path to be replaced")
}
}

func TestMain(m *testing.M) {
var err error
configImpl, err = InitConfig("../../test/fixtures/config/config_test.yaml")
Expand Down
5 changes: 5 additions & 0 deletions pkg/fabric-ca-client/mocks/mockconfig.go
Expand Up @@ -91,6 +91,11 @@ func (c *MockConfig) SecurityLevel() int {

}

// OrderersConfig returns a list of defined orderers
func (c *MockConfig) OrderersConfig() ([]api.OrdererConfig, error) {
return nil, nil
}

// RandomOrdererConfig not implemented
func (c *MockConfig) RandomOrdererConfig() (*api.OrdererConfig, error) {
return nil, nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/fabric-client/mocks/mockconfig.go
Expand Up @@ -91,6 +91,11 @@ func (c *MockConfig) SecurityLevel() int {

}

// OrderersConfig returns a list of defined orderers
func (c *MockConfig) OrderersConfig() ([]api.OrdererConfig, error) {
return nil, nil
}

// RandomOrdererConfig not implemented
func (c *MockConfig) RandomOrdererConfig() (*api.OrdererConfig, error) {
return nil, nil
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/docker-compose.yaml
Expand Up @@ -104,6 +104,7 @@ services:
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
- CORE_PEER_ID=peer0.org2.example.com
- CORE_LOGGING_PEER=debug
- CORE_PEER_LOCALMSPID=Org2MSP
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
- CORE_PEER_ADDRESS=peer0.org2.example.com:7051
Expand Down
82 changes: 82 additions & 0 deletions test/integration/orgs/multiple_orgs_test.go
@@ -0,0 +1,82 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package orgs

import (
"fmt"
"strconv"
"testing"

"github.com/hyperledger/fabric-sdk-go/api"
fabrictxn "github.com/hyperledger/fabric-sdk-go/fabric-txn"
)

// TestOrgsEndToEnd creates a channel with two organisations, installs chaincode
// on each of them, and finally invokes a transaction on an org2 peer and queries
// the result from an org1 peer
func TestOrgsEndToEnd(t *testing.T) {
// Bootstrap network
initializeFabricClient(t)
loadOrgUsers(t)
loadOrgPeers(t)
loadOrderer(t)
createTestChannel(t)
joinTestChannel(t)
installAndInstantiate(t)

fmt.Printf("peer0 is %+v, peer1 is %+v\n", orgTestPeer0, orgTestPeer1)

// Query initial value on org1 peer
orgTestClient.SetUserContext(org1User)
orgTestChannel.SetPrimaryPeer(orgTestPeer0)
result, err := fabrictxn.QueryChaincode(orgTestClient, orgTestChannel,
"exampleCC", generateQueryArgs())
failTestIfError(err, t)
initialValue, err := strconv.Atoi(result)
failTestIfError(err, t)

// Change value on org2 peer
orgTestClient.SetUserContext(org2User)
orgTestChannel.SetPrimaryPeer(orgTestPeer1)
err = fabrictxn.InvokeChaincode(orgTestClient, orgTestChannel, []api.Peer{orgTestPeer1},
peer0EventHub, "exampleCC", generateInvokeArgs(), nil)
failTestIfError(err, t)

// Assert changed value on org1 peer
orgTestClient.SetUserContext(org1User)
orgTestChannel.SetPrimaryPeer(orgTestPeer0)
result, err = fabrictxn.QueryChaincode(orgTestClient, orgTestChannel,
"exampleCC", generateQueryArgs())
failTestIfError(err, t)
finalValue, err := strconv.Atoi(result)
failTestIfError(err, t)

if initialValue+1 != finalValue {
t.Fatalf("Org1 invoke result was not propagated to org2. Expected %d, got: %d",
(initialValue + 1), finalValue)
}
}

func generateQueryArgs() []string {
var args []string
args = append(args, "invoke")
args = append(args, "query")
args = append(args, "b")

return args
}

func generateInvokeArgs() []string {
var args []string
args = append(args, "invoke")
args = append(args, "move")
args = append(args, "a")
args = append(args, "b")
args = append(args, "1")

return args
}

0 comments on commit f0f2ef5

Please sign in to comment.