|
| 1 | +/* |
| 2 | +Copyright IBM Corp All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package ledger |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "path/filepath" |
| 12 | + |
| 13 | + . "github.com/onsi/ginkgo" |
| 14 | + |
| 15 | + "github.com/hyperledger/fabric/integration/nwo" |
| 16 | +) |
| 17 | + |
| 18 | +// This test generate sample ledger data that can be used to verify rebuild ledger function and upgrade function (in a future release). |
| 19 | +// It is skipped in general. To generate sample ledger data, comment out the line `xxx` and run this test in isolation. |
| 20 | +// It does not delete the network directory so that you can copy the generated data to a different directory for unit tests. |
| 21 | +// At the end of test, it prints `setup.testDir is <directory>`. Copy the network data under <directory> to |
| 22 | +// the unit test directory for rebuild tests as needed. |
| 23 | +// It generates the following blocks: |
| 24 | +// block 0: genesis |
| 25 | +// block 1 to 4: network setup |
| 26 | +// block 5 to 8: marblesp chaincode instantiation |
| 27 | +// block 9 to 12: marbles chancode instantiation |
| 28 | +// block 13: marblesp chaincode invocation |
| 29 | +// block 14 to 17: upgrade marblesp chaincode with a new collection config |
| 30 | +// block 18: marbles chaincode invocation |
| 31 | +var _ = Describe("sample ledger generation", func() { |
| 32 | + var ( |
| 33 | + setup *setup |
| 34 | + helper *testHelper |
| 35 | + chaincodemp nwo.Chaincode |
| 36 | + chaincodem nwo.Chaincode |
| 37 | + ) |
| 38 | + |
| 39 | + BeforeEach(func() { |
| 40 | + setup = initThreeOrgsSetup() |
| 41 | + nwo.EnableCapabilities(setup.network, setup.channelID, "Application", "V2_0", setup.orderer, setup.peers...) |
| 42 | + helper = &testHelper{ |
| 43 | + networkHelper: &networkHelper{ |
| 44 | + Network: setup.network, |
| 45 | + orderer: setup.orderer, |
| 46 | + peers: setup.peers, |
| 47 | + testDir: setup.testDir, |
| 48 | + channelID: setup.channelID, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + chaincodemp = nwo.Chaincode{ |
| 53 | + Name: "marblesp", |
| 54 | + Version: "1.0", |
| 55 | + Path: components.Build("github.com/hyperledger/fabric/integration/chaincode/marbles_private/cmd"), |
| 56 | + Lang: "binary", |
| 57 | + PackageFile: filepath.Join(setup.testDir, "marbles-pvtdata.tar.gz"), |
| 58 | + Label: "marbles-private-20", |
| 59 | + SignaturePolicy: `OR ('Org1MSP.member','Org2MSP.member', 'Org3MSP.member')`, |
| 60 | + CollectionsConfig: filepath.Join("testdata", "collection_configs", "collections_config1.json"), |
| 61 | + Sequence: "1", |
| 62 | + } |
| 63 | + |
| 64 | + chaincodem = nwo.Chaincode{ |
| 65 | + Name: "marbles", |
| 66 | + Version: "0.0", |
| 67 | + Path: "github.com/hyperledger/fabric/integration/chaincode/marbles/cmd", |
| 68 | + Lang: "golang", |
| 69 | + PackageFile: filepath.Join(setup.testDir, "marbles.tar.gz"), |
| 70 | + Label: "marbles", |
| 71 | + SignaturePolicy: `OR ('Org1MSP.member','Org2MSP.member', 'Org3MSP.member')`, |
| 72 | + Sequence: "1", |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + AfterEach(func() { |
| 77 | + setup.terminateAllProcess() |
| 78 | + setup.network.Cleanup() |
| 79 | + // do not delete testDir and log it so that we can copy the test data to unit tests for verification purpose |
| 80 | + fmt.Printf("The test dir is %s. Use peers/org2.peer0/filesystem/ledgersData as the sample ledger for kvledger rebuild tests\n", setup.testDir) |
| 81 | + }) |
| 82 | + |
| 83 | + It("creates marbles", func() { |
| 84 | + Skip("Uncomment to generate sample ledger in v2.0 with new lifecycle chaincode deployment") |
| 85 | + |
| 86 | + org2peer0 := setup.network.Peer("org2", "peer0") |
| 87 | + height := helper.getLedgerHeight(org2peer0) |
| 88 | + |
| 89 | + By(fmt.Sprintf("deploying marblesp chaincode at block height %d", height)) |
| 90 | + helper.deployChaincode(chaincodemp) |
| 91 | + |
| 92 | + height = helper.getLedgerHeight(org2peer0) |
| 93 | + By(fmt.Sprintf("deploying marbles chaincode at block height %d", height)) |
| 94 | + helper.deployChaincode(chaincodem) |
| 95 | + |
| 96 | + height = helper.getLedgerHeight(org2peer0) |
| 97 | + By(fmt.Sprintf("creating marbles1 with marblesp chaincode at block height %d", height)) |
| 98 | + helper.addMarble("marblesp", `{"name":"marble1", "color":"blue", "size":35, "owner":"tom", "price":99}`, org2peer0) |
| 99 | + helper.waitUntilEqualLedgerHeight(height + 1) |
| 100 | + |
| 101 | + By("verifying marble1 exist in collectionMarbles & collectionMarblePrivateDetails in peer0.org2") |
| 102 | + helper.assertPresentInCollectionM("marblesp", "marble1", org2peer0) |
| 103 | + helper.assertPresentInCollectionMPD("marblesp", "marble1", org2peer0) |
| 104 | + |
| 105 | + By(fmt.Sprintf("upgrading marblesp chaincode at block height %d", helper.getLedgerHeight(org2peer0))) |
| 106 | + chaincodemp.Version = "1.1" |
| 107 | + chaincodemp.CollectionsConfig = filepath.Join("testdata", "collection_configs", "collections_config2.json") |
| 108 | + chaincodemp.Sequence = "2" |
| 109 | + nwo.DeployChaincode(setup.network, setup.channelID, setup.orderer, chaincodemp) |
| 110 | + |
| 111 | + mhelper := &marblesTestHelper{ |
| 112 | + networkHelper: &networkHelper{ |
| 113 | + Network: setup.network, |
| 114 | + orderer: setup.orderer, |
| 115 | + peers: setup.peers, |
| 116 | + testDir: setup.testDir, |
| 117 | + channelID: setup.channelID, |
| 118 | + }, |
| 119 | + } |
| 120 | + By(fmt.Sprintf("creating marble100 with marbles chaincode at block height %d", helper.getLedgerHeight(org2peer0))) |
| 121 | + mhelper.invokeMarblesChaincode("marbles", org2peer0, "initMarble", "marble100", "blue", "35", "tom") |
| 122 | + By("transferring marble100 owner") |
| 123 | + mhelper.invokeMarblesChaincode("marbles", org2peer0, "transferMarble", "marble100", "jerry") |
| 124 | + |
| 125 | + By("verifying marble100 new owner after transfer by color") |
| 126 | + expectedResult := newMarble("marble100", "blue", 35, "jerry") |
| 127 | + mhelper.assertMarbleExists("marbles", org2peer0, expectedResult, "marble100") |
| 128 | + |
| 129 | + By("getting history for marble100") |
| 130 | + expectedHistoryResult := []*marbleHistoryResult{ |
| 131 | + {IsDelete: "false", Value: newMarble("marble100", "blue", 35, "jerry")}, |
| 132 | + {IsDelete: "false", Value: newMarble("marble100", "blue", 35, "tom")}, |
| 133 | + } |
| 134 | + mhelper.assertGetHistoryForMarble("marbles", org2peer0, expectedHistoryResult, "marble100") |
| 135 | + }) |
| 136 | +}) |
0 commit comments