Skip to content

Commit

Permalink
[FAB-4946] Improve UT coverage of orderer/ledger/file
Browse files Browse the repository at this point in the history
This patchset also cleans up the test style to use assert,
as well as moving some factory-specific tests to its own
test file from impl test file.

Change-Id: I02e6c5e364b3e5ae254c45853595acd32eabcd7b
Signed-off-by: Jay Guo <guojiannan1101@gmail.com>
  • Loading branch information
guoger committed Jun 23, 2017
1 parent 4e219e9 commit 8ba92d3
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 93 deletions.
92 changes: 92 additions & 0 deletions orderer/ledger/file/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
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 fileledger

import (
"fmt"
"io/ioutil"
"testing"

"github.com/hyperledger/fabric/common/configtx/tool/provisional"
"github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/orderer/ledger"
"github.com/stretchr/testify/assert"
)

type mockBlockStoreProvider struct {
blockstore blkstorage.BlockStore
exists bool
list []string
error error
}

func (mbsp *mockBlockStoreProvider) CreateBlockStore(ledgerid string) (blkstorage.BlockStore, error) {
return mbsp.blockstore, mbsp.error
}

func (mbsp *mockBlockStoreProvider) OpenBlockStore(ledgerid string) (blkstorage.BlockStore, error) {
return mbsp.blockstore, mbsp.error
}

func (mbsp *mockBlockStoreProvider) Exists(ledgerid string) (bool, error) {
return mbsp.exists, mbsp.error
}

func (mbsp *mockBlockStoreProvider) List() ([]string, error) {
return mbsp.list, mbsp.error
}

func (mbsp *mockBlockStoreProvider) Close() {
}

func TestBlockstoreProviderError(t *testing.T) {
flf := &fileLedgerFactory{
blkstorageProvider: &mockBlockStoreProvider{error: fmt.Errorf("blockstorage provider error")},
ledgers: make(map[string]ledger.ReadWriter),
}
assert.Panics(
t,
func() { flf.ChainIDs() },
"Expected ChainIDs to panic if storage provider cannot list chain IDs")

_, err := flf.GetOrCreate("foo")
assert.Error(t, err, "Expected GetOrCreate to return error if blockstorage provider cannot open")
assert.Empty(t, flf.ledgers, "Expected no new ledger is created")
}

func TestMultiReinitialization(t *testing.T) {
dir, err := ioutil.TempDir("", "hyperledger_fabric")
assert.NoError(t, err, "Error creating temp dir: %s", err)

flf := New(dir)
_, err = flf.GetOrCreate(provisional.TestChainID)
assert.NoError(t, err, "Error GetOrCreate chain")
assert.Equal(t, 1, len(flf.ChainIDs()), "Expected 1 chain")
flf.Close()

flf = New(dir)
_, err = flf.GetOrCreate("foo")
assert.NoError(t, err, "Error creating chain")
assert.Equal(t, 2, len(flf.ChainIDs()), "Expected chain to be recovered")
flf.Close()

flf = New(dir)
_, err = flf.GetOrCreate("bar")
assert.NoError(t, err, "Error creating chain")
assert.Equal(t, 3, len(flf.ChainIDs()), "Expected chain to be recovered")
flf.Close()
}
4 changes: 2 additions & 2 deletions orderer/ledger/file/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func (fl *fileLedger) Iterator(startPosition *ab.SeekPosition) (ledger.Iterator,
return &ledger.NotFoundErrorIterator{}, 0
}
return &fileLedgerIterator{ledger: fl, blockNumber: start.Specified.Number}, start.Specified.Number
default:
return &ledger.NotFoundErrorIterator{}, 0
}
// This line should be unreachable, but the compiler requires it
return &ledger.NotFoundErrorIterator{}, 0
}

// Height returns the number of blocks on the ledger
Expand Down
Loading

0 comments on commit 8ba92d3

Please sign in to comment.