-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_test_setup.go
621 lines (533 loc) · 21.4 KB
/
base_test_setup.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package integration
import (
"fmt"
"os"
"path/filepath"
"testing"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/huijinchain/fabric-sdk-go/pkg/client/channel"
mspclient "github.com/huijinchain/fabric-sdk-go/pkg/client/msp"
"github.com/huijinchain/fabric-sdk-go/pkg/client/resmgmt"
"github.com/huijinchain/fabric-sdk-go/pkg/common/errors/retry"
"github.com/huijinchain/fabric-sdk-go/pkg/common/errors/status"
contextAPI "github.com/huijinchain/fabric-sdk-go/pkg/common/providers/context"
"github.com/huijinchain/fabric-sdk-go/pkg/common/providers/core"
fabAPI "github.com/huijinchain/fabric-sdk-go/pkg/common/providers/fab"
"github.com/huijinchain/fabric-sdk-go/pkg/common/providers/msp"
contextImpl "github.com/huijinchain/fabric-sdk-go/pkg/context"
"github.com/huijinchain/fabric-sdk-go/pkg/fab/resource"
"github.com/huijinchain/fabric-sdk-go/pkg/fabsdk"
"github.com/huijinchain/fabric-sdk-go/pkg/util/test"
"github.com/huijinchain/fabric-sdk-go/test/metadata"
"github.com/huijinchain/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/policydsl"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
// BaseSetupImpl implementation of BaseTestSetup
type BaseSetupImpl struct {
Identity msp.Identity
Targets []string
ConfigFile string
OrgID string
ChannelID string
ChannelConfigTxFile string
}
// Initial B values for ExampleCC
const (
ExampleCCInitB = "200"
ExampleCCUpgradeB = "400"
AdminUser = "Admin"
OrdererOrgName = "OrdererOrg"
keyExp = "key-%s-%s"
)
// ExampleCC query and transaction arguments
var defaultQueryArgs = [][]byte{[]byte("query"), []byte("b")}
var defaultTxArgs = [][]byte{[]byte("move"), []byte("a"), []byte("b"), []byte("1")}
// ExampleCC init and upgrade args
var initArgs = [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte(ExampleCCInitB)}
var upgradeArgs = [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte(ExampleCCUpgradeB)}
var resetArgs = [][]byte{[]byte("a"), []byte("100"), []byte("b"), []byte(ExampleCCInitB)}
// ExampleCCDefaultQueryArgs returns example cc query args
func ExampleCCDefaultQueryArgs() [][]byte {
return defaultQueryArgs
}
// ExampleCCQueryArgs returns example cc query args
func ExampleCCQueryArgs(key string) [][]byte {
return [][]byte{[]byte("query"), []byte(key)}
}
// ExampleCCTxArgs returns example cc query args
func ExampleCCTxArgs(from, to, val string) [][]byte {
return [][]byte{[]byte("move"), []byte(from), []byte(to), []byte(val)}
}
// ExampleCCDefaultTxArgs returns example cc move funds args
func ExampleCCDefaultTxArgs() [][]byte {
return defaultTxArgs
}
// ExampleCCTxRandomSetArgs returns example cc set args with random key-value pairs
func ExampleCCTxRandomSetArgs() [][]byte {
return [][]byte{[]byte("set"), []byte(GenerateRandomID()), []byte(GenerateRandomID())}
}
//ExampleCCTxSetArgs sets the given key value in examplecc
func ExampleCCTxSetArgs(key, value string) [][]byte {
return [][]byte{[]byte("set"), []byte(key), []byte(value)}
}
//ExampleCCInitArgs returns example cc initialization args
func ExampleCCInitArgs() [][]byte {
return initArgs
}
//ExampleCCInitArgsLc returns example cc initialization args
func ExampleCCInitArgsLc() [][]byte {
return initArgs[1:]
}
//ExampleCCUpgradeArgs returns example cc upgrade args
func ExampleCCUpgradeArgs() [][]byte {
return upgradeArgs
}
//ExampleCCUpgradeArgsLc returns example cc upgrade args
func ExampleCCUpgradeArgsLc() [][]byte {
return upgradeArgs[1:]
}
// IsJoinedChannel returns true if the given peer has joined the given channel
func IsJoinedChannel(channelID string, resMgmtClient *resmgmt.Client, peer fabAPI.Peer) (bool, error) {
resp, err := resMgmtClient.QueryChannels(resmgmt.WithTargets(peer))
if err != nil {
return false, err
}
for _, chInfo := range resp.Channels {
if chInfo.ChannelId == channelID {
return true, nil
}
}
return false, nil
}
// Initialize reads configuration from file and sets up client and channel
func (setup *BaseSetupImpl) Initialize(sdk *fabsdk.FabricSDK) error {
mspClient, err := mspclient.New(sdk.Context(), mspclient.WithOrg(setup.OrgID))
adminIdentity, err := mspClient.GetSigningIdentity(AdminUser)
if err != nil {
return errors.WithMessage(err, "failed to get client context")
}
setup.Identity = adminIdentity
var cfgBackends []core.ConfigBackend
configBackend, err := sdk.Config()
if err != nil {
//For some tests SDK may not have backend set, try with config file if backend is missing
cfgBackends, err = ConfigBackend()
if err != nil {
return errors.Wrapf(err, "failed to get config backend from config: %s", err)
}
} else {
cfgBackends = append(cfgBackends, configBackend)
}
targets, err := OrgTargetPeers([]string{setup.OrgID}, cfgBackends...)
if err != nil {
return errors.Wrapf(err, "loading target peers from config failed")
}
setup.Targets = targets
r, err := os.Open(setup.ChannelConfigTxFile)
if err != nil {
return errors.Wrapf(err, "opening channel config file failed")
}
defer func() {
if err = r.Close(); err != nil {
test.Logf("close error %v", err)
}
}()
// Create channel for tests
req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfig: r, SigningIdentities: []msp.SigningIdentity{adminIdentity}}
if err = InitializeChannel(sdk, setup.OrgID, req, targets); err != nil {
return errors.WithMessage(err, "failed to initialize channel")
}
return nil
}
// GetDeployPath returns the path to the chaincode fixtures
func GetDeployPath() string {
const ccPath = "test/fixtures/testdata/go"
return filepath.Join(metadata.GetProjectPath(), ccPath)
}
// GetLcDeployPath returns the path to the chaincode fixtures
func GetLcDeployPath() string {
const ccPath = "test/fixtures/testdata/go/src/github.com/example_cc"
return filepath.Join(metadata.GetProjectPath(), ccPath)
}
// GetLcPvtDeployPath returns the path to the chaincode fixtures
func GetLcPvtDeployPath() string {
const ccPath = "test/fixtures/testdata/go/src/github.com/example_pvt_cc"
return filepath.Join(metadata.GetProjectPath(), ccPath)
}
// GetJavaDeployPath returns the path to the java chaincode fixtrues
func GetJavaDeployPath() string {
const ccPath = "test/fixtures/testdata/java"
return filepath.Join(metadata.GetProjectPath(), ccPath)
}
// GetNodeDeployPath returns the path to the node chaincode fixtrues
func GetNodeDeployPath() string {
const ccPath = "test/fixtures/testdata/node"
return filepath.Join(metadata.GetProjectPath(), ccPath)
}
// GetChannelConfigTxPath returns the path to the named channel config file
func GetChannelConfigTxPath(filename string) string {
return filepath.Join(metadata.GetProjectPath(), metadata.ChannelConfigPath, filename)
}
// GetConfigPath returns the path to the named config fixture file
func GetConfigPath(filename string) string {
const configPath = "test/fixtures/config"
return filepath.Join(metadata.GetProjectPath(), configPath, filename)
}
// GetConfigOverridesPath returns the path to the named config override fixture file
func GetConfigOverridesPath(filename string) string {
const configPath = "test/fixtures/config"
return filepath.Join(metadata.GetProjectPath(), configPath, "overrides", filename)
}
// GetCryptoConfigPath returns the path to the named crypto-config override fixture file
func GetCryptoConfigPath(filename string) string {
const configPath = "test/fixtures/fabric/v1/crypto-config"
return filepath.Join(metadata.GetProjectPath(), configPath, filename)
}
// OrgContext provides SDK client context for a given org
type OrgContext struct {
OrgID string
CtxProvider contextAPI.ClientProvider
SigningIdentity msp.SigningIdentity
ResMgmt *resmgmt.Client
Peers []fabAPI.Peer
AnchorPeerConfigFile string
}
// CreateChannelAndUpdateAnchorPeers creates the channel and updates all of the anchor peers for all orgs
func CreateChannelAndUpdateAnchorPeers(t *testing.T, sdk *fabsdk.FabricSDK, channelID string, channelConfigFile string, orgsContext []*OrgContext) error {
ordererCtx := sdk.Context(fabsdk.WithUser(AdminUser), fabsdk.WithOrg(OrdererOrgName))
// Channel management client is responsible for managing channels (create/update channel)
chMgmtClient, err := resmgmt.New(ordererCtx)
if err != nil {
return errors.New("failed to get a new resmgmt client for orderer")
}
var lastConfigBlock uint64
var signingIdentities []msp.SigningIdentity
for _, orgCtx := range orgsContext {
signingIdentities = append(signingIdentities, orgCtx.SigningIdentity)
}
req := resmgmt.SaveChannelRequest{
ChannelID: channelID,
ChannelConfigPath: GetChannelConfigTxPath(channelConfigFile),
SigningIdentities: signingIdentities,
}
_, err = chMgmtClient.SaveChannel(req, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com"))
if err != nil {
return err
}
lastConfigBlock = WaitForOrdererConfigUpdate(t, orgsContext[0].ResMgmt, channelID, true, lastConfigBlock)
for _, orgCtx := range orgsContext {
req := resmgmt.SaveChannelRequest{
ChannelID: channelID,
ChannelConfigPath: GetChannelConfigTxPath(orgCtx.AnchorPeerConfigFile),
SigningIdentities: []msp.SigningIdentity{orgCtx.SigningIdentity},
}
if _, err := orgCtx.ResMgmt.SaveChannel(req, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint("orderer.example.com")); err != nil {
return err
}
lastConfigBlock = WaitForOrdererConfigUpdate(t, orgCtx.ResMgmt, channelID, false, lastConfigBlock)
}
return nil
}
// JoinPeersToChannel joins all peers in all of the given orgs to the given channel
func JoinPeersToChannel(channelID string, orgsContext []*OrgContext) error {
for _, orgCtx := range orgsContext {
err := orgCtx.ResMgmt.JoinChannel(
channelID,
resmgmt.WithRetry(retry.DefaultResMgmtOpts),
resmgmt.WithOrdererEndpoint("orderer.example.com"),
resmgmt.WithTargets(orgCtx.Peers...),
)
if err != nil {
return errors.Wrapf(err, "failed to join peers in org [%s] to channel [%s]", orgCtx.OrgID, channelID)
}
}
return nil
}
// InstallChaincodeWithOrgContexts installs the given chaincode to orgs
func InstallChaincodeWithOrgContexts(orgs []*OrgContext, ccPkg *resource.CCPackage, ccPath, ccID, ccVersion string) error {
for _, orgCtx := range orgs {
if err := InstallChaincode(orgCtx.ResMgmt, ccPkg, ccPath, ccID, ccVersion, orgCtx.Peers); err != nil {
return errors.Wrapf(err, "failed to install chaincode to peers in org [%s]", orgCtx.OrgID)
}
}
return nil
}
// InstallChaincode installs the given chaincode to the given peers
func InstallChaincode(resMgmt *resmgmt.Client, ccPkg *resource.CCPackage, ccPath, ccName, ccVersion string, localPeers []fabAPI.Peer) error {
installCCReq := resmgmt.InstallCCRequest{Name: ccName, Path: ccPath, Version: ccVersion, Package: ccPkg}
_, err := resMgmt.InstallCC(installCCReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts))
if err != nil {
return err
}
installed, err := queryInstalledCC(resMgmt, ccName, ccVersion, localPeers)
if err != nil {
return err
}
if !installed {
return errors.New("chaincode was not installed on all peers")
}
return nil
}
// InstantiateChaincode instantiates the given chaincode to the given channel
func InstantiateChaincode(resMgmt *resmgmt.Client, channelID, ccName, ccPath, ccVersion string, ccPolicyStr string, args [][]byte, collConfigs ...*pb.CollectionConfig) (resmgmt.InstantiateCCResponse, error) {
ccPolicy, err := policydsl.FromString(ccPolicyStr)
if err != nil {
return resmgmt.InstantiateCCResponse{}, errors.Wrapf(err, "error creating CC policy [%s]", ccPolicyStr)
}
return resMgmt.InstantiateCC(
channelID,
resmgmt.InstantiateCCRequest{
Name: ccName,
Path: ccPath,
Version: ccVersion,
Args: args,
Policy: ccPolicy,
CollConfig: collConfigs,
},
resmgmt.WithRetry(retry.DefaultResMgmtOpts),
)
}
// InstantiateJavaChaincode instantiates the given java chaincode to the given channel
func InstantiateJavaChaincode(resMgmt *resmgmt.Client, channelID, ccName, ccPath, ccVersion string, ccPolicyStr string, args [][]byte, collConfigs ...*pb.CollectionConfig) (resmgmt.InstantiateCCResponse, error) {
ccPolicy, err := policydsl.FromString(ccPolicyStr)
if err != nil {
return resmgmt.InstantiateCCResponse{}, errors.Wrapf(err, "error creating CC policy [%s]", ccPolicyStr)
}
return resMgmt.InstantiateCC(
channelID,
resmgmt.InstantiateCCRequest{
Name: ccName,
Path: ccPath,
Version: ccVersion,
Lang: pb.ChaincodeSpec_JAVA,
Args: args,
Policy: ccPolicy,
CollConfig: collConfigs,
},
resmgmt.WithRetry(retry.DefaultResMgmtOpts),
)
}
// InstantiateNodeChaincode instantiates the given node chaincode to the given channel
func InstantiateNodeChaincode(resMgmt *resmgmt.Client, channelID, ccName, ccPath, ccVersion string, ccPolicyStr string, args [][]byte, collConfigs ...*pb.CollectionConfig) (resmgmt.InstantiateCCResponse, error) {
ccPolicy, err := policydsl.FromString(ccPolicyStr)
if err != nil {
return resmgmt.InstantiateCCResponse{}, errors.Wrapf(err, "error creating CC policy [%s]", ccPolicyStr)
}
return resMgmt.InstantiateCC(
channelID,
resmgmt.InstantiateCCRequest{
Name: ccName,
Path: ccPath,
Version: ccVersion,
Lang: pb.ChaincodeSpec_NODE,
Args: args,
Policy: ccPolicy,
CollConfig: collConfigs,
},
resmgmt.WithRetry(retry.DefaultResMgmtOpts),
)
}
// UpgradeChaincode upgrades the given chaincode on the given channel
func UpgradeChaincode(resMgmt *resmgmt.Client, channelID, ccName, ccPath, ccVersion string, ccPolicyStr string, args [][]byte, collConfigs ...*pb.CollectionConfig) (resmgmt.UpgradeCCResponse, error) {
ccPolicy, err := policydsl.FromString(ccPolicyStr)
if err != nil {
return resmgmt.UpgradeCCResponse{}, errors.Wrapf(err, "error creating CC policy [%s]", ccPolicyStr)
}
return resMgmt.UpgradeCC(
channelID,
resmgmt.UpgradeCCRequest{
Name: ccName,
Path: ccPath,
Version: ccVersion,
Args: args,
Policy: ccPolicy,
CollConfig: collConfigs,
},
resmgmt.WithRetry(retry.DefaultResMgmtOpts),
)
}
// UpgradeJavaChaincode upgrades the given java chaincode on the given channel
func UpgradeJavaChaincode(resMgmt *resmgmt.Client, channelID, ccName, ccPath, ccVersion string, ccPolicyStr string, args [][]byte, collConfigs ...*pb.CollectionConfig) (resmgmt.UpgradeCCResponse, error) {
ccPolicy, err := policydsl.FromString(ccPolicyStr)
if err != nil {
return resmgmt.UpgradeCCResponse{}, errors.Wrapf(err, "error creating CC policy [%s]", ccPolicyStr)
}
return resMgmt.UpgradeCC(
channelID,
resmgmt.UpgradeCCRequest{
Name: ccName,
Path: ccPath,
Version: ccVersion,
Lang: pb.ChaincodeSpec_JAVA,
Args: args,
Policy: ccPolicy,
CollConfig: collConfigs,
},
resmgmt.WithRetry(retry.DefaultResMgmtOpts),
)
}
// UpgradeNodeChaincode upgrades the given node chaincode on the given channel
func UpgradeNodeChaincode(resMgmt *resmgmt.Client, channelID, ccName, ccPath, ccVersion string, ccPolicyStr string, args [][]byte, collConfigs ...*pb.CollectionConfig) (resmgmt.UpgradeCCResponse, error) {
ccPolicy, err := policydsl.FromString(ccPolicyStr)
if err != nil {
return resmgmt.UpgradeCCResponse{}, errors.Wrapf(err, "error creating CC policy [%s]", ccPolicyStr)
}
return resMgmt.UpgradeCC(
channelID,
resmgmt.UpgradeCCRequest{
Name: ccName,
Path: ccPath,
Version: ccVersion,
Lang: pb.ChaincodeSpec_NODE,
Args: args,
Policy: ccPolicy,
CollConfig: collConfigs,
},
resmgmt.WithRetry(retry.DefaultResMgmtOpts),
)
}
// DiscoverLocalPeers queries the local peers for the given MSP context and returns all of the peers. If
// the number of peers does not match the expected number then an error is returned.
func DiscoverLocalPeers(ctxProvider contextAPI.ClientProvider, expectedPeers int) ([]fabAPI.Peer, error) {
ctx, err := contextImpl.NewLocal(ctxProvider)
if err != nil {
return nil, errors.Wrap(err, "error creating local context")
}
discoveredPeers, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
peers, serviceErr := ctx.LocalDiscoveryService().GetPeers()
if serviceErr != nil {
return nil, errors.Wrapf(serviceErr, "error getting peers for MSP [%s]", ctx.Identifier().MSPID)
}
if len(peers) < expectedPeers {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("Expecting %d peers but got %d", expectedPeers, len(peers)), nil)
}
return peers, nil
},
)
if err != nil {
return nil, err
}
return discoveredPeers.([]fabAPI.Peer), nil
}
// EnsureChannelCreatedAndPeersJoined creates a channel, joins all peers in the given orgs to the channel and updates the anchor peers of each org.
func EnsureChannelCreatedAndPeersJoined(t *testing.T, sdk *fabsdk.FabricSDK, channelID string, channelTxFile string, orgsContext []*OrgContext) error {
joined, err := IsJoinedChannel(channelID, orgsContext[0].ResMgmt, orgsContext[0].Peers[0])
if err != nil {
return err
}
if joined {
return nil
}
// Create the channel and update anchor peers for all orgs
if err := CreateChannelAndUpdateAnchorPeers(t, sdk, channelID, channelTxFile, orgsContext); err != nil {
return err
}
return JoinPeersToChannel(channelID, orgsContext)
}
// WaitForOrdererConfigUpdate waits until the config block update has been committed.
// In Fabric 1.0 there is a bug that panics the orderer if more than one config update is added to the same block.
// This function may be invoked after each config update as a workaround.
func WaitForOrdererConfigUpdate(t *testing.T, client *resmgmt.Client, channelID string, genesis bool, lastConfigBlock uint64) uint64 {
blockNum, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
chConfig, err := client.QueryConfigFromOrderer(channelID, resmgmt.WithOrdererEndpoint("orderer.example.com"))
if err != nil {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), err.Error(), nil)
}
currentBlock := chConfig.BlockNumber()
if currentBlock <= lastConfigBlock && !genesis {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("Block number was not incremented [%d, %d]", currentBlock, lastConfigBlock), nil)
}
block, err := client.QueryConfigBlockFromOrderer(channelID, resmgmt.WithOrdererEndpoint("orderer.example.com"))
if err != nil {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), err.Error(), nil)
}
if block.Header.Number != currentBlock {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("Invalid block number [%d, %d]", block.Header.Number, currentBlock), nil)
}
return ¤tBlock, nil
},
)
require.NoError(t, err)
return *blockNum.(*uint64)
}
func queryInstalledCC(resMgmt *resmgmt.Client, ccName, ccVersion string, peers []fabAPI.Peer) (bool, error) {
installed, err := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
ok, err := isCCInstalled(resMgmt, ccName, ccVersion, peers)
if err != nil {
return &ok, err
}
if !ok {
return &ok, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("Chaincode [%s:%s] is not installed on all peers in Org1", ccName, ccVersion), nil)
}
return &ok, nil
},
)
if err != nil {
s, ok := status.FromError(err)
if ok && s.Code == status.GenericTransient.ToInt32() {
return false, nil
}
return false, errors.WithMessage(err, "isCCInstalled invocation failed")
}
return *(installed).(*bool), nil
}
func isCCInstalled(resMgmt *resmgmt.Client, ccName, ccVersion string, peers []fabAPI.Peer) (bool, error) {
installedOnAllPeers := true
for _, peer := range peers {
resp, err := resMgmt.QueryInstalledChaincodes(resmgmt.WithTargets(peer))
if err != nil {
return false, errors.WithMessage(err, "querying for installed chaincodes failed")
}
found := false
for _, ccInfo := range resp.Chaincodes {
if ccInfo.Name == ccName && ccInfo.Version == ccVersion {
found = true
break
}
}
if !found {
installedOnAllPeers = false
}
}
return installedOnAllPeers, nil
}
//GetKeyName creates random key name based on test name
func GetKeyName(t *testing.T) string {
return fmt.Sprintf(keyExp, t.Name(), GenerateRandomID())
}
//ResetKeys resets given set of keys in example cc to given value
func ResetKeys(t *testing.T, ctx contextAPI.ChannelProvider, chaincodeID, value string, keys ...string) {
chClient, err := channel.New(ctx)
require.NoError(t, err, "Failed to create new channel client for resetting keys")
for _, key := range keys {
// Synchronous transaction
req := channel.Request{
ChaincodeID: chaincodeID,
Fcn: "invoke",
Args: ExampleCCTxSetArgs(key, value),
}
e := invoke(chClient, req)
require.NoError(t, e, "Failed to reset keys")
}
}
func invoke(chClient *channel.Client, req channel.Request) error {
// Synchronous transaction
_, e := retry.NewInvoker(retry.New(retry.TestRetryOpts)).Invoke(
func() (interface{}, error) {
re, e := chClient.Execute(req)
if e != nil {
return nil, status.New(status.TestStatus, status.GenericTransient.ToInt32(), fmt.Sprintf("Execute returned error: %v", e), nil)
}
return re, e
},
)
return e
}