pkg_test.go 3.25 KB
Newer Older
manish's avatar
manish committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fsblkstorage

import (
	"fmt"
denyeart's avatar
denyeart committed
21
	"math"
manish's avatar
manish committed
22
23
24
	"os"
	"testing"

25
26
	"github.com/hyperledger/fabric/common/ledger/blkstorage"
	"github.com/hyperledger/fabric/common/ledger/testutil"
27

28
	"github.com/hyperledger/fabric/protos/common"
manish's avatar
manish committed
29
30
)

31
var testPath = "/tmp/fabric/ledgertests/blkstorage/fsblkstorage"
manish's avatar
manish committed
32

manish's avatar
manish committed
33
type testEnv struct {
manish's avatar
manish committed
34
35
	t        testing.TB
	provider *FsBlockstoreProvider
manish's avatar
manish committed
36
37
}

manish's avatar
manish committed
38
func newTestEnv(t testing.TB, conf *Conf) *testEnv {
manish's avatar
manish committed
39
40
41
42
	attrsToIndex := []blkstorage.IndexableAttr{
		blkstorage.IndexableAttrBlockHash,
		blkstorage.IndexableAttrBlockNum,
		blkstorage.IndexableAttrTxID,
43
		blkstorage.IndexableAttrBlockNumTranNum,
manish's avatar
manish committed
44
	}
manish's avatar
manish committed
45
46
47
48
49
50
	return newTestEnvSelectiveIndexing(t, conf, attrsToIndex)
}

func newTestEnvSelectiveIndexing(t testing.TB, conf *Conf, attrsToIndex []blkstorage.IndexableAttr) *testEnv {
	indexConfig := &blkstorage.IndexConfig{AttrsToIndex: attrsToIndex}
	return &testEnv{t, NewProvider(conf, indexConfig).(*FsBlockstoreProvider)}
manish's avatar
manish committed
51
52
53
}

func (env *testEnv) Cleanup() {
manish's avatar
manish committed
54
55
56
57
58
59
60
	env.provider.Close()
	env.removeFSPath()
}

func (env *testEnv) removeFSPath() {
	fsPath := env.provider.conf.blockStorageDir
	os.RemoveAll(fsPath)
manish's avatar
manish committed
61
62
63
64
65
66
67
}

type testBlockfileMgrWrapper struct {
	t            testing.TB
	blockfileMgr *blockfileMgr
}

manish's avatar
manish committed
68
69
70
71
func newTestBlockfileWrapper(env *testEnv, ledgerid string) *testBlockfileMgrWrapper {
	blkStore, err := env.provider.OpenBlockStore(ledgerid)
	testutil.AssertNoError(env.t, err, "")
	return &testBlockfileMgrWrapper{env.t, blkStore.(*fsBlockStore).fileMgr}
manish's avatar
manish committed
72
73
}

74
func (w *testBlockfileMgrWrapper) addBlocks(blocks []*common.Block) {
manish's avatar
manish committed
75
76
77
78
79
80
	for _, blk := range blocks {
		err := w.blockfileMgr.addBlock(blk)
		testutil.AssertNoError(w.t, err, "Error while adding block to blockfileMgr")
	}
}

81
func (w *testBlockfileMgrWrapper) testGetBlockByHash(blocks []*common.Block) {
manish's avatar
manish committed
82
	for i, block := range blocks {
83
		hash := block.Header.Hash()
manish's avatar
manish committed
84
		b, err := w.blockfileMgr.retrieveBlockByHash(hash)
manish's avatar
manish committed
85
86
87
88
89
		testutil.AssertNoError(w.t, err, fmt.Sprintf("Error while retrieving [%d]th block from blockfileMgr", i))
		testutil.AssertEquals(w.t, b, block)
	}
}

90
func (w *testBlockfileMgrWrapper) testGetBlockByNumber(blocks []*common.Block, startingNum uint64) {
manish's avatar
manish committed
91
92
93
94
95
	for i := 0; i < len(blocks); i++ {
		b, err := w.blockfileMgr.retrieveBlockByNumber(startingNum + uint64(i))
		testutil.AssertNoError(w.t, err, fmt.Sprintf("Error while retrieving [%d]th block from blockfileMgr", i))
		testutil.AssertEquals(w.t, b, blocks[i])
	}
denyeart's avatar
denyeart committed
96
97
98
	// test getting the last block
	b, err := w.blockfileMgr.retrieveBlockByNumber(math.MaxUint64)
	iLastBlock := len(blocks) - 1
99
	testutil.AssertNoError(w.t, err, "Error while retrieving last block from blockfileMgr")
denyeart's avatar
denyeart committed
100
	testutil.AssertEquals(w.t, b, blocks[iLastBlock])
manish's avatar
manish committed
101
102
103
104
105
}

func (w *testBlockfileMgrWrapper) close() {
	w.blockfileMgr.close()
}