From edec005fe375c9534d8a06ac9646b195892f8c53 Mon Sep 17 00:00:00 2001 From: Matthew Sykes Date: Tue, 3 Jul 2018 15:17:03 -0400 Subject: [PATCH] [FAB-11051] refactor install/instantiate/upgrade - remove InstallChaincode and InstantiateChaincode from network - create install, instantiate, and ensure instantiated helpers to deploy - create upgrade command and helpers Change-Id: I9b1395d41be25aba2e3f2067500b0ed265c1196a Signed-off-by: Matthew Sykes --- integration/nwo/commands/peer.go | 53 ++++++++++++++--- integration/nwo/deploy.go | 97 ++++++++++++++++++++++++-------- integration/nwo/network.go | 28 --------- integration/nwo/network_test.go | 30 ++++------ 4 files changed, 131 insertions(+), 77 deletions(-) diff --git a/integration/nwo/commands/peer.go b/integration/nwo/commands/peer.go index 179c3095890..42cc214bbff 100644 --- a/integration/nwo/commands/peer.go +++ b/integration/nwo/commands/peer.go @@ -108,12 +108,13 @@ func (c ChaincodeInstall) Args() []string { } type ChaincodeInstantiate struct { - ChannelID string - Orderer string - Name string - Version string - Ctor string - Policy string + ChannelID string + Orderer string + Name string + Version string + Ctor string + Policy string + CollectionsConfig string } func (c ChaincodeInstantiate) SessionName() string { @@ -121,7 +122,7 @@ func (c ChaincodeInstantiate) SessionName() string { } func (c ChaincodeInstantiate) Args() []string { - return []string{ + args := []string{ "chaincode", "instantiate", "--channelID", c.ChannelID, "--orderer", c.Orderer, @@ -130,6 +131,10 @@ func (c ChaincodeInstantiate) Args() []string { "--ctor", c.Ctor, "--policy", c.Policy, } + if c.CollectionsConfig != "" { + args = append(args, "--collections-config", c.CollectionsConfig) + } + return args } type ChaincodeListInstalled struct{} @@ -208,6 +213,40 @@ func (c ChaincodeInvoke) Args() []string { return args } +type ChaincodeUpgrade struct { + Name string + Version string + Path string // optional + ChannelID string + Orderer string + Ctor string + Policy string + CollectionsConfig string // optional +} + +func (c ChaincodeUpgrade) SessionName() string { + return "peer-chaincode-upgrade" +} + +func (c ChaincodeUpgrade) Args() []string { + args := []string{ + "chaincode", "upgrade", + "--name", c.Name, + "--version", c.Version, + "--channelID", c.ChannelID, + "--orderer", c.Orderer, + "--ctor", c.Ctor, + "--policy", c.Policy, + } + if c.Path != "" { + args = append(args, "--path", c.Path) + } + if c.CollectionsConfig != "" { + args = append(args, "--collections-config", c.CollectionsConfig) + } + return args +} + type SignConfigTx struct { File string } diff --git a/integration/nwo/deploy.go b/integration/nwo/deploy.go index d7b7fd7a1e7..0d58fd0f16d 100644 --- a/integration/nwo/deploy.go +++ b/integration/nwo/deploy.go @@ -18,47 +18,100 @@ import ( ) type Chaincode struct { - Name string - Version string - Path string - Ctor string - Policy string + Name string + Version string + Path string + Ctor string + Policy string + CollectionsConfig string // optional } // DeployChaincode is a helper that will install chaincode to all peers that // are connected to the specified channel, instantiate the chaincode on one of // the peers, and wait for the instantiation to complete on all of the peers. -func DeployChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode) { - peers := n.PeersWithChannel(channel) +func DeployChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode, peers ...*Peer) { + if len(peers) == 0 { + peers = n.PeersWithChannel(channel) + } if len(peers) == 0 { return } // install on all peers - n.InstallChaincode(peers, commands.ChaincodeInstall{ - Name: chaincode.Name, - Version: chaincode.Version, - Path: chaincode.Path, - }) + InstallChaincode(n, chaincode, peers...) // instantiate on the first peer - n.InstantiateChaincode(peers[0], commands.ChaincodeInstantiate{ - ChannelID: channel, - Orderer: n.OrdererAddress(orderer, ListenPort), - Name: chaincode.Name, - Version: chaincode.Version, - Ctor: chaincode.Ctor, - Policy: chaincode.Policy, + InstantiateChaincode(n, channel, orderer, chaincode, peers[0], peers...) +} + +func InstallChaincode(n *Network, chaincode Chaincode, peers ...*Peer) { + for _, p := range peers { + sess, err := n.PeerAdminSession(p, commands.ChaincodeInstall{ + Name: chaincode.Name, + Version: chaincode.Version, + Path: chaincode.Path, + }) + Expect(err).NotTo(HaveOccurred()) + Eventually(sess, time.Minute).Should(gexec.Exit(0)) + + sess, err = n.PeerAdminSession(p, commands.ChaincodeListInstalled{}) + Expect(err).NotTo(HaveOccurred()) + Eventually(sess, time.Minute).Should(gexec.Exit(0)) + Expect(sess).To(gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", chaincode.Name, chaincode.Version))) + } +} + +func InstantiateChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode, peer *Peer, checkPeers ...*Peer) { + sess, err := n.PeerAdminSession(peer, commands.ChaincodeInstantiate{ + ChannelID: channel, + Orderer: n.OrdererAddress(orderer, ListenPort), + Name: chaincode.Name, + Version: chaincode.Version, + Ctor: chaincode.Ctor, + Policy: chaincode.Policy, + CollectionsConfig: chaincode.CollectionsConfig, }) + Expect(err).NotTo(HaveOccurred()) + Eventually(sess, time.Minute).Should(gexec.Exit(0)) + + EnsureInstantiated(n, channel, chaincode.Name, chaincode.Version, checkPeers...) +} - // make sure the instantiation of visible across the remaining peers - for _, p := range peers[1:] { +func EnsureInstantiated(n *Network, channel, name, version string, peers ...*Peer) { + for _, p := range peers { Eventually(listInstantiated(n, p, channel), time.Minute).Should( - gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", chaincode.Name, chaincode.Version)), + gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", name, version)), ) } } +func UpgradeChaincode(n *Network, channel string, orderer *Orderer, chaincode Chaincode, peers ...*Peer) { + if len(peers) == 0 { + peers = n.PeersWithChannel(channel) + } + if len(peers) == 0 { + return + } + + // install on all peers + InstallChaincode(n, chaincode, peers...) + + // upgrade from the first peer + sess, err := n.PeerAdminSession(peers[0], commands.ChaincodeUpgrade{ + ChannelID: channel, + Orderer: n.OrdererAddress(orderer, ListenPort), + Name: chaincode.Name, + Version: chaincode.Version, + Ctor: chaincode.Ctor, + Policy: chaincode.Policy, + CollectionsConfig: chaincode.CollectionsConfig, + }) + Expect(err).NotTo(HaveOccurred()) + Eventually(sess, time.Minute).Should(gexec.Exit(0)) + + EnsureInstantiated(n, channel, chaincode.Name, chaincode.Version, peers...) +} + func listInstantiated(n *Network, peer *Peer, channel string) func() *gbytes.Buffer { return func() *gbytes.Buffer { sess, err := n.PeerAdminSession(peer, commands.ChaincodeListInstantiated{ diff --git a/integration/nwo/network.go b/integration/nwo/network.go index e28fc808dd5..413f37401db 100644 --- a/integration/nwo/network.go +++ b/integration/nwo/network.go @@ -26,7 +26,6 @@ import ( "github.com/hyperledger/fabric/integration/runner" "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/onsi/gomega/gbytes" "github.com/onsi/gomega/gexec" "github.com/tedsuo/ifrit" "github.com/tedsuo/ifrit/ginkgomon" @@ -632,33 +631,6 @@ func (n *Network) JoinChannel(name string, o *Orderer, peers ...*Peer) { } } -// InstallChaincode installs chaincode to the listed peers and verifies that -// the install has completed. -func (n *Network) InstallChaincode(peers []*Peer, install commands.ChaincodeInstall) { - for _, p := range peers { - sess, err := n.PeerAdminSession(p, install) - Expect(err).NotTo(HaveOccurred()) - Eventually(sess, time.Minute).Should(gexec.Exit(0)) - - sess, err = n.PeerAdminSession(p, commands.ChaincodeListInstalled{}) - Expect(err).NotTo(HaveOccurred()) - Eventually(sess, time.Minute).Should(gexec.Exit(0)) - Expect(sess).To(gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", install.Name, install.Version))) - } -} - -// InstantiateChaincode instantiates chaincode at the specified peer and verifies that -// the instantiation is complete before returning. -func (n *Network) InstantiateChaincode(peer *Peer, instantiate commands.ChaincodeInstantiate) { - sess, err := n.PeerAdminSession(peer, instantiate) - Expect(err).NotTo(HaveOccurred()) - Eventually(sess, time.Minute).Should(gexec.Exit(0)) - - Eventually(listInstantiated(n, peer, instantiate.ChannelID), time.Minute).Should( - gbytes.Say(fmt.Sprintf("Name: %s, Version: %s,", instantiate.Name, instantiate.Version)), - ) -} - // Cryptogen starts a gexec.Session for the provided cryptogen command. func (n *Network) Cryptogen(command Command) (*gexec.Session, error) { cmd := NewCommand(n.Components.Cryptogen(), command) diff --git a/integration/nwo/network_test.go b/integration/nwo/network_test.go index 3d2c22eaaf3..fc0cededf90 100644 --- a/integration/nwo/network_test.go +++ b/integration/nwo/network_test.go @@ -165,26 +165,16 @@ var _ = Describe("Network", func() { network.CreateChannel("testchannel", orderer, testPeers[0]) network.JoinChannel("testchannel", orderer, testPeers...) - network.InstallChaincode( - testPeers, - commands.ChaincodeInstall{ - Name: "mycc", - Version: "0.0", - Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd", - }, - ) - - network.InstantiateChaincode( - testPeers[0], - commands.ChaincodeInstantiate{ - ChannelID: "testchannel", - Orderer: network.OrdererAddress(orderer, nwo.ListenPort), - Name: "mycc", - Version: "0.0", - Ctor: `{"Args":["init","a","100","b","200"]}`, - Policy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`, - }, - ) + chaincode := nwo.Chaincode{ + Name: "mycc", + Version: "0.0", + Path: "github.com/hyperledger/fabric/integration/chaincode/simple/cmd", + Ctor: `{"Args":["init","a","100","b","200"]}`, + Policy: `AND ('Org1ExampleCom.member','Org2ExampleCom.member')`, + } + nwo.InstallChaincode(network, chaincode, testPeers...) + nwo.InstantiateChaincode(network, "testchannel", orderer, chaincode, testPeers[0]) + nwo.EnsureInstantiated(network, "testchannel", "mycc", "0.0", testPeers...) RunQueryInvokeQuery(network, orderer, testPeers[0]) })