github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb_test_export.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 stateleveldb
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
    24  	"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
    25  )
    26  
    27  // TestVDBEnv provides a level db backed versioned db for testing
    28  type TestVDBEnv struct {
    29  	t          testing.TB
    30  	DBProvider statedb.VersionedDBProvider
    31  }
    32  
    33  // NewTestVDBEnv instantiates and new level db backed TestVDB
    34  func NewTestVDBEnv(t testing.TB) *TestVDBEnv {
    35  	t.Logf("Creating new TestVDBEnv")
    36  	removeDBPath(t, "NewTestVDBEnv")
    37  	dbProvider := NewVersionedDBProvider()
    38  	return &TestVDBEnv{t, dbProvider}
    39  }
    40  
    41  // Cleanup closes the db and removes the db folder
    42  func (env *TestVDBEnv) Cleanup() {
    43  	env.t.Logf("Cleaningup TestVDBEnv")
    44  	env.DBProvider.Close()
    45  	removeDBPath(env.t, "Cleanup")
    46  }
    47  
    48  func removeDBPath(t testing.TB, caller string) {
    49  	dbPath := ledgerconfig.GetStateLevelDBPath()
    50  	if err := os.RemoveAll(dbPath); err != nil {
    51  		t.Fatalf("Err: %s", err)
    52  		t.FailNow()
    53  	}
    54  	logger.Debugf("Removed folder [%s] for test environment for %s", dbPath, caller)
    55  }