diff --git a/core/ledger/kvledger/tests/customtx_processor_test.go b/core/ledger/kvledger/tests/customtx_processor_test.go index c03ea4bd8c8..b7c58f64c1e 100644 --- a/core/ledger/kvledger/tests/customtx_processor_test.go +++ b/core/ledger/kvledger/tests/customtx_processor_test.go @@ -37,7 +37,7 @@ func TestReadWriteCustomTxProcessor(t *testing.T) { defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") l.simulateDataTx("tx0", func(s *simulator) { s.setState("ns", "key1", "value1") s.setState("ns", "key2", "value2") @@ -90,7 +90,7 @@ func TestRangeReadAndWriteCustomTxProcessor(t *testing.T) { defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") l.simulateDataTx("tx0", func(s *simulator) { s.setState("ns", "key1", "value1") s.setState("ns", "key2", "value2") diff --git a/core/ledger/kvledger/tests/env.go b/core/ledger/kvledger/tests/env.go index e7bdf14c0cb..89407ac727b 100644 --- a/core/ledger/kvledger/tests/env.go +++ b/core/ledger/kvledger/tests/env.go @@ -45,7 +45,7 @@ const ( ) type env struct { - assert *require.Assertions + t *testing.T initializer *ledgermgmt.Initializer ledgerMgr *ledgermgmt.LedgerMgr } @@ -65,7 +65,7 @@ func newEnvWithInitializer(t *testing.T, initializer *ledgermgmt.Initializer) *e populateMissingsWithTestDefaults(t, initializer) return &env{ - assert: require.New(t), + t: t, initializer: initializer, } } @@ -90,35 +90,35 @@ func (e *env) closeAllLedgersAndRemoveDirContents(flags rebuildable) { indexPath := e.getBlockIndexDBPath() logger.Infof("Deleting blockstore indexdb path [%s]", indexPath) e.verifyNonEmptyDirExists(indexPath) - e.assert.NoError(fileutil.RemoveContents(indexPath)) + require.NoError(e.t, fileutil.RemoveContents(indexPath)) } if flags&rebuildableStatedb == rebuildableStatedb { statedbPath := e.getLevelstateDBPath() logger.Infof("Deleting statedb path [%s]", statedbPath) e.verifyNonEmptyDirExists(statedbPath) - e.assert.NoError(fileutil.RemoveContents(statedbPath)) + require.NoError(e.t, fileutil.RemoveContents(statedbPath)) } if flags&rebuildableConfigHistory == rebuildableConfigHistory { configHistoryPath := e.getConfigHistoryDBPath() logger.Infof("Deleting configHistory db path [%s]", configHistoryPath) e.verifyNonEmptyDirExists(configHistoryPath) - e.assert.NoError(fileutil.RemoveContents(configHistoryPath)) + require.NoError(e.t, fileutil.RemoveContents(configHistoryPath)) } if flags&rebuildableBookkeeper == rebuildableBookkeeper { bookkeeperPath := e.getBookkeeperDBPath() logger.Infof("Deleting bookkeeper db path [%s]", bookkeeperPath) e.verifyNonEmptyDirExists(bookkeeperPath) - e.assert.NoError(fileutil.RemoveContents(bookkeeperPath)) + require.NoError(e.t, fileutil.RemoveContents(bookkeeperPath)) } if flags&rebuildableHistoryDB == rebuildableHistoryDB { historyPath := e.getHistoryDBPath() logger.Infof("Deleting history db path [%s]", historyPath) e.verifyNonEmptyDirExists(historyPath) - e.assert.NoError(fileutil.RemoveContents(historyPath)) + require.NoError(e.t, fileutil.RemoveContents(historyPath)) } e.verifyRebuilableDirEmpty(flags) @@ -162,14 +162,14 @@ func (e *env) verifyRebuilableDirEmpty(flags rebuildable) { func (e *env) verifyNonEmptyDirExists(path string) { empty, err := fileutil.DirEmpty(path) - e.assert.NoError(err) - e.assert.False(empty) + require.NoError(e.t, err) + require.False(e.t, empty) } func (e *env) verifyDirEmpty(path string) { empty, err := fileutil.DirEmpty(path) - e.assert.NoError(err) - e.assert.True(empty) + require.NoError(e.t, err) + require.True(e.t, empty) } func (e *env) initLedgerMgmt() { diff --git a/core/ledger/kvledger/tests/ledger_test.go b/core/ledger/kvledger/tests/ledger_test.go index aa5de41a854..9970699121f 100644 --- a/core/ledger/kvledger/tests/ledger_test.go +++ b/core/ledger/kvledger/tests/ledger_test.go @@ -29,8 +29,8 @@ func TestLedgerAPIs(t *testing.T) { env.initLedgerMgmt() // create two ledgers - testLedger1 := env.createTestLedger("ledger1", t) - testLedger2 := env.createTestLedger("ledger2", t) + testLedger1 := env.createTestLedger("ledger1") + testLedger2 := env.createTestLedger("ledger2") // populate ledgers with sample data dataHelper := newSampleDataHelper(t) diff --git a/core/ledger/kvledger/tests/missing_pvtdata_retrieval_test.go b/core/ledger/kvledger/tests/missing_pvtdata_retrieval_test.go index 11198620060..54102368c85 100644 --- a/core/ledger/kvledger/tests/missing_pvtdata_retrieval_test.go +++ b/core/ledger/kvledger/tests/missing_pvtdata_retrieval_test.go @@ -48,11 +48,17 @@ func TestGetMissingPvtData(t *testing.T) { blk2 := l.cutBlockAndCommitLegacy() l.verifyPvtState("cc1", "coll1", "key2", "value2") // key2 should have been committed + l.simulateDataTx("", func(s *simulator) { - l.assertError(s.GetPrivateData("cc1", "coll1", "key1")) // key1 would be stale with respect to hashed version + // key1 would be stale with respect to hashed version + _, err := s.GetPrivateData("cc1", "coll1", "key1") + require.EqualError(t, err, "private data matching public hash version is not available. Public hash version = {BlockNum: 2, TxNum: 0}, Private data version = ") }) + l.simulateDataTx("", func(s *simulator) { - l.assertError(s.GetPrivateData("cc1", "coll1", "key3")) // key3 would be stale with respect to hashed version + // key3 would be stale with respect to hashed version + _, err := s.GetPrivateData("cc1", "coll1", "key3") + require.EqualError(t, err, "private data matching public hash version is not available. Public hash version = {BlockNum: 2, TxNum: 2}, Private data version = ") }) // verify missing pvtdata info @@ -69,7 +75,7 @@ func TestGetMissingPvtData(t *testing.T) { env := newEnv(t) defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") blk, expectedMissingPvtDataInfo := setup(l) @@ -95,7 +101,7 @@ func TestGetMissingPvtData(t *testing.T) { require.NoError(t, err) env.initLedgerMgmt() - l = env.openTestLedger("ledger1", t) + l = env.openTestLedger("ledger1") l.verifyLedgerHeight(3) // verify block & pvtdata @@ -138,7 +144,7 @@ func TestGetMissingPvtData(t *testing.T) { env := newEnvWithInitializer(t, initializer) defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") _, expectedMissingPvtDataInfo := setup(l) @@ -152,7 +158,7 @@ func TestGetMissingPvtData(t *testing.T) { env.initializer.Config.PrivateDataConfig.DeprioritizedDataReconcilerInterval = 0 * time.Second env.initLedgerMgmt() - l = env.openTestLedger("ledger1", t) + l = env.openTestLedger("ledger1") for i := 0; i < 5; i++ { l.verifyMissingPvtDataSameAs(2, expectedMissingPvtDataInfo) } @@ -161,7 +167,7 @@ func TestGetMissingPvtData(t *testing.T) { env.initializer.Config.PrivateDataConfig.DeprioritizedDataReconcilerInterval = 120 * time.Minute env.initLedgerMgmt() - l = env.openTestLedger("ledger1", t) + l = env.openTestLedger("ledger1") for i := 0; i < 5; i++ { l.verifyMissingPvtDataSameAs(2, ledger.MissingPvtDataInfo{}) } diff --git a/core/ledger/kvledger/tests/pvtdata_test.go b/core/ledger/kvledger/tests/pvtdata_test.go index 1f567fead7f..ce3c06011e8 100644 --- a/core/ledger/kvledger/tests/pvtdata_test.go +++ b/core/ledger/kvledger/tests/pvtdata_test.go @@ -10,13 +10,14 @@ import ( "testing" "github.com/hyperledger/fabric/core/ledger" + "github.com/stretchr/testify/require" ) func TestMissingCollConfig(t *testing.T) { env := newEnv(t) defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") collConf := []*collConf{{name: "coll1", btl: 5}} @@ -26,9 +27,15 @@ func TestMissingCollConfig(t *testing.T) { // pvt data operations should give error as no collection config defined l.simulateDataTx("", func(s *simulator) { - l.assertError(s.GetPrivateData("cc1", "coll1", "key")) - l.assertError(s.SetPrivateData("cc1", "coll1", "key", []byte("value"))) - l.assertError(s.DeletePrivateData("cc1", "coll1", "key")) + expectedErr := "collection config not defined for chaincode [cc1], pass the collection configuration upon chaincode definition/instantiation" + _, err := s.GetPrivateData("cc1", "coll1", "key") + require.EqualError(t, err, expectedErr) + + err = s.SetPrivateData("cc1", "coll1", "key", []byte("value")) + require.EqualError(t, err, expectedErr) + + err = s.DeletePrivateData("cc1", "coll1", "key") + require.EqualError(t, err, expectedErr) }) // upgrade cc1 (add collConf) @@ -38,12 +45,24 @@ func TestMissingCollConfig(t *testing.T) { // operations on coll1 should not give error // operations on coll2 should give error (because, only coll1 is defined in collConf) l.simulateDataTx("", func(s *simulator) { - l.assertNoError(s.GetPrivateData("cc1", "coll1", "key1")) - l.assertNoError(s.SetPrivateData("cc1", "coll1", "key2", []byte("value"))) - l.assertNoError(s.DeletePrivateData("cc1", "coll1", "key3")) - l.assertError(s.GetPrivateData("cc1", "coll2", "key")) - l.assertError(s.SetPrivateData("cc1", "coll2", "key", []byte("value"))) - l.assertError(s.DeletePrivateData("cc1", "coll2", "key")) + _, err := s.GetPrivateData("cc1", "coll1", "key1") + require.NoError(t, err) + + err = s.SetPrivateData("cc1", "coll1", "key2", []byte("value")) + require.NoError(t, err) + + err = s.DeletePrivateData("cc1", "coll1", "key3") + require.NoError(t, err) + + expectedErr := "collection [coll2] not defined in the collection config for chaincode [cc1]" + _, err = s.GetPrivateData("cc1", "coll2", "key") + require.EqualError(t, err, expectedErr) + + err = s.SetPrivateData("cc1", "coll2", "key", []byte("value")) + require.EqualError(t, err, expectedErr) + + err = s.DeletePrivateData("cc1", "coll2", "key") + require.EqualError(t, err, expectedErr) }) } @@ -51,7 +70,7 @@ func TestTxWithMissingPvtdata(t *testing.T) { env := newEnv(t) defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") collConf := []*collConf{{name: "coll1", btl: 5}} @@ -73,7 +92,8 @@ func TestTxWithMissingPvtdata(t *testing.T) { l.verifyPvtState("cc1", "coll1", "key2", "value2") // key2 should have been committed l.simulateDataTx("", func(s *simulator) { - l.assertError(s.GetPrivateData("cc1", "coll1", "key1")) // key1 would be stale with respect to hashed version + _, err := s.GetPrivateData("cc1", "coll1", "key1") // key1 would be stale with respect to hashed version + require.EqualError(t, err, "private data matching public hash version is not available. Public hash version = {BlockNum: 2, TxNum: 0}, Private data version = ") }) // verify missing pvtdata info @@ -96,7 +116,7 @@ func TestTxWithWrongPvtdata(t *testing.T) { env := newEnv(t) defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") collConf := []*collConf{{name: "coll1", btl: 5}} @@ -122,7 +142,7 @@ func TestBTL(t *testing.T) { env := newEnv(t) defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") collConf := []*collConf{{name: "coll1", btl: 0}, {name: "coll2", btl: 5}} // deploy cc1 with 'collConf' diff --git a/core/ledger/kvledger/tests/rebuild_test.go b/core/ledger/kvledger/tests/rebuild_test.go index 53908dd3aee..dda5441d56b 100644 --- a/core/ledger/kvledger/tests/rebuild_test.go +++ b/core/ledger/kvledger/tests/rebuild_test.go @@ -15,7 +15,7 @@ func TestRebuildComponents(t *testing.T) { defer env.cleanup() env.initLedgerMgmt() - l1, l2 := env.createTestLedger("ledger1", t), env.createTestLedger("ledger2", t) + l1, l2 := env.createTestLedger("ledger1"), env.createTestLedger("ledger2") dataHelper := newSampleDataHelper(t) dataHelper.populateLedger(l1) @@ -27,7 +27,7 @@ func TestRebuildComponents(t *testing.T) { t.Run("rebuild only statedb", func(t *testing.T) { env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb) - l1, l2 := env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t) + l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2") dataHelper.verifyLedgerContent(l1) dataHelper.verifyLedgerContent(l2) }, @@ -36,7 +36,7 @@ func TestRebuildComponents(t *testing.T) { t.Run("rebuild statedb and config history", func(t *testing.T) { env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableConfigHistory) - l1, l2 := env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t) + l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2") dataHelper.verifyLedgerContent(l1) dataHelper.verifyLedgerContent(l2) }, @@ -45,7 +45,7 @@ func TestRebuildComponents(t *testing.T) { t.Run("rebuild statedb and block index", func(t *testing.T) { env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableBlockIndex) - l1, l2 := env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t) + l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2") dataHelper.verifyLedgerContent(l1) dataHelper.verifyLedgerContent(l2) }, @@ -54,7 +54,7 @@ func TestRebuildComponents(t *testing.T) { t.Run("rebuild statedb and historydb", func(t *testing.T) { env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableHistoryDB) - l1, l2 := env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t) + l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2") dataHelper.verifyLedgerContent(l1) dataHelper.verifyLedgerContent(l2) }, @@ -65,7 +65,7 @@ func TestRebuildComponentsWithBTL(t *testing.T) { env := newEnv(t) defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") collConf := []*collConf{{name: "coll1", btl: 0}, {name: "coll2", btl: 1}} // deploy cc1 with 'collConf' @@ -105,7 +105,7 @@ func TestRebuildComponentsWithBTL(t *testing.T) { // rebuild statedb and bookkeeper env.closeAllLedgersAndRemoveDirContents(rebuildableStatedb | rebuildableBookkeeper) - l = env.openTestLedger("ledger1", t) + l = env.openTestLedger("ledger1") l.verifyPvtState("cc1", "coll1", "key1", "value1") // key1 should still exist in the state l.verifyPvtState("cc1", "coll2", "key2", "") // key2 should have been purged from the state l.verifyBlockAndPvtData(2, nil, func(r *retrievedBlockAndPvtdata) { // retrieve the pvtdata for block 2 from pvtdata storage diff --git a/core/ledger/kvledger/tests/reset_test.go b/core/ledger/kvledger/tests/reset_test.go index 49e17b46aac..b921275315d 100644 --- a/core/ledger/kvledger/tests/reset_test.go +++ b/core/ledger/kvledger/tests/reset_test.go @@ -32,7 +32,7 @@ func TestResetAllLedgers(t *testing.T) { ledgerIDs := make([]string, numLedgers) for i := 0; i < numLedgers; i++ { ledgerIDs[i] = fmt.Sprintf("ledger-%d", i) - l := env.createTestLedger(ledgerIDs[i], t) + l := env.createTestLedger(ledgerIDs[i]) dataHelper.populateLedger(l) dataHelper.verifyLedgerContent(l) gb, err := l.lgr.GetBlockByNumber(0) @@ -61,7 +61,7 @@ func TestResetAllLedgers(t *testing.T) { // and final blockchainInfo and ledger state same as before reset for i := 0; i < 10; i++ { ledgerID := fmt.Sprintf("ledger-%d", i) - l := env.openTestLedger(ledgerID, t) + l := env.openTestLedger(ledgerID) l.verifyLedgerHeight(1) require.Equal(t, blockchainsInfo[i].Height, preResetHt[ledgerID]) gb, err := l.lgr.GetBlockByNumber(0) @@ -108,7 +108,7 @@ func TestResetAllLedgersWithBTL(t *testing.T) { env := newEnv(t) defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") collConf := []*collConf{{name: "coll1", btl: 0}, {name: "coll2", btl: 1}} // deploy cc1 with 'collConf' @@ -163,7 +163,7 @@ func TestResetAllLedgersWithBTL(t *testing.T) { require.NoError(t, err) t.Logf("preResetHt = %#v", preResetHt) require.Equal(t, uint64(5), preResetHt["ledger1"]) - l = env.openTestLedger("ledger1", t) + l = env.openTestLedger("ledger1") l.verifyLedgerHeight(1) // recommit blocks @@ -192,7 +192,7 @@ func TestResetLedgerWithoutDroppingDBs(t *testing.T) { dataHelper := newSampleDataHelper(t) // create ledgers and pouplate with sample data - l := env.createTestLedger("ledger-1", t) + l := env.createTestLedger("ledger-1") dataHelper.populateLedger(l) dataHelper.verifyLedgerContent(l) env.closeLedgerMgmt() @@ -211,7 +211,6 @@ func TestResetLedgerWithoutDroppingDBs(t *testing.T) { require.NoError(t, err) require.Equal(t, uint64(9), preResetHt["ledger-1"]) _, err = env.ledgerMgr.OpenLedger("ledger-1") - require.Error(t, err) // populateLedger() stores 8 block in total require.EqualError(t, err, "the state database [height=9] is ahead of the block store [height=1]. "+ "This is possible when the state database is not dropped after a ledger reset/rollback. "+ diff --git a/core/ledger/kvledger/tests/rollback_test.go b/core/ledger/kvledger/tests/rollback_test.go index d1f1f39a9ad..765c0352690 100644 --- a/core/ledger/kvledger/tests/rollback_test.go +++ b/core/ledger/kvledger/tests/rollback_test.go @@ -22,7 +22,7 @@ func TestRollbackKVLedger(t *testing.T) { // populate ledgers with sample data dataHelper := newSampleDataHelper(t) - l := env.createTestLedger("testLedger", t) + l := env.createTestLedger("testLedger") // populate creates 8 blocks dataHelper.populateLedger(l) dataHelper.verifyLedgerContent(l) @@ -50,7 +50,7 @@ func TestRollbackKVLedger(t *testing.T) { require.Equal(t, bcInfo.Height, preResetHt["testLedger"]) t.Logf("preResetHt = %#v", preResetHt) - l = env.openTestLedger("testLedger", t) + l = env.openTestLedger("testLedger") l.verifyLedgerHeight(targetBlockNum + 1) targetBlockNumIndex := targetBlockNum - 1 for _, b := range dataHelper.submittedData["testLedger"].Blocks[targetBlockNumIndex+1:] { @@ -69,7 +69,7 @@ func TestRollbackKVLedgerWithBTL(t *testing.T) { env := newEnv(t) defer env.cleanup() env.initLedgerMgmt() - l := env.createTestLedger("ledger1", t) + l := env.createTestLedger("ledger1") collConf := []*collConf{{name: "coll1", btl: 0}, {name: "coll2", btl: 1}} // deploy cc1 with 'collConf' @@ -120,7 +120,7 @@ func TestRollbackKVLedgerWithBTL(t *testing.T) { env.verifyRebuilableDirEmpty(rebuildable) env.initLedgerMgmt() - l = env.openTestLedger("ledger1", t) + l = env.openTestLedger("ledger1") l.verifyPvtState("cc1", "coll1", "key1", "value1") // key1 should still exist in the state l.verifyPvtState("cc1", "coll2", "key2", "") // key2 should have been purged from the state l.verifyBlockAndPvtData(2, nil, func(r *retrievedBlockAndPvtdata) { // retrieve the pvtdata for block 2 from pvtdata storage diff --git a/core/ledger/kvledger/tests/sanpshot_reset_rollback_rebuild_test.go b/core/ledger/kvledger/tests/sanpshot_reset_rollback_rebuild_test.go index 1c21826cf84..d5c3b41abd0 100644 --- a/core/ledger/kvledger/tests/sanpshot_reset_rollback_rebuild_test.go +++ b/core/ledger/kvledger/tests/sanpshot_reset_rollback_rebuild_test.go @@ -22,7 +22,7 @@ func TestResetRollbackRebuildFailsIfAnyLedgerBootstrappedFromSnapshot(t *testing // populate ledgers with sample data dataHelper := newSampleDataHelper(t) - l := env.createTestLedger("ledger_from_snapshot", t) + l := env.createTestLedger("ledger_from_snapshot") dataHelper.populateLedger(l) dataHelper.verifyLedgerContent(l) bcInfo, err := l.lgr.GetBlockchainInfo() @@ -46,7 +46,7 @@ func TestResetRollbackRebuildFailsIfAnyLedgerBootstrappedFromSnapshot(t *testing defer env2.cleanup() env2.initLedgerMgmt() - env2.createTestLedger("ledger_from_genesis_block", t) + env2.createTestLedger("ledger_from_genesis_block") callbackCounter := 0 callback := func(l ledger.PeerLedger, cid string) { callbackCounter++ } diff --git a/core/ledger/kvledger/tests/testledger.go b/core/ledger/kvledger/tests/testledger.go index d389ee6a890..7cf20c4bd2f 100644 --- a/core/ledger/kvledger/tests/testledger.go +++ b/core/ledger/kvledger/tests/testledger.go @@ -23,60 +23,49 @@ type testLedger struct { *client *committer *verifier - lgr ledger.PeerLedger - lgrid string - assert *require.Assertions + lgr ledger.PeerLedger + lgrid string + t *testing.T } // createTestLedger creates a new ledger and retruns a 'testhelper' for the ledger -func (env *env) createTestLedger(id string, t *testing.T) *testLedger { +func (env *env) createTestLedger(id string) *testLedger { + t := env.t genesisBlk, err := constructTestGenesisBlock(id) - require.NoError(t, err) + require.NoError(env.t, err) lgr, err := env.ledgerMgr.CreateLedger(id, genesisBlk) require.NoError(t, err) client, committer, verifier := newClient(lgr, id, t), newCommitter(lgr, t), newVerifier(lgr, t) - return &testLedger{client, committer, verifier, lgr, id, require.New(t)} + return &testLedger{client, committer, verifier, lgr, id, t} } // openTestLedger opens an existing ledger and retruns a 'testhelper' for the ledger -func (env *env) openTestLedger(id string, t *testing.T) *testLedger { +func (env *env) openTestLedger(id string) *testLedger { + t := env.t lgr, err := env.ledgerMgr.OpenLedger(id) require.NoError(t, err) client, committer, verifier := newClient(lgr, id, t), newCommitter(lgr, t), newVerifier(lgr, t) - return &testLedger{client, committer, verifier, lgr, id, require.New(t)} + return &testLedger{client, committer, verifier, lgr, id, t} } // cutBlockAndCommitLegacy gathers all the transactions simulated by the test code (by calling // the functions available in the 'client') and cuts the next block and commits to the ledger -func (h *testLedger) cutBlockAndCommitLegacy() *ledger.BlockAndPvtData { +func (l *testLedger) cutBlockAndCommitLegacy() *ledger.BlockAndPvtData { defer func() { - h.simulatedTrans = nil - h.missingPvtData = make(ledger.TxMissingPvtData) + l.simulatedTrans = nil + l.missingPvtData = make(ledger.TxMissingPvtData) }() - return h.committer.cutBlockAndCommitLegacy(h.simulatedTrans, h.missingPvtData) + return l.committer.cutBlockAndCommitLegacy(l.simulatedTrans, l.missingPvtData) } -func (h *testLedger) cutBlockAndCommitExpectError() *ledger.BlockAndPvtData { +func (l *testLedger) cutBlockAndCommitExpectError() *ledger.BlockAndPvtData { defer func() { - h.simulatedTrans = nil - h.missingPvtData = make(ledger.TxMissingPvtData) + l.simulatedTrans = nil + l.missingPvtData = make(ledger.TxMissingPvtData) }() - return h.committer.cutBlockAndCommitExpectError(h.simulatedTrans, h.missingPvtData) -} - -func (h *testLedger) commitPvtDataOfOldBlocks(blocksPvtData []*ledger.ReconciledPvtdata, unreconciled ledger.MissingPvtDataInfo) ([]*ledger.PvtdataHashMismatch, error) { - return h.lgr.CommitPvtDataOfOldBlocks(blocksPvtData, unreconciled) -} - -// assertError is a helper function that can be called as assertError(f()) where 'f' is some other function -// this function assumes that the last return type of function 'f' is of type 'error' -func (h *testLedger) assertError(output ...interface{}) { - lastParam := output[len(output)-1] - require.NotNil(h.t, lastParam) - h.assert.Error(lastParam.(error)) + return l.committer.cutBlockAndCommitExpectError(l.simulatedTrans, l.missingPvtData) } -// assertNoError see comment on function 'assertError' -func (h *testLedger) assertNoError(output ...interface{}) { - h.assert.Nil(output[len(output)-1]) +func (l *testLedger) commitPvtDataOfOldBlocks(blocksPvtData []*ledger.ReconciledPvtdata, unreconciled ledger.MissingPvtDataInfo) ([]*ledger.PvtdataHashMismatch, error) { + return l.lgr.CommitPvtDataOfOldBlocks(blocksPvtData, unreconciled) } diff --git a/core/ledger/kvledger/tests/v1x_test.go b/core/ledger/kvledger/tests/v1x_test.go index bd2abf5f6a6..9d98c12b9ec 100644 --- a/core/ledger/kvledger/tests/v1x_test.go +++ b/core/ledger/kvledger/tests/v1x_test.go @@ -46,7 +46,7 @@ func TestV11(t *testing.T) { env.initLedgerMgmt() - l1, l2 := env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t) + l1, l2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2") dataHelper := &v1xSampleDataHelper{sampleDataVersion: "v1.1", t: t} dataHelper.verify(l1) @@ -58,7 +58,7 @@ func TestV11(t *testing.T) { env.verifyRebuilableDirEmpty(rebuildable) env.initLedgerMgmt() - l1, l2 = env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t) + l1, l2 = env.openTestLedger("ledger1"), env.openTestLedger("ledger2") dataHelper.verify(l1) dataHelper.verify(l2) @@ -177,7 +177,7 @@ func testV11CommitHashes(t *testing.T, env.verifyRebuilableDirEmpty(rebuildable) env.initLedgerMgmt() - h := env.openTestLedger("ledger1", t) + h := env.openTestLedger("ledger1") blocksAndPvtData := h.retrieveCommittedBlocksAndPvtdata(0, h.currentHeight()-1) var commitHashPreReset []byte @@ -192,7 +192,7 @@ func testV11CommitHashes(t *testing.T, resetFunc(h, ledgerFSRoot) env.initLedgerMgmt() - h = env.openTestLedger("ledger1", t) + h = env.openTestLedger("ledger1") for i := int(h.currentHeight()); i < len(blocksAndPvtData); i++ { d := blocksAndPvtData[i] // add metadata slot for commit hash, as this would have be missing in the blocks from 1.1 prior to this feature @@ -255,7 +255,7 @@ func TestV13WithStateCouchdb(t *testing.T) { env.initLedgerMgmt() - h1, h2 := env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t) + h1, h2 := env.openTestLedger("ledger1"), env.openTestLedger("ledger2") dataHelper := &v1xSampleDataHelper{sampleDataVersion: "v1.3", t: t} dataHelper.verify(h1) dataHelper.verify(h2) @@ -267,7 +267,7 @@ func TestV13WithStateCouchdb(t *testing.T) { env.verifyRebuilableDirEmpty(rebuildable) env.initLedgerMgmt() - h1, h2 = env.openTestLedger("ledger1", t), env.openTestLedger("ledger2", t) + h1, h2 = env.openTestLedger("ledger1"), env.openTestLedger("ledger2") dataHelper.verify(h1) dataHelper.verify(h2) } diff --git a/core/ledger/kvledger/tests/v20_test.go b/core/ledger/kvledger/tests/v20_test.go index 8e72a894315..57fb2086cf3 100644 --- a/core/ledger/kvledger/tests/v20_test.go +++ b/core/ledger/kvledger/tests/v20_test.go @@ -27,14 +27,14 @@ func TestV20SampleLedger(t *testing.T) { require.NoError(t, testutil.Unzip("testdata/v20/sample_ledgers/ledgersData.zip", ledgerFSRoot, false)) env.initLedgerMgmt() - l1 := env.openTestLedger("testchannel", t) + l1 := env.openTestLedger("testchannel") dataHelper.verify(l1) // rebuild and verify again env.closeLedgerMgmt() require.NoError(t, kvledger.RebuildDBs(env.initializer.Config)) env.initLedgerMgmt() - l1 = env.openTestLedger("testchannel", t) + l1 = env.openTestLedger("testchannel") dataHelper.verify(l1) }