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