github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/ledger/blkstorage/fsblkstorage/blockfile_scan_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package fsblkstorage
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric-protos-go/common"
    24  	"github.com/hyperledger/fabric/common/ledger/testutil"
    25  	"github.com/hyperledger/fabric/common/ledger/util"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestBlockFileScanSmallTxOnly(t *testing.T) {
    30  	env := newTestEnv(t, NewConf(testPath(), 0))
    31  	defer env.Cleanup()
    32  	ledgerid := "testLedger"
    33  	blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
    34  	bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
    35  	blocks := []*common.Block{gb}
    36  	blocks = append(blocks, bg.NextTestBlock(0, 0))
    37  	blocks = append(blocks, bg.NextTestBlock(0, 0))
    38  	blocks = append(blocks, bg.NextTestBlock(0, 0))
    39  	blkfileMgrWrapper.addBlocks(blocks)
    40  	blkfileMgrWrapper.close()
    41  
    42  	filePath := deriveBlockfilePath(env.provider.conf.getLedgerBlockDir(ledgerid), 0)
    43  	_, fileSize, err := util.FileExists(filePath)
    44  	assert.NoError(t, err)
    45  
    46  	lastBlockBytes, endOffsetLastBlock, numBlocks, err := scanForLastCompleteBlock(env.provider.conf.getLedgerBlockDir(ledgerid), 0, 0)
    47  	assert.NoError(t, err)
    48  	assert.Equal(t, len(blocks), numBlocks)
    49  	assert.Equal(t, fileSize, endOffsetLastBlock)
    50  
    51  	expectedLastBlockBytes, _, err := serializeBlock(blocks[len(blocks)-1])
    52  	assert.NoError(t, err)
    53  	assert.Equal(t, expectedLastBlockBytes, lastBlockBytes)
    54  }
    55  
    56  func TestBlockFileScanSmallTxLastTxIncomplete(t *testing.T) {
    57  	env := newTestEnv(t, NewConf(testPath(), 0))
    58  	defer env.Cleanup()
    59  	ledgerid := "testLedger"
    60  	blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
    61  	bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
    62  	blocks := []*common.Block{gb}
    63  	blocks = append(blocks, bg.NextTestBlock(0, 0))
    64  	blocks = append(blocks, bg.NextTestBlock(0, 0))
    65  	blocks = append(blocks, bg.NextTestBlock(0, 0))
    66  	blkfileMgrWrapper.addBlocks(blocks)
    67  	blkfileMgrWrapper.close()
    68  
    69  	filePath := deriveBlockfilePath(env.provider.conf.getLedgerBlockDir(ledgerid), 0)
    70  	_, fileSize, err := util.FileExists(filePath)
    71  	assert.NoError(t, err)
    72  
    73  	file, err := os.OpenFile(filePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
    74  	assert.NoError(t, err)
    75  	defer file.Close()
    76  	err = file.Truncate(fileSize - 1)
    77  	assert.NoError(t, err)
    78  
    79  	lastBlockBytes, _, numBlocks, err := scanForLastCompleteBlock(env.provider.conf.getLedgerBlockDir(ledgerid), 0, 0)
    80  	assert.NoError(t, err)
    81  	assert.Equal(t, len(blocks)-1, numBlocks)
    82  
    83  	expectedLastBlockBytes, _, err := serializeBlock(blocks[len(blocks)-2])
    84  	assert.NoError(t, err)
    85  	assert.Equal(t, expectedLastBlockBytes, lastBlockBytes)
    86  }