-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathcouchdb_indexes_test.go
275 lines (238 loc) · 10.2 KB
/
couchdb_indexes_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
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package ledger
import (
"encoding/json"
"os"
"path/filepath"
"syscall"
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/hyperledger/fabric/integration/nwo/fabricconfig"
"github.com/hyperledger/fabric/integration/nwo/runner"
. "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"
)
const (
chaincodePathWithNoIndex = "github.com/hyperledger/fabric/integration/chaincode/marbles/cmd"
chaincodePathWithIndex = "github.com/hyperledger/fabric/integration/chaincode/marbles/cmdwithindexspec"
chaincodePathWithIndexes = "github.com/hyperledger/fabric/integration/chaincode/marbles/cmdwithindexspecs"
)
var (
filesWithIndex = map[string]string{
"../chaincode/marbles/cmdwithindexspec/META-INF/statedb/couchdb/indexes/indexSizeSortDoc.json": "metadata/statedb/couchdb/indexes/indexSizeSortDoc.json",
}
filesWithIndices = map[string]string{
"../chaincode/marbles/cmdwithindexspecs/META-INF/statedb/couchdb/indexes/indexSizeSortDoc.json": "metadata/statedb/couchdb/indexes/indexSizeSortDoc.json",
"../chaincode/marbles/cmdwithindexspecs/META-INF/statedb/couchdb/indexes/indexColorSortDoc.json": "metadata/statedb/couchdb/indexes/indexColorSortDoc.json",
}
)
var _ = Describe("CouchDB indexes", func() {
var (
testDir string
client *docker.Client
network *nwo.Network
orderer *nwo.Orderer
ordererRunner *ginkgomon.Runner
ordererProcess, peerProcess ifrit.Process
couchAddr string
couchDB *runner.CouchDB
couchProcess ifrit.Process
chaincode nwo.Chaincode
)
BeforeEach(func() {
var err error
testDir, err = os.MkdirTemp("", "ledger")
Expect(err).NotTo(HaveOccurred())
client, err = docker.NewClientFromEnv()
Expect(err).NotTo(HaveOccurred())
network = nwo.New(nwo.FullEtcdRaft(), testDir, client, StartPort(), components)
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
network.ExternalBuilders = append(network.ExternalBuilders, fabricconfig.ExternalBuilder{
Path: filepath.Join(cwd, "..", "externalbuilders", "golang"),
Name: "external-golang",
PropagateEnvironment: []string{"GOPATH", "GOCACHE", "GOPROXY", "HOME", "PATH"},
})
network.GenerateConfigTree()
// configure only one of four peers (Org1, peer0) to use couchdb.
// Note that we do not support a channel with mixed DBs.
// However, for testing, it would be fine to use couchdb for one
// peer and sending all the couchdb related test queries to this peer
couchDB = &runner.CouchDB{}
couchProcess = ifrit.Invoke(couchDB)
Eventually(couchProcess.Ready(), runner.DefaultStartTimeout).Should(BeClosed())
Consistently(couchProcess.Wait()).ShouldNot(Receive())
couchAddr = couchDB.Address()
peer := network.Peer("Org1", "peer0")
core := network.ReadPeerConfig(peer)
core.Ledger.State.StateDatabase = "CouchDB"
core.Ledger.State.CouchDBConfig.CouchDBAddress = couchAddr
network.WritePeerConfig(peer, core)
// start the network
network.Bootstrap()
// Start all the fabric processes
ordererRunner, ordererProcess, peerProcess = network.StartSingleOrdererNetwork("orderer")
By("setting up the channel")
orderer = network.Orderer("orderer")
channelparticipation.JoinOrdererJoinPeersAppChannel(network, "testchannel", orderer, ordererRunner)
network.VerifyMembership(network.PeersWithChannel("testchannel"), "testchannel")
chaincode = nwo.Chaincode{
Name: "marbles",
Version: "0.0",
Path: components.Build(chaincodePathWithIndex),
Lang: "binary",
CodeFiles: filesWithIndex,
PackageFile: filepath.Join(testDir, "marbles.tar.gz"),
SignaturePolicy: `OR ('Org1MSP.member','Org2MSP.member')`,
Sequence: "1",
Label: "marbles",
}
})
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())
}
couchProcess.Signal(syscall.SIGTERM)
Eventually(couchProcess.Wait(), network.EventuallyTimeout).Should(Receive())
network.Cleanup()
os.RemoveAll(testDir)
})
When("chaincode is deployed via new lifecycle (using the docker chaincode build) ", func() {
BeforeEach(func() {
chaincode.Path = chaincodePathWithIndex
chaincode.Lang = "golang"
})
It("creates indexes", func() {
nwo.EnableCapabilities(network, "testchannel", "Application", "V2_0", orderer, network.Peer("Org1", "peer0"), network.Peer("Org2", "peer0"))
nwo.DeployChaincode(network, "testchannel", orderer, chaincode, network.Peers...)
initMarble(network, "testchannel", orderer, network.Peer("Org1", "peer0"), "marbles", "marble_indexed")
verifySizeIndexExists(network, "testchannel", orderer, network.Peer("Org1", "peer0"), "marbles")
})
})
When("chaincode is defined and installed via new lifecycle and then upgraded with an additional index", func() {
It("creates indexes", func() {
nwo.EnableCapabilities(network, "testchannel", "Application", "V2_0", orderer, network.Peer("Org1", "peer0"), network.Peer("Org2", "peer0"))
nwo.DeployChaincode(network, "testchannel", orderer, chaincode, network.Peers...)
initMarble(network, "testchannel", orderer, network.Peer("Org1", "peer0"), "marbles", "marble_indexed")
verifySizeIndexExists(network, "testchannel", orderer, network.Peer("Org1", "peer0"), "marbles")
verifyColorIndexDoesNotExist(network, "testchannel", orderer, network.Peer("Org1", "peer0"), "marbles")
By("upgrading the chaincode to include an additional index")
chaincode.Sequence = "2"
chaincode.CodeFiles = filesWithIndices
chaincode.PackageFile = filepath.Join(testDir, "marbles-two-indexes.tar.gz")
chaincode.Label = "marbles-two-indexes"
nwo.PackageChaincodeBinary(chaincode)
nwo.InstallChaincode(network, chaincode, network.Peers...)
nwo.ApproveChaincodeForMyOrg(network, "testchannel", orderer, chaincode, network.Peers...)
nwo.CheckCommitReadinessUntilReady(network, "testchannel", chaincode, network.PeerOrgs(), network.Peers...)
nwo.CommitChaincode(network, "testchannel", orderer, chaincode, network.Peers[0], network.Peers...)
verifySizeIndexExists(network, "testchannel", orderer, network.Peer("Org1", "peer0"), "marbles")
verifyColorIndexExists(network, "testchannel", orderer, network.Peer("Org1", "peer0"), "marbles")
})
})
})
func initMarble(n *nwo.Network, channel string, orderer *nwo.Orderer, peer *nwo.Peer, ccName, marbleName string) {
By("invoking initMarble function of the chaincode")
sess, err := n.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
ChannelID: channel,
Orderer: n.OrdererAddress(orderer, nwo.ListenPort),
Name: ccName,
Ctor: prepareChaincodeInvokeArgs("initMarble", marbleName, "blue", "35", "tom"),
PeerAddresses: []string{
n.PeerAddress(peer, nwo.ListenPort),
},
WaitForEvent: true,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit())
Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful."))
}
func prepareChaincodeInvokeArgs(args ...string) string {
m, err := json.Marshal(map[string][]string{
"Args": args,
})
Expect(err).NotTo(HaveOccurred())
return string(m)
}
func verifySizeIndexExists(n *nwo.Network, channel string, orderer *nwo.Orderer, peer *nwo.Peer, ccName string) {
verifySizeIndexPresence(n, channel, orderer, peer, ccName, true)
}
func verifySizeIndexDoesNotExist(n *nwo.Network, channel string, orderer *nwo.Orderer, peer *nwo.Peer, ccName string) {
verifySizeIndexPresence(n, channel, orderer, peer, ccName, false)
}
func verifySizeIndexPresence(n *nwo.Network, channel string, orderer *nwo.Orderer, peer *nwo.Peer, ccName string, expectIndexPresent bool) {
query := `{
"selector":{
"docType":{
"$eq":"marble"
},
"owner":{
"$eq":"tom"
},
"size":{
"$gt":0
}
},
"fields":["docType","owner","size"],
"sort":[{"size":"desc"}],
"use_index":"_design/indexSizeSortDoc"
}`
verifyIndexPresence(n, channel, orderer, peer, ccName, expectIndexPresent, query)
}
func verifyColorIndexExists(n *nwo.Network, channel string, orderer *nwo.Orderer, peer *nwo.Peer, ccName string) {
verifyColorIndexPresence(n, channel, orderer, peer, ccName, true)
}
func verifyColorIndexDoesNotExist(n *nwo.Network, channel string, orderer *nwo.Orderer, peer *nwo.Peer, ccName string) {
verifyColorIndexPresence(n, channel, orderer, peer, ccName, false)
}
func verifyColorIndexPresence(n *nwo.Network, channel string, orderer *nwo.Orderer, peer *nwo.Peer, ccName string, expectIndexPresent bool) {
query := `{
"selector":{
"docType":{
"$eq":"marble"
},
"owner":{
"$eq":"tom"
},
"color":{
"$eq":"blue"
}
},
"fields":["docType","owner","size"],
"sort":[{"color":"desc"}],
"use_index":"_design/indexColorSortDoc"
}`
verifyIndexPresence(n, channel, orderer, peer, ccName, expectIndexPresent, query)
}
func verifyIndexPresence(n *nwo.Network, channel string, orderer *nwo.Orderer, peer *nwo.Peer, ccName string, expectIndexPresent bool, indexQuery string) {
By("invoking queryMarbles function with a user constructed query that requires an index due to a sort")
sess, err := n.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
ChannelID: channel,
Name: ccName,
Ctor: prepareChaincodeInvokeArgs("queryMarbles", indexQuery),
Orderer: n.OrdererAddress(orderer, nwo.ListenPort),
PeerAddresses: []string{n.PeerAddress(peer, nwo.ListenPort)},
})
Expect(err).NotTo(HaveOccurred())
if expectIndexPresent {
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful."))
} else {
Eventually(sess, n.EventuallyTimeout).Should(gexec.Exit())
Expect(sess.Err).To(gbytes.Say("Error:no_usable_index"))
}
}