-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: James Taylor <jamest@uk.ibm.com>
- Loading branch information
Showing
5 changed files
with
261 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters