github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/ledger/file/fileledger_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 "bytes" 21 "io/ioutil" 22 "os" 23 "testing" 24 25 "github.com/hyperledger/fabric/common/configtx/tool/provisional" 26 ordererledger "github.com/hyperledger/fabric/orderer/ledger" 27 cb "github.com/hyperledger/fabric/protos/common" 28 ab "github.com/hyperledger/fabric/protos/orderer" 29 30 logging "github.com/op/go-logging" 31 ) 32 33 var genesisBlock = cb.NewBlock(0, nil) 34 35 func init() { 36 logging.SetLevel(logging.DEBUG, "") 37 } 38 39 type testEnv struct { 40 t *testing.T 41 location string 42 } 43 44 func initialize(t *testing.T) (*testEnv, *fileLedger) { 45 name, err := ioutil.TempDir("", "hyperledger_fabric") 46 if err != nil { 47 t.Fatalf("Error creating temp dir: %s", err) 48 } 49 flf := New(name).(*fileLedgerFactory) 50 fl, err := flf.GetOrCreate(provisional.TestChainID) 51 if err != nil { 52 panic(err) 53 } 54 55 fl.Append(genesisBlock) 56 return &testEnv{location: name, t: t}, fl.(*fileLedger) 57 } 58 59 func (tev *testEnv) tearDown() { 60 err := os.RemoveAll(tev.location) 61 if err != nil { 62 tev.t.Fatalf("Error tearing down env: %s", err) 63 } 64 } 65 66 func TestInitialization(t *testing.T) { 67 tev, fl := initialize(t) 68 defer tev.tearDown() 69 if fl.height != 1 { 70 t.Fatalf("Block height should be 1") 71 } 72 block, found := fl.readBlock(0) 73 if block == nil || !found { 74 t.Fatalf("Error retrieving genesis block") 75 } 76 if !bytes.Equal(block.Header.Hash(), fl.lastHash) { 77 t.Fatalf("Block hashes did no match") 78 } 79 } 80 81 func TestReinitialization(t *testing.T) { 82 tev, ofl := initialize(t) 83 defer tev.tearDown() 84 ofl.Append(ordererledger.CreateNextBlock(ofl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}})) 85 flf := New(tev.location) 86 chains := flf.ChainIDs() 87 if len(chains) != 1 { 88 t.Fatalf("Should have recovered the chain") 89 } 90 91 tfl, err := flf.GetOrCreate(chains[0]) 92 if err != nil { 93 t.Fatalf("Unexpected error: %s", err) 94 } 95 96 fl := tfl.(*fileLedger) 97 if fl.height != 2 { 98 t.Fatalf("Block height should be 2") 99 } 100 block, found := fl.readBlock(1) 101 if block == nil || !found { 102 t.Fatalf("Error retrieving block 1") 103 } 104 if !bytes.Equal(block.Header.Hash(), fl.lastHash) { 105 t.Fatalf("Block hashes did no match") 106 } 107 } 108 109 func TestMultiReinitialization(t *testing.T) { 110 tev, _ := initialize(t) 111 defer tev.tearDown() 112 flf := New(tev.location) 113 114 _, err := flf.GetOrCreate("foo") 115 if err != nil { 116 t.Fatalf("Error creating chain") 117 } 118 119 _, err = flf.GetOrCreate("bar") 120 if err != nil { 121 t.Fatalf("Error creating chain") 122 } 123 124 flf = New(tev.location) 125 chains := flf.ChainIDs() 126 if len(chains) != 3 { 127 t.Fatalf("Should have recovered the chains") 128 } 129 } 130 131 func TestAddition(t *testing.T) { 132 tev, fl := initialize(t) 133 defer tev.tearDown() 134 prevHash := fl.lastHash 135 fl.Append(ordererledger.CreateNextBlock(fl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}})) 136 if fl.height != 2 { 137 t.Fatalf("Block height should be 2") 138 } 139 block, found := fl.readBlock(1) 140 if block == nil || !found { 141 t.Fatalf("Error retrieving genesis block") 142 } 143 if !bytes.Equal(block.Header.PreviousHash, prevHash) { 144 t.Fatalf("Block hashes did no match") 145 } 146 } 147 148 func TestRetrieval(t *testing.T) { 149 tev, fl := initialize(t) 150 defer tev.tearDown() 151 fl.Append(ordererledger.CreateNextBlock(fl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}})) 152 it, num := fl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Oldest{}}) 153 if num != 0 { 154 t.Fatalf("Expected genesis block iterator, but got %d", num) 155 } 156 signal := it.ReadyChan() 157 select { 158 case <-signal: 159 default: 160 t.Fatalf("Should be ready for block read") 161 } 162 block, status := it.Next() 163 if status != cb.Status_SUCCESS { 164 t.Fatalf("Expected to successfully read the genesis block") 165 } 166 if block.Header.Number != 0 { 167 t.Fatalf("Expected to successfully retrieve the genesis block") 168 } 169 signal = it.ReadyChan() 170 select { 171 case <-signal: 172 default: 173 t.Fatalf("Should still be ready for block read") 174 } 175 block, status = it.Next() 176 if status != cb.Status_SUCCESS { 177 t.Fatalf("Expected to successfully read the second block") 178 } 179 if block.Header.Number != 1 { 180 t.Fatalf("Expected to successfully retrieve the second block but got block number %d", block.Header.Number) 181 } 182 } 183 184 func TestBlockedRetrieval(t *testing.T) { 185 tev, fl := initialize(t) 186 defer tev.tearDown() 187 it, num := fl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: 1}}}) 188 if num != 1 { 189 t.Fatalf("Expected block iterator at 1, but got %d", num) 190 } 191 signal := it.ReadyChan() 192 select { 193 case <-signal: 194 t.Fatalf("Should not be ready for block read") 195 default: 196 } 197 fl.Append(ordererledger.CreateNextBlock(fl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}})) 198 select { 199 case <-signal: 200 default: 201 t.Fatalf("Should now be ready for block read") 202 } 203 block, status := it.Next() 204 if status != cb.Status_SUCCESS { 205 t.Fatalf("Expected to successfully read the second block") 206 } 207 if block.Header.Number != 1 { 208 t.Fatalf("Expected to successfully retrieve the second block") 209 } 210 }