Skip to content

Commit

Permalink
[FAB-10686] testutil->testify fsblkstorage
Browse files Browse the repository at this point in the history
Change-Id: I4daf106d190b42053ce2967bc31e6ad79894ac7e
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
sykesm committed Sep 22, 2018
1 parent a29746b commit ef92892
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 177 deletions.
25 changes: 13 additions & 12 deletions common/ledger/blkstorage/fsblkstorage/block_serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
31 changes: 16 additions & 15 deletions common/ledger/blkstorage/fsblkstorage/block_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -41,22 +42,22 @@ 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
}
blockCount++
}
// 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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -123,19 +124,19 @@ 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
}
blockCount++
}
// 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)
}
21 changes: 11 additions & 10 deletions common/ledger/blkstorage/fsblkstorage/blockfile_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
}
Loading

0 comments on commit ef92892

Please sign in to comment.