github.com/renegr87/renegr87@v2.1.1+incompatible/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 "io/ioutil" 21 "os" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 26 "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb" 27 ) 28 29 // TestVDBEnv provides a level db backed versioned db for testing 30 type TestVDBEnv struct { 31 t testing.TB 32 DBProvider statedb.VersionedDBProvider 33 dbPath string 34 } 35 36 // NewTestVDBEnv instantiates and new level db backed TestVDB 37 func NewTestVDBEnv(t testing.TB) *TestVDBEnv { 38 t.Logf("Creating new TestVDBEnv") 39 dbPath, err := ioutil.TempDir("", "statelvldb") 40 if err != nil { 41 t.Fatalf("Failed to create leveldb directory: %s", err) 42 } 43 dbProvider, err := NewVersionedDBProvider(dbPath) 44 assert.NoError(t, err) 45 return &TestVDBEnv{t, dbProvider, dbPath} 46 } 47 48 // Cleanup closes the db and removes the db folder 49 func (env *TestVDBEnv) Cleanup() { 50 env.t.Logf("Cleaningup TestVDBEnv") 51 env.DBProvider.Close() 52 os.RemoveAll(env.dbPath) 53 }