forked from SmartBFT-Go/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devmode_test.go
285 lines (252 loc) · 9.33 KB
/
devmode_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package devmode
import (
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"time"
docker "github.com/fsouza/go-dockerclient"
"github.com/hyperledger/fabric/integration/channelparticipation"
"github.com/hyperledger/fabric/integration/nwo"
"github.com/hyperledger/fabric/integration/nwo/commands"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"github.com/tedsuo/ifrit"
ginkgomon "github.com/tedsuo/ifrit/ginkgomon_v2"
)
var _ = Describe("Devmode", func() {
var (
testDir string
client *docker.Client
network *nwo.Network
ordererRunner *ginkgomon.Runner
ordererProcess, peerProcess ifrit.Process
chaincode nwo.Chaincode
chaincodeRunner *ginkgomon.Runner
chaincodeProcess ifrit.Process
channelName string
)
BeforeEach(func() {
var err error
channelName = "testchannel"
testDir, err = os.MkdirTemp("", "devmode")
Expect(err).NotTo(HaveOccurred())
client, err = docker.NewClientFromEnv()
Expect(err).NotTo(HaveOccurred())
network = nwo.New(devModeEtcdraft, testDir, client, StartPort(), components)
network.TLSEnabled = false
network.Peer("Org1", "peer0").DevMode = true
network.GenerateConfigTree()
network.Bootstrap()
// Start all the fabric processes
ordererRunner, ordererProcess, peerProcess = network.StartSingleOrdererNetwork("orderer")
})
AfterEach(func() {
if ordererProcess != nil {
ordererProcess.Signal(syscall.SIGTERM)
Eventually(ordererProcess.Wait(), network.EventuallyTimeout).Should(Receive())
}
if peerProcess != nil {
peerProcess.Signal(syscall.SIGTERM)
Eventually(peerProcess.Wait(), network.EventuallyTimeout).Should(Receive())
}
if chaincodeProcess != nil {
chaincodeProcess.Signal(syscall.SIGTERM)
}
if network != nil {
network.Cleanup()
}
os.RemoveAll(testDir)
})
It("executes chaincode in dev mode", func() {
chaincode = nwo.Chaincode{
Name: "mycc",
Version: "0.0",
Path: components.Build("github.com/hyperledger/fabric/integration/chaincode/simple/cmd"),
Lang: "binary",
PackageFile: filepath.Join(testDir, "simplecc.tar.gz"),
Ctor: `{"Args":["init","a","100","b","200"]}`,
SignaturePolicy: `OR ('Org1MSP.member')`,
Sequence: "1",
InitRequired: true,
Label: "my_prebuilt_chaincode",
}
org1peer0 := network.Peer("Org1", "peer0")
orderer := network.Orderer("orderer")
By("setting up the channel")
channelparticipation.JoinOrdererJoinPeersAppChannel(network, "testchannel", orderer, ordererRunner)
By("enabling V2_0 application capabilities")
nwo.EnableCapabilities(network, channelName, "Application", "V2_0", orderer, org1peer0)
By("running the chaincode")
chaincodeID := chaincode.Name + ":" + chaincode.Version
peerChaincodeAddress := network.PeerAddress(org1peer0, nwo.ChaincodePort)
envs := []string{
"CORE_PEER_TLS_ENABLED=false",
"CORE_PEER_ADDRESS=" + peerChaincodeAddress,
"CORE_CHAINCODE_ID_NAME=" + chaincodeID,
"DEVMODE_ENABLED=true",
}
cmd := exec.Command(chaincode.Path, "-peer.address", peerChaincodeAddress)
cmd.Env = append(cmd.Env, envs...)
chaincodeRunner = ginkgomon.New(ginkgomon.Config{
Name: "chaincode",
Command: cmd,
StartCheckTimeout: 15 * time.Second,
StartCheck: "starting up in devmode...",
})
chaincodeProcess = ifrit.Invoke(chaincodeRunner)
Eventually(chaincodeProcess.Ready(), network.EventuallyTimeout).Should(BeClosed())
By("approving chaincode for orgs")
ApproveChaincodeForMyOrg(network, channelName, orderer, chaincode, org1peer0)
By("committing the chaincode definition")
nwo.CheckCommitReadinessUntilReady(network, channelName, chaincode, network.PeerOrgs(), org1peer0)
nwo.CommitChaincode(network, channelName, orderer, chaincode, org1peer0, org1peer0)
By("initializing chaincode if required")
if chaincode.InitRequired {
nwo.InitChaincode(network, channelName, orderer, chaincode, org1peer0)
}
By("querying and invoking the chaincode")
RunQueryInvokeQuery(network, orderer, org1peer0, channelName, 100)
Eventually(chaincodeRunner.Buffer()).Should(gbytes.Say("invoking in devmode"))
By("killing chaincode process")
chaincodeProcess.Signal(syscall.SIGKILL)
Eventually(chaincodeProcess.Wait(), network.EventuallyTimeout).Should(Receive())
By("invoking chaincode after it has been killed, expecting it to fail")
sess, err := network.PeerUserSession(org1peer0, "User1", commands.ChaincodeInvoke{
ChannelID: channelName,
Orderer: network.OrdererAddress(orderer, nwo.ListenPort),
Name: "mycc",
Ctor: `{"Args":["invoke","a","b","10"]}`,
PeerAddresses: []string{
network.PeerAddress(network.Peer("Org1", "peer0"), nwo.ListenPort),
},
WaitForEvent: true,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, network.EventuallyTimeout).Should(gexec.Exit(1))
By("restarting the chaincode process")
cmd = exec.Command(chaincode.Path, []string{"-peer.address", peerChaincodeAddress}...)
cmd.Env = append(cmd.Env, envs...)
chaincodeRunner = ginkgomon.New(ginkgomon.Config{
Name: "chaincode",
Command: cmd,
StartCheckTimeout: 15 * time.Second,
StartCheck: "starting up in devmode...",
})
chaincodeProcess = ifrit.Invoke(chaincodeRunner)
Eventually(chaincodeProcess.Ready(), network.EventuallyTimeout).Should(BeClosed())
By("querying and invoking the chaincode")
RunQueryInvokeQuery(network, orderer, org1peer0, channelName, 90)
Eventually(chaincodeRunner).Should(gbytes.Say("invoking in devmode"))
})
})
func RunQueryInvokeQuery(n *nwo.Network, orderer *nwo.Orderer, peer *nwo.Peer, channel string, queryValue int) {
By("querying the chaincode")
sess, err := n.PeerUserSession(peer, "User1", commands.ChaincodeQuery{
ChannelID: channel,
Name: "mycc",
Ctor: `{"Args":["query","a"]}`,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
Expect(sess).To(gbytes.Say(strconv.Itoa(queryValue)))
By("invoking the chaincode")
sess, err = n.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
ChannelID: channel,
Orderer: n.OrdererAddress(orderer, nwo.ListenPort),
Name: "mycc",
Ctor: `{"Args":["invoke","a","b","10"]}`,
PeerAddresses: []string{
n.PeerAddress(peer, nwo.ListenPort),
},
WaitForEvent: true,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
By("querying the chaincode again")
sess, err = n.PeerUserSession(peer, "User1", commands.ChaincodeQuery{
ChannelID: channel,
Name: "mycc",
Ctor: `{"Args":["query","a"]}`,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
Expect(sess).To(gbytes.Say(strconv.Itoa(queryValue - 10)))
}
// ApproveChaincodeForMyOrg should only be used when devmode is enabled.
// It does not set PackageID form ChaincodeApproveForMyOrg command.
func ApproveChaincodeForMyOrg(n *nwo.Network, channel string, orderer *nwo.Orderer, chaincode nwo.Chaincode, peers ...*nwo.Peer) {
approvedOrgs := map[string]bool{}
for _, p := range peers {
if _, ok := approvedOrgs[p.Organization]; !ok {
sess, err := n.PeerAdminSession(p, commands.ChaincodeApproveForMyOrg{
ChannelID: channel,
Orderer: n.OrdererAddress(orderer, nwo.ListenPort),
Name: chaincode.Name,
Version: chaincode.Version,
Sequence: chaincode.Sequence,
EndorsementPlugin: chaincode.EndorsementPlugin,
ValidationPlugin: chaincode.ValidationPlugin,
SignaturePolicy: chaincode.SignaturePolicy,
ChannelConfigPolicy: chaincode.ChannelConfigPolicy,
InitRequired: chaincode.InitRequired,
CollectionsConfig: chaincode.CollectionsConfig,
ClientAuth: n.ClientAuthRequired,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
approvedOrgs[p.Organization] = true
Eventually(sess.Err, n.EventuallyTimeout).Should(gbytes.Say(`\Qcommitted with status (VALID)\E`))
}
}
}
var devModeEtcdraft = &nwo.Config{
Organizations: []*nwo.Organization{{
Name: "OrdererOrg",
MSPID: "OrdererMSP",
Domain: "example.com",
EnableNodeOUs: false,
Users: 0,
CA: &nwo.CA{Hostname: "ca"},
}, {
Name: "Org1",
MSPID: "Org1MSP",
Domain: "org1.example.com",
EnableNodeOUs: true,
Users: 2,
CA: &nwo.CA{Hostname: "ca"},
}},
Consensus: &nwo.Consensus{
Type: "etcdraft",
},
Orderers: []*nwo.Orderer{
{Name: "orderer", Organization: "OrdererOrg"},
},
Channels: []*nwo.Channel{
{
Name: "testchannel",
Profile: "OneOrgChannel",
},
},
Peers: []*nwo.Peer{{
Name: "peer0",
Organization: "Org1",
Channels: []*nwo.PeerChannel{
{Name: "testchannel", Anchor: true},
},
}},
Profiles: []*nwo.Profile{{
Name: "OneOrgChannel",
Consortium: "SampleConsortium",
Orderers: []string{"orderer"},
Organizations: []string{"Org1"},
}},
}