github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/ledger/util/leveldbhelper/pkg_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package leveldbhelper
     8  
     9  import (
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  const testDBPath = "/tmp/fabric/ledgertests/util/leveldbhelper"
    17  
    18  type testDBEnv struct {
    19  	t    *testing.T
    20  	path string
    21  	db   *DB
    22  }
    23  
    24  type testDBProviderEnv struct {
    25  	t        *testing.T
    26  	path     string
    27  	provider *Provider
    28  }
    29  
    30  func newTestDBEnv(t *testing.T, path string) *testDBEnv {
    31  	testDBEnv := &testDBEnv{t: t, path: path}
    32  	testDBEnv.cleanup()
    33  	testDBEnv.db = CreateDB(&Conf{DBPath: path})
    34  	return testDBEnv
    35  }
    36  
    37  func newTestProviderEnv(t *testing.T, path string) *testDBProviderEnv {
    38  	testProviderEnv := &testDBProviderEnv{t: t, path: path}
    39  	testProviderEnv.cleanup()
    40  	var err error
    41  	testProviderEnv.provider, err = NewProvider(&Conf{DBPath: path})
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  	return testProviderEnv
    46  }
    47  
    48  func (dbEnv *testDBEnv) cleanup() {
    49  	if dbEnv.db != nil {
    50  		dbEnv.db.Close()
    51  	}
    52  	assert.NoError(dbEnv.t, os.RemoveAll(dbEnv.path))
    53  }
    54  
    55  func (providerEnv *testDBProviderEnv) cleanup() {
    56  	if providerEnv.provider != nil {
    57  		providerEnv.provider.Close()
    58  	}
    59  	assert.NoError(providerEnv.t, os.RemoveAll(providerEnv.path))
    60  }