Skip to content

Commit

Permalink
Initial gateway integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: James Taylor <jamest@uk.ibm.com>
  • Loading branch information
jt-nti authored and sykesm committed Mar 3, 2021
1 parent 4a37575 commit 6279191
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 10 deletions.
103 changes: 103 additions & 0 deletions integration/gateway/gateway_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package gateway

import (
"encoding/json"
"testing"

"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/gateway"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/integration"
"github.com/hyperledger/fabric/integration/nwo"
"github.com/hyperledger/fabric/protoutil"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestGateway(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Gateway Service Suite")
}

var (
buildServer *nwo.BuildServer
components *nwo.Components
)

var _ = SynchronizedBeforeSuite(func() []byte {
buildServer = nwo.NewBuildServer()
buildServer.Serve()

components = buildServer.Components()
payload, err := json.Marshal(components)
Expect(err).NotTo(HaveOccurred())

return payload
}, func(payload []byte) {
err := json.Unmarshal(payload, &components)
Expect(err).NotTo(HaveOccurred())

flogging.SetWriter(GinkgoWriter)
})

var _ = SynchronizedAfterSuite(func() {
}, func() {
buildServer.Shutdown()
})

func StartPort() int {
return integration.GatewayBasePort.StartPortForNode()
}

func NewProposedTransaction(signingIdentity *nwo.SigningIdentity, channelName, chaincodeName, transactionName string, args ...[]byte) *gateway.ProposedTransaction {
proposal, txnID := newProposalProto(signingIdentity, channelName, chaincodeName, transactionName, args...)
signedProposal, err := protoutil.GetSignedProposal(proposal, signingIdentity)
Expect(err).NotTo(HaveOccurred())

txn := &gateway.ProposedTransaction{
Proposal: signedProposal,
TxId: txnID,
ChannelId: channelName,
}

return txn
}

func newProposalProto(signingIdentity *nwo.SigningIdentity, channelName, chaincodeName, transactionName string, args ...[]byte) (*peer.Proposal, string) {
creator, err := signingIdentity.Serialize()
Expect(err).NotTo(HaveOccurred())

invocationSpec := &peer.ChaincodeInvocationSpec{
ChaincodeSpec: &peer.ChaincodeSpec{
Type: peer.ChaincodeSpec_NODE,
ChaincodeId: &peer.ChaincodeID{Name: chaincodeName},
Input: &peer.ChaincodeInput{Args: chaincodeArgs(transactionName, args...)},
},
}

result, transactionID, err := protoutil.CreateChaincodeProposal(
common.HeaderType_ENDORSER_TRANSACTION,
channelName,
invocationSpec,
creator,
)
Expect(err).NotTo(HaveOccurred())

return result, transactionID
}

func chaincodeArgs(transactionName string, args ...[]byte) [][]byte {
result := make([][]byte, len(args)+1)

result[0] = []byte(transactionName)
copy(result[1:], args)

return result
}
143 changes: 143 additions & 0 deletions integration/gateway/gateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package gateway

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"syscall"

docker "github.com/fsouza/go-dockerclient"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/gateway"
"github.com/hyperledger/fabric/integration/nwo"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/tedsuo/ifrit"
)

var _ = Describe("GatewayService", func() {
var (
testDir string
network *nwo.Network
org1Peer0 *nwo.Peer
process ifrit.Process
)

BeforeEach(func() {
var err error
testDir, err = ioutil.TempDir("", "gateway")
Expect(err).NotTo(HaveOccurred())

client, err := docker.NewClientFromEnv()
Expect(err).NotTo(HaveOccurred())

config := nwo.BasicEtcdRaft()
network = nwo.New(config, testDir, client, StartPort(), components)

network.GatewayEnabled = true

network.GenerateConfigTree()
network.Bootstrap()

networkRunner := network.NetworkGroupRunner()
process = ifrit.Invoke(networkRunner)
Eventually(process.Ready(), network.EventuallyTimeout).Should(BeClosed())

orderer := network.Orderer("orderer")
network.CreateAndJoinChannel(orderer, "testchannel")
network.UpdateChannelAnchors(orderer, "testchannel")
network.VerifyMembership(
network.PeersWithChannel("testchannel"),
"testchannel",
)
nwo.EnableCapabilities(
network,
"testchannel",
"Application", "V2_0",
orderer,
network.PeersWithChannel("testchannel")...,
)

chaincode := nwo.Chaincode{
Name: "gatewaycc",
Version: "0.0",
Path: components.Build("github.com/hyperledger/fabric/integration/chaincode/simple/cmd"),
Lang: "binary",
PackageFile: filepath.Join(testDir, "gatewaycc.tar.gz"),
Ctor: `{"Args":[]}`,
SignaturePolicy: `AND ('Org1MSP.peer')`,
Sequence: "1",
InitRequired: false,
Label: "gatewaycc_label",
}

nwo.DeployChaincode(network, "testchannel", orderer, chaincode)

org1Peer0 = network.Peer("Org1", "peer0")
})

AfterEach(func() {
if process != nil {
process.Signal(syscall.SIGTERM)
Eventually(process.Wait(), network.EventuallyTimeout).Should(Receive())
}
if network != nil {
network.Cleanup()
}
os.RemoveAll(testDir)
})

Describe("calling Evaluate", func() {
It("should respond with the expected result", func() {
conn := network.PeerClientConn(org1Peer0)
defer conn.Close()
gatewayClient := gateway.NewGatewayClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), network.EventuallyTimeout)
defer cancel()

signingIdentity := network.PeerUserSigner(org1Peer0, "User1")
txn := NewProposedTransaction(signingIdentity, "testchannel", "gatewaycc", "respond", []byte("200"), []byte("conga message"), []byte("conga payload"))

result, err := gatewayClient.Evaluate(ctx, txn)
Expect(err).NotTo(HaveOccurred())
expectedResult := &gateway.Result{
Value: []byte("conga payload"),
}
Expect(result.Value).To(Equal(expectedResult.Value))
Expect(proto.Equal(result, expectedResult)).To(BeTrue(), "Expected\n\t%#v\nto proto.Equal\n\t%#v", result, expectedResult)
})
})

Describe("calling Submit", func() {
It("should respond with the expected result", func() {
conn := network.PeerClientConn(org1Peer0)
defer conn.Close()
gatewayClient := gateway.NewGatewayClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), network.EventuallyTimeout)
defer cancel()

signingIdentity := network.PeerUserSigner(org1Peer0, "User1")
proposedTransaction := NewProposedTransaction(signingIdentity, "testchannel", "gatewaycc", "respond", []byte("200"), []byte("conga message"), []byte("conga payload"))

preparedTransaction, err := gatewayClient.Endorse(ctx, proposedTransaction)
Expect(err).NotTo(HaveOccurred())

result := preparedTransaction.GetResponse()
expectedResult := &gateway.Result{
Value: []byte("conga payload"),
}
Expect(result.Value).To(Equal(expectedResult.Value))
Expect(proto.Equal(result, expectedResult)).To(BeTrue(), "Expected\n\t%#v\nto proto.Equal\n\t%#v", result, expectedResult)

_, err = gatewayClient.Submit(ctx, preparedTransaction)
Expect(err).NotTo(HaveOccurred())
})
})
})
22 changes: 12 additions & 10 deletions integration/nwo/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ type Network struct {
StatsdEndpoint string
ClientAuthRequired bool
TLSEnabled bool
GatewayEnabled bool

PortsByBrokerID map[string]Ports
PortsByOrdererID map[string]Ports
Expand Down Expand Up @@ -191,16 +192,17 @@ func New(c *Config, rootDir string, dockerClient *docker.Client, startPort int,
PortsByOrdererID: map[string]Ports{},
PortsByPeerID: map[string]Ports{},

Organizations: c.Organizations,
Consensus: c.Consensus,
Orderers: c.Orderers,
Peers: c.Peers,
SystemChannel: c.SystemChannel,
Channels: c.Channels,
Profiles: c.Profiles,
Consortiums: c.Consortiums,
Templates: c.Templates,
TLSEnabled: true, // Set TLS enabled as true for default
Organizations: c.Organizations,
Consensus: c.Consensus,
Orderers: c.Orderers,
Peers: c.Peers,
SystemChannel: c.SystemChannel,
Channels: c.Channels,
Profiles: c.Profiles,
Consortiums: c.Consortiums,
Templates: c.Templates,
TLSEnabled: true, // Set TLS enabled as true for default
GatewayEnabled: false, // Set Gateway enabled as false for default

mutex: &sync.Mutex{},
lastExecuted: make(map[string]time.Time),
Expand Down
2 changes: 2 additions & 0 deletions integration/nwo/template/core_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ peer:
concurrency:
endorserService: 100
deliverService: 100
gateway:
enabled: {{ .GatewayEnabled }}
vm:
endpoint: unix:///var/run/docker.sock
Expand Down
1 change: 1 addition & 0 deletions integration/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
DevModePort
DiscoveryBasePort
E2EBasePort
GatewayBasePort
GossipBasePort
IdemixBasePort
KafkaBasePort
Expand Down

0 comments on commit 6279191

Please sign in to comment.