Skip to content

Commit

Permalink
FAB-3400: Improve unit tests for ledger/kvledger
Browse files Browse the repository at this point in the history
Add TestRecovery to test recoverUnderConstructionLedger. 2 cases
1. geneis block is written, ledgerID is not created
2. genesis block is not-written

Added checks to make sure undeconstruction flag is cleared and
ledger can be opened.

Change-Id: I69264a469444cba938d6a85523115e8bd1993ee4
Signed-off-by: Balaji Viswanathan <balaji.viswanathan@gmail.com>
  • Loading branch information
bviswana101 committed May 2, 2017
1 parent a97886a commit b5bfe6a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
47 changes: 47 additions & 0 deletions core/ledger/kvledger/kv_ledger_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func TestLedgerProvider(t *testing.T) {
testutil.AssertEquals(t, ledgerIds[i], constructTestLedgerID(i))
}
for i := 0; i < numLedgers; i++ {
status, _ := provider.Exists(constructTestLedgerID(i))
testutil.AssertEquals(t, status, true)
ledger, err := provider.Open(constructTestLedgerID(i))
testutil.AssertNoError(t, err, "")
bcInfo, err := ledger.GetBlockchainInfo()
Expand All @@ -71,10 +73,55 @@ func TestLedgerProvider(t *testing.T) {
_, err = provider.Create(gb)
testutil.AssertEquals(t, err, ErrLedgerIDExists)

status, err := provider.Exists(constructTestLedgerID(numLedgers))
testutil.AssertNoError(t, err, "Failed to check for ledger existence")
testutil.AssertEquals(t, false, status)

_, err = provider.Open(constructTestLedgerID(numLedgers))
testutil.AssertEquals(t, err, ErrNonExistingLedgerID)
}

func TestRecovery(t *testing.T) {
env := newTestEnv(t)
defer env.cleanup()
provider, _ := NewProvider()

// now create the genesis block
genesisBlock, _ := configtxtest.MakeGenesisBlock(constructTestLedgerID(1))
ledger, err := provider.(*Provider).openInternal(constructTestLedgerID(1))
ledger.Commit(genesisBlock)
ledger.Close()

// Case 1: assume a crash happens, force underconstruction flag to be set to simulate
// a failure where ledgerid is being created - ie., block is written but flag is not unset
provider.(*Provider).idStore.setUnderConstructionFlag(constructTestLedgerID(1))
provider.Close()

// construct a new provider to invoke recovery
provider, err = NewProvider()
testutil.AssertNoError(t, err, "Provider failed to recover an underConstructionLedger")
// verify the underecoveryflag and open the ledger
flag, err := provider.(*Provider).idStore.getUnderConstructionFlag()
testutil.AssertNoError(t, err, "Failed to read the underconstruction flag")
testutil.AssertEquals(t, flag, "")
ledger, err = provider.Open(constructTestLedgerID(1))
testutil.AssertNoError(t, err, "Failed to open the ledger")
ledger.Close()

// Case 0: assume a crash happens before the genesis block of ledger 2 is comitted
// Open the ID store (inventory of chainIds/ledgerIds)
provider.(*Provider).idStore.setUnderConstructionFlag(constructTestLedgerID(2))
provider.Close()

// construct a new provider to invoke recovery
provider, err = NewProvider()
testutil.AssertNoError(t, err, "Provider failed to recover an underConstructionLedger")
flag, err = provider.(*Provider).idStore.getUnderConstructionFlag()
testutil.AssertNoError(t, err, "Failed to read the underconstruction flag")
testutil.AssertEquals(t, flag, "")

}

func TestMultipleLedgerBasicRW(t *testing.T) {
env := newTestEnv(t)
defer env.cleanup()
Expand Down
9 changes: 9 additions & 0 deletions core/ledger/kvledger/kv_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/ledger/queryresult"
"github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -100,6 +101,14 @@ func TestKVLedgerBlockStorage(t *testing.T) {
retrievedTxEnv2 := processedTran2.TransactionEnvelope
testutil.AssertEquals(t, retrievedTxEnv2, txEnv2)

// get the tran id from the 2nd block, then use it to test GetBlockByTxID
b1, _ = ledger.GetBlockByTxID(txID2)
testutil.AssertEquals(t, b1, block1)

// get the transaction validation code for this transaction id
validCode, _ := ledger.GetTxValidationCodeByTxID(txID2)
testutil.AssertEquals(t, validCode, peer.TxValidationCode_VALID)

}

func TestKVLedgerDBRecovery(t *testing.T) {
Expand Down

0 comments on commit b5bfe6a

Please sign in to comment.