github.com/anjalikarhana/fabric@v2.1.1+incompatible/integration/nwo/network_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package nwo_test 8 9 import ( 10 "fmt" 11 "io/ioutil" 12 "os" 13 "path/filepath" 14 "syscall" 15 16 docker "github.com/fsouza/go-dockerclient" 17 "github.com/hyperledger/fabric/integration/nwo" 18 "github.com/hyperledger/fabric/integration/nwo/commands" 19 20 . "github.com/onsi/ginkgo" 21 . "github.com/onsi/gomega" 22 "github.com/onsi/gomega/gbytes" 23 "github.com/onsi/gomega/gexec" 24 "github.com/tedsuo/ifrit" 25 yaml "gopkg.in/yaml.v2" 26 ) 27 28 var _ = Describe("Network", func() { 29 var ( 30 client *docker.Client 31 tempDir string 32 ) 33 34 BeforeEach(func() { 35 var err error 36 tempDir, err = ioutil.TempDir("", "nwo") 37 Expect(err).NotTo(HaveOccurred()) 38 39 client, err = docker.NewClientFromEnv() 40 Expect(err).NotTo(HaveOccurred()) 41 }) 42 43 AfterEach(func() { 44 os.RemoveAll(tempDir) 45 }) 46 47 Describe("solo network", func() { 48 var network *nwo.Network 49 var process ifrit.Process 50 51 BeforeEach(func() { 52 soloBytes, err := ioutil.ReadFile("solo.yaml") 53 Expect(err).NotTo(HaveOccurred()) 54 55 var config *nwo.Config 56 err = yaml.Unmarshal(soloBytes, &config) 57 Expect(err).NotTo(HaveOccurred()) 58 59 network = nwo.New(config, tempDir, client, StartPort(), components) 60 61 // Generate config and bootstrap the network 62 network.GenerateConfigTree() 63 network.Bootstrap() 64 65 // Start all of the fabric processes 66 networkRunner := network.NetworkGroupRunner() 67 process = ifrit.Invoke(networkRunner) 68 Eventually(process.Ready(), network.EventuallyTimeout).Should(BeClosed()) 69 }) 70 71 AfterEach(func() { 72 // Shutdown processes and cleanup 73 process.Signal(syscall.SIGTERM) 74 Eventually(process.Wait(), network.EventuallyTimeout).Should(Receive()) 75 network.Cleanup() 76 }) 77 78 It("deploys and executes chaincode (simple) using the legacy lifecycle", func() { 79 orderer := network.Orderer("orderer0") 80 peer := network.Peer("org1", "peer2") 81 82 legacyChaincode := nwo.Chaincode{ 83 Name: "mycc", 84 Version: "0.0", 85 Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd", 86 Ctor: `{"Args":["init","a","100","b","200"]}`, 87 Policy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`, 88 } 89 90 network.CreateAndJoinChannels(orderer) 91 nwo.DeployChaincodeLegacy(network, "testchannel", orderer, legacyChaincode) 92 RunQueryInvokeQuery(network, orderer, peer, 100) 93 }) 94 95 It("deploys and executes chaincode (simple) using _lifecycle", func() { 96 orderer := network.Orderer("orderer0") 97 peer := network.Peer("org1", "peer2") 98 99 chaincode := nwo.Chaincode{ 100 Name: "mycc", 101 Version: "0.0", 102 Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd", 103 Lang: "golang", 104 PackageFile: filepath.Join(tempDir, "simplecc.tar.gz"), 105 Ctor: `{"Args":["init","a","100","b","200"]}`, 106 SignaturePolicy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`, 107 Sequence: "1", 108 InitRequired: true, 109 Label: "my_simple_chaincode", 110 } 111 112 network.CreateAndJoinChannels(orderer) 113 114 network.UpdateChannelAnchors(orderer, "testchannel") 115 network.VerifyMembership(network.PeersWithChannel("testchannel"), "testchannel") 116 117 nwo.EnableCapabilities(network, "testchannel", "Application", "V2_0", orderer, network.Peer("org1", "peer1"), network.Peer("org2", "peer1")) 118 nwo.DeployChaincode(network, "testchannel", orderer, chaincode) 119 120 RunQueryInvokeQuery(network, orderer, peer, 100) 121 }) 122 }) 123 124 Describe("kafka network", func() { 125 var ( 126 config nwo.Config 127 network *nwo.Network 128 processes map[string]ifrit.Process 129 ) 130 131 BeforeEach(func() { 132 soloBytes, err := ioutil.ReadFile("solo.yaml") 133 Expect(err).NotTo(HaveOccurred()) 134 135 err = yaml.Unmarshal(soloBytes, &config) 136 Expect(err).NotTo(HaveOccurred()) 137 138 // Switch from solo to kafka 139 config.Consensus.Type = "kafka" 140 config.Consensus.ZooKeepers = 1 141 config.Consensus.Brokers = 1 142 143 network = nwo.New(&config, tempDir, client, StartPort(), components) 144 network.GenerateConfigTree() 145 network.Bootstrap() 146 processes = map[string]ifrit.Process{} 147 }) 148 149 AfterEach(func() { 150 for _, p := range processes { 151 p.Signal(syscall.SIGTERM) 152 Eventually(p.Wait(), network.EventuallyTimeout).Should(Receive()) 153 } 154 network.Cleanup() 155 }) 156 157 It("deploys and executes chaincode (the hard way) using the legacy lifecycle", func() { 158 // This demonstrates how to control the processes that make up a network. 159 // If you don't care about a collection of processes (like the brokers or 160 // the orderers) use the group runner to manage those processes. 161 zookeepers := []string{} 162 for i := 0; i < network.Consensus.ZooKeepers; i++ { 163 zk := network.ZooKeeperRunner(i) 164 zookeepers = append(zookeepers, fmt.Sprintf("%s:2181", zk.Name)) 165 166 p := ifrit.Invoke(zk) 167 processes[zk.Name] = p 168 Eventually(p.Ready(), network.EventuallyTimeout).Should(BeClosed()) 169 } 170 171 for i := 0; i < network.Consensus.Brokers; i++ { 172 b := network.BrokerRunner(i, zookeepers) 173 p := ifrit.Invoke(b) 174 processes[b.Name] = p 175 Eventually(p.Ready(), network.EventuallyTimeout).Should(BeClosed()) 176 } 177 178 for _, o := range network.Orderers { 179 or := network.OrdererRunner(o) 180 p := ifrit.Invoke(or) 181 processes[o.ID()] = p 182 Eventually(p.Ready(), network.EventuallyTimeout).Should(BeClosed()) 183 } 184 185 for _, peer := range network.Peers { 186 pr := network.PeerRunner(peer) 187 p := ifrit.Invoke(pr) 188 processes[peer.ID()] = p 189 Eventually(p.Ready(), network.EventuallyTimeout).Should(BeClosed()) 190 } 191 192 orderer := network.Orderer("orderer0") 193 testPeers := network.PeersWithChannel("testchannel") 194 network.CreateChannel("testchannel", orderer, testPeers[0]) 195 network.JoinChannel("testchannel", orderer, testPeers...) 196 197 legacyChaincode := nwo.Chaincode{ 198 Name: "mycc", 199 Version: "0.0", 200 Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd", 201 Ctor: `{"Args":["init","a","100","b","200"]}`, 202 Policy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`, 203 } 204 nwo.InstallChaincodeLegacy(network, legacyChaincode, testPeers...) 205 nwo.InstantiateChaincodeLegacy(network, "testchannel", orderer, legacyChaincode, testPeers[0]) 206 nwo.EnsureInstantiatedLegacy(network, "testchannel", "mycc", "0.0", testPeers...) 207 208 RunQueryInvokeQuery(network, orderer, testPeers[0], 100) 209 }) 210 211 It("packages and installs chaincode (the hard way) using _lifecycle", func() { 212 // This demonstrates how to control the processes that make up a network. 213 // If you don't care about a collection of processes (like the brokers or 214 // the orderers) use the group runner to manage those processes. 215 zookeepers := []string{} 216 for i := 0; i < network.Consensus.ZooKeepers; i++ { 217 zk := network.ZooKeeperRunner(i) 218 zookeepers = append(zookeepers, fmt.Sprintf("%s:2181", zk.Name)) 219 220 p := ifrit.Invoke(zk) 221 processes[zk.Name] = p 222 Eventually(p.Ready(), network.EventuallyTimeout).Should(BeClosed()) 223 } 224 225 for i := 0; i < network.Consensus.Brokers; i++ { 226 b := network.BrokerRunner(i, zookeepers) 227 p := ifrit.Invoke(b) 228 processes[b.Name] = p 229 Eventually(p.Ready(), network.EventuallyTimeout).Should(BeClosed()) 230 } 231 232 for _, o := range network.Orderers { 233 or := network.OrdererRunner(o) 234 p := ifrit.Invoke(or) 235 processes[o.ID()] = p 236 Eventually(p.Ready(), network.EventuallyTimeout).Should(BeClosed()) 237 } 238 239 for _, peer := range network.Peers { 240 pr := network.PeerRunner(peer) 241 p := ifrit.Invoke(pr) 242 processes[peer.ID()] = p 243 Eventually(p.Ready(), network.EventuallyTimeout).Should(BeClosed()) 244 } 245 246 orderer := network.Orderer("orderer0") 247 testPeers := network.PeersWithChannel("testchannel") 248 network.CreateChannel("testchannel", orderer, testPeers[0]) 249 network.JoinChannel("testchannel", orderer, testPeers...) 250 251 network.UpdateChannelAnchors(orderer, "testchannel") 252 network.VerifyMembership(testPeers, "testchannel") 253 254 nwo.EnableCapabilities(network, "testchannel", "Application", "V2_0", orderer, network.Peer("org1", "peer1"), network.Peer("org2", "peer1")) 255 256 chaincode := nwo.Chaincode{ 257 Name: "mycc", 258 Version: "0.0", 259 Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd", 260 Lang: "golang", 261 PackageFile: filepath.Join(tempDir, "simplecc.tar.gz"), 262 Ctor: `{"Args":["init","a","100","b","200"]}`, 263 EndorsementPlugin: "escc", 264 ValidationPlugin: "vscc", 265 SignaturePolicy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`, 266 Sequence: "1", 267 InitRequired: true, 268 Label: "my_simple_chaincode", 269 } 270 271 nwo.PackageChaincode(network, chaincode, testPeers[0]) 272 273 nwo.InstallChaincode(network, chaincode, testPeers...) 274 275 nwo.ApproveChaincodeForMyOrg(network, "testchannel", orderer, chaincode, testPeers...) 276 277 nwo.CheckCommitReadinessUntilReady(network, "testchannel", chaincode, network.PeerOrgs(), testPeers...) 278 nwo.CommitChaincode(network, "testchannel", orderer, chaincode, testPeers[0], testPeers...) 279 nwo.InitChaincode(network, "testchannel", orderer, chaincode, testPeers...) 280 281 RunQueryInvokeQuery(network, orderer, testPeers[0], 100) 282 }) 283 }) 284 }) 285 286 func RunQueryInvokeQuery(n *nwo.Network, orderer *nwo.Orderer, peer *nwo.Peer, initialQueryResult int) { 287 By("querying the chaincode") 288 sess, err := n.PeerUserSession(peer, "User1", commands.ChaincodeQuery{ 289 ChannelID: "testchannel", 290 Name: "mycc", 291 Ctor: `{"Args":["query","a"]}`, 292 }) 293 Expect(err).NotTo(HaveOccurred()) 294 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 295 Expect(sess).To(gbytes.Say(fmt.Sprint(initialQueryResult))) 296 297 sess, err = n.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{ 298 ChannelID: "testchannel", 299 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 300 Name: "mycc", 301 Ctor: `{"Args":["invoke","a","b","10"]}`, 302 PeerAddresses: []string{ 303 n.PeerAddress(n.Peer("org1", "peer1"), nwo.ListenPort), 304 n.PeerAddress(n.Peer("org2", "peer2"), nwo.ListenPort), 305 }, 306 WaitForEvent: true, 307 }) 308 Expect(err).NotTo(HaveOccurred()) 309 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 310 Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200")) 311 312 sess, err = n.PeerUserSession(peer, "User1", commands.ChaincodeQuery{ 313 ChannelID: "testchannel", 314 Name: "mycc", 315 Ctor: `{"Args":["query","a"]}`, 316 }) 317 Expect(err).NotTo(HaveOccurred()) 318 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 319 Expect(sess).To(gbytes.Say(fmt.Sprint(initialQueryResult - 10))) 320 }