Commit ef92892c authored by Matthew Sykes's avatar Matthew Sykes
Browse files

[FAB-10686] testutil->testify fsblkstorage



Change-Id: I4daf106d190b42053ce2967bc31e6ad79894ac7e
Signed-off-by: default avatarMatthew Sykes <sykesmat@us.ibm.com>
parent a29746be
......@@ -22,45 +22,46 @@ import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/ledger/testutil"
putils "github.com/hyperledger/fabric/protos/utils"
"github.com/stretchr/testify/assert"
)
func TestBlockSerialization(t *testing.T) {
block := testutil.ConstructTestBlock(t, 1, 10, 100)
bb, _, err := serializeBlock(block)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
deserializedBlock, err := deserializeBlock(bb)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, deserializedBlock, block)
assert.NoError(t, err)
assert.Equal(t, block, deserializedBlock)
}
func TestExtractTxid(t *testing.T) {
txEnv, txid, _ := testutil.ConstructTransaction(t, testutil.ConstructRandomBytes(t, 50), "", false)
txEnvBytes, _ := putils.GetBytesEnvelope(txEnv)
extractedTxid, err := extractTxID(txEnvBytes)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, extractedTxid, txid)
assert.NoError(t, err)
assert.Equal(t, txid, extractedTxid)
}
func TestSerializedBlockInfo(t *testing.T) {
block := testutil.ConstructTestBlock(t, 1, 10, 100)
bb, info, err := serializeBlock(block)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
infoFromBB, err := extractSerializedBlockInfo(bb)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, infoFromBB, info)
testutil.AssertEquals(t, len(info.txOffsets), len(block.Data.Data))
assert.NoError(t, err)
assert.Equal(t, info, infoFromBB)
assert.Equal(t, len(block.Data.Data), len(info.txOffsets))
for txIndex, txEnvBytes := range block.Data.Data {
txid, err := extractTxID(txEnvBytes)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
indexInfo := info.txOffsets[txIndex]
indexTxID := indexInfo.txID
indexOffset := indexInfo.loc
testutil.AssertEquals(t, txid, indexTxID)
assert.Equal(t, indexTxID, txid)
b := bb[indexOffset.offset:]
length, num := proto.DecodeVarint(b)
txEnvBytesFromBB := b[num : num+int(length)]
testutil.AssertEquals(t, txEnvBytesFromBB, txEnvBytes)
assert.Equal(t, txEnvBytes, txEnvBytesFromBB)
}
}
......@@ -22,6 +22,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/protos/common"
"github.com/stretchr/testify/assert"
)
func TestBlockfileStream(t *testing.T) {
......@@ -41,12 +42,12 @@ func testBlockfileStream(t *testing.T, numBlocks int) {
s, err := newBlockfileStream(w.blockfileMgr.rootDir, 0, 0)
defer s.close()
testutil.AssertNoError(t, err, "Error in constructing blockfile stream")
assert.NoError(t, err, "Error in constructing blockfile stream")
blockCount := 0
for {
blockBytes, err := s.nextBlockBytes()
testutil.AssertNoError(t, err, "Error in getting next block")
assert.NoError(t, err, "Error in getting next block")
if blockBytes == nil {
break
}
......@@ -54,9 +55,9 @@ func testBlockfileStream(t *testing.T, numBlocks int) {
}
// After the stream has been exhausted, both blockBytes and err should be nil
blockBytes, err := s.nextBlockBytes()
testutil.AssertNil(t, blockBytes)
testutil.AssertNoError(t, err, "Error in getting next block after exhausting the file")
testutil.AssertEquals(t, blockCount, numBlocks)
assert.Nil(t, blockBytes)
assert.NoError(t, err, "Error in getting next block after exhausting the file")
assert.Equal(t, numBlocks, blockCount)
}
func TestBlockFileStreamUnexpectedEOF(t *testing.T) {
......@@ -82,16 +83,16 @@ func testBlockFileStreamUnexpectedEOF(t *testing.T, numBlocks int, partialBlockB
w.close()
s, err := newBlockfileStream(blockfileMgr.rootDir, 0, 0)
defer s.close()
testutil.AssertNoError(t, err, "Error in constructing blockfile stream")
assert.NoError(t, err, "Error in constructing blockfile stream")
for i := 0; i < numBlocks; i++ {
blockBytes, err := s.nextBlockBytes()
testutil.AssertNotNil(t, blockBytes)
testutil.AssertNoError(t, err, "Error in getting next block")
assert.NotNil(t, blockBytes)
assert.NoError(t, err, "Error in getting next block")
}
blockBytes, err := s.nextBlockBytes()
testutil.AssertNil(t, blockBytes)
testutil.AssertSame(t, err, ErrUnexpectedEndOfBlockfile)
assert.Nil(t, blockBytes)
assert.Exactly(t, ErrUnexpectedEndOfBlockfile, err)
}
func TestBlockStream(t *testing.T) {
......@@ -123,11 +124,11 @@ func testBlockStream(t *testing.T, numFiles int) {
}
s, err := newBlockStream(blockfileMgr.rootDir, 0, 0, numFiles-1)
defer s.close()
testutil.AssertNoError(t, err, "Error in constructing new block stream")
assert.NoError(t, err, "Error in constructing new block stream")
blockCount := 0
for {
blockBytes, err := s.nextBlockBytes()
testutil.AssertNoError(t, err, "Error in getting next block")
assert.NoError(t, err, "Error in getting next block")
if blockBytes == nil {
break
}
......@@ -135,7 +136,7 @@ func testBlockStream(t *testing.T, numFiles int) {
}
// After the stream has been exhausted, both blockBytes and err should be nil
blockBytes, err := s.nextBlockBytes()
testutil.AssertNil(t, blockBytes)
testutil.AssertNoError(t, err, "Error in getting next block after exhausting the file")
testutil.AssertEquals(t, blockCount, numFiles*numBlocksInEachFile)
assert.Nil(t, blockBytes)
assert.NoError(t, err, "Error in getting next block after exhausting the file")
assert.Equal(t, numFiles*numBlocksInEachFile, blockCount)
}
......@@ -13,6 +13,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/common/ledger/util"
"github.com/stretchr/testify/assert"
)
func TestConstructCheckpointInfoFromBlockFiles(t *testing.T) {
......@@ -26,8 +27,8 @@ func TestConstructCheckpointInfoFromBlockFiles(t *testing.T) {
// checkpoint constructed on an empty block folder should return CPInfo with isChainEmpty: true
cpInfo, err := constructCheckpointInfoFromBlockFiles(blkStoreDir)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, cpInfo, &checkpointInfo{isChainEmpty: true, lastBlockNumber: 0, latestFileChunksize: 0, latestFileChunkSuffixNum: 0})
assert.NoError(t, err)
assert.Equal(t, &checkpointInfo{isChainEmpty: true, lastBlockNumber: 0, latestFileChunksize: 0, latestFileChunkSuffixNum: 0}, cpInfo)
w := newTestBlockfileWrapper(env, ledgerid)
defer w.close()
......@@ -54,7 +55,7 @@ func TestConstructCheckpointInfoFromBlockFiles(t *testing.T) {
// Write a partial block (to simulate a crash) and verify that cpinfo derived from filesystem should be same as from the blockfile manager
lastTestBlk := bg.NextTestBlocks(1)[0]
blockBytes, _, err := serializeBlock(lastTestBlk)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
partialByte := append(proto.EncodeVarint(uint64(len(blockBytes))), blockBytes[len(blockBytes)/2:]...)
blockfileMgr.currentFileWriter.append(partialByte, true)
checkCPInfoFromFile(t, blkStoreDir, blockfileMgr.cpInfo)
......@@ -64,24 +65,24 @@ func TestConstructCheckpointInfoFromBlockFiles(t *testing.T) {
w.close()
env.provider.Close()
indexFolder := conf.getIndexDir()
testutil.AssertNoError(t, os.RemoveAll(indexFolder), "")
assert.NoError(t, os.RemoveAll(indexFolder))
env = newTestEnv(t, conf)
w = newTestBlockfileWrapper(env, ledgerid)
blockfileMgr = w.blockfileMgr
testutil.AssertEquals(t, blockfileMgr.cpInfo, cpInfoBeforeClose)
assert.Equal(t, cpInfoBeforeClose, blockfileMgr.cpInfo)
lastBlkIndexed, err := blockfileMgr.index.getLastBlockIndexed()
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, lastBlkIndexed, uint64(6))
assert.NoError(t, err)
assert.Equal(t, uint64(6), lastBlkIndexed)
// Add the last block again after start and check cpinfo again
testutil.AssertNoError(t, blockfileMgr.addBlock(lastTestBlk), "")
assert.NoError(t, blockfileMgr.addBlock(lastTestBlk))
checkCPInfoFromFile(t, blkStoreDir, blockfileMgr.cpInfo)
}
func checkCPInfoFromFile(t *testing.T, blkStoreDir string, expectedCPInfo *checkpointInfo) {
cpInfo, err := constructCheckpointInfoFromBlockFiles(blkStoreDir)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, cpInfo, expectedCPInfo)
assert.NoError(t, err)
assert.Equal(t, expectedCPInfo, cpInfo)
}
......@@ -17,13 +17,12 @@ limitations under the License.
package fsblkstorage
import (
"fmt"
"strings"
"testing"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/ledger/testutil"
ledgerutil "github.com/hyperledger/fabric/core/ledger/util"
"github.com/stretchr/testify/assert"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/peer"
......@@ -51,8 +50,8 @@ func TestAddBlockWithWrongHash(t *testing.T) {
lastBlock := blocks[9]
lastBlock.Header.PreviousHash = []byte("someJunkHash") // set the hash to something unexpected
err := blkfileMgrWrapper.blockfileMgr.addBlock(lastBlock)
testutil.AssertError(t, err, "An error is expected when adding a block with some unexpected hash")
testutil.AssertEquals(t, strings.Contains(err.Error(), "unexpected Previous block hash. Expected PreviousHash"), true)
assert.Error(t, err, "An error is expected when adding a block with some unexpected hash")
assert.Contains(t, err.Error(), "unexpected Previous block hash. Expected PreviousHash")
t.Logf("err = %s", err)
}
......@@ -116,7 +115,7 @@ func testBlockfileMgrCrashDuringWriting(t *testing.T, numBlocksBeforeCheckpoint
blkfileMgrWrapper = newTestBlockfileWrapper(env, ledgerid)
defer blkfileMgrWrapper.close()
cpInfo3 := blkfileMgrWrapper.blockfileMgr.cpInfo
testutil.AssertEquals(t, cpInfo3, cpInfo2)
assert.Equal(t, cpInfo2, cpInfo3)
// add fresh blocks after restart
blkfileMgrWrapper.addBlocks(blocksAfterRestart)
......@@ -137,18 +136,18 @@ func testBlockfileMgrBlockIterator(t *testing.T, blockfileMgr *blockfileMgr,
firstBlockNum int, lastBlockNum int, expectedBlocks []*common.Block) {
itr, err := blockfileMgr.retrieveBlocks(uint64(firstBlockNum))
defer itr.Close()
testutil.AssertNoError(t, err, "Error while getting blocks iterator")
assert.NoError(t, err, "Error while getting blocks iterator")
numBlocksItrated := 0
for {
block, err := itr.Next()
testutil.AssertNoError(t, err, fmt.Sprintf("Error while getting block number [%d] from iterator", numBlocksItrated))
testutil.AssertEquals(t, block, expectedBlocks[numBlocksItrated])
assert.NoError(t, err, "Error while getting block number [%d] from iterator", numBlocksItrated)
assert.Equal(t, expectedBlocks[numBlocksItrated], block)
numBlocksItrated++
if numBlocksItrated == lastBlockNum-firstBlockNum+1 {
break
}
}
testutil.AssertEquals(t, numBlocksItrated, lastBlockNum-firstBlockNum+1)
assert.Equal(t, lastBlockNum-firstBlockNum+1, numBlocksItrated)
}
func TestBlockfileMgrBlockchainInfo(t *testing.T) {
......@@ -158,12 +157,12 @@ func TestBlockfileMgrBlockchainInfo(t *testing.T) {
defer blkfileMgrWrapper.close()
bcInfo := blkfileMgrWrapper.blockfileMgr.getBlockchainInfo()
testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{Height: 0, CurrentBlockHash: nil, PreviousBlockHash: nil})
assert.Equal(t, &common.BlockchainInfo{Height: 0, CurrentBlockHash: nil, PreviousBlockHash: nil}, bcInfo)
blocks := testutil.ConstructTestBlocks(t, 10)
blkfileMgrWrapper.addBlocks(blocks)
bcInfo = blkfileMgrWrapper.blockfileMgr.getBlockchainInfo()
testutil.AssertEquals(t, bcInfo.Height, uint64(10))
assert.Equal(t, uint64(10), bcInfo.Height)
}
func TestBlockfileMgrGetTxById(t *testing.T) {
......@@ -177,12 +176,12 @@ func TestBlockfileMgrGetTxById(t *testing.T) {
for j, txEnvelopeBytes := range blk.Data.Data {
// blockNum starts with 0
txID, err := extractTxID(blk.Data.Data[j])
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
txEnvelopeFromFileMgr, err := blkfileMgrWrapper.blockfileMgr.retrieveTransactionByID(txID)
testutil.AssertNoError(t, err, "Error while retrieving tx from blkfileMgr")
assert.NoError(t, err, "Error while retrieving tx from blkfileMgr")
txEnvelope, err := putil.GetEnvelopeFromBlock(txEnvelopeBytes)
testutil.AssertNoError(t, err, "Error while unmarshalling tx")
testutil.AssertEquals(t, txEnvelopeFromFileMgr, txEnvelope)
assert.NoError(t, err, "Error while unmarshalling tx")
assert.Equal(t, txEnvelope, txEnvelopeFromFileMgr)
}
}
}
......@@ -193,10 +192,10 @@ func TestBlockfileMgrGetTxByIdDuplicateTxid(t *testing.T) {
env := newTestEnv(t, NewConf(testPath(), 0))
defer env.Cleanup()
blkStore, err := env.provider.OpenBlockStore("testLedger")
testutil.AssertNoError(env.t, err, "")
assert.NoError(env.t, err)
blkFileMgr := blkStore.(*fsBlockStore).fileMgr
bg, gb := testutil.NewBlockGenerator(t, "testLedger", false)
testutil.AssertNoError(t, blkFileMgr.addBlock(gb), "")
assert.NoError(t, blkFileMgr.addBlock(gb))
block1 := bg.NextBlockWithTxid(
[][]byte{
......@@ -211,7 +210,7 @@ func TestBlockfileMgrGetTxByIdDuplicateTxid(t *testing.T) {
txValidationFlags.SetFlag(1, peer.TxValidationCode_INVALID_OTHER_REASON)
txValidationFlags.SetFlag(2, peer.TxValidationCode_DUPLICATE_TXID)
block1.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = txValidationFlags
testutil.AssertNoError(t, blkFileMgr.addBlock(block1), "")
assert.NoError(t, blkFileMgr.addBlock(block1))
block2 := bg.NextBlockWithTxid(
[][]byte{
......@@ -224,35 +223,35 @@ func TestBlockfileMgrGetTxByIdDuplicateTxid(t *testing.T) {
txValidationFlags.SetFlag(0, peer.TxValidationCode_VALID)
txValidationFlags.SetFlag(1, peer.TxValidationCode_DUPLICATE_TXID)
block2.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = txValidationFlags
testutil.AssertNoError(t, blkFileMgr.addBlock(block2), "")
assert.NoError(t, blkFileMgr.addBlock(block2))
txenvp1, err := putil.GetEnvelopeFromBlock(block1.Data.Data[0])
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
txenvp2, err := putil.GetEnvelopeFromBlock(block1.Data.Data[1])
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
txenvp3, err := putil.GetEnvelopeFromBlock(block2.Data.Data[0])
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
indexedTxenvp, _ := blkFileMgr.retrieveTransactionByID("txid-1")
testutil.AssertEquals(t, indexedTxenvp, txenvp1)
assert.Equal(t, txenvp1, indexedTxenvp)
indexedTxenvp, _ = blkFileMgr.retrieveTransactionByID("txid-2")
testutil.AssertEquals(t, indexedTxenvp, txenvp2)
assert.Equal(t, txenvp2, indexedTxenvp)
indexedTxenvp, _ = blkFileMgr.retrieveTransactionByID("txid-3")
testutil.AssertEquals(t, indexedTxenvp, txenvp3)
assert.Equal(t, txenvp3, indexedTxenvp)
blk, _ := blkFileMgr.retrieveBlockByTxID("txid-1")
testutil.AssertEquals(t, blk, block1)
assert.Equal(t, block1, blk)
blk, _ = blkFileMgr.retrieveBlockByTxID("txid-2")
testutil.AssertEquals(t, blk, block1)
assert.Equal(t, block1, blk)
blk, _ = blkFileMgr.retrieveBlockByTxID("txid-3")
testutil.AssertEquals(t, blk, block2)
assert.Equal(t, block2, blk)
validationCode, _ := blkFileMgr.retrieveTxValidationCodeByTxID("txid-1")
testutil.AssertEquals(t, validationCode, peer.TxValidationCode_VALID)
assert.Equal(t, peer.TxValidationCode_VALID, validationCode)
validationCode, _ = blkFileMgr.retrieveTxValidationCodeByTxID("txid-2")
testutil.AssertEquals(t, validationCode, peer.TxValidationCode_INVALID_OTHER_REASON)
assert.Equal(t, peer.TxValidationCode_INVALID_OTHER_REASON, validationCode)
validationCode, _ = blkFileMgr.retrieveTxValidationCodeByTxID("txid-3")
testutil.AssertEquals(t, validationCode, peer.TxValidationCode_VALID)
assert.Equal(t, peer.TxValidationCode_VALID, validationCode)
}
func TestBlockfileMgrGetTxByBlockNumTranNum(t *testing.T) {
......@@ -266,10 +265,10 @@ func TestBlockfileMgrGetTxByBlockNumTranNum(t *testing.T) {
for tranIndex, txEnvelopeBytes := range blk.Data.Data {
// blockNum and tranNum both start with 0
txEnvelopeFromFileMgr, err := blkfileMgrWrapper.blockfileMgr.retrieveTransactionByBlockNumTranNum(uint64(blockIndex), uint64(tranIndex))
testutil.AssertNoError(t, err, "Error while retrieving tx from blkfileMgr")
assert.NoError(t, err, "Error while retrieving tx from blkfileMgr")
txEnvelope, err := putil.GetEnvelopeFromBlock(txEnvelopeBytes)
testutil.AssertNoError(t, err, "Error while unmarshalling tx")
testutil.AssertEquals(t, txEnvelopeFromFileMgr, txEnvelope)
assert.NoError(t, err, "Error while unmarshalling tx")
assert.Equal(t, txEnvelope, txEnvelopeFromFileMgr)
}
}
}
......@@ -282,14 +281,14 @@ func TestBlockfileMgrRestart(t *testing.T) {
blocks := testutil.ConstructTestBlocks(t, 10)
blkfileMgrWrapper.addBlocks(blocks)
expectedHeight := uint64(10)
testutil.AssertEquals(t, blkfileMgrWrapper.blockfileMgr.getBlockchainInfo().Height, expectedHeight)
assert.Equal(t, expectedHeight, blkfileMgrWrapper.blockfileMgr.getBlockchainInfo().Height)
blkfileMgrWrapper.close()
blkfileMgrWrapper = newTestBlockfileWrapper(env, ledgerid)
defer blkfileMgrWrapper.close()
testutil.AssertEquals(t, int(blkfileMgrWrapper.blockfileMgr.cpInfo.lastBlockNumber), 9)
assert.Equal(t, 9, int(blkfileMgrWrapper.blockfileMgr.cpInfo.lastBlockNumber))
blkfileMgrWrapper.testGetBlockByHash(blocks)
testutil.AssertEquals(t, blkfileMgrWrapper.blockfileMgr.getBlockchainInfo().Height, expectedHeight)
assert.Equal(t, expectedHeight, blkfileMgrWrapper.blockfileMgr.getBlockchainInfo().Height)
}
func TestBlockfileMgrFileRolling(t *testing.T) {
......@@ -297,7 +296,7 @@ func TestBlockfileMgrFileRolling(t *testing.T) {
size := 0
for _, block := range blocks[:100] {
by, _, err := serializeBlock(block)
testutil.AssertNoError(t, err, "Error while serializing block")
assert.NoError(t, err, "Error while serializing block")
blockBytesSize := len(by)
encodedLen := proto.EncodeVarint(uint64(blockBytesSize))
size += blockBytesSize + len(encodedLen)
......@@ -309,14 +308,14 @@ func TestBlockfileMgrFileRolling(t *testing.T) {
ledgerid := "testLedger"
blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
blkfileMgrWrapper.addBlocks(blocks[:100])
testutil.AssertEquals(t, blkfileMgrWrapper.blockfileMgr.cpInfo.latestFileChunkSuffixNum, 1)
assert.Equal(t, 1, blkfileMgrWrapper.blockfileMgr.cpInfo.latestFileChunkSuffixNum)
blkfileMgrWrapper.testGetBlockByHash(blocks[:100])
blkfileMgrWrapper.close()
blkfileMgrWrapper = newTestBlockfileWrapper(env, ledgerid)
defer blkfileMgrWrapper.close()
blkfileMgrWrapper.addBlocks(blocks[100:])
testutil.AssertEquals(t, blkfileMgrWrapper.blockfileMgr.cpInfo.latestFileChunkSuffixNum, 2)
assert.Equal(t, 2, blkfileMgrWrapper.blockfileMgr.cpInfo.latestFileChunkSuffixNum)
blkfileMgrWrapper.testGetBlockByHash(blocks[100:])
}
......@@ -331,11 +330,11 @@ func TestBlockfileMgrGetBlockByTxID(t *testing.T) {
for j := range blk.Data.Data {
// blockNum starts with 1
txID, err := extractTxID(blk.Data.Data[j])
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
blockFromFileMgr, err := blkfileMgrWrapper.blockfileMgr.retrieveBlockByTxID(txID)
testutil.AssertNoError(t, err, "Error while retrieving block from blkfileMgr")
testutil.AssertEquals(t, blockFromFileMgr, blk)
assert.NoError(t, err, "Error while retrieving block from blkfileMgr")
assert.Equal(t, blk, blockFromFileMgr)
}
}
}
......@@ -22,6 +22,7 @@ import (
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/common/ledger/util"
"github.com/stretchr/testify/assert"
"github.com/hyperledger/fabric/protos/common"
)
......@@ -41,15 +42,15 @@ func TestBlockFileScanSmallTxOnly(t *testing.T) {
filePath := deriveBlockfilePath(env.provider.conf.getLedgerBlockDir(ledgerid), 0)
_, fileSize, err := util.FileExists(filePath)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
lastBlockBytes, endOffsetLastBlock, numBlocks, err := scanForLastCompleteBlock(env.provider.conf.getLedgerBlockDir(ledgerid), 0, 0)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, numBlocks, len(blocks))
testutil.AssertEquals(t, endOffsetLastBlock, fileSize)
assert.NoError(t, err)
assert.Equal(t, len(blocks), numBlocks)
assert.Equal(t, fileSize, endOffsetLastBlock)
expectedLastBlockBytes, _, err := serializeBlock(blocks[len(blocks)-1])
testutil.AssertEquals(t, lastBlockBytes, expectedLastBlockBytes)
assert.Equal(t, expectedLastBlockBytes, lastBlockBytes)
}
func TestBlockFileScanSmallTxLastTxIncomplete(t *testing.T) {
......@@ -67,18 +68,18 @@ func TestBlockFileScanSmallTxLastTxIncomplete(t *testing.T) {
filePath := deriveBlockfilePath(env.provider.conf.getLedgerBlockDir(ledgerid), 0)
_, fileSize, err := util.FileExists(filePath)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
file, err := os.OpenFile(filePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
defer file.Close()
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
err = file.Truncate(fileSize - 1)
testutil.AssertNoError(t, err, "")
assert.NoError(t, err)
lastBlockBytes, _, numBlocks, err := scanForLastCompleteBlock(env.provider.conf.getLedgerBlockDir(ledgerid), 0, 0)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, numBlocks, len(blocks)-1)
assert.NoError(t, err)
assert.Equal(t, len(blocks)-1, numBlocks)
expectedLastBlockBytes, _, err := serializeBlock(blocks[len(blocks)-2])
testutil.AssertEquals(t, lastBlockBytes, expectedLastBlockBytes)
assert.Equal(t, expectedLastBlockBytes, lastBlockBytes)
}
......@@ -26,6 +26,7 @@ import (
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/peer"
putil "github.com/hyperledger/fabric/protos/utils"
"github.com/stretchr/testify/assert"
)
type noopIndex struct {
......@@ -89,14 +90,14 @@ func testBlockIndexSync(t *testing.T, numBlocks int, numBlocksToIndex int, syncB
// The first set of blocks should be present in the original index
for i := 0; i < numBlocksToIndex; i++ {
block, err := blkfileMgr.retrieveBlockByNumber(uint64(i))
testutil.AssertNoError(t, err, fmt.Sprintf("block [%d] should have been present in the index", i))
testutil.AssertEquals(t, block, blocks[i])
assert.NoError(t, err, "block [%d] should have been present in the index", i)
assert.Equal(t, blocks[i], block)
}
// The last set of blocks should not be present in the original index
for i := numBlocksToIndex + 1; i <= numBlocks; i++ {
_, err := blkfileMgr.retrieveBlockByNumber(uint64(i))
testutil.AssertSame(t, err, blkstorage.ErrNotFoundInIndex)
assert.Exactly(t, blkstorage.ErrNotFoundInIndex, err)
}
// perform index sync
......@@ -112,8 +113,8 @@ func testBlockIndexSync(t *testing.T, numBlocks int, numBlocksToIndex int, syncB
// Now, last set of blocks should also be present in original index
for i := numBlocksToIndex; i < numBlocks; i++ {
block, err := blkfileMgr.retrieveBlockByNumber(uint64(i))
testutil.AssertNoError(t, err, fmt.Sprintf("block [%d] should have been present in the index", i))
testutil.AssertEquals(t, block, blocks[i])
assert.NoError(t, err, "block [%d] should have been present in the index", i)
assert.Equal(t, blocks[i], block)
}
})
}
......@@ -134,8 +135,10 @@ func testBlockIndexSelectiveIndexingWrongConfig(t *testing.T, indexItems []blkst
t.Run(testName, func(t *testing.T) {
env := newTestEnvSelectiveIndexing(t, NewConf(testPath(), 0), indexItems)
defer env.Cleanup()
defer testutil.AssertPanic(t, "A panic is expected when index is opened with wrong configs")
env.provider.OpenBlockStore("test-ledger")
assert.Panics(t, func() {
env.provider.OpenBlockStore("test-ledger")
}, "A panic is expected when index is opened with wrong configs")
})
}
......@@ -170,57 +173,57 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []blkstorage.Index
// if index has been configured for an indexItem then the item should be indexed else not
// test 'retrieveBlockByHash'
block, err := blockfileMgr.retrieveBlockByHash(blocks[0].Header.Hash())
if testutil.Contains(indexItems, blkstorage.IndexableAttrBlockHash) {
testutil.AssertNoError(t, err, "Error while retrieving block by hash")
testutil.AssertEquals(t, block, blocks[0])
if containsAttr(indexItems, blkstorage.IndexableAttrBlockHash) {
assert.NoError(t, err, "Error while retrieving block by hash")
assert.Equal(t, blocks[0], block)
} else {
testutil.AssertSame(t, err, blkstorage.ErrAttrNotIndexed)
assert.Exactly(t, blkstorage.ErrAttrNotIndexed, err)
}
// test 'retrieveBlockByNumber'
block, err = blockfileMgr.retrieveBlockByNumber(0)
if testutil.Contains(indexItems, blkstorage.IndexableAttrBlockNum) {
testutil.AssertNoError(t, err, "Error while retrieving block by number")
testutil.AssertEquals(t, block, blocks[0])