Skip to content

Commit ef92892

Browse files
committed
[FAB-10686] testutil->testify fsblkstorage
Change-Id: I4daf106d190b42053ce2967bc31e6ad79894ac7e Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent a29746b commit ef92892

10 files changed

+194
-177
lines changed

common/ledger/blkstorage/fsblkstorage/block_serialization_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,46 @@ import (
2222
"github.com/golang/protobuf/proto"
2323
"github.com/hyperledger/fabric/common/ledger/testutil"
2424
putils "github.com/hyperledger/fabric/protos/utils"
25+
"github.com/stretchr/testify/assert"
2526
)
2627

2728
func TestBlockSerialization(t *testing.T) {
2829
block := testutil.ConstructTestBlock(t, 1, 10, 100)
2930
bb, _, err := serializeBlock(block)
30-
testutil.AssertNoError(t, err, "")
31+
assert.NoError(t, err)
3132
deserializedBlock, err := deserializeBlock(bb)
32-
testutil.AssertNoError(t, err, "")
33-
testutil.AssertEquals(t, deserializedBlock, block)
33+
assert.NoError(t, err)
34+
assert.Equal(t, block, deserializedBlock)
3435
}
3536

3637
func TestExtractTxid(t *testing.T) {
3738
txEnv, txid, _ := testutil.ConstructTransaction(t, testutil.ConstructRandomBytes(t, 50), "", false)
3839
txEnvBytes, _ := putils.GetBytesEnvelope(txEnv)
3940
extractedTxid, err := extractTxID(txEnvBytes)
40-
testutil.AssertNoError(t, err, "")
41-
testutil.AssertEquals(t, extractedTxid, txid)
41+
assert.NoError(t, err)
42+
assert.Equal(t, txid, extractedTxid)
4243
}
4344

4445
func TestSerializedBlockInfo(t *testing.T) {
4546
block := testutil.ConstructTestBlock(t, 1, 10, 100)
4647
bb, info, err := serializeBlock(block)
47-
testutil.AssertNoError(t, err, "")
48+
assert.NoError(t, err)
4849
infoFromBB, err := extractSerializedBlockInfo(bb)
49-
testutil.AssertNoError(t, err, "")
50-
testutil.AssertEquals(t, infoFromBB, info)
51-
testutil.AssertEquals(t, len(info.txOffsets), len(block.Data.Data))
50+
assert.NoError(t, err)
51+
assert.Equal(t, info, infoFromBB)
52+
assert.Equal(t, len(block.Data.Data), len(info.txOffsets))
5253
for txIndex, txEnvBytes := range block.Data.Data {
5354
txid, err := extractTxID(txEnvBytes)
54-
testutil.AssertNoError(t, err, "")
55+
assert.NoError(t, err)
5556

5657
indexInfo := info.txOffsets[txIndex]
5758
indexTxID := indexInfo.txID
5859
indexOffset := indexInfo.loc
5960

60-
testutil.AssertEquals(t, txid, indexTxID)
61+
assert.Equal(t, indexTxID, txid)
6162
b := bb[indexOffset.offset:]
6263
length, num := proto.DecodeVarint(b)
6364
txEnvBytesFromBB := b[num : num+int(length)]
64-
testutil.AssertEquals(t, txEnvBytesFromBB, txEnvBytes)
65+
assert.Equal(t, txEnvBytes, txEnvBytesFromBB)
6566
}
6667
}

common/ledger/blkstorage/fsblkstorage/block_stream_test.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/golang/protobuf/proto"
2323
"github.com/hyperledger/fabric/common/ledger/testutil"
2424
"github.com/hyperledger/fabric/protos/common"
25+
"github.com/stretchr/testify/assert"
2526
)
2627

2728
func TestBlockfileStream(t *testing.T) {
@@ -41,22 +42,22 @@ func testBlockfileStream(t *testing.T, numBlocks int) {
4142

4243
s, err := newBlockfileStream(w.blockfileMgr.rootDir, 0, 0)
4344
defer s.close()
44-
testutil.AssertNoError(t, err, "Error in constructing blockfile stream")
45+
assert.NoError(t, err, "Error in constructing blockfile stream")
4546

4647
blockCount := 0
4748
for {
4849
blockBytes, err := s.nextBlockBytes()
49-
testutil.AssertNoError(t, err, "Error in getting next block")
50+
assert.NoError(t, err, "Error in getting next block")
5051
if blockBytes == nil {
5152
break
5253
}
5354
blockCount++
5455
}
5556
// After the stream has been exhausted, both blockBytes and err should be nil
5657
blockBytes, err := s.nextBlockBytes()
57-
testutil.AssertNil(t, blockBytes)
58-
testutil.AssertNoError(t, err, "Error in getting next block after exhausting the file")
59-
testutil.AssertEquals(t, blockCount, numBlocks)
58+
assert.Nil(t, blockBytes)
59+
assert.NoError(t, err, "Error in getting next block after exhausting the file")
60+
assert.Equal(t, numBlocks, blockCount)
6061
}
6162

6263
func TestBlockFileStreamUnexpectedEOF(t *testing.T) {
@@ -82,16 +83,16 @@ func testBlockFileStreamUnexpectedEOF(t *testing.T, numBlocks int, partialBlockB
8283
w.close()
8384
s, err := newBlockfileStream(blockfileMgr.rootDir, 0, 0)
8485
defer s.close()
85-
testutil.AssertNoError(t, err, "Error in constructing blockfile stream")
86+
assert.NoError(t, err, "Error in constructing blockfile stream")
8687

8788
for i := 0; i < numBlocks; i++ {
8889
blockBytes, err := s.nextBlockBytes()
89-
testutil.AssertNotNil(t, blockBytes)
90-
testutil.AssertNoError(t, err, "Error in getting next block")
90+
assert.NotNil(t, blockBytes)
91+
assert.NoError(t, err, "Error in getting next block")
9192
}
9293
blockBytes, err := s.nextBlockBytes()
93-
testutil.AssertNil(t, blockBytes)
94-
testutil.AssertSame(t, err, ErrUnexpectedEndOfBlockfile)
94+
assert.Nil(t, blockBytes)
95+
assert.Exactly(t, ErrUnexpectedEndOfBlockfile, err)
9596
}
9697

9798
func TestBlockStream(t *testing.T) {
@@ -123,19 +124,19 @@ func testBlockStream(t *testing.T, numFiles int) {
123124
}
124125
s, err := newBlockStream(blockfileMgr.rootDir, 0, 0, numFiles-1)
125126
defer s.close()
126-
testutil.AssertNoError(t, err, "Error in constructing new block stream")
127+
assert.NoError(t, err, "Error in constructing new block stream")
127128
blockCount := 0
128129
for {
129130
blockBytes, err := s.nextBlockBytes()
130-
testutil.AssertNoError(t, err, "Error in getting next block")
131+
assert.NoError(t, err, "Error in getting next block")
131132
if blockBytes == nil {
132133
break
133134
}
134135
blockCount++
135136
}
136137
// After the stream has been exhausted, both blockBytes and err should be nil
137138
blockBytes, err := s.nextBlockBytes()
138-
testutil.AssertNil(t, blockBytes)
139-
testutil.AssertNoError(t, err, "Error in getting next block after exhausting the file")
140-
testutil.AssertEquals(t, blockCount, numFiles*numBlocksInEachFile)
139+
assert.Nil(t, blockBytes)
140+
assert.NoError(t, err, "Error in getting next block after exhausting the file")
141+
assert.Equal(t, numFiles*numBlocksInEachFile, blockCount)
141142
}

common/ledger/blkstorage/fsblkstorage/blockfile_helper_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/golang/protobuf/proto"
1414
"github.com/hyperledger/fabric/common/ledger/testutil"
1515
"github.com/hyperledger/fabric/common/ledger/util"
16+
"github.com/stretchr/testify/assert"
1617
)
1718

1819
func TestConstructCheckpointInfoFromBlockFiles(t *testing.T) {
@@ -26,8 +27,8 @@ func TestConstructCheckpointInfoFromBlockFiles(t *testing.T) {
2627

2728
// checkpoint constructed on an empty block folder should return CPInfo with isChainEmpty: true
2829
cpInfo, err := constructCheckpointInfoFromBlockFiles(blkStoreDir)
29-
testutil.AssertNoError(t, err, "")
30-
testutil.AssertEquals(t, cpInfo, &checkpointInfo{isChainEmpty: true, lastBlockNumber: 0, latestFileChunksize: 0, latestFileChunkSuffixNum: 0})
30+
assert.NoError(t, err)
31+
assert.Equal(t, &checkpointInfo{isChainEmpty: true, lastBlockNumber: 0, latestFileChunksize: 0, latestFileChunkSuffixNum: 0}, cpInfo)
3132

3233
w := newTestBlockfileWrapper(env, ledgerid)
3334
defer w.close()
@@ -54,7 +55,7 @@ func TestConstructCheckpointInfoFromBlockFiles(t *testing.T) {
5455
// Write a partial block (to simulate a crash) and verify that cpinfo derived from filesystem should be same as from the blockfile manager
5556
lastTestBlk := bg.NextTestBlocks(1)[0]
5657
blockBytes, _, err := serializeBlock(lastTestBlk)
57-
testutil.AssertNoError(t, err, "")
58+
assert.NoError(t, err)
5859
partialByte := append(proto.EncodeVarint(uint64(len(blockBytes))), blockBytes[len(blockBytes)/2:]...)
5960
blockfileMgr.currentFileWriter.append(partialByte, true)
6061
checkCPInfoFromFile(t, blkStoreDir, blockfileMgr.cpInfo)
@@ -64,24 +65,24 @@ func TestConstructCheckpointInfoFromBlockFiles(t *testing.T) {
6465
w.close()
6566
env.provider.Close()
6667
indexFolder := conf.getIndexDir()
67-
testutil.AssertNoError(t, os.RemoveAll(indexFolder), "")
68+
assert.NoError(t, os.RemoveAll(indexFolder))
6869

6970
env = newTestEnv(t, conf)
7071
w = newTestBlockfileWrapper(env, ledgerid)
7172
blockfileMgr = w.blockfileMgr
72-
testutil.AssertEquals(t, blockfileMgr.cpInfo, cpInfoBeforeClose)
73+
assert.Equal(t, cpInfoBeforeClose, blockfileMgr.cpInfo)
7374

7475
lastBlkIndexed, err := blockfileMgr.index.getLastBlockIndexed()
75-
testutil.AssertNoError(t, err, "")
76-
testutil.AssertEquals(t, lastBlkIndexed, uint64(6))
76+
assert.NoError(t, err)
77+
assert.Equal(t, uint64(6), lastBlkIndexed)
7778

7879
// Add the last block again after start and check cpinfo again
79-
testutil.AssertNoError(t, blockfileMgr.addBlock(lastTestBlk), "")
80+
assert.NoError(t, blockfileMgr.addBlock(lastTestBlk))
8081
checkCPInfoFromFile(t, blkStoreDir, blockfileMgr.cpInfo)
8182
}
8283

8384
func checkCPInfoFromFile(t *testing.T, blkStoreDir string, expectedCPInfo *checkpointInfo) {
8485
cpInfo, err := constructCheckpointInfoFromBlockFiles(blkStoreDir)
85-
testutil.AssertNoError(t, err, "")
86-
testutil.AssertEquals(t, cpInfo, expectedCPInfo)
86+
assert.NoError(t, err)
87+
assert.Equal(t, expectedCPInfo, cpInfo)
8788
}

0 commit comments

Comments
 (0)