github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/orderer/ledger/file/factory_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 fileledger 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "testing" 23 24 "github.com/hyperledger/fabric/common/configtx/tool/provisional" 25 "github.com/hyperledger/fabric/common/ledger/blkstorage" 26 "github.com/hyperledger/fabric/orderer/ledger" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 type mockBlockStoreProvider struct { 31 blockstore blkstorage.BlockStore 32 exists bool 33 list []string 34 error error 35 } 36 37 func (mbsp *mockBlockStoreProvider) CreateBlockStore(ledgerid string) (blkstorage.BlockStore, error) { 38 return mbsp.blockstore, mbsp.error 39 } 40 41 func (mbsp *mockBlockStoreProvider) OpenBlockStore(ledgerid string) (blkstorage.BlockStore, error) { 42 return mbsp.blockstore, mbsp.error 43 } 44 45 func (mbsp *mockBlockStoreProvider) Exists(ledgerid string) (bool, error) { 46 return mbsp.exists, mbsp.error 47 } 48 49 func (mbsp *mockBlockStoreProvider) List() ([]string, error) { 50 return mbsp.list, mbsp.error 51 } 52 53 func (mbsp *mockBlockStoreProvider) Close() { 54 } 55 56 func TestBlockstoreProviderError(t *testing.T) { 57 flf := &fileLedgerFactory{ 58 blkstorageProvider: &mockBlockStoreProvider{error: fmt.Errorf("blockstorage provider error")}, 59 ledgers: make(map[string]ledger.ReadWriter), 60 } 61 assert.Panics( 62 t, 63 func() { flf.ChainIDs() }, 64 "Expected ChainIDs to panic if storage provider cannot list chain IDs") 65 66 _, err := flf.GetOrCreate("foo") 67 assert.Error(t, err, "Expected GetOrCreate to return error if blockstorage provider cannot open") 68 assert.Empty(t, flf.ledgers, "Expected no new ledger is created") 69 } 70 71 func TestMultiReinitialization(t *testing.T) { 72 dir, err := ioutil.TempDir("", "hyperledger_fabric") 73 assert.NoError(t, err, "Error creating temp dir: %s", err) 74 75 flf := New(dir) 76 _, err = flf.GetOrCreate(provisional.TestChainID) 77 assert.NoError(t, err, "Error GetOrCreate chain") 78 assert.Equal(t, 1, len(flf.ChainIDs()), "Expected 1 chain") 79 flf.Close() 80 81 flf = New(dir) 82 _, err = flf.GetOrCreate("foo") 83 assert.NoError(t, err, "Error creating chain") 84 assert.Equal(t, 2, len(flf.ChainIDs()), "Expected chain to be recovered") 85 flf.Close() 86 87 flf = New(dir) 88 _, err = flf.GetOrCreate("bar") 89 assert.NoError(t, err, "Error creating chain") 90 assert.Equal(t, 3, len(flf.ChainIDs()), "Expected chain to be recovered") 91 flf.Close() 92 }