Skip to content

Commit b7a57a9

Browse files
author
Brad Gorman
committed
Adding examples to make unit-test
I noticed a lot of the examples no longer compiled after the recent changes to the chaincode Function and Args. To counter this issue in future I would like to bring the examples into the normal unit testing run. I've excluded specific examples as they have various issues: chaintool has issues with the GOPATH not containing its dynamically built files. go/asset_management* tests panic on some RocksDB calls. go/rbac_tcerts_no_attrs has some sort of missing config. go/utxo relies on bitcoin source. Change-Id: Ibfca3f6161ec66574eea117b6c1da4db95d15214 Signed-off-by: Bradley Gorman <[email protected]>
1 parent 2552dd0 commit b7a57a9

File tree

6 files changed

+37
-35
lines changed

6 files changed

+37
-35
lines changed

examples/chaincode/go/asset_management/app/app_internal.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func deployInternal(deployer crypto.Client, adminCert crypto.CertificateHandler)
143143
Type: 1,
144144
ChaincodeID: &pb.ChaincodeID{Path: "github.com/hyperledger/fabric/examples/chaincode/go/asset_management"},
145145
//ChaincodeID: &pb.ChaincodeID{Name: chaincodeName},
146-
CtorMsg: &pb.ChaincodeInput{Function: "init", Args: []string{}},
146+
CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("init")},
147147
Metadata: adminCert.GetCertificate(),
148148
ConfidentialityLevel: confidentialityLevel,
149149
}
@@ -187,8 +187,7 @@ func assignOwnershipInternal(invoker crypto.Client, invokerCert crypto.Certifica
187187
}
188188

189189
chaincodeInput := &pb.ChaincodeInput{
190-
Function: "assign",
191-
Args: []string{asset, base64.StdEncoding.EncodeToString(newOwnerCert.GetCertificate())},
190+
Args: util.ToChaincodeArgs("assign", asset, base64.StdEncoding.EncodeToString(newOwnerCert.GetCertificate())),
192191
}
193192
chaincodeInputRaw, err := proto.Marshal(chaincodeInput)
194193
if err != nil {
@@ -239,8 +238,7 @@ func transferOwnershipInternal(owner crypto.Client, ownerCert crypto.Certificate
239238
}
240239

241240
chaincodeInput := &pb.ChaincodeInput{
242-
Function: "transfer",
243-
Args: []string{asset, base64.StdEncoding.EncodeToString(newOwnerCert.GetCertificate())},
241+
Args: util.ToChaincodeArgs("transfer", asset, base64.StdEncoding.EncodeToString(newOwnerCert.GetCertificate())),
244242
}
245243
chaincodeInputRaw, err := proto.Marshal(chaincodeInput)
246244
if err != nil {
@@ -275,7 +273,7 @@ func transferOwnershipInternal(owner crypto.Client, ownerCert crypto.Certificate
275273
}
276274

277275
func whoIsTheOwner(invoker crypto.Client, asset string) (transaction *pb.Transaction, resp *pb.Response, err error) {
278-
chaincodeInput := &pb.ChaincodeInput{Function: "query", Args: []string{asset}}
276+
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs("query", asset)}
279277

280278
// Prepare spec and submit
281279
spec := &pb.ChaincodeSpec{

examples/chaincode/go/asset_management02/asset_management02_test.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"testing"
2525
"time"
2626

27-
"io/ioutil"
2827
"os"
2928
"path/filepath"
3029

@@ -34,6 +33,7 @@ import (
3433
"github.com/hyperledger/fabric/core/container"
3534
"github.com/hyperledger/fabric/core/crypto"
3635
"github.com/hyperledger/fabric/core/ledger"
36+
"github.com/hyperledger/fabric/core/util"
3737
"github.com/hyperledger/fabric/membersrvc/ca"
3838
pb "github.com/hyperledger/fabric/protos"
3939
"github.com/op/go-logging"
@@ -330,7 +330,7 @@ func deploy(admCert crypto.CertificateHandler) error {
330330
spec := &pb.ChaincodeSpec{
331331
Type: 1,
332332
ChaincodeID: &pb.ChaincodeID{Name: "mycc"},
333-
CtorMsg: &pb.ChaincodeInput{Function: "init", Args: []string{}},
333+
CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("init")},
334334
Metadata: []byte("issuer"),
335335
ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC,
336336
}
@@ -373,7 +373,7 @@ func assignOwnership(assigner crypto.Client, newOwnerCert crypto.CertificateHand
373373
return err
374374
}
375375

376-
chaincodeInput := &pb.ChaincodeInput{Function: "assignOwnership", Args: []string{base64.StdEncoding.EncodeToString(newOwnerCert.GetCertificate()), attributeName, amount}}
376+
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs("assignOwnership", base64.StdEncoding.EncodeToString(newOwnerCert.GetCertificate()), attributeName, amount)}
377377

378378
// Prepare spec and submit
379379
spec := &pb.ChaincodeSpec{
@@ -419,12 +419,13 @@ func transferOwnership(owner crypto.Client, ownerCert crypto.CertificateHandler,
419419
return err
420420
}
421421

422-
args := []string{base64.StdEncoding.EncodeToString(ownerCert.GetCertificate()),
422+
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs(
423+
"transferOwnership",
424+
base64.StdEncoding.EncodeToString(ownerCert.GetCertificate()),
423425
fromAttributes,
424426
base64.StdEncoding.EncodeToString(newOwnerCert.GetCertificate()),
425427
toAttributes,
426-
amount}
427-
chaincodeInput := &pb.ChaincodeInput{Function: "transferOwnership", Args: args}
428+
amount)}
428429

429430
// Prepare spec and submit
430431
spec := &pb.ChaincodeSpec{
@@ -466,7 +467,7 @@ func getBalance(accountID string) ([]byte, error) {
466467
}
467468

468469
func Query(function, accountID string) ([]byte, error) {
469-
chaincodeInput := &pb.ChaincodeInput{Function: function, Args: []string{accountID}}
470+
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs(function, accountID)}
470471

471472
// Prepare spec and submit
472473
spec := &pb.ChaincodeSpec{
@@ -526,7 +527,8 @@ func setup() {
526527
}
527528

528529
func initMembershipSrvc() {
529-
ca.LogInit(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr, os.Stdout)
530+
// ca.LogInit seems to have been removed
531+
//ca.LogInit(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr, os.Stdout)
530532
ca.CacheConfiguration() // Cache configuration
531533
aca = ca.NewACA()
532534
eca = ca.NewECA()

examples/chaincode/go/asset_management_with_roles/asset_management_with_roles_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"time"
2424

2525
"encoding/base64"
26-
"io/ioutil"
2726
"os"
2827
"path/filepath"
2928

@@ -33,6 +32,7 @@ import (
3332
"github.com/hyperledger/fabric/core/container"
3433
"github.com/hyperledger/fabric/core/crypto"
3534
"github.com/hyperledger/fabric/core/ledger"
35+
"github.com/hyperledger/fabric/core/util"
3636
"github.com/hyperledger/fabric/membersrvc/ca"
3737
pb "github.com/hyperledger/fabric/protos"
3838
"github.com/op/go-logging"
@@ -180,7 +180,7 @@ func deploy(admCert crypto.CertificateHandler) error {
180180
spec := &pb.ChaincodeSpec{
181181
Type: 1,
182182
ChaincodeID: &pb.ChaincodeID{Name: "mycc"},
183-
CtorMsg: &pb.ChaincodeInput{Function: "init", Args: []string{}},
183+
CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("init")},
184184
Metadata: []byte("assigner"),
185185
ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC,
186186
}
@@ -224,7 +224,7 @@ func assignOwnership(assigner crypto.Client, asset string, newOwnerCert crypto.C
224224
}
225225

226226
newOwner := base64.StdEncoding.EncodeToString(newOwnerCert.GetCertificate())
227-
chaincodeInput := &pb.ChaincodeInput{Function: "assign", Args: []string{asset, newOwner}}
227+
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs("assign", asset, newOwner)}
228228

229229
// Prepare spec and submit
230230
spec := &pb.ChaincodeSpec{
@@ -270,7 +270,7 @@ func transferOwnership(owner crypto.Client, ownerCert crypto.CertificateHandler,
270270
}
271271

272272
newOwner := base64.StdEncoding.EncodeToString(newOwnerCert.GetCertificate())
273-
chaincodeInput := &pb.ChaincodeInput{Function: "transfer", Args: []string{asset, newOwner}}
273+
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs("transfer", asset, newOwner)}
274274

275275
// Prepare spec and submit
276276
spec := &pb.ChaincodeSpec{
@@ -304,7 +304,7 @@ func transferOwnership(owner crypto.Client, ownerCert crypto.CertificateHandler,
304304
}
305305

306306
func whoIsTheOwner(asset string) ([]byte, error) {
307-
chaincodeInput := &pb.ChaincodeInput{Function: "query", Args: []string{asset}}
307+
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs("query", asset)}
308308

309309
// Prepare spec and submit
310310
spec := &pb.ChaincodeSpec{
@@ -364,7 +364,6 @@ func setup() {
364364
}
365365

366366
func initMembershipSrvc() {
367-
ca.LogInit(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr, os.Stdout)
368367
ca.CacheConfiguration() // Cache configuration
369368
aca = ca.NewACA()
370369
eca = ca.NewECA()

examples/chaincode/go/chaincode_example05/chaincode_example05_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/hyperledger/fabric/core/chaincode/shim"
2323
ex02 "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
24-
main "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example05"
2524
)
2625

2726
// chaincode_example02's hash is used here and must be updated if the example is changed
@@ -78,7 +77,7 @@ func checkInvoke(t *testing.T, stub *shim.MockStub, args []string) {
7877
}
7978

8079
func TestExample04_Init(t *testing.T) {
81-
scc := new(main.SimpleChaincode)
80+
scc := new(SimpleChaincode)
8281
stub := shim.NewMockStub("ex05", scc)
8382

8483
// Init A=123 B=234
@@ -88,7 +87,7 @@ func TestExample04_Init(t *testing.T) {
8887
}
8988

9089
func TestExample04_Query(t *testing.T) {
91-
scc := new(main.SimpleChaincode)
90+
scc := new(SimpleChaincode)
9291
stub := shim.NewMockStub("ex05", scc)
9392

9493
ccEx2 := new(ex02.SimpleChaincode)
@@ -103,7 +102,7 @@ func TestExample04_Query(t *testing.T) {
103102
}
104103

105104
func TestExample04_Invoke(t *testing.T) {
106-
scc := new(main.SimpleChaincode)
105+
scc := new(SimpleChaincode)
107106
stub := shim.NewMockStub("ex05", scc)
108107

109108
ccEx2 := new(ex02.SimpleChaincode)

examples/chaincode/go/rbac_tcerts_no_attrs/rbac_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"testing"
2626
"time"
2727

28-
"io/ioutil"
2928
"os"
3029
"path/filepath"
3130

@@ -35,6 +34,7 @@ import (
3534
"github.com/hyperledger/fabric/core/container"
3635
"github.com/hyperledger/fabric/core/crypto"
3736
"github.com/hyperledger/fabric/core/ledger"
37+
"github.com/hyperledger/fabric/core/util"
3838
"github.com/hyperledger/fabric/membersrvc/ca"
3939
pb "github.com/hyperledger/fabric/protos"
4040
"github.com/op/go-logging"
@@ -175,7 +175,7 @@ func deploy(admCert crypto.CertificateHandler) error {
175175
spec := &pb.ChaincodeSpec{
176176
Type: 1,
177177
ChaincodeID: &pb.ChaincodeID{Name: "mycc"},
178-
CtorMsg: &pb.ChaincodeInput{Function: "init", Args: []string{}},
178+
CtorMsg: &pb.ChaincodeInput{Args: util.ToChaincodeArgs("init")},
179179
Metadata: admCert.GetCertificate(),
180180
ConfidentialityLevel: pb.ConfidentialityLevel_PUBLIC,
181181
}
@@ -197,7 +197,7 @@ func deploy(admCert crypto.CertificateHandler) error {
197197

198198
ledger, err := ledger.GetLedger()
199199
ledger.BeginTxBatch("1")
200-
_, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
200+
_, _, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
201201
if err != nil {
202202
return fmt.Errorf("Error deploying chaincode: %s", err)
203203
}
@@ -222,7 +222,7 @@ func addRole(admCert crypto.CertificateHandler, idCert crypto.CertificateHandler
222222
return err
223223
}
224224

225-
chaincodeInput := &pb.ChaincodeInput{Function: "addRole", Args: []string{string(idCert.GetCertificate()), role}}
225+
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs("addRole", string(idCert.GetCertificate()), role)}
226226
chaincodeInputRaw, err := proto.Marshal(chaincodeInput)
227227
if err != nil {
228228
return err
@@ -263,7 +263,7 @@ func addRole(admCert crypto.CertificateHandler, idCert crypto.CertificateHandler
263263

264264
ledger, err := ledger.GetLedger()
265265
ledger.BeginTxBatch("1")
266-
_, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
266+
_, _, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
267267
if err != nil {
268268
return fmt.Errorf("Error deploying chaincode: %s", err)
269269
}
@@ -289,7 +289,7 @@ func write(invoker crypto.Client, invokerCert crypto.CertificateHandler, value [
289289
return err
290290
}
291291

292-
chaincodeInput := &pb.ChaincodeInput{Function: "write", Args: []string{string(value)}}
292+
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs("write", string(value))}
293293
chaincodeInputRaw, err := proto.Marshal(chaincodeInput)
294294
if err != nil {
295295
return err
@@ -330,7 +330,7 @@ func write(invoker crypto.Client, invokerCert crypto.CertificateHandler, value [
330330

331331
ledger, err := ledger.GetLedger()
332332
ledger.BeginTxBatch("1")
333-
_, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
333+
_, _, err = chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
334334
if err != nil {
335335
return fmt.Errorf("Error deploying chaincode: %s", err)
336336
}
@@ -357,7 +357,7 @@ func read(invoker crypto.Client, invokerCert crypto.CertificateHandler) ([]byte,
357357
return nil, err
358358
}
359359

360-
chaincodeInput := &pb.ChaincodeInput{Function: "read", Args: []string{}}
360+
chaincodeInput := &pb.ChaincodeInput{Args: util.ToChaincodeArgs("read")}
361361
chaincodeInputRaw, err := proto.Marshal(chaincodeInput)
362362
if err != nil {
363363
return nil, err
@@ -398,7 +398,7 @@ func read(invoker crypto.Client, invokerCert crypto.CertificateHandler) ([]byte,
398398

399399
ledger, err := ledger.GetLedger()
400400
ledger.BeginTxBatch("1")
401-
result, err := chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
401+
result, _, err := chaincode.Execute(ctx, chaincode.GetChain(chaincode.DefaultChain), transaction)
402402
if err != nil {
403403
return nil, fmt.Errorf("Error deploying chaincode: %s", err)
404404
}
@@ -440,7 +440,6 @@ func setup() {
440440
}
441441

442442
func initMemershipServices() {
443-
ca.LogInit(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr, os.Stdout)
444443
ca.CacheConfiguration() // Cache configuration
445444
eca = ca.NewECA()
446445
tca = ca.NewTCA(eca)

scripts/goUnitTests.sh

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ echo "Cleaning membership services folder"
1919
rm -rf membersrvc/ca/.ca/
2020

2121
echo -n "Obtaining list of tests to run.."
22-
PKGS=`go list github.com/hyperledger/fabric/... | grep -v /vendor/ | grep -v /examples/`
22+
# Some examples don't play nice with `go test`
23+
PKGS=`go list github.com/hyperledger/fabric/... | grep -v /vendor/ | \
24+
grep -v /examples/chaincode/chaintool/ | \
25+
grep -v /examples/chaincode/go/asset_management | \
26+
grep -v /examples/chaincode/go/utxo | \
27+
grep -v /examples/chaincode/go/rbac_tcerts_no_attrs`
2328
echo "DONE!"
2429

2530
echo -n "Starting peer.."

0 commit comments

Comments
 (0)