|
| 1 | +/* |
| 2 | +Copyright IBM Corp All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package lifecycle |
| 8 | + |
| 9 | +import ( |
| 10 | + "io/ioutil" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "syscall" |
| 14 | + |
| 15 | + docker "github.com/fsouza/go-dockerclient" |
| 16 | + "github.com/hyperledger/fabric/integration/nwo" |
| 17 | + "github.com/hyperledger/fabric/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 | + "gopkg.in/yaml.v2" |
| 24 | +) |
| 25 | + |
| 26 | +var _ = Describe("Lifecycle", func() { |
| 27 | + var ( |
| 28 | + client *docker.Client |
| 29 | + tempDir string |
| 30 | + network *nwo.Network |
| 31 | + process ifrit.Process |
| 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 | + soloBytes, err := ioutil.ReadFile("solo.yaml") |
| 43 | + Expect(err).NotTo(HaveOccurred()) |
| 44 | + |
| 45 | + var config *nwo.Config |
| 46 | + err = yaml.Unmarshal(soloBytes, &config) |
| 47 | + Expect(err).NotTo(HaveOccurred()) |
| 48 | + |
| 49 | + network = nwo.New(config, tempDir, client, StartPort(), components) |
| 50 | + |
| 51 | + // Generate config and bootstrap the network |
| 52 | + network.GenerateConfigTree() |
| 53 | + network.Bootstrap() |
| 54 | + |
| 55 | + // Start all of the fabric processes |
| 56 | + networkRunner := network.NetworkGroupRunner() |
| 57 | + process = ifrit.Invoke(networkRunner) |
| 58 | + Eventually(process.Ready(), network.EventuallyTimeout).Should(BeClosed()) |
| 59 | + }) |
| 60 | + |
| 61 | + AfterEach(func() { |
| 62 | + // Shutdown processes and cleanup |
| 63 | + process.Signal(syscall.SIGTERM) |
| 64 | + Eventually(process.Wait(), network.EventuallyTimeout).Should(Receive()) |
| 65 | + network.Cleanup() |
| 66 | + |
| 67 | + os.RemoveAll(tempDir) |
| 68 | + }) |
| 69 | + |
| 70 | + It("deploys and executes chaincode (simple) using _lifecycle and upgrades it", func() { |
| 71 | + orderer := network.Orderer("orderer0") |
| 72 | + testPeers := network.PeersWithChannel("testchannel") |
| 73 | + org1peer2 := network.Peer("org1", "peer2") |
| 74 | + |
| 75 | + chaincode := nwo.Chaincode{ |
| 76 | + Name: "mycc", |
| 77 | + Version: "0.0", |
| 78 | + Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd", |
| 79 | + Lang: "golang", |
| 80 | + PackageFile: filepath.Join(tempDir, "simplecc.tar.gz"), |
| 81 | + Ctor: `{"Args":["init","a","100","b","200"]}`, |
| 82 | + Policy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`, |
| 83 | + ChannelConfigPolicy: "/Channel/Application/Endorsement", |
| 84 | + Sequence: "1", |
| 85 | + InitRequired: true, |
| 86 | + Label: "my_simple_chaincode", |
| 87 | + } |
| 88 | + |
| 89 | + network.CreateAndJoinChannels(orderer) |
| 90 | + |
| 91 | + network.UpdateChannelAnchors(orderer, "testchannel") |
| 92 | + network.VerifyMembership(network.PeersWithChannel("testchannel"), "testchannel") |
| 93 | + |
| 94 | + nwo.EnableCapabilities(network, "testchannel", "Application", "V2_0", orderer, network.Peer("org1", "peer1"), network.Peer("org2", "peer1")) |
| 95 | + nwo.DeployChaincodeNewLifecycle(network, "testchannel", orderer, chaincode) |
| 96 | + |
| 97 | + RunQueryInvokeQuery(network, orderer, org1peer2, 100) |
| 98 | + |
| 99 | + By("setting a bad package ID to temporarily disable endorsements on org1") |
| 100 | + savedPackageID := chaincode.PackageID |
| 101 | + // note that in theory it should be sufficient to set it to an |
| 102 | + // empty string, but the ApproveChaincodeForMyOrgNewLifecycle |
| 103 | + // function fills the packageID field if empty |
| 104 | + chaincode.PackageID = "bad" |
| 105 | + nwo.ApproveChaincodeForMyOrgNewLifecycle(network, "testchannel", orderer, chaincode, org1peer2) |
| 106 | + |
| 107 | + By("querying the chaincode and expecting the invocation to fail") |
| 108 | + sess, err := network.PeerUserSession(org1peer2, "User1", commands.ChaincodeQuery{ |
| 109 | + ChannelID: "testchannel", |
| 110 | + Name: "mycc", |
| 111 | + Ctor: `{"Args":["query","a"]}`, |
| 112 | + }) |
| 113 | + Expect(err).NotTo(HaveOccurred()) |
| 114 | + Eventually(sess, network.EventuallyTimeout).Should(gexec.Exit(1)) |
| 115 | + Expect(sess.Err).To(gbytes.Say("Error: endorsement failure during query. response: status:500 " + |
| 116 | + "message:\"make sure the chaincode mycc has been successfully defined on channel testchannel and try " + |
| 117 | + "again: chaincode definition for 'mycc' exists, but chaincode is not installed\"")) |
| 118 | + |
| 119 | + By("setting the correct package ID to restore the chaincode") |
| 120 | + chaincode.PackageID = savedPackageID |
| 121 | + nwo.ApproveChaincodeForMyOrgNewLifecycle(network, "testchannel", orderer, chaincode, org1peer2) |
| 122 | + |
| 123 | + By("querying the chaincode and expecting the invocation to succeed") |
| 124 | + sess, err = network.PeerUserSession(org1peer2, "User1", commands.ChaincodeQuery{ |
| 125 | + ChannelID: "testchannel", |
| 126 | + Name: "mycc", |
| 127 | + Ctor: `{"Args":["query","a"]}`, |
| 128 | + }) |
| 129 | + Expect(err).NotTo(HaveOccurred()) |
| 130 | + Eventually(sess, network.EventuallyTimeout).Should(gexec.Exit(0)) |
| 131 | + Expect(sess).To(gbytes.Say("90")) |
| 132 | + |
| 133 | + By("upgrading the chaincode to sequence 2") |
| 134 | + chaincode.Sequence = "2" |
| 135 | + |
| 136 | + nwo.ApproveChaincodeForMyOrgNewLifecycle(network, "testchannel", orderer, chaincode, testPeers...) |
| 137 | + nwo.EnsureApproved(network, "testchannel", chaincode, network.PeerOrgs(), testPeers...) |
| 138 | + |
| 139 | + nwo.CommitChaincodeNewLifecycle(network, "testchannel", orderer, chaincode, testPeers[0], testPeers...) |
| 140 | + |
| 141 | + RunQueryInvokeQuery(network, orderer, testPeers[0], 90) |
| 142 | + }) |
| 143 | +}) |
0 commit comments