pkg_test.go 3.29 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,
44
		blkstorage.IndexableAttrBlockTxID,
manish's avatar
manish committed
45
	}
manish's avatar
manish committed
46
47
48
49
50
51
	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
52
53
54
}

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

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

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

manish's avatar
manish committed
69
70
71
72
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
73
74
}

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

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

91
func (w *testBlockfileMgrWrapper) testGetBlockByNumber(blocks []*common.Block, startingNum uint64) {
manish's avatar
manish committed
92
93
94
95
96
	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
97
98
99
	// test getting the last block
	b, err := w.blockfileMgr.retrieveBlockByNumber(math.MaxUint64)
	iLastBlock := len(blocks) - 1
100
	testutil.AssertNoError(w.t, err, "Error while retrieving last block from blockfileMgr")
denyeart's avatar
denyeart committed
101
	testutil.AssertEquals(w.t, b, blocks[iLastBlock])
manish's avatar
manish committed
102
103
104
105
106
}

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