github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/ledger/blkstorage/fsblkstorage/pkg_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  	"fmt"
    21  	"io/ioutil"
    22  	"math"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/hyperledger/fabric/common/ledger/blkstorage"
    27  	"github.com/hyperledger/fabric/common/ledger/testutil"
    28  
    29  	"github.com/hyperledger/fabric/protos/common"
    30  )
    31  
    32  func testPath() string {
    33  	if path, err := ioutil.TempDir("", "fsblkstorage-"); err != nil {
    34  		panic(err)
    35  	} else {
    36  		return path
    37  	}
    38  }
    39  
    40  type testEnv struct {
    41  	t        testing.TB
    42  	provider *FsBlockstoreProvider
    43  }
    44  
    45  func newTestEnv(t testing.TB, conf *Conf) *testEnv {
    46  	attrsToIndex := []blkstorage.IndexableAttr{
    47  		blkstorage.IndexableAttrBlockHash,
    48  		blkstorage.IndexableAttrBlockNum,
    49  		blkstorage.IndexableAttrTxID,
    50  		blkstorage.IndexableAttrBlockNumTranNum,
    51  		blkstorage.IndexableAttrBlockTxID,
    52  		blkstorage.IndexableAttrTxValidationCode,
    53  	}
    54  	return newTestEnvSelectiveIndexing(t, conf, attrsToIndex)
    55  }
    56  
    57  func newTestEnvSelectiveIndexing(t testing.TB, conf *Conf, attrsToIndex []blkstorage.IndexableAttr) *testEnv {
    58  	indexConfig := &blkstorage.IndexConfig{AttrsToIndex: attrsToIndex}
    59  	return &testEnv{t, NewProvider(conf, indexConfig).(*FsBlockstoreProvider)}
    60  }
    61  
    62  func (env *testEnv) Cleanup() {
    63  	env.provider.Close()
    64  	env.removeFSPath()
    65  }
    66  
    67  func (env *testEnv) removeFSPath() {
    68  	fsPath := env.provider.conf.blockStorageDir
    69  	os.RemoveAll(fsPath)
    70  }
    71  
    72  type testBlockfileMgrWrapper struct {
    73  	t            testing.TB
    74  	blockfileMgr *blockfileMgr
    75  }
    76  
    77  func newTestBlockfileWrapper(env *testEnv, ledgerid string) *testBlockfileMgrWrapper {
    78  	blkStore, err := env.provider.OpenBlockStore(ledgerid)
    79  	testutil.AssertNoError(env.t, err, "")
    80  	return &testBlockfileMgrWrapper{env.t, blkStore.(*fsBlockStore).fileMgr}
    81  }
    82  
    83  func (w *testBlockfileMgrWrapper) addBlocks(blocks []*common.Block) {
    84  	for _, blk := range blocks {
    85  		err := w.blockfileMgr.addBlock(blk)
    86  		testutil.AssertNoError(w.t, err, "Error while adding block to blockfileMgr")
    87  	}
    88  }
    89  
    90  func (w *testBlockfileMgrWrapper) testGetBlockByHash(blocks []*common.Block) {
    91  	for i, block := range blocks {
    92  		hash := block.Header.Hash()
    93  		b, err := w.blockfileMgr.retrieveBlockByHash(hash)
    94  		testutil.AssertNoError(w.t, err, fmt.Sprintf("Error while retrieving [%d]th block from blockfileMgr", i))
    95  		testutil.AssertEquals(w.t, b, block)
    96  	}
    97  }
    98  
    99  func (w *testBlockfileMgrWrapper) testGetBlockByNumber(blocks []*common.Block, startingNum uint64) {
   100  	for i := 0; i < len(blocks); i++ {
   101  		b, err := w.blockfileMgr.retrieveBlockByNumber(startingNum + uint64(i))
   102  		testutil.AssertNoError(w.t, err, fmt.Sprintf("Error while retrieving [%d]th block from blockfileMgr", i))
   103  		testutil.AssertEquals(w.t, b.Header, blocks[i].Header)
   104  	}
   105  	// test getting the last block
   106  	b, err := w.blockfileMgr.retrieveBlockByNumber(math.MaxUint64)
   107  	iLastBlock := len(blocks) - 1
   108  	testutil.AssertNoError(w.t, err, "Error while retrieving last block from blockfileMgr")
   109  	testutil.AssertEquals(w.t, b, blocks[iLastBlock])
   110  }
   111  
   112  func (w *testBlockfileMgrWrapper) close() {
   113  	w.blockfileMgr.close()
   114  }