github.com/suchongming/fabric@v2.1.1+incompatible/integration/sbe/sbe_test.go (about) 1 /* 2 Copyright IBM Corp All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package sbe 8 9 import ( 10 "encoding/json" 11 "io/ioutil" 12 "os" 13 "path/filepath" 14 "strings" 15 "syscall" 16 17 docker "github.com/fsouza/go-dockerclient" 18 "github.com/hyperledger/fabric-protos-go/common" 19 "github.com/hyperledger/fabric/integration/nwo" 20 "github.com/hyperledger/fabric/integration/nwo/commands" 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 "github.com/onsi/gomega/gbytes" 24 "github.com/onsi/gomega/gexec" 25 "github.com/tedsuo/ifrit" 26 ) 27 28 var _ = Describe("SBE_E2E", func() { 29 var ( 30 testDir string 31 client *docker.Client 32 network *nwo.Network 33 chaincode nwo.Chaincode 34 process ifrit.Process 35 tempDir string 36 ) 37 38 BeforeEach(func() { 39 var err error 40 testDir, err = ioutil.TempDir("", "e2e_sbe") 41 Expect(err).NotTo(HaveOccurred()) 42 43 client, err = docker.NewClientFromEnv() 44 Expect(err).NotTo(HaveOccurred()) 45 46 chaincode = nwo.Chaincode{ 47 Name: "mycc", 48 Version: "0.0", 49 Path: "github.com/hyperledger/fabric/integration/chaincode/keylevelep/cmd", 50 Ctor: `{"Args":["init"]}`, 51 CollectionsConfig: "testdata/collection_config.json", 52 } 53 54 tempDir, err = ioutil.TempDir("", "nwo") 55 }) 56 57 AfterEach(func() { 58 if process != nil { 59 process.Signal(syscall.SIGTERM) 60 Eventually(process.Wait(), network.EventuallyTimeout).Should(Receive()) 61 } 62 if network != nil { 63 network.Cleanup() 64 } 65 os.RemoveAll(testDir) 66 }) 67 68 Describe("basic solo network with 2 orgs", func() { 69 BeforeEach(func() { 70 network = nwo.New(nwo.BasicSolo(), testDir, client, StartPort(), components) 71 network.GenerateConfigTree() 72 network.Bootstrap() 73 74 networkRunner := network.NetworkGroupRunner() 75 process = ifrit.Invoke(networkRunner) 76 Eventually(process.Ready(), network.EventuallyTimeout).Should(BeClosed()) 77 }) 78 79 It("executes a basic solo network with 2 orgs and SBE checks", func() { 80 By("getting the orderer by name") 81 orderer := network.Orderer("orderer") 82 83 By("setting up the channel") 84 network.CreateAndJoinChannel(orderer, "testchannel") 85 86 By("updating the anchor peers") 87 network.UpdateChannelAnchors(orderer, "testchannel") 88 89 By("deploying the chaincode") 90 nwo.DeployChaincodeLegacy(network, "testchannel", orderer, chaincode) 91 92 By("deploying a second instance of the chaincode") 93 chaincode.Name = "mycc2" 94 nwo.DeployChaincodeLegacy(network, "testchannel", orderer, chaincode) 95 96 RunSBE(network, orderer, "pub") 97 RunSBE(network, orderer, "priv") 98 }) 99 100 It("executes a basic solo network with 2 orgs and SBE checks with _lifecycle", func() { 101 chaincode = nwo.Chaincode{ 102 Name: "mycc", 103 Version: "0.0", 104 Path: "github.com/hyperledger/fabric/integration/chaincode/keylevelep/cmd", 105 Lang: "golang", 106 PackageFile: filepath.Join(tempDir, "simplecc.tar.gz"), 107 Ctor: `{"Args":["init"]}`, 108 SignaturePolicy: `OR('Org1MSP.member','Org2MSP.member')`, 109 Sequence: "1", 110 InitRequired: true, 111 Label: "my_simple_chaincode", 112 CollectionsConfig: "testdata/collection_config.json", 113 } 114 115 By("getting the orderer by name") 116 orderer := network.Orderer("orderer") 117 118 By("setting up the channel") 119 network.CreateAndJoinChannel(orderer, "testchannel") 120 121 By("updating the anchor peers") 122 network.UpdateChannelAnchors(orderer, "testchannel") 123 network.VerifyMembership(network.PeersWithChannel("testchannel"), "testchannel") 124 125 By("enabling 2.0 application capabilities") 126 nwo.EnableCapabilities(network, "testchannel", "Application", "V2_0", orderer, network.Peer("Org1", "peer0"), network.Peer("Org2", "peer0")) 127 128 By("deploying the chaincode") 129 nwo.DeployChaincode(network, "testchannel", orderer, chaincode) 130 131 By("deploying a second instance of the chaincode") 132 chaincode.Name = "mycc2" 133 chaincode.PackageFile = filepath.Join(tempDir, "simplecc2.tar.gz") 134 chaincode.Label = "my_other_simple_chaincode" 135 nwo.DeployChaincode(network, "testchannel", orderer, chaincode) 136 137 RunSBE(network, orderer, "pub") 138 RunSBE(network, orderer, "priv") 139 }) 140 }) 141 }) 142 143 func RunSBE(n *nwo.Network, orderer *nwo.Orderer, mode string) { 144 peerOrg1 := n.Peer("Org1", "peer0") 145 peerOrg2 := n.Peer("Org2", "peer0") 146 147 By("org1 initializes the key") 148 sess, err := n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 149 ChannelID: "testchannel", 150 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 151 Name: "mycc", 152 Ctor: `{"Args":["setval", "` + mode + `", "foo"]}`, 153 PeerAddresses: []string{ 154 n.PeerAddress(peerOrg1, nwo.ListenPort), 155 }, 156 WaitForEvent: true, 157 }) 158 Expect(err).NotTo(HaveOccurred()) 159 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 160 161 syncLedgerHeights(n, peerOrg1, peerOrg2) 162 163 By("org2 checks that setting the value was successful by reading it") 164 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 165 ChannelID: "testchannel", 166 Name: "mycc", 167 Ctor: `{"Args":["getval", "` + mode + `"]}`, 168 }) 169 Expect(err).NotTo(HaveOccurred()) 170 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 171 Expect(sess).To(gbytes.Say("foo")) 172 173 By("org1 adds org1 to the state-based ep of a key") 174 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 175 ChannelID: "testchannel", 176 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 177 Name: "mycc", 178 Ctor: `{"Args":["addorgs", "` + mode + `", "Org1MSP"]}`, 179 PeerAddresses: []string{ 180 n.PeerAddress(peerOrg1, nwo.ListenPort), 181 }, 182 WaitForEvent: true, 183 }) 184 Expect(err).NotTo(HaveOccurred()) 185 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 186 187 By("checking that the modification succeeded through listing the orgs in the ep") 188 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeQuery{ 189 ChannelID: "testchannel", 190 Name: "mycc", 191 Ctor: `{"Args":["listorgs", "` + mode + `"]}`, 192 }) 193 Expect(err).NotTo(HaveOccurred()) 194 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 195 Expect(sess).To(gbytes.Say("Org1MSP")) 196 197 By("org1 sets the value of the key") 198 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 199 ChannelID: "testchannel", 200 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 201 Name: "mycc", 202 Ctor: `{"Args":["setval", "` + mode + `", "val1"]}`, 203 PeerAddresses: []string{ 204 n.PeerAddress(peerOrg1, nwo.ListenPort), 205 }, 206 WaitForEvent: true, 207 }) 208 Expect(err).NotTo(HaveOccurred()) 209 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 210 211 syncLedgerHeights(n, peerOrg1, peerOrg2) 212 213 By("org2 checks that setting the value was successful by reading it") 214 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 215 ChannelID: "testchannel", 216 Name: "mycc", 217 Ctor: `{"Args":["getval", "` + mode + `"]}`, 218 }) 219 Expect(err).NotTo(HaveOccurred()) 220 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 221 Expect(sess).To(gbytes.Say("val1")) 222 223 By("org2 sets the value of the key") 224 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeInvoke{ 225 ChannelID: "testchannel", 226 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 227 Name: "mycc", 228 Ctor: `{"Args":["setval", "` + mode + `", "val2"]}`, 229 PeerAddresses: []string{ 230 n.PeerAddress(peerOrg2, nwo.ListenPort), 231 }, 232 WaitForEvent: true, 233 }) 234 Expect(err).NotTo(HaveOccurred()) 235 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(1)) 236 Expect(sess.Err).To(gbytes.Say(`\Qcommitted with status (ENDORSEMENT_POLICY_FAILURE)\E`)) 237 238 By("org2 checks that setting the value was not successful by reading it") 239 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 240 ChannelID: "testchannel", 241 Name: "mycc", 242 Ctor: `{"Args":["getval", "` + mode + `"]}`, 243 }) 244 Expect(err).NotTo(HaveOccurred()) 245 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 246 Expect(sess).To(gbytes.Say("val1")) 247 248 syncLedgerHeights(n, peerOrg2, peerOrg1) 249 250 By("org1 adds org2 to the ep of the key") 251 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 252 ChannelID: "testchannel", 253 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 254 Name: "mycc", 255 Ctor: `{"Args":["addorgs", "` + mode + `", "Org2MSP"]}`, 256 PeerAddresses: []string{ 257 n.PeerAddress(peerOrg1, nwo.ListenPort), 258 }, 259 WaitForEvent: true, 260 }) 261 Expect(err).NotTo(HaveOccurred()) 262 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 263 264 By("org1 lists the orgs of the ep to check that both org1 and org2 are there") 265 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeQuery{ 266 ChannelID: "testchannel", 267 Name: "mycc", 268 Ctor: `{"Args":["listorgs", "` + mode + `"]}`, 269 }) 270 Expect(err).NotTo(HaveOccurred()) 271 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 272 orgs := [2]string{"Org1MSP", "Org2MSP"} 273 orgsList, err := json.Marshal(orgs) 274 Expect(err).NotTo(HaveOccurred()) 275 Expect(sess).To(gbytes.Say(string(orgsList))) 276 277 syncLedgerHeights(n, peerOrg1, peerOrg2) 278 279 By("org2 sets the value of the key") 280 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeInvoke{ 281 ChannelID: "testchannel", 282 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 283 Name: "mycc", 284 Ctor: `{"Args":["setval", "` + mode + `", "val3"]}`, 285 PeerAddresses: []string{ 286 n.PeerAddress(peerOrg2, nwo.ListenPort), 287 }, 288 WaitForEvent: true, 289 }) 290 Expect(err).NotTo(HaveOccurred()) 291 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(1)) 292 Expect(sess.Err).To(gbytes.Say(`\Qcommitted with status (ENDORSEMENT_POLICY_FAILURE)\E`)) 293 294 By("org2 checks that seting the value was not successful by reading it") 295 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 296 ChannelID: "testchannel", 297 Name: "mycc", 298 Ctor: `{"Args":["getval", "` + mode + `"]}`, 299 }) 300 Expect(err).NotTo(HaveOccurred()) 301 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 302 Expect(sess).To(gbytes.Say("val1")) 303 304 syncLedgerHeights(n, peerOrg2, peerOrg1) 305 306 By("org1 and org2 set the value of the key") 307 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 308 ChannelID: "testchannel", 309 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 310 Name: "mycc", 311 Ctor: `{"Args":["setval", "` + mode + `", "val4"]}`, 312 PeerAddresses: []string{ 313 n.PeerAddress(peerOrg1, nwo.ListenPort), 314 n.PeerAddress(peerOrg2, nwo.ListenPort), 315 }, 316 WaitForEvent: true, 317 }) 318 Expect(err).NotTo(HaveOccurred()) 319 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 320 321 By("org1 checks that setting the value was successful by reading it") 322 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeQuery{ 323 ChannelID: "testchannel", 324 Name: "mycc", 325 Ctor: `{"Args":["getval", "` + mode + `"]}`, 326 }) 327 Expect(err).NotTo(HaveOccurred()) 328 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 329 Expect(sess).To(gbytes.Say("val4")) 330 331 syncLedgerHeights(n, peerOrg1, peerOrg2) 332 333 By("org2 deletes org1 from the ep of the key") 334 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeInvoke{ 335 ChannelID: "testchannel", 336 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 337 Name: "mycc", 338 Ctor: `{"Args":["delorgs", "` + mode + `", "Org1MSP"]}`, 339 PeerAddresses: []string{ 340 n.PeerAddress(peerOrg2, nwo.ListenPort), 341 }, 342 WaitForEvent: true, 343 }) 344 Expect(err).NotTo(HaveOccurred()) 345 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(1)) 346 Expect(sess.Err).To(gbytes.Say(`\Qcommitted with status (ENDORSEMENT_POLICY_FAILURE)\E`)) 347 348 By("org2 lists the orgs of the key to check that deleting org1 did not succeed") 349 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 350 ChannelID: "testchannel", 351 Name: "mycc", 352 Ctor: `{"Args":["listorgs", "` + mode + `"]}`, 353 }) 354 Expect(err).NotTo(HaveOccurred()) 355 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 356 Expect(sess).To(gbytes.Say(string(orgsList))) 357 358 syncLedgerHeights(n, peerOrg2, peerOrg1) 359 360 By("org1 and org2 delete org1 from the ep of the key") 361 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeInvoke{ 362 ChannelID: "testchannel", 363 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 364 Name: "mycc", 365 Ctor: `{"Args":["delorgs", "` + mode + `", "Org1MSP"]}`, 366 PeerAddresses: []string{ 367 n.PeerAddress(peerOrg1, nwo.ListenPort), 368 n.PeerAddress(peerOrg2, nwo.ListenPort), 369 }, 370 WaitForEvent: true, 371 }) 372 Expect(err).NotTo(HaveOccurred()) 373 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 374 375 By("org2 lists the orgs of the key's ep to check that removing org1 from the ep was successful") 376 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 377 ChannelID: "testchannel", 378 Name: "mycc", 379 Ctor: `{"Args":["listorgs", "` + mode + `"]}`, 380 }) 381 Expect(err).NotTo(HaveOccurred()) 382 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 383 Expect(sess).To(gbytes.Say("Org2MSP")) 384 385 By("org2 uses cc2cc invocation to set the value of the key") 386 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeInvoke{ 387 ChannelID: "testchannel", 388 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 389 Name: "mycc2", 390 Ctor: `{"Args":["cc2cc", "testchannel", "mycc", "setval", "` + mode + `", "cc2cc_org2"]}`, 391 PeerAddresses: []string{ 392 n.PeerAddress(peerOrg2, nwo.ListenPort), 393 }, 394 WaitForEvent: true, 395 }) 396 Expect(err).NotTo(HaveOccurred()) 397 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 398 399 By("org2 reads the value of the key to check that setting it was successful") 400 sess, err = n.PeerUserSession(peerOrg2, "User1", commands.ChaincodeQuery{ 401 ChannelID: "testchannel", 402 Name: "mycc", 403 Ctor: `{"Args":["getval", "` + mode + `"]}`, 404 }) 405 Expect(err).NotTo(HaveOccurred()) 406 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 407 Expect(sess).To(gbytes.Say("cc2cc_org2")) 408 409 syncLedgerHeights(n, peerOrg2, peerOrg1) 410 411 By("org1 uses cc2cc to set the value of the key") 412 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeInvoke{ 413 ChannelID: "testchannel", 414 Orderer: n.OrdererAddress(orderer, nwo.ListenPort), 415 Name: "mycc2", 416 Ctor: `{"Args":["cc2cc", "testchannel", "mycc", "setval", "` + mode + `", "cc2cc_org1"]}`, 417 PeerAddresses: []string{ 418 n.PeerAddress(peerOrg1, nwo.ListenPort), 419 }, 420 WaitForEvent: true, 421 }) 422 Expect(err).NotTo(HaveOccurred()) 423 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(1)) 424 Expect(sess.Err).To(gbytes.Say(`\Qcommitted with status (ENDORSEMENT_POLICY_FAILURE)\E`)) 425 426 By("org1 reads the value of the key to check that setting it was not successful") 427 sess, err = n.PeerUserSession(peerOrg1, "User1", commands.ChaincodeQuery{ 428 ChannelID: "testchannel", 429 Name: "mycc", 430 Ctor: `{"Args":["getval", "` + mode + `"]}`, 431 }) 432 Expect(err).NotTo(HaveOccurred()) 433 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 434 Expect(sess).To(gbytes.Say("cc2cc_org2")) 435 } 436 437 func getLedgerHeight(n *nwo.Network, peer *nwo.Peer, channelName string) int { 438 sess, err := n.PeerUserSession(peer, "User1", commands.ChannelInfo{ 439 ChannelID: channelName, 440 }) 441 Expect(err).NotTo(HaveOccurred()) 442 Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0)) 443 444 channelInfoStr := strings.TrimPrefix(string(sess.Buffer().Contents()[:]), "Blockchain info:") 445 var channelInfo = common.BlockchainInfo{} 446 json.Unmarshal([]byte(channelInfoStr), &channelInfo) 447 return int(channelInfo.Height) 448 } 449 450 func syncLedgerHeights(n *nwo.Network, peer1 *nwo.Peer, peer2 *nwo.Peer) { 451 // get height from peer1 452 height := getLedgerHeight(n, peer1, "testchannel") 453 // wait for same height on peer2 454 Eventually(func() int { 455 return getLedgerHeight(n, peer2, "testchannel") 456 }, n.EventuallyTimeout).Should(Equal(height)) 457 458 }