github.com/lzy4123/fabric@v2.1.1+incompatible/integration/ledger/ledger_generate_test.go (about)

     1  // +build generate
     2  
     3  /*
     4  Copyright IBM Corp All Rights Reserved.
     5  
     6  SPDX-License-Identifier: Apache-2.0
     7  */
     8  
     9  package ledger
    10  
    11  import (
    12  	"fmt"
    13  	"path/filepath"
    14  
    15  	. "github.com/onsi/ginkgo"
    16  
    17  	"github.com/hyperledger/fabric/integration/nwo"
    18  )
    19  
    20  // This test generate sample ledger data that can be used to verify rebuild ledger function and upgrade function (in a future release).
    21  // It is skipped in general. To generate sample ledger data, use build tag 'generate' and run this test in isolation.
    22  // It does not delete the network directory so that you can copy the generated data to a different directory for unit tests.
    23  // At the end of test, it prints `setup.testDir is <directory>`. Copy the network data under <directory> to
    24  // the unit test directory for rebuild tests as needed.
    25  // It generates the following blocks:
    26  // block 0: genesis
    27  // block 1 to 4: network setup
    28  // block 5 to 8: marblesp chaincode instantiation
    29  // block 9 to 12: marbles chancode instantiation
    30  // block 13: marblesp chaincode invocation
    31  // block 14 to 17: upgrade marblesp chaincode with a new collection config
    32  // block 18: marbles chaincode invocation
    33  var _ = Describe("sample ledger generation", func() {
    34  	var (
    35  		setup       *setup
    36  		helper      *testHelper
    37  		chaincodemp nwo.Chaincode
    38  		chaincodem  nwo.Chaincode
    39  	)
    40  
    41  	BeforeEach(func() {
    42  		setup = initThreeOrgsSetup()
    43  		nwo.EnableCapabilities(setup.network, setup.channelID, "Application", "V2_0", setup.orderer, setup.peers...)
    44  		helper = &testHelper{
    45  			networkHelper: &networkHelper{
    46  				Network:   setup.network,
    47  				orderer:   setup.orderer,
    48  				peers:     setup.peers,
    49  				testDir:   setup.testDir,
    50  				channelID: setup.channelID,
    51  			},
    52  		}
    53  
    54  		chaincodemp = nwo.Chaincode{
    55  			Name:              "marblesp",
    56  			Version:           "1.0",
    57  			Path:              components.Build("github.com/hyperledger/fabric/integration/chaincode/marbles_private/cmd"),
    58  			Lang:              "binary",
    59  			PackageFile:       filepath.Join(setup.testDir, "marbles-pvtdata.tar.gz"),
    60  			Label:             "marbles-private-20",
    61  			SignaturePolicy:   `OR ('Org1MSP.member','Org2MSP.member', 'Org3MSP.member')`,
    62  			CollectionsConfig: filepath.Join("testdata", "collection_configs", "collections_config1.json"),
    63  			Sequence:          "1",
    64  		}
    65  
    66  		chaincodem = nwo.Chaincode{
    67  			Name:            "marbles",
    68  			Version:         "0.0",
    69  			Path:            "github.com/hyperledger/fabric/integration/chaincode/marbles/cmd",
    70  			Lang:            "golang",
    71  			PackageFile:     filepath.Join(setup.testDir, "marbles.tar.gz"),
    72  			Label:           "marbles",
    73  			SignaturePolicy: `OR ('Org1MSP.member','Org2MSP.member', 'Org3MSP.member')`,
    74  			Sequence:        "1",
    75  		}
    76  	})
    77  
    78  	AfterEach(func() {
    79  		setup.terminateAllProcess()
    80  		setup.network.Cleanup()
    81  		// do not delete testDir and log it so that we can copy the test data to unit tests for verification purpose
    82  		fmt.Fprintf(GinkgoWriter, "The test dir is %s. Use peers/org2.peer0/filesystem/ledgersData as the sample ledger for kvledger rebuild tests\n", setup.testDir)
    83  	})
    84  
    85  	It("creates marbles", func() {
    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  })