github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/integration/idemix/idemix_test.go (about) 1 /* 2 Copyright hechain All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package idemix 8 9 import ( 10 "io/ioutil" 11 "os" 12 "path/filepath" 13 "syscall" 14 15 docker "github.com/fsouza/go-dockerclient" 16 "github.com/hechain20/hechain/integration/nwo" 17 "github.com/hechain20/hechain/integration/nwo/commands" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 "github.com/onsi/gomega/gbytes" 21 "github.com/onsi/gomega/gexec" 22 "github.com/tedsuo/ifrit" 23 ) 24 25 var _ = Describe("EndToEnd", func() { 26 var ( 27 testDir string 28 client *docker.Client 29 network *nwo.Network 30 chaincode nwo.Chaincode 31 process ifrit.Process 32 ) 33 34 BeforeEach(func() { 35 var err error 36 testDir, err = ioutil.TempDir("", "idemix_e2e") 37 Expect(err).NotTo(HaveOccurred()) 38 39 client, err = docker.NewClientFromEnv() 40 Expect(err).NotTo(HaveOccurred()) 41 42 chaincode = nwo.Chaincode{ 43 Name: "mycc", 44 Version: "0.0", 45 Path: components.Build("github.com/hechain20/hechain/integration/chaincode/simple/cmd"), 46 Lang: "binary", 47 PackageFile: filepath.Join(testDir, "simplecc.tar.gz"), 48 Ctor: `{"Args":["init","a","100","b","200"]}`, 49 SignaturePolicy: `AND ('Org1MSP.member','Org2MSP.member')`, 50 Sequence: "1", 51 InitRequired: true, 52 Label: "my_prebuilt_chaincode", 53 } 54 }) 55 56 AfterEach(func() { 57 if process != nil { 58 process.Signal(syscall.SIGTERM) 59 Eventually(process.Wait(), network.EventuallyTimeout).Should(Receive()) 60 } 61 if network != nil { 62 network.Cleanup() 63 } 64 os.RemoveAll(testDir) 65 }) 66 67 Describe("basic solo network with 2 orgs", func() { 68 BeforeEach(func() { 69 network = nwo.New(nwo.BasicSoloWithIdemix(), testDir, client, StartPort(), components) 70 network.GenerateConfigTree() 71 network.Bootstrap() 72 73 networkRunner := network.NetworkGroupRunner() 74 process = ifrit.Invoke(networkRunner) 75 Eventually(process.Ready(), network.EventuallyTimeout).Should(BeClosed()) 76 }) 77 78 It("executes a basic solo network with 2 orgs", func() { 79 By("getting the orderer by name") 80 orderer := network.Orderer("orderer") 81 82 By("setting up the channel") 83 network.CreateAndJoinChannel(orderer, "testchannel") 84 nwo.EnableCapabilities(network, "testchannel", "Application", "V2_0", orderer, network.Peer("Org1", "peer0"), network.Peer("Org2", "peer0")) 85 86 By("deploying the chaincode") 87 nwo.DeployChaincode(network, "testchannel", orderer, chaincode) 88 89 By("getting the client peer by name") 90 peer := network.Peer("Org1", "peer0") 91 92 Query(network, peer, "testchannel", "100") 93 Invoke(network, orderer, peer, "testchannel") 94 Query(network, peer, "testchannel", "90") 95 96 By("getting the idemix client peer by name") 97 idemixOrg := network.Organization("Org3") 98 QueryWithIdemix(network, peer, idemixOrg, "testchannel", "90") 99 InvokeWithIdemix(network, orderer, peer, idemixOrg, "testchannel") 100 QueryWithIdemix(network, peer, idemixOrg, "testchannel", "80") 101 Query(network, peer, "testchannel", "80") 102 }) 103 }) 104 }) 105 106 func Query(n *nwo.Network, peer *nwo.Peer, channel string, expectedOutput string) { 107 By("querying the chaincode") 108 sess, err := n.PeerUserSession(peer, "User1", commands.ChaincodeQuery{ 109 ChannelID: channel, 110 Name: "mycc", 111 Ctor: `{"Args":["query","a"]}`, 112 }) 113 Expect(err).NotTo(HaveOccurred()) 114 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 115 Expect(sess).To(gbytes.Say(expectedOutput)) 116 } 117 118 func QueryWithIdemix(n *nwo.Network, peer *nwo.Peer, idemixOrg *nwo.Organization, channel string, expectedOutput string) { 119 By("querying the chaincode") 120 sess, err := n.IdemixUserSession(peer, idemixOrg, "User1", commands.ChaincodeQuery{ 121 ChannelID: channel, 122 Name: "mycc", 123 Ctor: `{"Args":["query","a"]}`, 124 }) 125 Expect(err).NotTo(HaveOccurred()) 126 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 127 Expect(sess).To(gbytes.Say(expectedOutput)) 128 } 129 130 func Invoke(n *nwo.Network, orderer *nwo.Orderer, peer *nwo.Peer, channel string) { 131 sess, err := n.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{ 132 ChannelID: channel, 133 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 134 Name: "mycc", 135 Ctor: `{"Args":["invoke","a","b","10"]}`, 136 PeerAddresses: []string{ 137 n.PeerAddress(n.Peer("Org1", "peer0"), nwo.ListenPort), 138 n.PeerAddress(n.Peer("Org2", "peer0"), nwo.ListenPort), 139 }, 140 WaitForEvent: true, 141 }) 142 Expect(err).NotTo(HaveOccurred()) 143 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 144 Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200")) 145 } 146 147 func InvokeWithIdemix(n *nwo.Network, orderer *nwo.Orderer, peer *nwo.Peer, idemixOrg *nwo.Organization, channel string) { 148 sess, err := n.IdemixUserSession(peer, idemixOrg, "User1", commands.ChaincodeInvoke{ 149 ChannelID: channel, 150 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 151 Name: "mycc", 152 Ctor: `{"Args":["invoke","a","b","10"]}`, 153 PeerAddresses: []string{ 154 n.PeerAddress(n.Peer("Org1", "peer0"), nwo.ListenPort), 155 n.PeerAddress(n.Peer("Org2", "peer0"), nwo.ListenPort), 156 }, 157 WaitForEvent: true, 158 }) 159 Expect(err).NotTo(HaveOccurred()) 160 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 161 Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200")) 162 }