Skip to content

Commit 9783d2d

Browse files
committed
Move _lifecycle tests out of nwo
FAB-15529 #done Change-Id: I1f57eb25568e4cdf64085fe5503aa9bfb8cd6cbc Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
1 parent a941571 commit 9783d2d

File tree

2 files changed

+145
-46
lines changed

2 files changed

+145
-46
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
})

integration/nwo/network_test.go

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var _ = Describe("Network", func() {
6969
})
7070

7171
AfterEach(func() {
72-
// Shutodwn processes and cleanup
72+
// Shutdown processes and cleanup
7373
process.Signal(syscall.SIGTERM)
7474
Eventually(process.Wait(), network.EventuallyTimeout).Should(Receive())
7575
network.Cleanup()
@@ -119,40 +119,6 @@ var _ = Describe("Network", func() {
119119
nwo.DeployChaincodeNewLifecycle(network, "testchannel", orderer, chaincode)
120120

121121
RunQueryInvokeQuery(network, orderer, peer, 100)
122-
123-
By("setting a bad package ID to temporarily disable endorsements on org1")
124-
savedPackageID := chaincode.PackageID
125-
// note that in theory it should be sufficient to set it to an
126-
// empty string, but the ApproveChaincodeForMyOrgNewLifecycle
127-
// function fills the packageID field if empty
128-
chaincode.PackageID = "bad"
129-
nwo.ApproveChaincodeForMyOrgNewLifecycle(network, "testchannel", orderer, chaincode, peer)
130-
131-
By("querying the chaincode and expecting the invocation to fail")
132-
sess, err := network.PeerUserSession(peer, "User1", commands.ChaincodeQuery{
133-
ChannelID: "testchannel",
134-
Name: "mycc",
135-
Ctor: `{"Args":["query","a"]}`,
136-
})
137-
Expect(err).NotTo(HaveOccurred())
138-
Eventually(sess, network.EventuallyTimeout).Should(gexec.Exit(1))
139-
Expect(sess.Err).To(gbytes.Say("Error: endorsement failure during query. response: status:500 " +
140-
"message:\"make sure the chaincode mycc has been successfully defined on channel testchannel and try " +
141-
"again: chaincode definition for 'mycc' exists, but chaincode is not installed\""))
142-
143-
By("setting the correct package ID to restore the chaincode")
144-
chaincode.PackageID = savedPackageID
145-
nwo.ApproveChaincodeForMyOrgNewLifecycle(network, "testchannel", orderer, chaincode, peer)
146-
147-
By("querying the chaincode and expecting the invocation to succeed")
148-
sess, err = network.PeerUserSession(peer, "User1", commands.ChaincodeQuery{
149-
ChannelID: "testchannel",
150-
Name: "mycc",
151-
Ctor: `{"Args":["query","a"]}`,
152-
})
153-
Expect(err).NotTo(HaveOccurred())
154-
Eventually(sess, network.EventuallyTimeout).Should(gexec.Exit(0))
155-
Expect(sess).To(gbytes.Say("90"))
156122
})
157123
})
158124

@@ -243,7 +209,7 @@ var _ = Describe("Network", func() {
243209
RunQueryInvokeQuery(network, orderer, testPeers[0], 100)
244210
})
245211

246-
It("packages and installs chaincode (the hard way) using _lifecycle and then upgrades it", func() {
212+
It("packages and installs chaincode (the hard way) using _lifecycle", func() {
247213
// This demonstrates how to control the processes that make up a network.
248214
// If you don't care about a collection of processes (like the brokers or
249215
// the orderers) use the group runner to manage those processes.
@@ -319,16 +285,6 @@ var _ = Describe("Network", func() {
319285
nwo.InitChaincodeNewLifecycle(network, "testchannel", orderer, chaincode, testPeers...)
320286

321287
RunQueryInvokeQuery(network, orderer, testPeers[0], 100)
322-
323-
// upgrade chaincode to sequence 2
324-
chaincode.Sequence = "2"
325-
326-
nwo.ApproveChaincodeForMyOrgNewLifecycle(network, "testchannel", orderer, chaincode, testPeers...)
327-
nwo.EnsureApproved(network, "testchannel", chaincode, network.PeerOrgs(), testPeers...)
328-
329-
nwo.CommitChaincodeNewLifecycle(network, "testchannel", orderer, chaincode, testPeers[0], testPeers...)
330-
331-
RunQueryInvokeQuery(network, orderer, testPeers[0], 90)
332288
})
333289
})
334290
})

0 commit comments

Comments
 (0)